diff --git a/.vscode/settings.json b/.vscode/settings.json index 3eaff06..a7fb1d5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,18 +2,16 @@ "editor.tabSize": 4, "editor.detectIndentation": false, "editor.insertSpaces": true, - "files.trimFinalNewlines": true, "files.trimTrailingWhitespace": true, "files.insertFinalNewline": true, "files.eol": "\n", "files.encoding": "utf8", + "editor.defaultFormatter": "zachflower.uncrustify", "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", - "[c]": { "editor.formatOnSave": true }, - "[markdown]": { "files.trimTrailingWhitespace": false, "files.trimFinalNewlines": false diff --git a/CMakeLists.txt b/CMakeLists.txt index 22a19c5..213c51c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_BUILD_TYPE Debug) # Cube file name without .ioc extension -project(stm32_project_template C ASM) +project(stm32_project_template C CXX ASM) # Set the board version to an empty string if your board doesn't have a version set(BOARD_VERSION "") @@ -112,7 +112,7 @@ set(LIB_INCLUDE_DIRECTORIES ## Include directories ############################################################################### -set(C_INCLUDE_DIRECTORIES +set(PROJECT_INCLUDE_DIRECTORIES ./inc ./cube/Inc ) @@ -125,12 +125,12 @@ set(TEST_INCLUDE_DIRECTORIES ## Input files ############################################################################### -file(GLOB_RECURSE C_SOURCES CONFIGURE_DEPENDS "src/*.c") -file(GLOB_RECURSE C_HEADERS CONFIGURE_DEPENDS "inc/*.h") +file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS "src/*.cpp" "src/*.c") +file(GLOB_RECURSE PROJECT_HEADERS CONFIGURE_DEPENDS "inc/*.hpp" "inc/*.h") -file(GLOB_RECURSE TESTS_SOURCES CONFIGURE_DEPENDS "tests/src/*.c") -file(GLOB_RECURSE TESTS_HEADERS CONFIGURE_DEPENDS "tests/inc/*.h") -file(GLOB_RECURSE TESTS_BIN CONFIGURE_DEPENDS "tests/bin/*.c") +file(GLOB_RECURSE TESTS_SOURCES CONFIGURE_DEPENDS "tests/src/*.cpp" "tests/src/*.c") +file(GLOB_RECURSE TESTS_HEADERS CONFIGURE_DEPENDS "tests/inc/*.hpp" "tests/inc/*.h") +file(GLOB_RECURSE TESTS_BIN CONFIGURE_DEPENDS "tests/bin/*.cpp" "tests/bin/*.c") file(GLOB_RECURSE CUBE_SOURCES CONFIGURE_DEPENDS "cube/Src/*.c") @@ -142,7 +142,7 @@ list(REMOVE_ITEM CUBE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cube/Src/system_${DEVI set(FORCED_INCLUDE_HEADERS ) -targets_generate_format_target(C_SOURCES C_HEADERS TESTS_SOURCES TESTS_HEADERS TESTS_BIN) +targets_generate_format_target(PROJECT_SOURCES PROJECT_HEADERS TESTS_SOURCES TESTS_HEADERS TESTS_BIN) ############################################################################### ## Main executable target @@ -150,12 +150,12 @@ targets_generate_format_target(C_SOURCES C_HEADERS TESTS_SOURCES TESTS_HEADERS T add_executable(${PROJECT_NAME} ${CUBE_SOURCES} - ${C_SOURCES} + ${PROJECT_SOURCES} ${LIB_SOURCES} ) target_include_directories(${PROJECT_NAME} PUBLIC - ${C_INCLUDE_DIRECTORIES} + ${PROJECT_INCLUDE_DIRECTORIES} ${LIB_INCLUDE_DIRECTORIES} ${CMSIS_INCLUDE_DIRS} ${HAL_INCLUDE_DIRS} @@ -184,24 +184,27 @@ targets_generate_helpme_target() ## Generate test executables ############################################################################### -# Since each test has its own main function, we don't need the main.c from the project -list(REMOVE_ITEM C_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c) +# Since each test has its own main function, we don't need the main.cpp from the project +list(REMOVE_ITEM PROJECT_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c +) foreach(TEST_FILE ${TESTS_BIN}) - # If TEST_FILE contains /dir1/dir2/file.c, TEST_NAME will be 'file' + # If TEST_FILE contains /dir1/dir2/file.cpp, TEST_NAME will be 'file' get_filename_component(TEST_NAME ${TEST_FILE} NAME_WLE) add_executable(${TEST_NAME} EXCLUDE_FROM_ALL ${TEST_FILE} ${CUBE_SOURCES} - ${C_SOURCES} + ${PROJECT_SOURCES} ${TESTS_SOURCES} ${LIB_SOURCES} ) target_include_directories(${TEST_NAME} PUBLIC - ${C_INCLUDE_DIRECTORIES} + ${PROJECT_INCLUDE_DIRECTORIES} ${LIB_INCLUDE_DIRECTORIES} ${CMSIS_INCLUDE_DIRS} ${HAL_INCLUDE_DIRS} diff --git a/README.md b/README.md index 6e1fa8d..a8ee989 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Para isso é necessário mudar o nome do projeto, o qual deve ter o mesmo do arq ```c # Cube file name without .ioc extension -project(stm32_project_template C ASM) +project(stm32_project_template C CXX ASM) ``` > Os argumentos `C` e `ASM` estão relacionados ao tipo de linguagem que o projeto utiliza (C e Assembly). diff --git a/cube/stm32_project_template.ioc b/cube/stm32_project_template.ioc index b4efc92..c5eaea7 100644 --- a/cube/stm32_project_template.ioc +++ b/cube/stm32_project_template.ioc @@ -32,8 +32,8 @@ Mcu.PinsNb=14 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F303RETx -MxCube.Version=6.9.0 -MxDb.Version=DB.6.0.90 +MxCube.Version=6.10.0 +MxDb.Version=DB.6.0.100 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:false\:true\:false\:false NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:false\:true\:false\:false NVIC.ForceEnableDMAVector=true diff --git a/inc/mcu.h b/inc/mcu.h deleted file mode 100644 index 3d09bf9..0000000 --- a/inc/mcu.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file mcu.h - * - * @brief MCU related - */ - -#ifndef __MCU_H__ -#define __MCU_H__ - -#include - -/***************************************** - * Public Function Prototypes - *****************************************/ - -/** - * @brief Initializes MCU and some peripherals. - */ -void mcu_init(void); - -/** - * @brief Initializes System Clock. - * @note Defined by cube. - */ -void SystemClock_Config(void); - -/** - * @brief Put the MCU to sleep. - * - * @param ms Sleep time in milliseconds - */ -void mcu_sleep(uint32_t ms); - -/** - * @brief Toggles LED. - */ -void led_toggle(void); - -#endif // __MCU_H__ diff --git a/inc/mcu.hpp b/inc/mcu.hpp new file mode 100644 index 0000000..168a505 --- /dev/null +++ b/inc/mcu.hpp @@ -0,0 +1,41 @@ +/** + * @file mcu.hpp + * + * @brief MCU related + */ + +#ifndef __MCU_HPP__ +#define __MCU_HPP__ + +#include + +/***************************************** + * Public Function Prototypes + *****************************************/ + +extern "C" +{ +/** + * @brief Initializes System Clock. + * @note Defined by cube. + */ +void SystemClock_Config(void); +} + +namespace hal { +class mcu { + public: + /** + * @brief Initializes MCU and some peripherals. + */ + static void init(void); + + /** + * @brief Put the MCU to sleep. + * + * @param ms Sleep time in milliseconds + */ + static void sleep(uint32_t ms); +}; +}; +#endif // __MCU_HPP__ diff --git a/src/main.c b/src/main.cpp similarity index 65% rename from src/main.c rename to src/main.cpp index 90f8109..94baf12 100644 --- a/src/main.c +++ b/src/main.cpp @@ -1,26 +1,25 @@ /** - * @file main.c + * @file main.cpp * * @brief Main function */ -#include "mcu.h" +#include "mcu.hpp" /***************************************** * Private Constant Definitions *****************************************/ -#define LED_TOGGLE_DELAY_MS 1500 +static constexpr uint16_t led_toggle_delay_ms = 1500; /***************************************** * Main Function *****************************************/ int main(void) { - mcu_init(); + hal::mcu::init(); for (;;) { - led_toggle(); - mcu_sleep(LED_TOGGLE_DELAY_MS); + hal::mcu::sleep(led_toggle_delay_ms); } } diff --git a/src/mcu.c b/src/mcu.cpp similarity index 66% rename from src/mcu.c rename to src/mcu.cpp index 12b6877..35df8e2 100644 --- a/src/mcu.c +++ b/src/mcu.cpp @@ -1,12 +1,12 @@ /** - * @file mcu.c + * @file mcu.cpp * * @brief MCU related */ #include -#include "mcu.h" +#include "mcu.hpp" #include "gpio.h" #include "main.h" @@ -15,7 +15,8 @@ * Public Function Body Definitions *****************************************/ -void mcu_init(void) { +namespace hal { +void mcu::init(void) { HAL_Init(); SystemClock_Config(); @@ -23,10 +24,7 @@ void mcu_init(void) { MX_GPIO_Init(); } -void mcu_sleep(uint32_t ms) { +void mcu::sleep(uint32_t ms) { HAL_Delay(ms); } - -void led_toggle(void) { - HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } diff --git a/tests/bin/test_main.c b/tests/bin/test_main.cpp similarity index 73% rename from tests/bin/test_main.c rename to tests/bin/test_main.cpp index 4cc9e4b..76447bd 100644 --- a/tests/bin/test_main.c +++ b/tests/bin/test_main.cpp @@ -1,11 +1,11 @@ /** - * @file test_main.c + * @file test_main.cpp * * @brief Main function for tests. */ #include -#include "tests_core.h" +#include "test_core.hpp" int main(void) { test_core_init(); diff --git a/tests/inc/test_core.h b/tests/inc/test_core.hpp similarity index 94% rename from tests/inc/test_core.h rename to tests/inc/test_core.hpp index 058dc8b..e81243f 100644 --- a/tests/inc/test_core.h +++ b/tests/inc/test_core.hpp @@ -1,5 +1,5 @@ /** - * @file test_core.h + * @file test_core.hpp * * @brief Core functions to the test * diff --git a/tests/src/test_core.c b/tests/src/test_core.cpp similarity index 74% rename from tests/src/test_core.c rename to tests/src/test_core.cpp index 3387397..ed9f909 100644 --- a/tests/src/test_core.c +++ b/tests/src/test_core.cpp @@ -1,5 +1,5 @@ /** - * @file test_core.c + * @file test_core.cpp * * @brief Core functions to the test * @@ -9,13 +9,13 @@ * */ -#include "test_core.h" -#include "mcu.h" +#include "test_core.hpp" +#include "mcu.hpp" /***************************************** * Public Functions Bodies Definitions *****************************************/ void test_core_init(void) { - mcu_init(); + hal::mcu::init(); }