概述/ U; s. }$ D6 l1 B6 p* m
每个任务都有一个32位的通知值,可以替代二值信号量,计数信号量,事件组,也可以代替长度为1的队列。& V+ c; `5 a1 d2 t/ A5 e7 c, Y
+ p) |% z* R, v
有以下几种方式发送通知给任务:
' z0 {" }6 i" W! ~/ L, c# r5 I0 m8 {( e% i& u- I
1.有通知未读,则不覆盖通知值
( ^* j+ F) B1 q% k( y% B( s
5 C( ?5 T# R/ O2.直接覆盖通知值
$ j- A- \+ A5 c7 L! [2 u: ?% y6 p. l- R2 d
3.设置通知值一个或者多个位,当做事件组来使用2 b: D9 b) h$ ~) Z, M% o- d
' e7 w* w, L& p; m& I3 f
4.递增/递减通知,当做信号量来使用* `' x, n& e: S9 q! o' j
# O/ q$ Y. E `
有以下缺点:
2 T+ L" H3 r" G
5 n: {, m* G" b) W0 P9 ?+ b4 v: T1.只能有一个任务接收通知消息
+ j8 w& Z6 B, z8 K) H% i: Y
; D3 T5 o! M, N$ _. ]2.只有等待通知的任务可以被阻塞,发送通知的任务都不会因为发送失败而进入阻塞。
$ m, I4 n# D1 o; }! H" e8 f% @9 B1 X. j0 f5 t$ V8 T4 H8 O. Y
代替二值信号量/ ? Q: U% ]5 i$ y/ @: X
在FreeRTOSConfig.h里开启任务通知
" o$ _, I/ t2 z% q9 J4 [- q0 y- /*Turn on task notifications.*/
1 Z/ N' G- {% c! d& x: p7 R - #define configUSE_TASK_NOTIFICATIONS 1
复制代码 , p. D, I) r: k' R7 w
在main.c文件里面修改代码, k0 ~7 r: w! Y
: w' S- ]/ S' c) c( z: P
- /* USER CODE BEGIN Includes */
9 n% A8 A. [* |3 m S" G3 r
; _, d' t, @$ l) U5 u- #include "FreeRTOS.h"
# [" W5 S% Z+ o ` - #include "task.h"
! b: K5 c$ m5 }# k; f" D- ? - #include <stdio.h>3 O$ H' e7 E5 _/ w& r0 U
- #include <string.h>' R! V9 p& L% J; k2 h- a! k+ f; U
) g8 \" Y) x& c: o8 N+ w7 k- /* USER CODE END Includes */! [2 {5 C2 z/ K+ v# ~7 P6 B
- M, F; R8 ?2 y4 w- z& K# n
- /* USER CODE BEGIN PV */
1 v3 d: S/ X( x$ M) n - static TaskHandle_t AppTask_Handle = NULL;+ F* E& I4 X8 b7 \! M; n1 k
- static TaskHandle_t KeyTask_Handle = NULL;; \ x# y' S9 j$ E1 k+ G1 {
- static TaskHandle_t LedTask_Handle = NULL;& |( F8 S# E$ M, l6 G0 ?+ f+ m: p
- $ y8 z" N! h8 z4 O- ~
- extern int f_printf(const char *format, ...);" S% ?- O3 j8 g% B2 O& X
- /* USER CODE END PV */4 R+ _& R0 t- q! T$ G" `* W$ k
- 7 ` o, P' {, T2 T- `- ~
- /* USER CODE BEGIN PFP */
4 ]! |7 E$ \ O( m - static void AppTaskCreate(void);7 @0 y9 ~% D+ y
- static void KeyTaskCreate(void);& u! a4 }9 t6 d$ L1 a# G" o4 ?
- static void LedTaskCreate(void);9 E! o0 x; A# Y5 N( u9 ~ E
- /* USER CODE END PFP */
2 _. N+ I' y f8 j, t, V1 g& g - 8 x! _+ P9 }! g7 p7 O7 V a& t& x: Q% O
- int main() z) M# o# s5 x+ K
- {
8 X6 B: g4 J: {; W
! m3 j4 X* |6 B8 }- /* USER CODE BEGIN WHILE */2 @: F1 {# @) Y6 u$ `
- BaseType_t xReturn = pdFAIL;
- N* I, H2 t4 M0 M - % o" n' Y8 l+ ?
- xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
' P0 g" s4 k& g$ C B) O7 k) R - (const char*) "APPTASKCREATE",
4 c3 A; ?$ g3 @$ ]) b& \ - (uint16_t) 128,
, e% }" Y. ~7 A9 z t8 ` - (void *) NULL,7 F9 i! p3 _% a2 Z4 D
- (UBaseType_t) 1 ,
8 b3 C9 m. b' A7 L - (TaskHandle_t*) &AppTask_Handle);- f' x' k: M. ^3 L3 x8 @
- : z3 m) z! A9 [8 ?- f
- if(xReturn == pdPASS)
0 {/ g. k9 H7 F% W/ I- D - {( n$ C" h ~& {4 U& P j- p
- f_printf("AppTask Create PASS.\r\n");
& r/ R' m1 m0 T$ y4 t8 o - vTaskStartScheduler();+ k% g! t, J/ y- V
- }9 F- K9 b! O3 b
- . m: G( `! g. M9 E4 M- H
- while (1)
# E( y K% H$ ~4 i - { ~: l/ c! V( `' |! Z
- /* USER CODE END WHILE */, C, k4 a% J; J7 Q. R5 q; L: R0 L
- /* USER CODE BEGIN 3 */
b0 ~% `# d* _: K* }) i4 M( Z - }
; W1 P) P( m* o1 r - /* USER CODE END 3 *// [) i/ v/ G* r- O8 o
- }
( U; ]/ s( Z( ] - % ?' x0 h' ^9 n* a( n9 k
- /* USER CODE BEGIN 4 */
* w# s3 I. n+ M& u - static void AppTaskCreate(void)# K0 y) z) q ?+ q0 r
- {; a( V7 a9 q2 B% o) L( b5 l
- BaseType_t xReturn = pdFAIL;2 ^' s) H0 K1 Q% |6 ~$ n
3 G- h4 Y j3 P$ z- xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
2 x* O R: z: o: z. R - (const char *) "LEDTASKCREATE",
- \7 X7 h9 r. E( G+ F% w3 D( ?9 X - (uint16_t) 128,
9 N; \& e# e) ~7 K5 E2 q - (void *) NULL,0 ?/ O# S9 ?4 ?0 ^$ v
- /*If the priority here is the same as the key task, printf will not print it out. */
4 _0 a. `% b a" l7 S - (UBaseType_t) 2,
" }: |0 _# {+ R8 j$ ^1 m4 c$ c" j9 | - (TaskHandle_t *)&LedTask_Handle);2 k1 M4 R" c5 V
- if(xReturn == pdPASS) Y+ r% I0 c: n8 Z, N
- f_printf("LedTask Create PASS.\r\n"); v, d _1 X2 ~" H
- , C3 p8 s* `# V* S
- xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,0 N& [% V5 p4 a- Y+ o J. U1 M
- (const char *) "KEYTASKCREATE",! ~0 Z; P+ E4 J% ^% o% t2 f. H
- (uint16_t) 128,, K( E) v1 N- _2 _: }8 b
- (void *) NULL,
" J1 t6 ]" g) \+ { - (UBaseType_t) 1,
: S t% b7 b, \# W4 L$ L z - (TaskHandle_t *)&KeyTask_Handle);
9 @* k. [3 \- v( U# t6 d! F - if(xReturn == pdPASS)
9 Q3 a5 V F7 C' @. S' I - {7 c' ?6 p4 S9 H* {2 d0 m, O4 l
- f_printf("KeyTask Create PASS.\r\n");
; P% q/ y- {" w7 ~8 E - vTaskDelete(AppTask_Handle);
* t% A1 _- e, s1 H - }/ m- _3 Y+ m8 Z8 U' A+ v, K
; V9 V* K' K; ~$ V! f) m, |: r- }
- R: j5 A( H( u9 p3 |/ d4 y - static void KeyTaskCreate(void)
. W# z# c6 ]& I - {% F+ V5 U% i# c! X- y/ h& s
- while(1)2 F8 G; I& c+ `
- {
2 ]" Z1 q% s0 z+ c - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
9 x3 n$ e; n: } - {
; R4 A9 @; }2 R( }; _7 z1 q P - vTaskDelay(50);
c) `! u0 R' U) r9 v - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
5 @: s; X4 Z' v5 l) f. \1 W* ]0 E - {1 }* W( e6 x9 S0 f2 J3 z3 S
- xTaskNotifyGive(LedTask_Handle);0 f& s% G% W! ?& v7 v
- f_printf("Give LedTask_Handle PASS.\r\n");
/ _8 n3 G1 D, Y - }. z S1 |) e( W- a) |) ^/ H0 q
- }) i+ R2 f7 U t- {( b1 t
- vTaskDelay(100);
7 g1 r6 v2 b' U) o+ ? - }- B# E9 ~$ j: v2 W3 o+ R! H" i; w6 b
- }
; F/ q9 A! z# r: m7 f/ |% s - static void LedTaskCreate(void)
; R8 d+ _5 [% K7 j* } X - {
0 U$ V( o% Q0 Q$ j+ v - uint32_t ulreturn=0;
. ~! | U: i5 `) R - while(1)+ ^$ s5 A; f; S
- {9 M; o! G4 O' k
- " B6 h7 A! n6 \1 E1 U- D6 ~
- ulreturn=ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
& X! n8 R8 v( T+ }) l6 I - if(ulreturn==1)( K& F$ E, m) a! t% t( E
- {. F8 F* t3 h7 m1 F3 p7 h
- /*It won’t be printed out at the same level*/
% \% Z: ^1 c2 f; e# @! { - f_printf("LedTask TAKE PASS.\r\n");
- r) E0 [8 c. e" Q% U - HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
$ s* F7 g. o; M+ h" L& [9 x - 9 B5 |# N2 E% B* T
- }
7 n. V/ \% c) U X1 ? - else
( }: V, O, L5 } - f_printf("LedTask TAKE FAIL.\r\n");
0 a( o0 T1 H# E6 a - vTaskDelay(100);
) k8 v8 W& d3 k) O" D; w - }
) N( _ w! ?1 _' } - }
" n( C: v! k+ l8 Q8 U9 H2 R - /* USER CODE END 4 */. q. x8 ~8 p: k* x4 N3 T( i* l
- 代替计数信号量8 i; }4 G7 V0 t8 U7 n8 J
- 在FreeRTOSConfig.h里开启任务通知5 i4 l4 ]0 F8 h* z1 p! _
- 1 f0 r. r/ ]# \4 e* _# Z; ~) s! h
- /*Turn on task notifications.*/
# W4 _" D' y* O5 E" U3 E - #define configUSE_TASK_NOTIFICATIONS 16 @2 C+ z! y* l1 u
- 在main.c文件里面修改代码; E( u9 Y2 }1 ~. e) T+ m
6 ?9 k! o w, {& z0 Z" y" M- /* USER CODE BEGIN Includes */
$ |" _( d( W; Z3 Z% ^( c6 k0 A
; o0 O8 d E7 B3 {, f8 L- #include "FreeRTOS.h"; M% L1 @+ u. ]$ y
- #include "task.h"
N3 D" c7 t+ c; F; [8 m - #include <stdio.h>
1 O* d$ K- U0 N; P4 U: l: Y - #include <string.h>
1 k0 J- N1 z+ P3 Q - / i5 f" t0 [! {1 `$ }% S+ W& q4 |# N
- /* USER CODE END Includes */9 |1 }% ?! M2 }) N" r
- 1 T* l# r& ?* Y5 @" i' Q
- /* USER CODE BEGIN PV */. W3 ?1 Q1 m; I0 e- u0 y
- static TaskHandle_t AppTask_Handle = NULL;
% g# h) k, v6 P) h g. h- W - static TaskHandle_t KeyTask_Handle = NULL;4 w# u' d& Q9 n9 y4 o: l
- static TaskHandle_t LedTask_Handle = NULL;; U. m, f* I9 K- u# \
8 l9 c; K% ^1 ]* M" k8 ~6 F- extern int f_printf(const char *format, ...);
/ S8 I0 n1 p' K1 p) g9 c
- I }- k7 |* y- /* USER CODE END PV */
$ d6 Y: A2 T8 o8 K# m2 |- U - , G% Y3 g3 m, {5 P/ s
- /* USER CODE BEGIN PFP */
+ E5 t2 }0 B+ ` - static void AppTaskCreate(void);
w+ f0 {) Q; D$ W7 t4 R - static void KeyTaskCreate(void);4 G9 I% q5 J' f7 n% P/ E! K
- static void LedTaskCreate(void);
; m H( y9 W: i8 P, l" Z - /* USER CODE END PFP */
+ V1 [: ]: ^3 P' i% g
: Y* d/ t/ w& f2 Y- a5 D/ L G+ _- int main()- _% M6 Y9 U) w% S. f
- {
- o6 M6 |) S: V+ L M$ E - /* USER CODE BEGIN WHILE */6 ^* c& ?8 f5 w3 G+ z
- BaseType_t xReturn = pdFAIL; + a. W. T' J/ Z
- 0 ?" N7 }: @) z4 H6 A4 n$ P
- xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,
% }; X& Y- o! a# S% I M - (const char *) "APPTASKCREATE",
; J0 }8 z) [) d2 j) P; o' ^ - (uint16_t) 128,7 K/ ]: [4 A8 [" p7 F9 X
- (void *) NULL,
. t8 Q# g) z* m, j' I* q( d; Z - (UBaseType_t) 1,
6 s4 V9 n- _: t; \, `' u - (TaskHandle_t*) &AppTask_Handle);
5 g- n; Z3 A" d$ }8 Q$ ~ - . q/ r2 @) b2 s y; S' a
- if(xReturn == pdPASS)
8 b9 r% a# e5 s$ L0 `, F' i7 U - {9 s% Y; u9 |; _) s( U5 S
- f_printf("AppTask Create PASS.\r\n");' _- _* ^/ r4 e5 t- `
- vTaskStartScheduler();
+ z; }# p, E% B( L - }
; T! W3 A" c5 a; u - while (1)
6 a7 b( {+ ~6 s3 Y6 y- y - {
4 I9 Q7 ]: f0 z5 m - /* USER CODE END WHILE */! n c! Z0 g1 v0 c) L) P( r
/ l3 L. ^$ N8 P& i& C- /* USER CODE BEGIN 3 */: H9 t. B& Q0 \
- }1 E# u, d2 D9 f, @! }" s
- /* USER CODE END 3 */
9 h; E7 d K- z( i7 ]$ ~ - }4 O- i* O$ ? o8 P. B9 k
0 y$ I7 m. N- f4 C+ ]6 t
F- V7 ?. u% K% {- /* USER CODE BEGIN 4 */2 R0 k; i8 o, g4 x" U8 c$ W0 N1 j! x
- static void AppTaskCreate(void): S. n. z+ Y& i# e! ?
- {7 }8 }& h" Z6 c0 g) e+ q/ A
- BaseType_t xReturn = pdFAIL;# g/ C9 ~8 [# K: R
/ U" Y; ? j& O5 Z. e3 E3 ]- xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,
6 C+ `8 \+ e5 G - (const char *) "LEDTASKCREATE",( K, I6 O( U ~2 s+ u
- (uint16_t) 128,- K9 [/ x/ O/ ?6 }# n
- (void*) NULL,
" T9 \. n# Q% @" w) T - (UBaseType_t) 1,# i" @4 v. O! v7 K+ j: h3 u
- (TaskHandle_t *)&LedTask_Handle);
( J0 g! b g' t - if( xReturn == pdPASS), [! c" {* S8 j* L, N. f
- f_printf("LED Task Create PASS.\r\n");
+ K# d( H6 e: r8 u. k5 E' P -
+ [2 M6 o# \4 H# r* g - xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
: B) j& `" s2 J: F9 c - (const char *) "KEYTASKCREATE",1 z" H8 h0 q7 ^# P: t9 @4 O
- (uint16_t ) 128,
( T, A% ?. o* x6 y# P0 I' l( y - (void *) NULL,, B& w) M' l- G/ I
- (UBaseType_t) 2,
j! [5 c4 t+ T( \ - (TaskHandle_t *)&KeyTask_Handle);* A& s1 f1 u3 x. Z4 ]
- if( xReturn == pdPASS)
3 q0 ~0 [1 p9 ?; B - {
% Q$ Q% `5 A5 u- A# K - f_printf("KEY Task Create PASS.\r\n");' I: w. k7 P1 q. H/ Z1 N
- vTaskDelete(AppTask_Handle);7 |0 n/ w$ T: ~7 N0 @
- }
( c8 d, B* k! b$ n9 u) v8 `2 p* k. J' q - }/ t, I/ R( g, ^1 R+ {1 t% _
- static void KeyTaskCreate(void)& g0 E Q5 a8 q V1 z* l/ x2 F0 E
- {/ K2 D3 u/ k- ~& c/ L
- while(1)
, f: F7 y$ X' W- a1 J - {% Q) i3 c" j+ R' o- n! P' r) r
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
' s( w9 a7 @: }5 ]+ g8 _ - {8 |0 R, g' `; w1 e8 C% c( O& t% _
- vTaskDelay(40);
4 J& \* h! @1 B1 E3 E* `' B. t. o - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))" \! F% O; t& [- ]) e4 k
- {
: c, I3 S/ v7 A) \& I - xTaskNotifyGive(LedTask_Handle);
; x; o1 h2 `0 o# O4 m2 e+ q - f_printf("LED TASK GIVE PASS.\r\n");0 m1 O8 x3 z: a' i
- }
8 Q* U6 H2 Z# R% W - }6 \8 H5 h W8 n) N$ |: s
- vTaskDelay(40);
& f( X7 g& i( l9 N3 u; S/ x - }
& }7 c! V4 H% `+ R4 J: S - ' `4 ?5 {) @! m9 A
- }9 j/ \+ T9 N, w# u, l
- static void LedTaskCreate(void)
% _" i+ O5 y' v5 c - {' k" w1 r+ p8 H/ E9 _# N) Z
- uint32_t ulValue = 0 ;& J/ L$ X* z. Y! m: w: s; U
- while(1)" T; X o, b, j1 e3 x
- {
7 G! y( l F- W2 N; | - ulValue = ulTaskNotifyTake( pdFALSE , 0);
0 Y0 t: X0 H* ?- ` - 5 ?3 q# \ i& i- Y
- /*Greater than 0 proves that there is still a notification value that can be
7 w: V% a& I7 q8 Q. r8 ^ i - *reduced by 1. Equal to 0 indicates that no notification is available
% d+ g, C, Q0 N! t5 N) B* I - */# }) ]3 p6 G5 o1 I
- if(ulValue >0)3 G: s1 G8 W8 f
- {9 W9 a3 F) Z. d. W
- HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1); [+ V, Z! Q1 q: X6 M
- f_printf("LED Task PASS.\r\n");
9 c$ h, M4 X' I) g9 j) A) A* c- B/ I - } 7 L2 k: A( p% Q. C! j0 a
- vTaskDelay(1500);
) a. I4 d6 i. l) u* V4 i - }
5 u: C& J/ K+ i& G2 V3 p - }7 G. j; K& `, m6 G* p
' u5 q" M( F2 ~( ~. u' Y8 t2 N- /* USER CODE END 4 */
复制代码 ' x/ F( _- ^1 [. F/ w- C9 a
代替事件组
4 b: Q" @- q2 u9 Y8 _3 f在FreeRTOSConfig.h里开启任务通知
1 j, y0 `/ e+ G( M" S( u, W' ?! c$ M& {1 V' B h Y
- /*Turn on task notifications.*/) t- a8 C7 p( h; H
- #define configUSE_TASK_NOTIFICATIONS 1
复制代码 8 I, H! [+ S# _5 O
在main.c文件里面修改代码$ h3 }- g9 b# M& @. r0 Z% Q# E
: E! T" c, t* L) B) S- h- k' I4 Y: L
- /* USER CODE BEGIN Includes */* g: r) D$ }3 r9 {+ U
7 d4 m4 o$ K/ r- a0 N. k3 |4 q- #include "FreeRTOS.h"/ g# p# q4 b3 E+ l- z( o C( J: v0 Y
- #include "task.h"
. d' p- v2 ]/ J0 Q% N% B9 f - " A D: g5 ? n
- #include <stdio.h>* w8 z8 f2 g; l: ^. K) x- T. R5 w
- #include <string.h>9 x e" e8 L+ T; h2 z5 x5 |( n
- ( Z7 i2 w/ A4 x0 R. J
- /* USER CODE END Includes */
* V6 |9 A6 w& }/ \/ l
- X5 N" H0 J) `- /* USER CODE BEGIN PV */
: m8 g. a. v. C1 e( ^ - static TaskHandle_t AppTask_Handle = NULL;3 g9 N3 J( F( q- l G% `2 S
- static TaskHandle_t KeyTask_Handle = NULL;
& g( \9 E8 n" U- H - static TaskHandle_t LedTask_Handle = NULL;8 l6 v( T e9 n) J- C
& e& _7 B" d$ }( j% m4 W. _/ N* x- extern int f_printf(const char *format, ...);
6 @' @0 b9 i7 Q' r0 n - /* USER CODE END PV */. W, ?; J; n0 e# O& V
- 0 i; {" i% [6 ~/ J5 N
- /* USER CODE BEGIN PFP */! A3 V! _+ `% G
- - l/ ? s2 N: Z
- static void AppTaskCreate(void);
% }6 q% A) g" i - static void KeyTaskCreate(void);7 y1 ^' P3 e4 l! ?
- static void LedTaskCreate(void);0 V _" j& {% N9 r. R' [9 K. @5 E
- # C9 g4 w! Y" t- ?4 ~5 d7 y& m
- /* USER CODE END PFP */! W& h1 @9 N: s4 S/ ?# y3 r
- 4 [' l/ X4 F0 O) s9 J ]
- int main()
9 d- S- L6 n" [$ n# Y6 R* P - {* G* v& H1 s( _1 a$ t+ g N
- /* USER CODE BEGIN WHILE */. M& `6 Q) ~4 ]; F3 _8 N& F
- BaseType_t xReturn = pdFAIL;
- x% I1 e* y. L - ( G! |* f6 S* Y/ h T
- xReturn = xTaskCreate((TaskFunction_t)AppTaskCreate,7 a# ?1 ^. j$ h7 K
- (const char *) "",
2 `$ s+ f1 @! W9 ]% J - (uint16_t) 128,
2 h: i% Y1 z8 E: q - (void *) NULL,
! p$ z$ {* o3 ~1 N* l7 Y. v b - (UBaseType_t) 1,
8 }* O V0 @3 _2 q+ @5 p; Y - (TaskHandle_t *)&AppTask_Handle);6 Q# U+ q4 W ^4 h o
- if( xReturn == pdPASS)
, W* S( U7 F; H! i- B8 F# c( } - {
# h/ q( s- ^8 g( { - f_printf("APP Task Start.\r\n");. z6 u$ O& B! n# ^
- vTaskStartScheduler();' q! x; d: H& @$ r
- }
2 r( U4 \& d& j3 `$ x$ d1 x. k5 L" T
4 z3 b$ ]) G" j/ z9 B0 J, \- while (1)
% ]0 y9 O. y( j0 q* a - {
5 n# b9 ?9 L% f- X/ P3 { - /* USER CODE END WHILE */
) q1 J2 @: ~% Q: c" @3 n0 K
& n$ E% @0 @% `: L+ O8 u- /* USER CODE BEGIN 3 */
2 L O4 R$ U6 c& I - }* y- m/ r0 X8 \' w! a1 e' @* L
- /* USER CODE END 3 */
9 y0 X# s/ L8 M0 u* n - }' z9 c0 f( E0 z8 y5 C: _" c
- ' L1 D% P4 G& q8 }# P
- /* USER CODE BEGIN 4 */
: m% A6 V$ Y# s: I" E, _ A1 x - static void AppTaskCreate(void) T' @9 S, j7 r [" J1 U4 g
- {
1 C8 B f" H& O - BaseType_t xReturn = pdFAIL;( B3 U) n: Q4 L; N* h- i% C7 A$ P) d
- 1 Y' x, p) f4 A1 O
- xReturn = xTaskCreate((TaskFunction_t)LedTaskCreate,( p: T& z5 H5 B) j
- (const char *) "",
' `5 `* R6 ]1 D. n - (uint16_t) 128,6 G. i/ _/ o y
- (void *) NULL,) s" C2 [' l& ^$ T7 ^ j( b
- (UBaseType_t) 1,
/ ^ C# X% Y: |3 H - (TaskHandle_t*) &LedTask_Handle);
& q& X* {# S+ @8 O3 ?% `* m - if( xReturn == pdPASS)
+ H6 h/ Z/ e5 D; k0 _3 b% D o0 T - f_printf("LED TASK PASS.\r\n");
. b/ \: N# p" e6 u S4 } -
. j$ I: m. n5 _$ _! N - xReturn = xTaskCreate((TaskFunction_t)KeyTaskCreate,
- ]0 Y6 h& l, G/ y/ w0 d# D5 I& | - (const char *) "",% P# `9 o3 `+ i/ w1 m/ N
- (uint16_t ) 128,
) l- x! Z0 N4 S* `+ W; w! F4 P - (void *) NULL,$ g9 o5 |- E& h0 i+ D
- (UBaseType_t) 2,
9 r+ \: o8 ?; V! d: D) N* \ - (TaskHandle_t *) &KeyTask_Handle);
! X' M: D! a! [& s - if( xReturn == pdPASS)) r4 C" w# A+ ?" g* g5 q) ]1 _" f: R
- {
Z& E, y1 q4 ~$ C* i2 P - f_printf("KEY TASK PASS.\r\n");. i( I6 g- N; X3 t- J+ e* t- X8 E
- vTaskDelete(AppTask_Handle);
4 Y' Y T; K. a6 l - } ! T& B" ]' g- Q' ?2 M6 c
- }
3 J% r: B; [0 K6 S2 j7 w- X# b: f7 x% Z - static void KeyTaskCreate(void)
: r$ h' Y+ l5 u4 u0 g1 q - {* z( p$ P4 a, m7 @8 p
- while(1)' I; J$ ^3 A6 q* _$ I2 L6 f/ |' k
- {) \$ r. c7 j% c8 M1 w# r2 A) d, c. N
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))
- z; c: e& \) }0 i4 J g" |: u - {6 ^5 S5 a" c5 W+ I3 {6 Y- U* k
- vTaskDelay(40);+ c% h& L1 v% b# N) c& T
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)), ]( V: s: h+ ?; s+ z- w
- {& t7 G/ a8 m& E; b
- xTaskNotify(LedTask_Handle,0x00000001,eSetBits);! I( ]1 k0 a5 v; w$ ^4 c
- f_printf("PA0.\r\n");6 h7 J9 p; j6 M4 S
- }
7 @$ p* \' m+ ]& s - }
2 X8 q( U/ f% j7 x- k - else if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12) == 0)
. k! N" B) t1 j - {
# T. p' {8 H# g - vTaskDelay(40);9 L8 |5 N1 ^1 M( m5 \
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_12) == 0); K$ u/ A( }. i0 q# T% s& j. I
- {% P2 q' a: M& H9 D; P
- xTaskNotify(LedTask_Handle,0x00000002,eSetBits);* B* |5 [% }! ?1 _4 `
- f_printf("PC12.\r\n");! C' Y4 Z4 A- n! a" y! D5 \2 e
- }
* X6 e L( q, w- r+ ^ - }
+ _% S+ B3 V6 r9 u -
+ r9 n9 T" o7 @3 Q2 ? - vTaskDelay(50);
0 x; d; P9 p+ m C - }4 E$ F# X4 H8 R2 ] R. G* E
- }/ J; _3 |* g0 s1 G& @+ ~6 W2 I; _
- static void LedTaskCreate(void)
0 @4 I& Q) A. C0 T8 ]$ y, V - {. _9 z2 m5 u, F# G
- uint32_t ulReturn = 0;
# S" J7 s, S, c. p" d - BaseType_t xReturn = pdFAIL;; W0 u* U/ N% I9 T! K" Q. l
- while(1)! x/ c' j+ n0 d+ x
- {3 _+ u/ E2 A7 g, Z( F1 o
- xReturn = xTaskNotifyWait( 0 , 0x00000003 , &ulReturn, portMAX_DELAY);
$ Y1 F+ D4 g* }8 i4 q0 G% I - z6 [+ L W9 ] \
- if(xReturn == pdTRUE)
0 l! _, B( c- H6 G; P, G5 t - {( F0 G2 f: r2 A: Y: R
- if( (ulReturn & 0x1)==1)9 h+ k/ Z" ]% E6 ~8 \5 U
- {' T5 f% [: d" r- m* G9 Y
- HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);9 t$ W4 {* l3 s/ C5 u
- f_printf("LED1.\r\n");
2 t; Q; l1 {- J4 P9 |% K$ | - }5 j8 o! f- p5 w ?* k8 Z/ {: b
- else if( (ulReturn & 0x02)==2)% b" ~5 k' K5 E# e& S
- {
% z, {6 Z: F1 h+ I7 M4 r: U, o$ c - HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
- |, u6 e2 P" I- j - f_printf("LED2.\r\n");
6 c6 C+ W; _! A3 ]7 ?- w - }" t7 q7 E/ L$ U* F2 |
- }
. L( f9 D1 o7 }4 L$ J( \7 |; e - else% J. R0 U: {# m) c$ }
- f_printf("Read Notify FAIL.\r\n");9 y5 _& y# g% [9 Q$ l4 g( R
- ' U+ v5 V" ]5 t
- vTaskDelay(1500);
0 S8 t; H; u3 W8 {, ` - }
, k* s% N" Z. H4 a0 l5 E- V6 z6 d - }8 |2 |2 {: B9 J5 z
- /* USER CODE END 4 */
复制代码 ' n. A3 s" P. H0 |# ~. J" ~
代替长度为1的消息队列
8 A& ?' ^, z6 O* z/ M3 f在FreeRTOSConfig.h里开启任务通知* K) x. v9 J& [; k% P: M
- /*Turn on task notifications.*/
: U7 R- J, B; Z& i" t; f1 ^ - #define configUSE_TASK_NOTIFICATIONS 1
复制代码 8 \- w& g( u) x. ]
在main.c文件里面修改代码# p& `9 |0 w8 a. x8 c! ^( C# @7 u
4 G# ~" C8 F1 e- /* USER CODE BEGIN Includes *// }0 F+ S, j- ]) T" O
: ], B; S( v5 _. b# W& N: N- #include "FreeRTOS.h". J' X" L- m9 }& B
- #include "task.h"
, b J/ }" n" @ - #include <stdio.h>; X, R8 O2 P! M) C! F) ~
- #include <string.h>
2 X/ X3 C- _( A+ `; M
3 b/ {8 t* r. d a8 {, {! `: }- /* USER CODE END Includes */
# A7 t1 v8 X7 ^; [/ X' ~
' O) @. e4 P/ \7 _- /* USER CODE BEGIN PV */
$ p9 n8 p6 E* L$ x( B0 M% T* u/ c - static TaskHandle_t AppTask_Handle = NULL;( f& L0 T( e) v% e
- static TaskHandle_t KeyTask_Handle = NULL;7 K) S4 k4 |: R* r) L7 Z6 a, Q
- static TaskHandle_t QueueTask_Handle = NULL;
5 B) v; U1 a& C; x - static uint32_t Message = 0;$ d t" D7 i9 Z
- static uint32_t *printf_value=&Message;9 |( p0 m6 R4 d" h/ i3 g
- extern int f_printf(const char *format, ...);
# f4 l4 u+ }' m - 6 N/ G0 Z: {3 S: X" r9 p
- /* USER CODE END PV */
# L" C# j& O$ K; u/ ~: ?# b
+ J! x; r* ]* U" J1 F- /* USER CODE BEGIN PFP */
$ K; l `. L9 i# K - static void AppTaskCreate(void);0 S; F2 s( ?; Y. _4 q
- static void KeyTaskCreate(void);0 p0 `9 P) S9 o8 H) y
- static void QueueTaskCreate(void);( T+ W" E2 J9 Y) r0 [5 s- |) N
- /* USER CODE END PFP */# K( |0 S& ^/ Q; p/ Q# B: |
- 3 }+ r; j; t/ `: |4 z* y# F7 _! D
- int main(). v. E5 `! |& _9 _) O- X
- {! d; y+ v7 C/ ?) T; W1 E+ c
- /* USER CODE BEGIN WHILE */
3 n$ i; K2 k1 d ~ - BaseType_t xReturn = pdFAIL;* p& h( P6 D- G/ N. ~
- - S. ^2 N$ }0 y, {
- xReturn = xTaskCreate((TaskFunction_t )AppTaskCreate,5 F+ c6 Z3 K! c7 E& z! _
- (const char *) "",
5 P: M) q: A' p( x& k' X) O8 V - (uint16_t) 128," l l; d. A! a( S
- (void *) NULL,
/ [+ G7 L4 a& j$ |+ w/ _/ N, l - (UBaseType_t) 1,$ K/ I$ }& K; O; W
- (TaskHandle_t *) &AppTask_Handle);
7 ? r4 Q3 [3 N) B8 K - 4 i) f D4 ^0 R3 D! r2 U
- if( xReturn == pdPASS)! c, W4 @; v0 w; ]. |& m+ `. U) Z! _
- {( x; ]/ ?( v$ c' ?5 j7 J% d
- f_printf("AppTask Start.\r\n");
3 v* B/ b2 G7 N& P5 R" A5 ? - vTaskStartScheduler();
- r! u+ }3 u) E - }
; z/ D: q0 i3 f( ^, [ - ( I7 M. c& ?5 `
- while (1) z5 p4 x1 ~4 ]7 T* g: v' V4 m
- {
1 M( E& c- a; e2 C4 ]0 z$ m- j/ X - /* USER CODE END WHILE */
0 i6 a& g' ~7 i, h: D4 X - # ]5 L5 D; P1 {! {$ o( P/ w
- /* USER CODE BEGIN 3 */0 K; U! X( \: z( l
- }/ B) e4 O. x9 Z) O2 x) H2 e2 u
- /* USER CODE END 3 */- k! |# U/ S, a, a! v* o! Z
- }
6 m( s3 Y9 q- Y/ a
0 Y6 F1 l) q4 a- /* USER CODE BEGIN 4 */
3 o/ D0 U# ^2 ~" \0 N - static void AppTaskCreate(void)- [7 q* t* J8 K) d e9 [# \2 f8 ^
- {
, ?4 V* T$ v: C' V- \+ B" u - BaseType_t xReturn ;7 y! W- ]: @" i9 ^$ Z% J3 G3 C8 A
-
( K1 a; r2 b7 D- ^ M1 T* H* T - xReturn = xTaskCreate( (TaskFunction_t)QueueTaskCreate,
5 n4 x: L4 J7 a" i- ?9 ^# c/ h - (const char *) "",8 |8 t. E l- I5 O; j- P K6 c8 H% g
- (uint16_t) 128,
9 M8 m7 x. \7 P. F9 x' E - (void *) NULL,+ z' {: a+ k. x8 u; Y4 Z( u
- (UBaseType_t) 2,4 m4 ?6 I& `& F9 X5 \+ G1 Y/ d
- (TaskHandle_t *)&QueueTask_Handle);
+ Z! V& S! d; m# {- o; r - if( xReturn ==pdPASS )
, f. V( h0 _2 I1 c4 W1 V3 g. l- f - f_printf("QueueTask Create PASS.\r\n");2 L3 s- y( _- \
- $ P1 j9 Y! r6 K9 s/ e. Z4 ]
- xReturn = xTaskCreate( (TaskFunction_t)KeyTaskCreate,# ~0 E* P2 x5 u( w1 O
- (const char *) "",
7 x: ?4 e# p8 }0 X+ [) k5 @) b - (uint16_t) 128,
8 m7 Q2 k( c3 l; T& v - (void *) NULL,
6 R. i- Z: s1 ? - (UBaseType_t) 1,
# C' K9 I$ E' _+ M1 |" C - (TaskHandle_t *)&KeyTask_Handle);" q4 A8 D7 o' K; B6 i5 N
- if (xReturn == pdPASS)
C4 N/ R4 r$ Z4 _- P/ k0 H - {
: p( ~6 c R8 b1 W+ i" y+ O - f_printf("KeyTask Create PASS.\r\n");$ `% S V1 U# B& k+ A
- vTaskDelete(AppTask_Handle);
! T- ?, g( `5 y3 X2 k - }0 r$ D4 e! d* y9 a
- }
9 ]7 f+ R. l* {3 X6 F* B - static void KeyTaskCreate(void)8 i4 }" u7 v2 s7 J3 G2 N# f
- {- ?* ?) q2 m7 a N- z9 E' a
- while(1)
' @9 Y% v! a1 m9 c - {& ?- g. I. [( y3 B
- if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))4 N" H: X, U+ R% s' b5 b- d( u
- {+ f$ \/ }7 F4 j3 Z
- vTaskDelay(80);
/ t) l2 D f2 `8 T - if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)) Q7 D! T: ~/ V# R+ A
- {
% N4 I; X" _8 i# e# o5 F: C4 B - xTaskNotify(QueueTask_Handle, (uint32_t)Message, eSetValueWithOverwrite);
+ m: c# s5 b6 o - }
2 F$ ^: K' v& K6 Y* A( ]8 D' V - }; C" U( K8 O5 C. B& Y# D. M
- vTaskDelay(80);9 R7 ^' E+ i& l% T) A9 i0 W
- }# x# F3 B8 C, E9 w" I7 b: R
- }$ m1 t4 o: [( U S1 M
- static void QueueTaskCreate(void)
* n c3 e/ L3 `6 k0 o* A& I* U - {- z) a3 \" S1 u4 E0 o# r# z
- while(1)
; @6 ~! a& R7 o - {
4 f" T) V4 b# _9 p3 |7 ]! j - /*work.*/. h8 j( S9 d! _' Q. Y3 S+ M& g
- xTaskNotifyWait(0, 0xFFFFFFFF, printf_value, portMAX_DELAY);
3 s: [! ~+ v7 n( {3 }8 O7 ^# N - f_printf("Message is :%d\r\n",*printf_value);) C7 z: T" D4 B
- Message=*printf_value;
, O4 W% O+ r5 y2 ^ -
0 @: J$ H" R0 {% j) k% v! r - /*The second method works too*/0 a% H1 K& n% N$ x
- /*3 S# J( @% X8 d% l7 ?
- xTaskNotifyWait(0, 0xFFFFFFFF, &Message, portMAX_DELAY);
7 U ]! m* E: @' A6 N+ [. ` - f_printf("Message is :%d\r\n",Message);
9 c7 \1 ]5 ^, j) b8 g - */
* K+ Y5 k& ^$ S G& u- T- I7 n - Message++;& h/ r1 D) n @0 l+ p5 D: M8 Q
- if(Message > 10), v4 B* g$ l. r
- Message = 0;
, u. ?9 j+ }& o8 C" _% L+ P - vTaskDelay(1000);
8 q* ^. p* ^4 B. _( m( o - }( f. j' o+ U0 M3 q7 ]6 ?
- }( v, k ~- X9 a9 S6 B0 n
- /* USER CODE END 4 */
复制代码 / Y' `3 V* @; c, a" F) u
5 i' T2 z) S* F# w
' O2 d2 W+ G3 B' V( T |