Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BasedOnStyle: LLVM
IndentWidth: 4

AccessModifierOffset: -4
Language: Cpp

DerivePointerAlignment: false
PointerAlignment: Left

AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCaseColons: false
34 changes: 15 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,24 @@ jobs:
- name: Checkout code
uses: actions/checkout@v6

- name: Install Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '4.0'

- name: Install Build Tools and Ceedling
- name: Install Build Tools
run: |
sudo apt-get update
sudo apt-get install -y gcc cmake ninja-build gcovr
gem install ceedling
sudo apt-get install -y gcc cmake ninja-build
pip install gcovr

- name: Fetch Dependencies
run: cmake -B build
- name: Configure cmake and run builds
run: |
cmake -B build -S .
cmake --build build

- name: Run Tests
run: ceedling gcov:all
- name: Run Tests and create coverage
run: |
cmake --build build --target test
cmake --build build --target coverage

- name: Upload Coverage
uses: actions/upload-artifact@v7
- name: Render Coverage Summary in CI
if: always()
with:
name: code-coverage-report
path: build/ceedling/artifacts/gcov/gcovr/GcovCoverageResults.html
archive: false
retention-days: 7
run: |
echo "## Code Coverage Summary" >> $GITHUB_STEP_SUMMARY
gcovr -r . -e "tests/" -e "build/" --markdown >> $GITHUB_STEP_SUMMARY
165 changes: 132 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.22)
enable_language(C)
project(CommonDrivers)
enable_language(C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(FetchContent)
Expand All @@ -13,14 +17,6 @@ FetchContent_Declare(bmi08
GIT_TAG master
)
FetchContent_MakeAvailable(bmi08)
add_library(bmi08 STATIC
${bmi08_SOURCE_DIR}/bmi08a.c
${bmi08_SOURCE_DIR}/bmi08g.c
${bmi08_SOURCE_DIR}/bmi08xa.c
${bmi08_SOURCE_DIR}/bmi088_mma.c
${bmi08_SOURCE_DIR}/bmi088_anymotiona.c
)
target_include_directories(bmi08 PUBLIC ${bmi08_SOURCE_DIR})

message(STATUS "Resolving bmp5 sensors api dependency...")
FetchContent_Declare(bmp5
Expand All @@ -29,8 +25,6 @@ FetchContent_Declare(bmp5
GIT_TAG master
)
FetchContent_MakeAvailable(bmp5)
add_library(bmp5 STATIC ${bmp5_SOURCE_DIR}/bmp5.c)
target_include_directories(bmp5 PUBLIC ${bmp5_SOURCE_DIR})

message(STATUS "Resolving littlefs dependency...")
FetchContent_Declare(littlefs
Expand All @@ -39,32 +33,137 @@ FetchContent_Declare(littlefs
GIT_TAG master
)
FetchContent_MakeAvailable(littlefs)
add_library(littlefs STATIC
${littlefs_SOURCE_DIR}/lfs.c
${littlefs_SOURCE_DIR}/lfs_util.c
)
target_include_directories(littlefs PUBLIC ${littlefs_SOURCE_DIR})

message(STATUS "Dependencies resolved, now creating library...")
add_library(common_drivers STATIC
"${CMAKE_CURRENT_SOURCE_DIR}/src/flash.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/flash/gd5f1gq5xe.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/sensors/bmp581.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/sensors/bmi088.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/sensors/CD-PA1616S.c"

message(STATUS "Resolving gtest dependency...")
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.18.0
)
target_include_directories(common_drivers PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/include/flash"
"${CMAKE_CURRENT_SOURCE_DIR}/include/sensors"
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

message(STATUS "Resolving fff dependency...")
FetchContent_Declare(
fff
GIT_REPOSITORY https://github.com/meekrosoft/fff.git
GIT_TAG master
)

message(STATUS "Linking libary to dependencies...")
target_link_libraries(common_drivers PUBLIC bmi08 bmp5 littlefs)
add_library(common_drivers STATIC)
if(TARGET stm32cubemx)
message(STATUS "Compiling for target host (MCU), linking libraries...")

if(NOT TARGET STM32_Drivers)
message(FATAL_ERROR "STM32_Drivers source was not added, something is wrong!")
endif()
get_target_property(STM32_Drivers_Src STM32_Drivers SOURCES)

# Autodetect series
# STM32 includes a file of the form (depending on the series)
# ${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/system_stm32f4xx.c
# to sources, which we can use regex detection to get the series
string(REGEX MATCH "stm32([a-z0-9]+)\\.c" FOUND_MATCH "${STM32_Drivers_Src}")
if(FOUND_MATCH)
# We then can use use this `HAL_STM32_SERIES` in
# hal.h to automatically include the right hal file.
message(STATUS "Detected " ${CMAKE_MATCH_1} " series...")
target_compile_definitions(common_drivers PUBLIC PLATFORM_STM32_SERIES=${CMAKE_MATCH_1})
else()
message(FATAL_ERROR "Unable to autodetect STM32 series!")
endif()

# Autodetect RTOS
# Note that STM32's HAL files do have a USE_RTOS definition. However relying
# on it is not a good idea because it is not specified for public
# use. Therefore we use our own detection and compile definition here
if(TARGET FreeRTOS)
message(STATUS "Detected FreeRTOS...")
target_compile_definitions(common_drivers PUBLIC PLATFORM_USE_RTOS)
endif()

# Autodetect perhipherals
# What this basically does is conditionally include the i2c, qspi, spi, and uart
# files if and only if they are enabled in cubemx.
#
# When cubemx enables the perhipherals, it adds something like this
# ${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c
# to STM32_Drivers_Src. Therefore we can just iterate through it and find if the
# given drivers exist, and if they do, we can safely include our thin HAL abstraction
# for the driver.
set(PATTERNS
"_hal_i2c\\.c"
"_hal_qspi\\.c"
"_hal_spi\\.c"
"_hal_uart\\.c"
)
set(DEFINES
I2C
QSPI
SPI
UART
)
foreach(PATTERN DEFINE IN ZIP_LISTS PATTERNS DEFINES)
string(REGEX MATCH "${PATTERN}" FOUND_MATCH "${STM32_Drivers_Src}")
if(FOUND_MATCH)
message(STATUS "Adding " ${DEFINE} " to build...")
set_property(GLOBAL APPEND PROPERTY PERHIPHERALS "${DEFINE}")
endif()
endforeach()

# Add libaries
add_library(bmi08 STATIC
${bmi08_SOURCE_DIR}/bmi08a.c
${bmi08_SOURCE_DIR}/bmi08g.c
${bmi08_SOURCE_DIR}/bmi08xa.c
${bmi08_SOURCE_DIR}/bmi088_mma.c
${bmi08_SOURCE_DIR}/bmi088_anymotiona.c
)
target_include_directories(bmi08 PUBLIC ${bmi08_SOURCE_DIR})

add_library(bmp5 STATIC ${bmp5_SOURCE_DIR}/bmp5.c)
target_include_directories(bmp5 PUBLIC ${bmp5_SOURCE_DIR})

add_library(littlefs STATIC
${littlefs_SOURCE_DIR}/lfs.c
${littlefs_SOURCE_DIR}/lfs_util.c
)
target_include_directories(littlefs PUBLIC ${littlefs_SOURCE_DIR})

message(STATUS "Linking dependencies to library...")
target_link_libraries(common_drivers PUBLIC bmi08 bmp5 littlefs)

if(NOT TARGET stm32cubemx)
# Empty for now, will add stuff for tests later
else()
message(STATUS "Linking stm32 drivers to library....")
target_link_libraries(common_drivers PUBLIC stm32cubemx)
else()
enable_testing()
message(STATUS "Compiling for tests, linking gtest, fff, and mocks to library...")
FetchContent_MakeAvailable(googletest)
FetchContent_MakeAvailable(fff)

add_library(bmp5 INTERFACE)
target_include_directories(bmp5 INTERFACE ${bmp5_SOURCE_DIR})

add_library(littlefs INTERFACE)
target_include_directories(littlefs INTERFACE ${littlefs_SOURCE_DIR})

add_library(bmi08 INTERFACE)
target_include_directories(bmi08 INTERFACE ${bmi08_SOURCE_DIR})

message(STATUS "Linking mock dependencies to library...")
target_link_libraries(common_drivers PUBLIC bmp5 littlefs bmi08)

message(STATUS "Creating tests for library...")
target_compile_definitions(common_drivers PUBLIC TEST)
add_subdirectory(tests)
endif()

message(STATUS "Adding source files and headers to library...")
add_subdirectory(include)
add_subdirectory(src)

add_custom_target(coverage
COMMAND mkdir -p coverage
COMMAND gcovr -r ${CMAKE_SOURCE_DIR} -e "${CMAKE_SOURCE_DIR}/tests/" -e "${CMAKE_BINARY_DIR}/" --html-details -o coverage/index.html .
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ To write a sensor, you must initialize a struct of the form

```c
struct sensor {
bool (*read)(void*, struct packet*);
bool (*read)(void*, Packet*);
void* ctx;
};
```
Expand Down
6 changes: 6 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target_include_directories(common_drivers PUBLIC
"."
"sensors"
"protocols"
"flash"
)
56 changes: 0 additions & 56 deletions include/defs.h

This file was deleted.

20 changes: 0 additions & 20 deletions include/flash.h

This file was deleted.

Loading