|
使用Platformio平台的libopencm3开发框架来开发STM32G0,下面为使用FreeRTOS系统。 * ?7 g5 x( H% }
1 新建项目在PIO主页新建项目,框架选择libopencm3,开发板选择 MonkeyPi_STM32_G070RB; 新建完成后在src目录新建主程序文件main.c; 然后更改项目文件platformio.ini的烧写和调试方式:
, k; v- q" @1 f' [
- 1upload_protocol = cmsis-dap" i. l- J5 ^4 I, t4 g9 K' Y
- 2debug_tool = cmsis-dap
复制代码 4 N( ^/ ^* S0 |# }1 y
5 }3 a3 ~; c$ l+ M& z$ f
2 添加FreeRTOS源码下载FreeRTOS源码 0 D* {: M$ ]4 r3 D$ f% R. z" [8 ]
3 G: [" i; b& O. i直接拷贝源码下的FreeRTOS文件夹到项目的lib目录中; D1 G3 F4 l ^3 n, r" L
添加到项目编译 + {0 q, C8 J) Y2 a3 ?9 e
在lib目录的FreeRTOS文件夹下新建 library.json 文件,内容如下: - 1{
& ?8 Q! A8 _8 m& ` - 2 "name": "FreeRTOS",
/ ~3 v5 K7 m& Z& O - 3 "version": "202112.00",( \% Z: @; Z3 b/ }
- 4 "build": {
/ U3 V M. g* X" M; {4 w7 U - 5 "flags": [4 F- J. B' @# ]: f8 w! K$ Y
- 6 "-Isource",
# U4 ]. } |! x - 7 "-Isource/include",
+ Q1 ?1 }9 n- H4 ]. y5 w3 b% E0 z - 8 "-Isource/portable/GCC/ARM_CM0",
6 i, S3 O6 Y/ H" ?* ~6 \: ^ - 9 "-Isource/portable/RVDS/ARM_CM0"
8 L/ v. i8 |- F' U: O$ M% I( R - 10 ],1 z4 x- \% \) j8 p+ G) L- R
- 11 "srcFilter": [1 @9 ?; u- M8 V: l. d
- 12 "+<source/*.c>",- D0 N& Z {' j
- 13 "+<source/portable/GCC/ARM_CM0/*.c>",; X5 U: p4 o9 X; b' C
- 14 "+<source/portable/MemMang/heap_4.c>"
. N- ~& ?" {( Z1 w2 O$ _ - 15 ]
/ j7 W8 E& M/ X, @9 t3 y - 16 }* I. _/ {+ n( F+ U/ Y
- 17}
复制代码添加FreeRTOS配置
4 w- S/ w/ s1 R8 \- A/ K5 Z! u
从FreeRTOS源码下的demo目录中拷贝一个FreeRTOSConfig.h文件到 lib/FreeRTOS/Source目录,并更改为如下内容: - 1/**7 C0 E& |7 ]4 G$ }1 c# j
- 2 * @file FreeRTOSConfig.h
( m3 O$ \# ]8 q5 F* h3 M - 3 *
! O6 i$ ~" _: I6 | - 4 * http://www.FreeRTOS.org
+ \8 O+ j+ h2 m# V - 5 * http://aws.amazon.com/freertos
; ~+ F" U% f0 D4 i% R - 6 * ) N) V% X) K0 @& ]- I; \
- 7 */. G" e: [4 W' ^% S) l& P; l* X
- 80 F0 h$ r I/ l* F
- 9#ifndef FREERTOS_CONFIG_H
. ?, O: D& s" F3 q2 b. i - 10#define FREERTOS_CONFIG_H/ l7 S1 n6 J2 y7 q7 k+ a
- 11
* k; m; j# q+ n- c - 12/*-----------------------------------------------------------, @4 ]5 F& T% y6 i
- 13 * Application specific definitions.. e$ ^% D' A0 _' m( r
- 14 *: ?* G0 H4 m+ C, k
- 15 * These definitions should be adjusted for your particular hardware and& [2 E8 a8 o, H6 u, |2 U) M. t, N
- 16 * application requirements.
, @' ]* d! X2 D" S: P - 17 *9 b* T* N% v0 z
- 18 * These parameters and more are described within the 'configuration' section of the
; q! C2 l) z G/ L$ f& J - 19 * FreeRTOS API documentation available on the FreeRTOS.org web site.4 K3 H/ T( |' S
- 20 *" h3 U: b! t/ b6 F" l w
- 21 * See http://www.freertos.org/a00110.html
7 A+ z3 f4 F# }9 T% z; e - 22 *----------------------------------------------------------*/
8 N* d+ e. F; m0 \2 R( r) s - 23
* W4 j% S. T* R( J, a ` - 24/* USER CODE BEGIN Includes */
, D" S6 K3 B/ l) j - 25/* Section where include file can be added */
, S- T) N5 _, I t: s4 c- N& n7 k$ q - 26/* USER CODE END Includes */9 S9 a$ [. K2 i/ r8 E, y. n5 ?
- 27
# V1 v; H$ K7 b. C+ [% D% R - 28/* Ensure definitions are only used by the compiler, and not by the assembler. */% ]/ `) C1 N3 Y9 s) K2 B
- 29#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
3 X$ s. {1 ^7 f - 30 #include <stdint.h>
2 M) D9 |9 ]- C/ V$ S - 31#endif
3 J2 a$ N/ k( c0 y( Q( |9 B - 32#define configENABLE_FPU 0
; k) v2 ?1 t, W* g* b( k( P - 33#define configENABLE_MPU 0
; W1 Q( W U2 ]! H7 X - 34/ d: S3 x1 ^ \( F7 Y4 o& e
- 35#define configUSE_PREEMPTION 1& Y! r2 {5 r2 C8 F9 f0 i1 z
- 36#define configSUPPORT_STATIC_ALLOCATION 0
9 w; K( P/ D( u; p' g1 k - 37#define configSUPPORT_DYNAMIC_ALLOCATION 1! J7 k8 H6 ?2 m( M; c' r
- 38#define configUSE_IDLE_HOOK 0( T* q; Y; Q B( i' c) H
- 39#define configUSE_TICK_HOOK 0
6 A+ `: z2 p: c1 Y) d( M8 k - 40#define configCPU_CLOCK_HZ ( ( unsigned long ) 64000000 )
6 j0 T4 v5 J( q! V. {8 A7 V! \ - 41#define configTICK_RATE_HZ ((TickType_t)1000)
! \: `1 D; S* r8 R- i @ - 42#define configMAX_PRIORITIES ( 7 )
6 N" H, q$ R" D# F5 _1 Q S - 43#define configMINIMAL_STACK_SIZE ((uint16_t)256)( k: N; Q8 i5 ~* v6 C
- 44#define configTOTAL_HEAP_SIZE ((size_t)1024*16)
; G' u. L4 ^$ `# ]9 Y: d K - 45#define configMAX_TASK_NAME_LEN ( 16 )
D/ z. h8 E* |' m, |: M3 D - 46#define configUSE_16_BIT_TICKS 0
* Z" c/ u* F5 O: o - 47#define configUSE_MUTEXES 1* W; J1 U6 s! f$ }
- 48#define configQUEUE_REGISTRY_SIZE 8
/ N& W2 m% q, V( N- J8 C - 49#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0: z- j- P- c" A4 t; j0 I
- 50
2 E5 R; w7 ?/ K9 N - 51/*Software timer related definitions. */
2 H$ h3 |8 s# r$ U' N - 52#define configUSE_TIMERS 17 w+ e" v2 t5 `$ P: {/ r
- 53#define configTIMER_TASK_PRIORITY 5
( {# ?2 g3 v2 G% p5 B f - 54#define configTIMER_QUEUE_LENGTH 10
5 K; x! W3 y$ k3 `/ y: H) z9 e4 M6 ] - 55#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
" H# o- ^- ~, w, U$ L5 F0 G8 h - 56
! g' X% q3 x6 f3 l - 57/* USER CODE BEGIN MESSAGE_BUFFER_LENGTH_TYPE */% a. w6 [: X3 G- w
- 58/* Defaults to size_t for backward compatibility, but can be changed ?- a* w6 T( V9 ~2 I/ W, o5 U
- 59 if lengths will always be less than the number of bytes in a size_t. */7 l$ S, C* K% M2 w
- 60#define configMESSAGE_BUFFER_LENGTH_TYPE size_t2 m! M: A( |* C) f
- 61/* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */6 f( o) g( f7 R5 w8 U
- 62
* E- s; X% _6 l, \: P* ~8 q/ V4 E - 63/* Co-routine definitions. *// b# V5 Z" p. V% N- m' a
- 64#define configUSE_CO_ROUTINES 0
B a4 W: H+ h - 65#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
2 b7 d0 f6 Z: ? - 66
" e' z7 V" R, v* E7 k& L - 67/* Set the following definitions to 1 to include the API function, or zero
& X/ }' _9 I) O2 m, v - 68to exclude the API function. */! _, I7 I( I& Y
- 69#define INCLUDE_vTaskPrioritySet 1
* I0 j6 ^+ v2 H5 Z. C) r - 70#define INCLUDE_uxTaskPriorityGet 1) P& \5 f0 B8 p+ H9 S4 I/ `: W( |
- 71#define INCLUDE_vTaskDelete 16 {3 Y; l" [4 k( @
- 72#define INCLUDE_vTaskCleanUpResources 0) g+ m o. p4 [! d* H
- 73#define INCLUDE_vTaskSuspend 1% B- J g( n: f" o5 v" [1 r2 j
- 74#define INCLUDE_vTaskDelayUntil 0
/ o0 j3 z( X. M9 ?# t- b& C* s - 75#define INCLUDE_vTaskDelay 1 L5 S2 ~9 Y: o
- 76#define INCLUDE_xTaskGetSchedulerState 1 K" h: f/ ?6 }5 H+ U
- 77: c! N) {% {. n$ r/ F! c
- 78/* Normal assert() semantics without relying on the provision of an assert.h% r1 ?+ b+ F% e9 \
- 79header file. */$ S; F2 [! n7 l" g5 }3 o9 U
- 80/* USER CODE BEGIN 1 */
( P! @/ A) p# i+ } - 81//void vAssertCalled(const char *file, int line);! ~7 J2 H) B7 w. R/ K a
- 82//#define configASSERT( x ) if( x == 0 ) { taskDISABLE_INTERRUPTS(); vAssertCalled(__FILE__,__LINE__); for(;;); }
4 N' D" t" a1 A2 V5 r/ t% x% j/ i - 83/* USER CODE END 1 */
! m q; O, n( K% a: ]7 [" y - 84
; u* L, C5 K: N, V - 85/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
6 y: Q( e: N! n/ O - 86standard names. */, ] H3 f9 F Q& N( |# j# ~2 a
- 87#define vPortSVCHandler sv_call_handler
! [" F5 _1 D. Y' L: r - 88#define xPortPendSVHandler pend_sv_handler2 K2 u3 ]' e' ^# z# h
- 89' j; y7 L& P! @1 h7 b9 R7 A8 L, d8 n
- 90/* IMPORTANT: This define is commented when used with STM32Cube firmware, when the timebase source is SysTick,! i0 F" ^# r/ y/ U+ [% a6 q+ F
- 91 to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
! r" [% m, T' {' O - 92
. M) j7 e4 l- ~# L. O - 93#define xPortSysTickHandler sys_tick_handler
5 B) L8 `7 e$ y5 L2 D - 94% A7 W0 g; {0 H
- 95/* USER CODE BEGIN Defines */, [6 ^- p4 T% A: j2 q
- 96/* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */
" [1 n R( N. A* {& E - 97/* USER CODE END Defines */
' C) b* t8 I* Q* @9 B3 q - 98* }; O9 a& o+ T" Q6 l# u
- 99#endif /* FREERTOS_CONFIG_H */
复制代码 4 p6 s* s* ^- Z9 C% Z2 ^+ B. Y2 f
上面配置需要根据自己的需要进行更改,如CPU频率,是否需要打开某项功能等; 配置好后的目录结构如下: ( y2 Y" j" Z# I, x
$ @; \8 x$ w* B- k1 O
3 FreeRTOS示例首先需要设置FreeRTOS需要的时钟 0 I3 W7 z7 g3 `. _/ S
- 1/**+ q2 a7 p0 ^, S x9 H* W
- 2 * @brief systick setup for rtos tick. Q& R' o+ k v
- 3 */- _. j+ a1 E( V% R8 ^2 ]' D" m5 E
- 4static void systick_setup(void)
2 l8 I/ r* Y) ?& e2 C - 5{
9 x# ?5 x5 i$ y. k - 6 systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); O1 w" t! V0 u4 `
- 7 systick_set_reload(64*1000);, \, N$ c4 h+ y6 {
- 8
: r0 W( ^( C7 n. q W4 H. t) m - 9 systick_interrupt_enable();
- C% _$ }& W0 T% \0 H' @) a6 v, D - 10
2 W7 u9 @+ ?( |: g2 V. l% k - 11 /* Start counting. */
7 ]1 [$ M3 C Y" o- a0 p - 12 systick_counter_enable();' Q* j9 l7 S: E* @: A& A: Z, o& D) [
- 13}
复制代码
; a0 o; @% t6 n8 O- E; K7 G6 r9 I c" A
这样在FreeRTOSConfig.h 文件中定义的 - 1#define xPortSysTickHandler sys_tick_handler
复制代码
7 @8 d1 O1 y& w, o+ u就可以通过systick中断提供FreeRTOS时钟; LED任务函数 . g6 g5 v: i9 L. E6 ^( P& ~* k
- 1static void led1_task(void *args); g9 Z9 z; D( a7 ~! x' K
- 2{
( A! N. { H) F6 ~) @. h. a - 3 rcc_periph_clock_enable(RCC_GPIOC);8 @8 a. g& W/ r6 `
- 4 gpio_mode_setup(GPIOC,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO12);/ [" i( U5 D; y7 Y6 X- d$ X3 v
- 54 ?9 T. _6 c8 K" s. C+ w. q
- 6 while (1)/ S% C7 R f2 a1 i9 P& `) B
- 7 {
* C. i9 t8 V, f! i. ~ - 8 gpio_toggle(GPIOC,GPIO12);( I- y$ a0 Y/ b- Y, D3 \
- 9 vTaskDelay(pdMS_TO_TICKS(500));
+ y* j3 K9 u9 n9 S7 Q - 10 }
4 r/ Z J# P3 }( l - 11}
复制代码
1 K: D: O" Y7 ~$ O) k, k: q
5 b7 B2 ? P6 a' R: m# r主程序中创建任务并开启多任务调度 # N+ X7 Y9 t: r' u
- 1#include <libopencm3/stm32/rcc.h>
1 u& w1 R2 ^; v7 s3 R - 2#include <libopencm3/stm32/gpio.h>
- `, T) G* p6 X% I+ O9 M; w' z - 3#include <libopencm3/cm3/systick.h>2 }+ J; r2 a, n i) U! c/ j
- 47 j+ A" P W. P) b# f( n
- 5#include "FreeRTOS.h"
6 h9 u& Z6 |: n a1 T% e - 6#include "task.h"
9 W; a: J z4 L8 J/ w2 Y9 W - 7% C: ~3 N* u! |; _) N" \' E, m
- 8
/ s7 _" V/ J* i6 E7 Q2 X- n& ?- ~ - 9int main(void)
# N# H, C7 G+ F, M - 10{4 k+ j v) y5 P! n: s3 Q
- 11 //system clock
7 A1 H6 }# K# t. r7 ~* ^- ^2 Z4 x2 g - 12 rcc_clock_setup(&rcc_clock_config[RCC_CLOCK_CONFIG_HSI_PLL_64MHZ]);
) u p, f# v6 M! N N4 ~" J2 f& d - 13
$ a" _0 r" x1 v4 l4 y/ a+ G - 14 systick_setup(); W/ R$ w* u& P2 U7 ]; v) j+ v& }
- 15) m( U8 Q3 C N. l7 L5 f6 x
- 16 xTaskCreate(led_task,"led task", 256, NULL,2,NULL);4 \( c U; W m# x; q
- 17
% Y9 E, t$ c/ o B: u( Q - 18 vTaskStartScheduler();
- h6 a8 R- }5 K' W - 19
1 y0 |8 V, i$ ]- ~ - 20 while(1){}' `4 p7 p; f( s
- 21# q* W5 m! S( N8 y* ~9 K
- 22 return 0;8 C. a/ O! C6 E+ Y: y8 K" M, ~
- 23}
复制代码
& q$ l6 U: ^: x" B- P1 t2 \+ B2 l8 X9 o v% v$ e9 ]
通过xTaskCreate创建多任务函数,然后使用 vTaskStartScheduler 开始调度; 注:如果VSCode中提示 FreeRTOS.h 头文件include path问题,可以将项目文件夹关闭,再重新打开即可;如果是在Clion中,可以在添加lib文件夹后,右键platformio.ini文件选择re init即可; & U& `% [8 a; p a8 m* I
4 烧写测试点击 PlatformIO:Upload按钮或Ctrl+Alt+U快捷键可以直接编译烧写到目标板,看到LED按预定的程序闪烁运行;
* K) v/ }' f/ D2 Y, t4 J2 M+ b* a. c
转载自: MakerInChina.cn
$ D" D# }: j0 g7 {$ r |