使用Platformio平臺的libopencm3開發框架來開發STM32G0,下面為使用FreeRTOS系統。
1 新建項目
- 在PIO主頁新建項目,框架選擇libopencm3,開發板選擇 MonkeyPi_STM32_G070RB;
- 新建完成后在src目錄新建主程序文件main.c;
- 然后更改項目文件platformio.ini的燒寫和調試方式:
1upload_protocol = cmsis-dap
2debug_tool = cmsis-dap
2 添加FreeRTOS源碼
- 下載FreeRTOS源碼
在freertos網站:https://www.freertos.org/a00104.html 下載好源碼,這里下載最新版202112.00;
- 添加源碼到項目:
直接拷貝源碼下的FreeRTOS文件夾到項目的lib目錄中;
- 添加到項目編譯
在lib目錄的FreeRTOS文件夾下新建 library.json 文件,內容如下:
1{
2 "name": "FreeRTOS",
3 "version": "202112.00",
4 "build": {
5 "flags": [
6 "-Isource",
7 "-Isource/include",
8 "-Isource/portable/GCC/ARM_CM0",
9 "-Isource/portable/RVDS/ARM_CM0"
10 ],
11 "srcFilter": [
12 "+,
13 "+,
14 "+
15 ]
16 }
17}
- 添加FreeRTOS配置
從FreeRTOS源碼下的demo目錄中拷貝一個FreeRTOSConfig.h文件到 lib/FreeRTOS/Source目錄,并更改為如下內容:
1/**
2 * @file FreeRTOSConfig.h
3 *
4 * http://www.FreeRTOS.org
5 * http://aws.amazon.com/freertos
6 *
7 */
8
9#ifndef FREERTOS_CONFIG_H
10#define FREERTOS_CONFIG_H
11
12/*-----------------------------------------------------------
13 * Application specific definitions.
14 *
15 * These definitions should be adjusted for your particular hardware and
16 * application requirements.
17 *
18 * These parameters and more are described within the 'configuration' section of the
19 * FreeRTOS API documentation available on the FreeRTOS.org web site.
20 *
21 * See http://www.freertos.org/a00110.html
22 *----------------------------------------------------------*/
23
24/* USER CODE BEGIN Includes */
25/* Section where include file can be added */
26/* USER CODE END Includes */
27
28/* Ensure definitions are only used by the compiler, and not by the assembler. */
29#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
30 #include
31#endif
32#define configENABLE_FPU 0
33#define configENABLE_MPU 0
34
35#define configUSE_PREEMPTION 1
36#define configSUPPORT_STATIC_ALLOCATION 0
37#define configSUPPORT_DYNAMIC_ALLOCATION 1
38#define configUSE_IDLE_HOOK 0
39#define configUSE_TICK_HOOK 0
40#define configCPU_CLOCK_HZ ( ( unsigned long ) 64000000 )
41#define configTICK_RATE_HZ ((TickType_t)1000)
42#define configMAX_PRIORITIES ( 7 )
43#define configMINIMAL_STACK_SIZE ((uint16_t)256)
44#define configTOTAL_HEAP_SIZE ((size_t)1024*16)
45#define configMAX_TASK_NAME_LEN ( 16 )
46#define configUSE_16_BIT_TICKS 0
47#define configUSE_MUTEXES 1
48#define configQUEUE_REGISTRY_SIZE 8
49#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
50
51/*Software timer related definitions. */
52#define configUSE_TIMERS 1
53#define configTIMER_TASK_PRIORITY 5
54#define configTIMER_QUEUE_LENGTH 10
55#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
56
57/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */
58/* Defaults to size_t for backward compatibility, but can be changed
59 if lengths will always be less than the number of bytes in a size_t. */
60#define configMESSAGE_BUFFER_LENGTH_TYPE size_t
61/* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */
62
63/* Co-routine definitions. */
64#define configUSE_CO_ROUTINES 0
65#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
66
67/* Set the following definitions to 1 to include the API function, or zero
68to exclude the API function. */
69#define INCLUDE_vTaskPrioritySet 1
70#define INCLUDE_uxTaskPriorityGet 1
71#define INCLUDE_vTaskDelete 1
72#define INCLUDE_vTaskCleanUpResources 0
73#define INCLUDE_vTaskSuspend 1
74#define INCLUDE_vTaskDelayUntil 0
75#define INCLUDE_vTaskDelay 1
76#define INCLUDE_xTaskGetSchedulerState 1
77
78/* Normal assert() semantics without relying on the provision of an assert.h
79header file. */
80/* USER CODE BEGIN 1 */
81//void vAssertCalled(const char *file, int line);
82//#define configASSERT( x ) if( x == 0 ) { taskDISABLE_INTERRUPTS(); vAssertCalled(__FILE__,__LINE__); for(;;); }
83/* USER CODE END 1 */
84
85/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
86standard names. */
87#define vPortSVCHandler sv_call_handler
88#define xPortPendSVHandler pend_sv_handler
89
90/* IMPORTANT: This define is commented when used with STM32Cube firmware, when the timebase source is SysTick,
91 to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
92
93#define xPortSysTickHandler sys_tick_handler
94
95/* USER CODE BEGIN Defines */
96/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
97/* USER CODE END Defines */
98
99#endif /* FREERTOS_CONFIG_H */
上面配置需要根據自己的需要進行更改,如CPU頻率,是否需要打開某項功能等;
- 配置好后的目錄結構如下:
image-20220910154501074
3 FreeRTOS示例
- 首先需要設置FreeRTOS需要的時鐘
1/**
2 * @brief systick setup for rtos tick
3 */
4static void systick_setup(void)
5{
6 systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
7 systick_set_reload(64*1000);
8
9 systick_interrupt_enable();
10
11 /* Start counting. */
12 systick_counter_enable();
13}
這樣在FreeRTOSConfig.h 文件中定義的
1#define xPortSysTickHandler sys_tick_handler
就可以通過systick中斷提供FreeRTOS時鐘;
- LED任務函數
1static void led1_task(void *args)
2{
3 rcc_periph_clock_enable(RCC_GPIOC);
4 gpio_mode_setup(GPIOC,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO12);
5
6 while (1)
7 {
8 gpio_toggle(GPIOC,GPIO12);
9 vTaskDelay(pdMS_TO_TICKS(500));
10 }
11}
- 主程序中創建任務并開啟多任務調度
1#include
2#include
3#include
4
5#include "FreeRTOS.h"
6#include "task.h"
7
8
9int main(void)
10{
11 //system clock
12 rcc_clock_setup(&rcc_clock_config[RCC_CLOCK_CONFIG_HSI_PLL_64MHZ]);
13
14 systick_setup();
15
16 xTaskCreate(led_task,"led task", 256, NULL,2,NULL);
17
18 vTaskStartScheduler();
19
20 while(1){}
21
22 return 0;
23}
通過xTaskCreate創建多任務函數,然后使用 vTaskStartScheduler 開始調度;
注:如果VSCode中提示 FreeRTOS.h 頭文件include path問題,可以將項目文件夾關閉,再重新打開即可;如果是在Clion中,可以在添加lib文件夾后,右鍵platformio.ini文件選擇re init即可;
4 燒寫測試
點擊 PlatformIO:Upload按鈕或Ctrl+Alt+U快捷鍵可以直接編譯燒寫到目標板,看到LED按預定的程序閃爍運行。
-
STM32
+關注
關注
2270文章
10900瀏覽量
355927 -
源碼
+關注
關注
8文章
640瀏覽量
29204 -
函數
+關注
關注
3文章
4331瀏覽量
62596 -
開發板
+關注
關注
25文章
5048瀏覽量
97442 -
FreeRTOS
+關注
關注
12文章
484瀏覽量
62166
發布評論請先 登錄
相關推薦
評論