二值信号量的用法示例
; W( L7 m4 Q$ a, A1.创建好带有printf-stdarg.c的FreeRTOS的工程
+ a$ I9 D* ?7 n6 b0 x F! q6 ]
" h7 _; w3 c4 @* F/ Y) l2.修改main.c文件. G: o" B! H1 D1 @9 u& [* F
$ t$ [9 |' Y1 u# u L' \, w! j- /* USER CODE BEGIN Includes */, r5 F/ X% h3 \4 r7 ^, u
# b. E0 L1 [5 \0 p6 O' ~- #include "FreeRTOS.h"
7 y4 T' ^0 y0 x) B( _ - #include "task.h"
# C; m R3 s" _ - #include "queue.h"
9 J9 x+ d. R- c0 F; c - #include "semphr.h". l- f' x! l2 n6 M. Q) t p
- #include <stdio.h>3 v8 @5 Y+ P2 l
- #include <string.h>
; x1 x, k |/ x8 R" A i" S @" n - 2 p/ ~" P* j. t5 `! V! E6 q5 r
- /* USER CODE END Includes */4 }% z8 o- r) j+ }# q: A6 l9 [
- /* USER CODE BEGIN PV */
$ u/ W0 m; ^+ Y9 K$ n! J! L
5 T; g! e$ n# H' [+ L4 c- static TaskHandle_t AppTaskCreate_Handle = NULL;2 m- j0 N! i2 u! H3 m1 l: R
- static TaskHandle_t KeyTask_Handle = NULL;
: ~3 M4 I" v+ r$ b# c6 M6 n - static TaskHandle_t Led1Task_Handle = NULL;
; L& [+ h! d- e& P6 ]7 \- J0 W
8 J1 k5 n) F9 V7 b/ p- /*Create a binary semaphore handle*/* g6 k, @5 B; {, ?+ l* ^
- static SemaphoreHandle_t Binary_Handle = NULL;+ Q! i: d0 K& ]! `( w! l1 Z
- 1 i7 L9 j5 V, R# h/ z
- extern int f_printf(const char *format, ...);$ M5 B& ]! N, f" M1 a9 H
- ( t, G% B, H8 R( H2 A
- /* USER CODE END PV */5 i1 y4 A( c O/ _% Y5 L
- /* USER CODE BEGIN PFP */
, \9 r. B3 B; q( }7 K3 T8 ~$ u3 Q7 B - 2 Z @, L! D9 @
- /*Create a task function*/
\ [' X* q9 E9 c6 s - static void AppTaskCreate(void);
9 Z8 v e0 U m, B( X - static void KeyTaskCreate(void);
7 N1 F7 G: h0 s+ L/ L; Z% |) F - static void Led1TaskCreate(void);
. |0 U* k. j$ m/ q - 3 V7 ~9 L: _+ z. g' z W
- /* USER CODE END PFP */
% k) ^6 R# q; Z5 A3 K
' [7 d$ x3 E/ K( d# o- int main()4 B" k4 e/ R+ S/ _
- {2 M# t/ n6 d2 p, t" v, F- ]
- /*
; H, {5 ^ z, a6 _( ^ - *& N6 s4 s- i" E# V
- */6 k7 d0 p& A0 p# j D( F _
- /* USER CODE BEGIN WHILE */
# K% t. O8 |; a" f9 d/ Q& | - UBaseType_t xReturn = pdPASS;
1 h- E; @9 d: I5 I( n - 5 F6 Q& ?* {1 L( k7 N0 Y
- xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
4 X4 x; A. ?" J1 k; M: C) l+ a- r6 ^ - (const char *) "APPTASKCREATE",
! X, }0 P, W# n" v$ R+ [9 O - (uint16_t) 128,5 o0 E5 z& c6 v8 Z5 B
- (void *) NULL,& x" [" S: U x0 T: z
- (UBaseType_t) 2,
& j1 y; E/ O" G1 F6 p - (TaskHandle_t *)&AppTaskCreate_Handle);
# ]0 ?% C( k4 H+ [4 F9 B2 l& _ - if(xReturn ==pdPASS)* `( v2 ^' [* k3 ~6 p8 a" {
- {/ f! U2 \: J0 i* v
- vTaskStartScheduler();
, m' l- w/ B/ d - f_printf("start..\r\n");* Y- x! X' Z1 a2 r4 j9 Q3 i# T2 j
- }
/ b' P# s/ F) _. w" n2 R, r - while (1)
+ ^* [$ x- {+ Y+ s' ?$ {( r: Y9 f - {' i, f. N- m+ I
- /* USER CODE END WHILE */
9 |: f5 K7 q" Y2 P+ Z$ z - 6 N2 |1 {' f# |
- /* USER CODE BEGIN 3 */
8 T, v4 O" y* O- ? - }! ]* J' Z4 q8 c) {8 w7 H, b
- /* USER CODE END 3 */9 b0 y% K0 O) A! E
# O3 |8 X& t* u5 o+ F' n
# h1 ^2 p* E" D2 G$ \5 B- }6 B3 O0 S) b. V. `
- 4 {; _, |8 u9 i$ g* }# Q5 c
- V0 Q$ ]4 ^8 o# D9 b- /* USER CODE BEGIN 4 */( F, i7 { m! g8 p( x$ i
- static void AppTaskCreate(void)
8 j9 F2 m, Z8 [ - {) ]8 X! T2 Z4 B* x
- UBaseType_t xReturn = pdPASS;
& V% z5 Y; u+ ^6 o2 L - 4 n q( h" e7 O5 K, R: [
( O) D( _3 ~4 T% T& [) |- xReturn = xTaskCreate((TaskFunction_t) KeyTaskCreate,$ z5 M* h) q7 `% s
- (const char *) "KEYTASKCREATE",
2 i2 a% u) Q" a H - (uint16_t) 128,
5 s) Q1 k8 ?4 w0 o - (void *) NULL,5 `' f! W" `! q$ Z0 [8 d
- (UBaseType_t ) 2,
" v$ v/ y" F( U: i - (TaskHandle_t *) &KeyTask_Handle);
$ q4 l6 e% `( K- D7 G - 7 Q8 k* F1 A# ?
- xReturn = xTaskCreate((TaskFunction_t) Led1TaskCreate,
7 g! J' r( R. w- J3 K - (const char *) "LED1TASKCREATE",; }" T6 Z4 k/ r* L, h5 N. V
- (uint16_t) 128,
" c, X+ ]: u4 `, | - (void *) NULL,$ v7 a- \& I; @
- (UBaseType_t) 1,- U$ A6 ?9 G% O% v8 V& g! l
- (TaskHandle_t *)&Led1Task_Handle);
& s' a: |- i9 G2 G, L1 h - + i) N% K5 [. G" z; P5 ^; G L I- a! F
- /*Create a binary semaphore*/% D$ J& F/ X L7 {9 Z: O6 c
- Binary_Handle = xSemaphoreCreateBinary();
( |! H$ v' j( R; C- J - if(Binary_Handle !=NULL)
9 h! W5 \- T4 w4 `7 C o - f_printf("Create Semph_Binary PASS.\r\n");
/ [8 ~5 p6 Q- X0 Q3 b' s -
+ S$ [6 p" e+ l) ?% X4 o+ `2 K - if(xReturn == pdPASS)
+ \ X( E7 N. O3 ?, j/ h! D - vTaskDelete((TaskHandle_t)AppTaskCreate_Handle);
3 h5 N' z* S9 A6 F$ Y - }: N' w0 p$ H: S. ~1 Q
- & M% d" R: s- ~7 R# d# T9 l
- static void KeyTaskCreate(void)
4 u# J- C- U8 P( G8 |- D7 d - {/ }3 u' o! H- ]6 \% z" K% A- ?
- while(1)
1 g( h! q( h+ `3 m - {
- z! {& v; c( V. p. _ f* l - UBaseType_t xReturn = pdPASS;4 `2 q- A' _3 A
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
7 o. Z7 m* D0 Q - {
$ P2 |3 Q+ P7 ?6 ]# F7 m8 Z g) V/ | - f_printf("KEY TASK PASS.\r\n");
, V' x7 Q, n0 d6 S q6 l - vTaskDelay(100);5 n( O0 C% K7 P; o: Z2 L6 u/ k
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
& A: g2 C8 N0 ^, w) _ - {
, w: R7 Q4 [1 l) f3 C4 _ - xReturn = xSemaphoreGive(Binary_Handle);: L' `% z+ H" E* ?
- if(xReturn == pdPASS)
; L! j9 X+ e4 t6 y# t- ]) D - f_printf("GIVE BINARY PASS.\r\n");
: A% o* V1 a$ C j# M/ _0 \ - else if(xReturn == errQUEUE_FULL)& G. {* x3 E- L( v& M7 \
- f_printf("QUEUE IS FULL.\r\n");
. C# g( Y( S( q! ] - }
4 Q+ b; Z( W. a) v7 R& C - }* j1 d% o1 k. y6 K9 o
-
& c1 |5 c, @1 d0 b - vTaskDelay(100);
; V( q7 n( b; ]) m5 r5 G* @( k- h - }. E* V' K, g; m' X' w
- ! ^( U8 @5 o# [
- }2 r& Y( S5 {: K# h5 ]: o: @7 W% X! W
- static void Led1TaskCreate(void)
+ v* j& I. y# x8 W$ G' Y$ d - {
8 Q- `# e3 E; `+ \# C2 H - while(1)4 j7 P8 G1 t, `0 { @9 X
- {4 }, V/ I. m5 Q' Q
- UBaseType_t xReturn = pdPASS;* R% ^7 J, V8 [- ~1 X0 v
-
0 X& N: |# h4 W; ?, h$ o" q. R - xReturn = xSemaphoreTake(Binary_Handle, portMAX_DELAY);* V6 I" p6 B$ l% ^
- if(xReturn == pdPASS)& h @% \7 u, ?+ j5 [# r+ ^" K' V
- {
2 T* V& b+ b! O - f_printf("TAKE BINARY PASS.\r\n");
' K9 Y' u N, ^% F - HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);
5 g9 t) D$ c0 I( V( i - }
; [% i- ?; H" X ~ -
5 v: ]' Z9 e" a - vTaskDelay(100);
+ Y+ T3 `7 D# C+ @! \ - }
! b2 R# a3 Z5 `0 e0 g, x - }& N' X7 e; E1 M, W, j' `6 ?
0 U, J2 E& s5 @, b5 q2 V' X! Z% O% v- /* USER CODE END 4 */
复制代码 r3 E, B+ @1 D& q; z1 g
计数信号量的用法示例8 P2 r. E; [, w9 z+ }
1.创建好带有printf-stdarg.c的FreeRTOS的工程- x2 c! R9 \; b% L' n% I
$ ~6 g4 G$ U. c( q$ E& p
2.修改main.c文件" R0 g0 |6 o! G3 i* {, o% P
0 `, J0 [( {+ N2 u8 C
- /* USER CODE BEGIN Includes */
P6 j) C- `- s4 _* r8 D. w
j$ T1 w0 {% p- H3 _) b/ K1 r! D' S- #include "FreeRTOS.h"/ f) l4 p, U) _. R* u
- #include "task.h", k. i& p( k0 O; X
- #include "queue.h"
9 h( g) |- B$ o - #include "semphr.h"
& ]( G9 c$ y; F- i/ B. h# I4 q" p - #include <stdio.h>
4 m' I3 o% S& [' w2 V% Z - #include <string.h>: \" C. w2 \' F D
- * i P& c8 i1 U- ^3 U3 V
- /* USER CODE END Includes */
8 j" r( |& V) ^2 Q1 B. ? - /* USER CODE BEGIN PV */" `* f! a# E. j" n& j! }
- static TaskHandle_t AppTaskCreate_Handle = NULL;
/ N* c0 K* x N - static TaskHandle_t KeyTask_Handle = NULL;3 K" A' i9 _6 G5 _' i# d
- static TaskHandle_t Led1_Handle = NULL;
+ c8 ?* F% |# N+ f
1 ~, O$ c$ E/ Z: A% I5 K- SemaphoreHandle_t SemaphCount_Handle = NULL;
4 [# q( \' V$ V- ? - . |! E3 ]6 w+ \5 h
- extern int f_printf(const char *format, ...);
) Q; A! K# L! k1 D6 N4 Z0 m
) ~1 j( P* g# f' X* g0 G/ N6 h- /* USER CODE END PV */& M3 o& o* m) a( t4 v. E! P" b
) q# o% l7 ]% k9 w+ C, E- /* USER CODE BEGIN PFP */5 O& M w: m8 \' {
- 7 ?( I8 s- P5 X* M2 l
- static void AppTaskCreate(void);* \1 h. N H7 G3 P: ?
- static void KeyTaskCreate(void);
( s. a. g. h& q: f$ S$ P8 C! p4 ~ - static void LED1TaskCreate(void);
: n+ q2 V* J J9 o9 M# O - % a7 T- x/ s- X% V$ ^1 s
- /* USER CODE END PFP */
& K, m9 ^$ q: t$ ]" n7 H' H
$ n' I) l) h3 ^ ^3 k- int main()
! n! y' O* f) I/ e - {
, ^9 ?) v. M1 s1 A - ***. n& O8 `1 z6 ~- S E
- - s' k( c1 [* Q1 }+ Z( o
- /* USER CODE BEGIN WHILE */( b4 } |- T) ~4 w3 _
- I3 q5 d: F$ a0 J# O, z* \- [7 ^- UBaseType_t xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,+ K. s$ a) }+ y) R* S
- (const char *)"APPTASKCREATE",
. M& W W0 y# v5 f0 d' J, y4 I( U - (uint16_t) 128,
5 X8 f( X. `9 s5 f - (void *) NULL,
; K( g& k1 D( H+ T" ~! s6 | - (UBaseType_t) 2,& f& T( I, D8 g- u+ J
- (TaskHandle_t *)&AppTaskCreate_Handle);
8 m, R# B* g, ~4 q9 ?# { G - if(xReturn ==pdPASS)
" {/ w' k$ I6 s7 f$ X2 L - {; C; f- S: s" _! t
- f_printf("TASK START.\r\n");
' b s- w2 o t. S, T* X- m - vTaskStartScheduler();- ?+ ^) d) `) r, ^3 R4 e+ L$ E" Z9 G: t
- }
j/ p: }% b2 {2 ?7 N' u$ u - while (1)
2 L: h Y. [8 D+ ~! I/ R - {
6 I5 K6 [) T* R3 h. j, c! J* x3 S - /* USER CODE END WHILE */4 K& r; o8 b7 e- o; L1 N
- $ h c' b% _/ s. o$ x
- /* USER CODE BEGIN 3 */
5 S# s1 H0 R6 Z4 K - }. f1 z3 I `; Q! l* ^! k0 c
- /* USER CODE END 3 */
) a6 M: r5 w/ ]- c5 J1 V - }
. p& Z: |& Q0 }0 X) s' {) M - ) w4 D' _, h8 r% q7 t
- /* USER CODE BEGIN 4 */) M P8 @( P* s5 ?
- static void AppTaskCreate(void)
. _% @% L$ u; J; }; G1 y - {$ `- x; a6 b6 Z. L2 D6 I
* X8 D3 s' C. c7 i' o% b- UBaseType_t xReturn ;+ W) B' f0 u5 r# F, {
- xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,- W# p C0 D" P9 L& @" O
- (const char *) "KEYTASKCREATE",
2 r0 m: \) ?" F5 G4 R+ @% P! m" g - (uint16_t) 128,+ ^( d! D! q* N+ p/ V! u1 |
- (void *) NULL,
* m# X: Q# l+ |; }/ @0 \+ a - (UBaseType_t) 2,0 M' ^+ l) p. J/ {4 o1 M3 k+ L$ A
- (TaskHandle_t*)&KeyTask_Handle);
0 I1 s' S9 |% O6 p9 q - if(xReturn == pdPASS)
7 J) u# l1 p6 ^; t: y% q3 T2 T. p - f_printf("KEY CREATE PASS.\r\n");3 X; ~: l2 Z' y6 n' I0 ?% C, g. Y
. {! Y- m0 v) K! F) A0 e" R" v- xReturn = xTaskCreate((TaskFunction_t)LED1TaskCreate,% ?2 Z! }4 v9 L9 }
- (const char *) "LED1TASKCREATE",. E" P' E2 t$ u* e
- (uint16_t) 128,
' ]1 `, V+ }& ?6 v* E5 k7 f! B - (void *) NULL,
4 f4 f) N' w/ M2 @, _4 r2 i1 V - (UBaseType_t) 1,
/ b" A8 ^9 C- ~% Y0 p) Q! m V - (TaskHandle_t*)&Led1_Handle);& t6 p w) Y" V# i( r9 @/ s9 G# V2 o
; K) L5 q, J }% B$ U- SemaphCount_Handle = xSemaphoreCreateCounting(3,0);2 y1 U3 n/ C9 X) K" t Z
- if(SemaphCount_Handle != NULL)
, Z7 z' p& e' o2 c% I - f_printf("SEMAPHORE COUNT CREATE PASS.\r\n");
+ L6 \" c! Z! K; s0 Y" W2 d, g - % s$ r, I* y1 @5 Z
- if(xReturn == pdPASS)
! k/ [4 L* Z& h0 O3 z, ? - {$ N( O' S/ s9 h5 p( n
- f_printf("LED CREATE PASS.\r\n");
0 m4 S, \$ Z# x4 F - vTaskDelete((TaskHandle_t)AppTaskCreate_Handle);& y/ h6 s& {$ p
- }, H- G0 f/ \ k
- }8 l+ x6 y/ C' @) V$ E$ ?- U# D
- 8 E4 [7 K1 F2 f
# H* L% c- a' [- static void KeyTaskCreate(void)4 ?* i0 c/ m6 e, i
- {) k3 {1 W* h8 b
- while(1)4 p7 P, L+ ~8 ^) M$ I
- {" f( N' ]& Y+ E9 F- w- ]- X y' j
- UBaseType_t xReturn = pdPASS;
" g. {- ]7 @# ?' W- ] - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
1 i! `( {9 k2 V, {! v6 h7 i' h - {" | e2 K5 R8 u2 ^4 E; ]
- vTaskDelay(100);
5 }: W6 H$ u: E6 [: | - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
$ q7 ?5 s1 c) j1 y: V9 R. L/ V( C - { T: s! i5 h1 x, o5 `) u
- f_printf("KEY PRESS.\r\n");
- U8 X8 @ o/ T% k) V% g - xReturn = xSemaphoreGive(SemaphCount_Handle);
' L" q3 e0 x6 R5 Y* P% r L - if(xReturn == pdPASS)1 T' S$ }1 J. A
- f_printf("GIVE COUNTING PASS.\r\n");1 y1 h8 [2 |" V1 q
- else if(xReturn == errQUEUE_FULL). Q; Y, ^( p- s0 E$ H
- f_printf("QUEUE IS FULL.\r\n");
: t4 V' u" c% O. ^) z/ W; @ - }3 E" n6 o+ ]5 K
- }
" [" V2 V: e8 K9 G - vTaskDelay(10);$ S; K: G( a; Z1 c) O: p! V
- }
& | Z' J. u( b# k7 h' u - }
! d$ B5 D# j7 @' b% M' P, v/ T - static void LED1TaskCreate(void)
8 d, \4 H% x0 o! i) ^! H/ _ - {
5 ?/ G1 |# v7 E! J3 U# ^ - while(1)
2 i$ W% @$ L2 C! S/ c; V2 C - {
6 G }% K8 t8 [- w - UBaseType_t xReturn = pdPASS;7 @" S, m% e5 A' ?' u q$ o2 \2 ]
-
* b3 D* m$ e1 }( V" I - xReturn = xSemaphoreTake(SemaphCount_Handle,portMAX_DELAY);
# m- y7 T* E" m, l - + o- Q( Y/ n3 g4 z' H- ~" Y0 U
- if(xReturn == pdTRUE)4 ^* N0 `8 L9 ]' _' W% A, c+ ^
- {& u+ D( c& J" b
- HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
! R }0 p2 i- v" X9 j! ~ - f_printf("Counting Take PASS.\r\n");
( j0 \1 H! m3 u& T2 C3 {0 \ - }
! b2 P) k" l1 b7 X) N - else if(xReturn == errQUEUE_EMPTY)
* M8 C: F" G& C3 @6 E# P; r - f_printf("semaphore is empty.\r\n");1 c8 N, b, ?* {0 q3 z
- vTaskDelay(2000);) y3 b# ~$ ?+ p/ U# j: M7 n
- }
" N1 p0 b; u" g2 t, T: z, X - }0 Z9 Y5 C% E9 ~+ _8 ]
- /* USER CODE END 4 */; M% H) s' d- M
( Q. }/ P7 M$ Z: q0 x$ y
复制代码 ' I1 N+ r3 ?0 T6 O+ @! h& P) _
|