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