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