Просмотр исходного кода

Merge branch 'master' into jj-mp1

Hish15 3 лет назад
Родитель
Сommit
b5487d79e2

+ 3 - 3
README.md

@@ -32,7 +32,7 @@ It uses cmake and GCC, along with newlib (libc), STM32Cube. Supports F0 F1 F2 F3
 * `blinky` ([examples/blinky](examples/blinky)) - blink led using STM32 HAL library and SysTick.
    It will compile a project for the `F4` family by default, but you can also compile for the
    `L0` and `F1` family by passing `BLINKY_L0_EXAMPLE=ON` or `BLINKY_F1_EXAMPLE=ON` to the CMake
-   generation call.
+   generation call. Using C++ instead of C is possible using `USE_CPP_FILE=ON`.
 * `freertos` ([examples/freertos](examples/freertos)) - blink led using STM32 HAL library and FreeRTOS.
    You need to specify at least one board by passing `FREERTOS_<BOARD>_EXAMPLE=ON` to CMake.
    Currently, the example can be built for the `H743ZI` and `F407VG` board targets.
@@ -109,8 +109,8 @@ STM32WB is a multi-cores device even if the second core is not accessible by end
 CMSIS consists of three main components:
 
 * Family-specific headers, e.g. `stm32f4xx.h`
-* Device type-specific startup sources (e.g. `startup_stm32f407xx.s`)
-* Device-specific linker scripts which requires information about memory sizes
+* Device type-specific startup sources (e.g. `startup_stm32f407xx.s`) (if ASM language is enabled)
+* Device-specific linker scripts which requires information about memory sizes (if ASM language is enabled)
 
 stm32-cmake uses modern CMake features notably imported targets and target properties.
 Every CMSIS component is CMake's target (aka library), which defines compiler definitions, compiler flags, include dirs, sources, etc. to build and propagate them as dependencies. So in a simple use-case all you need is to link your executable with library `CMSIS::STM32::<device>`:

+ 7 - 1
cmake/FindCMSIS.cmake

@@ -235,7 +235,13 @@ foreach(COMP ${CMSIS_FIND_COMPONENTS_FAMILIES})
         stm32_get_chip_type(${FAMILY} ${DEVICE} TYPE)
         string(TOLOWER ${DEVICE} DEVICE_L)
         string(TOLOWER ${TYPE} TYPE_L)
-
+        
+        get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
+        if(NOT "ASM" IN_LIST languages)
+            message(STATUS "FindCMSIS: Not generating target for startup file and linker script because ASM language is not enabled")
+            continue()
+        endif()
+        
         find_file(CMSIS_${FAMILY}${CORE_U}_${TYPE}_STARTUP
             NAMES startup_stm32${TYPE_L}.s 
                   startup_stm32${TYPE_L}${CORE_Ucm}.s

+ 9 - 8
cmake/stm32/common.cmake

@@ -19,15 +19,16 @@ if(NOT STM32_TOOLCHAIN_PATH)
         message(STATUS "Detected toolchain path STM32_TOOLCHAIN_PATH in environmental variables: ")
         message(STATUS "$ENV{STM32_TOOLCHAIN_PATH}")
         set(STM32_TOOLCHAIN_PATH $ENV{STM32_TOOLCHAIN_PATH})
-    endif()
-    if(NOT CMAKE_C_COMPILER)
-        set(STM32_TOOLCHAIN_PATH "/usr")
-        message(STATUS "No STM32_TOOLCHAIN_PATH specified, using default: " ${STM32_TOOLCHAIN_PATH})
     else()
-        # keep only directory of compiler
-        get_filename_component(STM32_TOOLCHAIN_PATH ${CMAKE_C_COMPILER} DIRECTORY)
-        # remove the last /bin directory
-        get_filename_component(STM32_TOOLCHAIN_PATH ${STM32_TOOLCHAIN_PATH} DIRECTORY)
+        if(NOT CMAKE_C_COMPILER)
+            set(STM32_TOOLCHAIN_PATH "/usr")
+            message(STATUS "No STM32_TOOLCHAIN_PATH specified, using default: " ${STM32_TOOLCHAIN_PATH})
+        else()
+            # keep only directory of compiler
+            get_filename_component(STM32_TOOLCHAIN_PATH ${CMAKE_C_COMPILER} DIRECTORY)
+            # remove the last /bin directory
+            get_filename_component(STM32_TOOLCHAIN_PATH ${STM32_TOOLCHAIN_PATH} DIRECTORY)
+        endif()
     endif()
     file(TO_CMAKE_PATH "${STM32_TOOLCHAIN_PATH}" STM32_TOOLCHAIN_PATH)
 endif()

+ 16 - 6
examples/blinky/CMakeLists.txt

@@ -1,14 +1,24 @@
 cmake_minimum_required(VERSION 3.16)
 set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/stm32_gcc.cmake)
 
-project(stm32-blinky C ASM)
-set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
-
 # Configure here which STM32 target(s) to build
 option(BLINKY_F4_EXAMPLE "Compile F4 example" ON)
 option(BLINKY_F1_EXAMPLE "Compile F1 example" OFF)
 option(BLINKY_L0_EXAMPLE "Compile L0 example" OFF)
 
+option(USE_CPP_FILE "Use the .cpp example file instead of the .c one" OFF)
+
+if(USE_CPP_FILE)
+    project(stm32-blinky C CXX ASM)
+    set(MAIN_SOURCE_FILE blinky.cpp)
+else()
+    project(stm32-blinky C ASM)
+    set(MAIN_SOURCE_FILE blinky.c)
+endif()
+
+set(CMAKE_INCLUDE_CURRENT_DIR TRUE)
+
+
 set(HAL_COMP_LIST RCC GPIO CORTEX)
 set(CMSIS_COMP_LIST "")
 
@@ -41,7 +51,7 @@ find_package(HAL COMPONENTS "${HAL_COMP_LIST}" REQUIRED)
 
 # STM32F4-Discovery
 if(BLINKY_F4_EXAMPLE)
-    add_executable(stm32-blinky-f4 blinky.c stm32f4xx_hal_conf.h)
+    add_executable(stm32-blinky-f4 ${MAIN_SOURCE_FILE} stm32f4xx_hal_conf.h)
     target_link_libraries(stm32-blinky-f4
         HAL::STM32::F4::RCC
         HAL::STM32::F4::GPIO
@@ -54,7 +64,7 @@ endif()
 
 # STM32VL-Discovery
 if(BLINKY_F1_EXAMPLE)
-    add_executable(stm32-blinky-f1 blinky.c stm32f1xx_hal_conf.h)
+    add_executable(stm32-blinky-f1 ${MAIN_SOURCE_FILE} stm32f1xx_hal_conf.h)
     target_link_libraries(stm32-blinky-f1
         HAL::STM32::F1::RCC
         HAL::STM32::F1::GPIO
@@ -67,7 +77,7 @@ endif()
 
 # STM32L0538-Discovery
 if(BLINKY_L0_EXAMPLE)
-    add_executable(stm32-blinky-l0 blinky.c stm32l0xx_hal_conf.h)
+    add_executable(stm32-blinky-l0 ${MAIN_SOURCE_FILE} stm32l0xx_hal_conf.h)
     target_link_libraries(stm32-blinky-l0
         HAL::STM32::L0::RCC
         HAL::STM32::L0::GPIO

+ 2 - 0
examples/blinky/blinky.c

@@ -27,7 +27,9 @@ void SysTick_Handler(void)
 
     // 1 Hz blinking
     if ((HAL_GetTick() % 500) == 0)
+    {
         HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
+    }
 }
 
 void initGPIO()

+ 64 - 0
examples/blinky/blinky.cpp

@@ -0,0 +1,64 @@
+#if defined STM32L0
+    #include <stm32l0xx_hal.h>
+
+    // STM32L0538-Discovery green led - PB4
+    #define LED_PORT                GPIOB
+    #define LED_PIN                 GPIO_PIN_4
+    #define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOB_CLK_ENABLE
+#elif defined STM32F1
+    #include <stm32f1xx_hal.h>
+
+    // STM32VL-Discovery green led - PC9
+    #define LED_PORT                GPIOC
+    #define LED_PIN                 GPIO_PIN_9
+    #define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOC_CLK_ENABLE
+#elif defined STM32F4
+    #include <stm32f4xx_hal.h>
+
+    // STM32F4-Discovery green led - PD12
+    #define LED_PORT                GPIOD
+    #define LED_PIN                 GPIO_PIN_12
+    #define LED_PORT_CLK_ENABLE     __HAL_RCC_GPIOD_CLK_ENABLE
+#endif
+
+//This prevent name mangling for functions used in C/assembly files.
+extern "C"
+{
+    void SysTick_Handler(void)
+    {
+        HAL_IncTick();
+
+        // 1 Hz blinking
+        if ((HAL_GetTick() % 500) == 0)
+        {
+            HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
+        }
+    }
+}
+
+void initGPIO()
+{
+    GPIO_InitTypeDef GPIO_Config;
+
+    GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
+    GPIO_Config.Pull = GPIO_NOPULL;
+    GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
+
+    GPIO_Config.Pin = LED_PIN;
+
+    LED_PORT_CLK_ENABLE();
+    HAL_GPIO_Init(LED_PORT, &GPIO_Config);
+}
+
+int main(void)
+{
+    HAL_Init();
+    initGPIO();
+    // 1kHz ticks
+    HAL_SYSTICK_Config(SystemCoreClock / 1000);
+
+    for (;;)
+        __WFI();
+
+    return 0;
+}