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

Merge pull request #285 from Hish15/P2H/Blinky++

Add blinky C++ example
Julien Staub 3 лет назад
Родитель
Сommit
17be60bc9a
4 измененных файлов с 83 добавлено и 7 удалено
  1. 1 1
      README.md
  2. 16 6
      examples/blinky/CMakeLists.txt
  3. 2 0
      examples/blinky/blinky.c
  4. 64 0
      examples/blinky/blinky.cpp

+ 1 - 1
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.

+ 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;
+}