一、下载FreeRTOS源码
, l9 m W" y% Y y" U; z0 t" R5 e- \7 K6 z: T. D& F1 b
下载地址:https://www.freertos.org/ ?; A0 y5 r. N# A# d
' H; S8 q( O# {8 H# W7 W
) U; N# b. N. d
+ {( ?1 P- `4 f0 ]. L, d8 B7 ^: _8 i$ ?
; ?( f- k% j2 m
二、添加文件
/ T. l2 ~) ]' F7 c5 C* k2 `' J' W3 F8 c% L9 c' ]5 Q
2.1、复制文件到项目下的middleware8 b. ~% Y5 ]0 J. K$ w
& X- g# @- Z& f6 Q
" D5 A$ `+ U5 U) {) u1 {5 I Q( S2.2、在工程中添加FreeRTOS源码" ^; t4 ~5 p; N; ]) t# ]
9 q+ x7 k; c) L9 {; q: ]
4 h _* z- V" Y$ M/ J
& k7 {/ h4 j3 t2 x/ I- k
三、程序代码
3 Z; {$ h3 X9 m u* s3 {, X7 h' f8 j" Y! @4 X$ I* V
3 A, Z/ `; X4 Z7 L$ V! a3.1、FreeRTOSConfig.h+ y5 c& B+ V7 v* L
- /*( g8 l$ ^- A% U/ d4 b
- * FreeRTOS V202212.01
4 @' p9 v" `' t4 b9 H0 D9 d; O& N - * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.. C( N1 r5 |( C4 X- S! q o
- *
! A4 ]: P& x; w - * Permission is hereby granted, free of charge, to any person obtaining a copy of; ]. v) Y3 F- O
- * this software and associated documentation files (the "Software"), to deal in! y# w. M% a' l/ i
- * the Software without restriction, including without limitation the rights to8 x1 K1 c! s% y b& ?% x
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of. {6 d- I7 {6 ^9 H2 B& Q2 ?9 N
- * the Software, and to permit persons to whom the Software is furnished to do so,
9 K3 r7 ~! ^1 q6 F) K - * subject to the following conditions:
( b4 g: L: R; ~1 m$ R- _6 {2 O - *, u3 E" g# s" n4 y* K% ~" `
- * The above copyright notice and this permission notice shall be included in all
- p' G7 a0 t3 E2 c - * copies or substantial portions of the Software.% z; N7 V+ k0 K+ E0 {+ c7 k
- *
9 u2 D; T3 \$ }8 ^- Y' n. F - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR2 U1 i5 _. ^4 ]$ {, {& S
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS1 ^. @. R! [. k1 Z
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
) y6 u, u% m# ]+ `: w - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
: K' x$ |3 F; q - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
) n4 K/ L. [# |+ X3 J' g - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE." ?+ {/ u) M* c a' W7 y4 u, v
- *+ y$ ~! C3 Z6 s& V- V
- * https://www.FreeRTOS.org" T: y' F/ j' i# l
- * https://github.com/FreeRTOS c$ P5 ~5 Z& F# ?: }' s& m2 w
- *
/ b; z! L+ F8 ~7 } - */
' ?/ ~$ C* C. _ - ' k9 V t# `7 \2 {
- #ifndef FREERTOS_CONFIG_H
5 |8 t0 H0 W/ ?* b - #define FREERTOS_CONFIG_H' P/ S" N5 f0 L* {$ w7 X
- W3 }) n' x6 ^, O' v+ Y- /*-----------------------------------------------------------4 |7 Q3 C& i7 j% X& o
- * Application specific definitions.' H* j. k0 w# i1 X: A- v7 M
- *- o: A! ?+ w7 M$ x0 j" z4 u8 [
- * These definitions should be adjusted for your particular hardware and- U( y( f% y5 o3 c4 _: M, S
- * application requirements.
1 i- c0 }! o: g9 J. ^+ B - *
6 @2 R2 e0 }5 g. h, [ - * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
- `+ R. s: F( ]$ }& Y - * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
" D# o. D- y' W7 x' J, A4 p - *7 i5 f, M% G& Q: E9 z. J
- * See http://www.freertos.org/a00110.html.
. {& W, L9 G. K- ?6 d# e - *----------------------------------------------------------*/5 Z" {, N! j3 c) `
- : n/ o& ]- j- ~% s7 Z
- /* Ensure definitions are only used by the compiler, and not by the assembler. */# a2 f7 y; l" u
- #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)# F# e/ H! u: H+ R5 {, ^
- #include <stdint.h>
q" B' q( K% U0 t& ~5 C% {; k - extern uint32_t SystemCoreClock;* w. U) k' K4 z* O ?5 t8 Z8 Y
- #endif
. T v+ k3 \8 K* r* o# p, w - ! \) t9 X2 Q+ i! E M" g/ r
- #ifndef configENABLE_FPU
0 a; T9 o. r1 b4 P( Z V - #define configENABLE_FPU 1
! u L* J; F5 _ @) c - #endif3 W6 t4 ]2 T" n' L1 c3 J& J
- #ifndef configENABLE_MPU
+ X' G4 O3 Z9 Z4 b+ h/ e - #define configENABLE_MPU 0% w" i7 V, z( O& W' T, |
- #endif
Q0 U- I4 D' q: ^$ K t4 k% c" e9 C - #ifndef configENABLE_TRUSTZONE
1 \& d( \" c+ y) X) ?7 R - #define configENABLE_TRUSTZONE 0. d$ \0 A- L. k$ _! H& q3 f
- #endif
) p' E5 k& k) P( F$ O - #ifndef configRUN_FREERTOS_SECURE_ONLY
- ~6 v2 y0 r2 A! R$ L5 v - #define configRUN_FREERTOS_SECURE_ONLY 1
Q3 @4 S" X; W _& e" o/ D - #endif
2 y9 c& O6 f$ w3 _. D; S( ^$ F( ]) G
+ s+ b( I2 z1 \( t" U
% {6 B" |+ M5 E$ ^- #define configENABLE_MVE 0
7 _) n+ q) z, V0 Q1 ~% F - 7 w5 T2 E2 f1 O' w
- #define configUSE_PREEMPTION 16 L& o, J* @+ a
- #define configSUPPORT_STATIC_ALLOCATION 0//1
) w( }0 z" y3 Q - #define configSUPPORT_DYNAMIC_ALLOCATION 14 Z* o9 L/ p' _8 I
- #define configUSE_IDLE_HOOK 0
$ T1 y3 A* c9 o7 K f/ ~6 O0 \ - #define configUSE_TICK_HOOK 0
5 k; Y6 }0 j8 s) v7 ~0 G0 t! | - #define configCPU_CLOCK_HZ ( SystemCoreClock )
0 S& m& }* J' }% H5 X* ` - #define configTICK_RATE_HZ ( ( TickType_t ) 1000 )" M9 z/ r/ q# c8 G
- #define configMAX_PRIORITIES ( 56 )$ W2 T3 K8 w# N+ {% I2 K
- #define configMINIMAL_STACK_SIZE ( ( uint16_t ) 512 )
; @/ Y+ x# V$ S' K. c! c - #define configTOTAL_HEAP_SIZE ( ( size_t ) 15 * 1024 )
_/ @6 A o3 q1 n - #define configMAX_TASK_NAME_LEN ( 16 )7 w# U" Y8 K$ M4 M3 I+ U
- #define configUSE_TRACE_FACILITY 1
. V& w9 R, @3 P$ N - #define configUSE_16_BIT_TICKS 0
' R1 s9 R9 \2 m0 |% v. O$ l - #define configUSE_MUTEXES 1
# v U2 Q' D: L* l - #define configQUEUE_REGISTRY_SIZE 8% ~/ C8 @4 [$ I+ T7 X$ `; b
- #define configUSE_RECURSIVE_MUTEXES 1
$ T7 ^) `4 c% l" g - #define configUSE_COUNTING_SEMAPHORES 1. k/ I5 k: _) ]% N7 F3 Q
- #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
$ T4 U/ H1 ^4 X) O - #define configUSE_MALLOC_FAILED_HOOK 0//1# v/ g) l/ L/ ~
- #define configCHECK_FOR_STACK_OVERFLOW 0//2
1 W/ O' }1 T( [! J% Z - ' V0 w$ l- x+ H$ y* i0 E( t0 U
- /* Defaults to size_t for backward compatibility, but can be changed- c/ T7 n x" e, v; D# Y# J) q
- * if lengths will always be less than the number of bytes in a size_t. */
* w/ a! g5 R9 y( L - #define configMESSAGE_BUFFER_LENGTH_TYPE size_t
# l0 _2 f0 ^; F4 T& F+ M, J+ U# H - /* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */1 t* h! i' F6 A9 J7 s+ y( @ w
- . w# F; [" M6 \+ ?2 r7 S
- /* Software timer definitions. */
. `) o, ?$ v6 _4 s3 x - #define configUSE_TIMERS 1. E) }0 w( N* o$ _# ~
- #define configTIMER_TASK_PRIORITY ( 2 )
2 i& J, N) n/ z) e4 U - #define configTIMER_QUEUE_LENGTH 10. E6 W) G" M# o0 ^" x- O
- #define configTIMER_TASK_STACK_DEPTH 2561 M- g* P6 D8 x1 G' [$ S) B
( o+ K$ ~% u' {/ Y1 c6 h) R- /* Set the following definitions to 1 to include the API function, or zero
& D+ D' I9 [8 P: F9 X( E - * to exclude the API function. */
7 `/ k$ ^; C' ~6 k+ ` - #define INCLUDE_vTaskPrioritySet 1
7 a3 M \' R2 A; h5 D" s - #define INCLUDE_uxTaskPriorityGet 1 H) m) N4 d. {5 T
- #define INCLUDE_vTaskDelete 1
/ ]' T# ]* Q8 y: T- O; ]2 z - #define INCLUDE_vTaskCleanUpResources 0( g4 c: w( c7 l9 ?6 l* h
- #define INCLUDE_vTaskSuspend 15 m% g+ {5 E8 D5 H- i
- #define INCLUDE_vTaskDelayUntil 1' k9 g9 V- {# ]' r. N( i
- #define INCLUDE_vTaskDelay 17 {$ U4 \6 s; L8 f1 r0 e% L
- #define INCLUDE_xTaskGetSchedulerState 1( y/ p* g* E9 X6 z r. X: W
- #define INCLUDE_xTimerPendFunctionCall 10 f6 c4 `; e- U4 Z& X7 c6 B
- #define INCLUDE_xQueueGetMutexHolder 1, s0 |% C+ O# |
- #define INCLUDE_uxTaskGetStackHighWaterMark 1
+ b+ ^9 n; U# y% R% w - #define INCLUDE_eTaskGetState 1; Y2 N! e/ g$ R% \( H# K, W
) C% [5 U {. G0 L* `- /* Cortex-M specific definitions. */
+ p$ |; Y( j: F - #ifdef __NVIC_PRIO_BITS" u T0 D9 M6 `0 W. K
- /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
' U" X" J: c9 _5 M* Q9 C* O - #define configPRIO_BITS __NVIC_PRIO_BITS" H7 E. o4 N3 n
- #else# D K0 K, F* R( @
- #define configPRIO_BITS 4
/ A- J. q5 G, O1 y) b& y - #endif
6 c- Z3 }9 G3 a! P8 m% l5 v7 U" y
' c; f) W) s* ?2 m- /* The lowest interrupt priority that can be used in a call to a "set priority"
* M m) {) m g* T3 i - * function. */
. W! l6 B- f! K - #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 159 \0 w: i* d, H! v: @. {8 X
4 O1 D. ~9 f; M: O/ ?. i- /* The highest interrupt priority that can be used by any interrupt service! D$ F5 n% X# D Q( R9 P
- * routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT
; _$ u! x7 f) k0 S1 k - * CALL INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A
7 S0 |0 y" `& b; i2 b1 ?8 P6 C - * HIGHER PRIORITY THAN THIS! (higher priorities are lower numeric values. */
& ~+ a: u- Z& h5 B - #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5& i" Q7 ?' [% L. H; K7 D. e% o5 N
, I2 ]! ~% ~" }0 p5 y- /* Interrupt priorities used by the kernel port layer itself. These are generic2 A) t9 ^- Q$ \4 Y
- * to all Cortex-M ports, and do not rely on any particular library functions. */" c- Z# o. r& t! n
- #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << ( 8 - configPRIO_BITS ) )! G/ F% h- m/ n
- /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
+ S3 Q6 k# A# t, [8 w3 [ - See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */+ e( \' N9 C/ T7 A
- #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << ( 8 - configPRIO_BITS ) )+ O% a$ o- q6 K" b
- 8 v- S) k$ L L7 H* ^% f. Y1 R
- /* Normal assert() semantics without relying on the provision of an assert.h- L3 {4 A1 `8 Z
- * header file. */" d1 ]7 q: B& A* S9 G
- #define configASSERT( x ) if ( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
! C9 I7 W8 P3 X$ C. j - & c& G1 O/ p& d, Y2 |
- /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
; y6 l! e3 U/ t/ f - * standard names. */
$ E+ q5 b1 A }$ e - #define vPortSVCHandler SVC_Handler) Q. @% p9 i3 Y
- #define xPortPendSVHandler PendSV_Handler% d4 T5 C, J( S) Z. ], T
- #define xPortSysTickHandler SysTick_Handler
* V! D! D2 ^/ Z. [
- ~! t6 j+ F% H# R- /* Allow system call from within FreeRTOS kernel only. */9 o/ }* ], M2 c( V6 ~6 Q u7 E" U0 y
- #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY 1
- u/ K: U6 R' J5 {7 G/ ~; a
( K6 |- o" z5 t L) Z, E- /* STM32H743 has 16 MPU regions and therefore it is necessary to configure8 l7 W, M2 ~$ C6 D8 G1 g8 N7 z
- * configTOTAL_MPU_REGIONS correctly. */
% r( E6 p; ^' `# M+ c& w- N - #define configTOTAL_MPU_REGIONS 16
) N/ j1 M' q9 t j1 I9 R' x- Q* i - # z- S/ F: l; b8 |' H. _0 _
- /* The default TEX,S,C,B setting marks the SRAM as shareable and as a result,$ } s# [) i# d+ O1 u; U& D
- * disables cache. Do not mark the SRAM as shareable because caching is being
- K1 g3 }2 z+ E {; { - * used. TEX=0, S=0, C=1, B=1. */
9 C o/ L( F( L5 m5 R; d$ | - #define configTEX_S_C_B_SRAM ( 0x03UL )
* A7 O0 y8 y+ ]& _! i, b( G - 0 u7 M: G H* g! E J; {1 t. N
- #endif /* FREERTOS_CONFIG_H */
$ Z; M7 C4 C6 r- h; ?- Q
复制代码
# i5 @1 V# G7 e; }7 h% E% q
, e" y" _ E8 x/ r" V. u: a, {3.2、屏蔽函数
8 p' ^% ^* Z. |: U% M6 p( G; j8 |+ S/ [3 Z
+ j3 o4 g) T& n在stm32n6xx_it.c6 W3 U9 E3 D: ]8 V, A# @
* {) B. \2 `4 _# ^! n2 H+ @
/ @+ D$ O7 E" I8 }( `4 m
4 L$ U+ f5 G- f9 B" }' b
9 m8 X/ z& ] z3 l( k
6 T# G- \* K5 z0 q
Y# q& \+ D0 o! O4 @3.3、main.c. ]) a/ E }+ ^. M( C5 w- h6 _
- #include "main.h"3 s1 e/ D! T8 g
' a! q* {: N7 K; t& L0 g- void SystemClock_Config(void);
% G9 ~3 g' L# t: l - void PeriphCommonClock_Config(void);
3 A0 |7 ]$ A$ j7 X, [% ^$ H- r - 7 T8 M6 y2 e/ L4 `) z* I! @$ k# C
- int main(void)+ p% q$ S5 _! A5 b T1 _
- {
! J* }) H0 z7 \# Z% b2 Z - HAL_Init();
4 N2 d5 }! S, r d - SystemClock_Config();
! u0 k% n/ \, r# d& n3 i4 R - PeriphCommonClock_Config();" M# F9 r: j |" Q. S$ z6 h
- usart_init(115200);7 S5 P& S7 Q. c7 _, Z
- init_led();" A5 C5 u. ~0 q, h! }2 f" O5 o
-
2 B6 o! z* W+ c8 K5 `9 \3 C - task_create();
; f/ k5 `# l' S( }
" N! e& k4 g, f- while (1)
) [ y/ T; ~; w! e! i) G8 I - {
- d% _1 U- L; ]1 Q h/ t- N7 n* @ - ! d3 t% b) L- b( Y6 B ?
- }
' m. O8 C' A* i9 C - }
, f9 Y! p& E9 M# M* K4 T
3 M) k; F5 |# P3 t$ [' a) W7 m! v/ A- / j* V2 ?% X6 {* T0 p, I6 w9 _
7 U& S- z( s- d% _/ L- ' W& A& t% p1 ~1 F, e" ?
) o+ r. p, q A4 p" s* D6 N9 J h- /**
6 K6 h! X) W+ |) a - * @brief System Clock Configuration
! i! T1 T8 x* ]' a6 V' g - * @retval None
; W3 M; b/ A% M) w% K& Y - */9 v) Y' [5 m+ e. [
- void SystemClock_Config(void)4 O4 z& }4 p, H
- {# p" P1 c$ w) I" b9 N1 |/ C1 l) m, b
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
) W, h/ L- J4 z - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};2 T& f) G) }8 o( e$ V
( \3 Z* M5 X" {7 d+ }+ }/ f- /** Configure the System Power Supply
( f# h; o$ S1 ?) n' V$ g6 @ - */
- \7 ~" u! o6 Y. o3 ?- b - if (HAL_PWREx_ConfigSupply(PWR_EXTERNAL_SOURCE_SUPPLY) != HAL_OK)& u ]7 i4 v& R7 o, H2 m
- {" r' r0 O9 ?# Y0 U: K
- Error_Handler();4 q+ E% v; v6 p, o2 M% y' o$ Z3 d
- }
9 e' C) @5 s- S7 u+ F7 m5 n
( f1 W, ]9 m) x- /* Enable HSI */# H# ?6 g; ~- \- h* r; R3 U
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
4 n$ U* t# ?+ R8 h [ - RCC_OscInitStruct.HSIState = RCC_HSI_ON;
1 n5 f+ }/ ~' h e5 d# Z' L4 F2 Q3 n - RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;/ K+ S$ v( J* z' t# {- ]; \1 i
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;3 u0 O3 z5 m9 @0 F
- RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_NONE;# [8 U; g. ^$ p0 j' y* K
- RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
& w; d3 T% l$ r - RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;7 n2 M( C, a: D4 \3 g; V
- RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;6 q! b( @; y, X) O5 p" y
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
8 }/ y! j0 n" M6 @8 P - {
2 @$ D; ~/ ?6 i2 x - Error_Handler();
: m% x+ f' G+ H# [. J - }
# {: H! t8 v% {$ K5 d - - U$ s! x4 j7 j. X% |) J
- /** Get current CPU/System buses clocks configuration and if necessary switch
% v- y. Y& j% Q7 i8 U" C% a - to intermediate HSI clock to ensure target clock can be set7 i+ D! W; i/ Y# r: L
- */
; h1 H: A) O" U6 D, E - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct);
: y) T' d6 M, }7 ~ - if ((RCC_ClkInitStruct.CPUCLKSource == RCC_CPUCLKSOURCE_IC1) ||
# N( K( x, A( [" ]+ T - (RCC_ClkInitStruct.SYSCLKSource == RCC_SYSCLKSOURCE_IC2_IC6_IC11))" F& e4 U9 G- p+ I, l- J1 @ t+ U
- {
& y. T1 _, i( v% c - RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_CPUCLK | RCC_CLOCKTYPE_SYSCLK);1 J. H, y' k1 n/ g8 [ U* p$ N. s
- RCC_ClkInitStruct.CPUCLKSource = RCC_CPUCLKSOURCE_HSI;: \* ~. t, m! J. _- k- a2 n
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
/ r& N1 Z% j/ R( J- K) \ - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
$ X6 ~% S. Z) m: r. _7 b/ k. e - {
}! Q0 H2 |; C% I, D# a4 ^" o - /* Initialization Error */# ^' [* |% ?! @9 [) G) ?& e( {
- Error_Handler();" _3 _4 q+ V) u: i+ c" c
- }" G4 P$ h% q3 O6 V( s9 P
- }: f1 o+ {4 w( r- H/ Y6 S6 B
- 9 i% k8 ?5 A; ~2 A) H
- /** Initializes the RCC Oscillators according to the specified parameters
0 N- |+ e& U1 k9 j. y" E - * in the RCC_OscInitTypeDef structure.: U2 i/ M; h2 ~; r# ]/ w
- */
8 k! T% x8 K# I - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
3 n" ]7 ~3 @! t g* w" h+ t - RCC_OscInitStruct.HSEState = RCC_HSE_ON;
+ @) H" g; {* K1 ]& u* q - RCC_OscInitStruct.PLL1.PLLState = RCC_PLL_ON;7 m8 M3 W' k& s" m! {3 N3 Y/ ?4 L
- RCC_OscInitStruct.PLL1.PLLSource = RCC_PLLSOURCE_HSE;
9 N+ e: a; l/ b5 c - RCC_OscInitStruct.PLL1.PLLM = 3;+ v7 }9 k- S9 T4 Q
- RCC_OscInitStruct.PLL1.PLLN = 100;/ x7 h) f! V% M9 U
- RCC_OscInitStruct.PLL1.PLLFractional = 0;
3 m& L; f X" }5 @$ Q - RCC_OscInitStruct.PLL1.PLLP1 = 2;
/ E- g. [5 ^9 y- G; f. K - RCC_OscInitStruct.PLL1.PLLP2 = 1;. O, B+ Q/ ?3 ?2 } M9 D
- RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;& j# }9 i. ^& E. N1 _' i" N: H
- RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;3 d! O4 j+ c9 }- T/ b
- RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
1 x: g- }8 j% N7 N6 O/ H8 |, |; I - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)' I" d- V, | h- ~, T
- {6 ^4 M1 C, R8 d/ [# O- p3 C
- Error_Handler();
& }6 h% ]: O1 @6 |* s - }/ T; p4 s8 c6 V1 h% X; M
- : h! Y0 A3 Z# t) m
- /** Initializes the CPU, AHB and APB buses clocks
$ M* P/ g3 H `- u8 u, @4 w0 G0 ? - */
. P7 X% p7 C/ j. v; n- X+ n - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_CPUCLK|RCC_CLOCKTYPE_HCLK: Z, K' N% w- K1 v2 w; A
- |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK17 D) ^8 q; s( B7 ]' G G
- |RCC_CLOCKTYPE_PCLK2|RCC_CLOCKTYPE_PCLK53 W2 Y3 l/ w h: e- s8 r4 ~/ Z
- |RCC_CLOCKTYPE_PCLK4;
$ M: D+ U2 c' N2 M - RCC_ClkInitStruct.CPUCLKSource = RCC_CPUCLKSOURCE_IC1;
. }6 R5 l+ v& Q( `+ } - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_IC2_IC6_IC11;
$ O- v A) \ E# ?0 q& p - RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
% ^* l U9 |7 y& u - RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;/ v5 \" v" Z- R. p |4 t7 f4 w- `8 ?2 v
- RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;; [7 u$ }/ C7 r5 ^. e9 T. d
- RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;/ T- H, x. h8 F6 v* ^
- RCC_ClkInitStruct.APB5CLKDivider = RCC_APB5_DIV1;8 E7 ]) z5 _8 G: \
- RCC_ClkInitStruct.IC1Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
5 ^+ P! I' x( b6 o - RCC_ClkInitStruct.IC1Selection.ClockDivider = 1;
# M% _! |3 _4 P1 D6 `: _ - RCC_ClkInitStruct.IC2Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;$ {0 J4 Q& k( x1 `5 u
- RCC_ClkInitStruct.IC2Selection.ClockDivider = 2;
- l" p1 u7 d1 R0 r. i1 Z* a - RCC_ClkInitStruct.IC6Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;, G$ k( h; D9 k
- RCC_ClkInitStruct.IC6Selection.ClockDivider = 4;3 }( H1 M) A0 d9 {9 A0 M) k0 r
- RCC_ClkInitStruct.IC11Selection.ClockSelection = RCC_ICCLKSOURCE_PLL1;
" ]3 u4 k2 k! [, G - RCC_ClkInitStruct.IC11Selection.ClockDivider = 8;
* ]' c8 z9 M' y- ^. x; U
- E2 j+ i5 @7 G( Z0 {4 m+ H: p8 Y- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
. \+ `( w7 a( `2 d& C* T* n- U$ Y - {+ J4 {, O% C( u# G- ]
- Error_Handler();/ J! s* I, o& \* R$ A$ `! h# r
- }
8 `1 E; M6 x+ B7 W6 ` - }
4 P, h) z4 E/ } - ) W# B8 w8 `- H% T0 N# o p5 Y! [
- /**
, |' K; o% o4 X6 s - * @brief Peripherals Common Clock Configuration
, p; K8 h+ p6 j6 @% H: j: \6 j - * @retval None: D. l& o$ t( r0 o( W1 D
- */# f! G1 }3 E3 ]$ `7 C! v: A& U4 H9 ^
- void PeriphCommonClock_Config(void)3 x* o$ i* u/ Y* J' v
- {) j& _- c% ?5 K0 A- m6 e( v7 C
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/ _; G6 b# V2 ]
+ q7 i2 a. t: s |* j1 `! [- /** Initializes the peripherals clock
3 E0 ~3 Z. O3 e; b7 h - */$ y8 y9 s/ F3 t3 l( K
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_TIM;# A' @+ [6 {$ x
- PeriphClkInitStruct.TIMPresSelection = RCC_TIMPRES_DIV1;( ` G9 [* [, V G
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK); ?) p+ l+ h$ q5 B3 T; d6 f
- {9 a0 o' i- E. Q
- Error_Handler();
9 A1 S# j$ x9 ~1 p9 s - }9 f" U% m' j7 Y- {; n
- }
2 B$ c$ \( x" z6 k0 M" @" z7 @ - ' @5 i- N3 k: `' x$ V, ^3 M
- /**
5 v. @ z9 \# |' M - * @brief ICACHE Initialization Function
; C8 T& p) I/ ^- U$ c" t - * @param None
) h. p% C! X8 I - * @retval None* W# }/ n5 \7 Q0 C+ O7 b1 }1 D! T
- */
9 u5 k9 q. b+ s! i - static void MX_ICACHE_Init(void)5 R; a: U3 U+ S8 D
- {2 v5 R( f. G% u( W
$ f0 Z: a6 @, p) ]' T* u- x- /* USER CODE BEGIN ICACHE_Init 0 */
" y5 {, X* {& z- r: N9 V+ c
) G" I7 i5 I/ t* V$ [* T% L. D1 O- A' T* `- /* USER CODE END ICACHE_Init 0 */+ ]# H: h6 o) t q4 `
, Z6 r5 V1 D) h7 l; @7 J- /* USER CODE BEGIN ICACHE_Init 1 */. M2 `' U; o& U) k
- 7 h. Y" n5 M2 u( @/ I
- /* USER CODE END ICACHE_Init 1 */
c* g9 ~7 l% [2 \0 y' v
$ a+ m) k8 R M H& V- /** Enable instruction cache in 1-way (direct mapped cache)
6 A' z1 {; l. u5 m% l) M - */
, n, h) \9 z" v8 M. B* _8 z - if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK) K- v1 H% @. d% C
- {. G5 c( o3 Q2 T C
- Error_Handler();
- ^# I3 O# {& k) _' f - } q1 N, E- c) T0 F% r* @5 M9 s$ y
- if (HAL_ICACHE_Enable() != HAL_OK)/ L& j3 b, U9 ~+ ~0 J) J
- {- ]2 c% f0 A' h; U/ u5 Q
- Error_Handler();
6 @0 y6 f( \9 F$ F. S - }
- v$ H& E8 S3 Z" `3 p - /* USER CODE BEGIN ICACHE_Init 2 */: _8 \8 I' z, |' T) u" G
0 Z. a) v0 k( v9 `, W' l9 K2 `0 \7 F- /* USER CODE END ICACHE_Init 2 */0 J. v2 z1 d% c# J& ^3 F# N! i+ g
, m: _* Z. e2 @- }
) C" S5 [$ h$ s: \( m3 F - /* USER CODE BEGIN 4 */! }0 M8 P* ^/ D) l2 M
! `% P% |; m: k2 u& j& N# V- /* USER CODE END 4 */4 p8 K- _- J5 O
- 4 Q$ u0 v( H- C, @
- /**5 T7 V% J: i# o; X7 |
- * @brief This function is executed in case of error occurrence.
8 ~, h* l# C1 { - * @retval None( y/ N4 p: n* U4 O* t) J
- */3 X2 _. D& g! C; a5 ~
- void Error_Handler(void), B' Y' n. g( E) k
- {6 L5 T( q7 q, b; T9 N, w
- /* USER CODE BEGIN Error_Handler_Debug */
9 C/ |( U, j$ q. y8 w; ]. ~- x - /* User can add his own implementation to report the HAL error return state */, `! a6 n- K6 ^. w% [0 ~" N
- __disable_irq();
( h, j' U4 j7 \: r - while (1)+ B3 b' I) q" {% y4 H
- {! t) S( }1 n7 S$ U1 T# h6 X9 k
- }
% v3 q' `( B# U - /* USER CODE END Error_Handler_Debug */# e2 O0 u5 y0 L
- }
1 a4 g' _* T# b, w+ ]1 ^7 E8 { - # ]# p2 J4 a' `/ p8 i$ I
- #ifdef USE_FULL_ASSERT6 C" A2 y/ X* Z9 o7 d
- /**. J1 e1 S4 L0 G# O
- * @brief Reports the name of the source file and the source line number( K, W$ G! R# d# L& m
- * where the assert_param error has occurred.
: A7 X8 e( c5 c/ j; ` - * @param file: pointer to the source file name
) e5 P- q |2 j% k/ M - * @param line: assert_param error line source number
7 F! e1 Q$ o0 P3 O$ C3 C - * @retval None
% r) {$ V4 H9 F: s# J M - */
/ p6 v8 W) Q# ]( Q# _" U - void assert_failed(uint8_t *file, uint32_t line): A+ p" L( ^8 h/ b
- {; |" u: i# [" |$ ^5 g. {; j# k2 W( v
- /* USER CODE BEGIN 6 */
% S3 d- P+ y0 p - /* User can add his own implementation to report the file name and line number,7 c" {' c1 J2 f$ ^( g/ d& t# S$ d
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
9 Z% `" \' z* y7 c# c: Q1 G9 M; |- p - /* USER CODE END 6 */! q- s% j; C1 M8 e; P0 \5 f
- }
% V* V, V. m/ w9 I - #endif /* USE_FULL_ASSERT */# {% f+ Q0 M" d) r8 d) y4 _
复制代码 ; @. l% M+ w9 g" |
9 u _; S+ d. v; K# t3.4、fun_task.c5 _- A6 S: |; D5 C
- #include "main.h"/ D# [4 X; v8 \* V) U; f" F
% e1 K9 w7 B0 p- _* T* y- #define START_TASK_PRO 1 ( T/ M9 H4 a5 E
- #define START_STK_SIZE 128 [' i7 a4 @, I! }
- TaskHandle_t StartTask_Handler;
" d: w" ^( ~6 t( @. ^& i
# o. a4 c! _! R' m! `9 v* s" k- #define TASK1_PRIO 2 % T7 b2 X1 { a/ u, G
- #define TASK1_STK_SIZE 128
3 |, {0 x8 d( Z# B - static TaskHandle_t Task1Task_Handler = NULL; * F5 x# g& n" d
-
" Y( E3 W7 h1 g) \- Z( x - #define TASK2_PRIO 3 9 k0 x o3 z) o6 ]4 x3 B, Y
- #define TASK2_STK_SIZE 128 + ~# N) t8 K* U* B
- static TaskHandle_t Task2Task_Handler = NULL;
2 i" h: U! \1 N4 h7 { - 9 Z' @+ T& E: w0 I4 N6 \7 v8 X! A
- #define TASK3_PRIO 4 1 G* V& N# E$ S3 ]; h* _+ C8 t4 F
- #define TASK3_STK_SIZE 128
/ d; w- s! l# V7 ~ U; f; T - static TaskHandle_t Task3Task_Handler = NULL; 1 J3 J @6 o1 `. T7 D& n
- / s: J' L$ x. h- {; t- F
- ! _4 I l1 G' R+ V" w4 c
- void start_task(void *pvParameters); 3 W! c4 e2 O0 a6 ^
- void gui_task(void *pvParameters);
. l+ O* Z5 I- q. M% l
. D+ r, d* T. y5 r6 b- void task1(void *pvParameters); 6 p2 t$ A; H! ?6 q0 o+ k- _" V* F
- void task2(void *pvParameters);! T5 |' V9 f9 ?
- void task3(void *pvParameters);
: o( S" ~3 F' x) R$ m, D" L% V - , w+ b& a2 u5 u/ n! Q
3 C# c8 b7 w+ ~1 ^( x) I7 F2 T- 6 i; y/ L2 }' R' W: ]3 \/ j/ p
- void task_create(void)
; H6 e9 i( h0 o, D/ P, | - {
0 s1 f5 H( k' `# \4 a, y -
& O6 S/ A/ U1 M U' |! c$ w - //start_task
/ p7 @! m! G' m# Z - xTaskCreate((TaskFunction_t )start_task,
) W: x& i' i/ ~2 I - (const char* )"start_task",
/ e6 Q' k5 i+ \# T W. I - (uint16_t )START_STK_SIZE,
2 a" \5 |. v( R9 X6 j - (void* )NULL, , E& v" A$ t, ~3 f) F+ q
- (UBaseType_t )START_TASK_PRO,
3 T# `1 ]& H9 l - (TaskHandle_t* )&StartTask_Handler);
! M' H# K( `' {% p9 r7 N# j2 L
! C9 U- n6 r m0 a8 ]6 T- vTaskStartScheduler();
9 h# Q a- c# y! d* Y! q4 C - }
" o- l1 g6 v# u5 a6 h' X6 |; u
8 w2 o* Q4 {3 a$ R7 E: b- void start_task(void *pvParameters)7 I7 U5 G' m7 Z- {% g4 k( p
- {
6 v% G, w9 d7 ?9 O+ j - taskENTER_CRITICAL(); 0 l: O! P2 t( ^# t. T
- //task1
' z4 [2 h% x& r: N. T - xTaskCreate((TaskFunction_t )task1, & F( x5 k6 G6 z/ M* w- w z
- (const char* )"task1",
, g7 x& g3 Q; Z - (uint16_t )TASK1_STK_SIZE,
1 G/ }3 {3 n1 J - (void* )NULL,
$ Y# g2 i/ I: I5 \% o$ [3 F+ T2 ` - (UBaseType_t )TASK1_PRIO, 6 S2 v. p! Z3 [7 Z7 n- T( |
- (TaskHandle_t* )&Task1Task_Handler);
& ?0 ? R( I" Q& O/ o1 T - //task2" U5 p# a3 ]9 ]( z
- xTaskCreate((TaskFunction_t )task2,
4 c& T" ^; A( @1 S# o - (const char* )"task2", 4 w1 y& y5 o* ]9 i
- (uint16_t )TASK2_STK_SIZE, 0 Y: v" R& [. I
- (void* )NULL, : a4 Z/ b, V; S5 V7 ]7 y* z& o$ ]8 p: x
- (UBaseType_t )TASK2_PRIO,
6 z! Y% v( c# G( g! V - (TaskHandle_t* )&Task2Task_Handler);
; S7 E0 b& h3 Z - //taskEXIT_CRITICAL();
7 F2 j1 C3 w9 u. O5 D" u -
% \ {( p+ x, C+ F* T- L! E7 G - //task3
+ o) b I5 Q4 Z" f% a4 T( T - xTaskCreate((TaskFunction_t )task3,
+ f5 `( \/ r k' E& w P5 W$ S - (const char* )"task3",
) @: D8 W9 @- m0 y - (uint16_t )TASK3_STK_SIZE, 0 [: f$ H% X1 ^! H
- (void* )NULL, * ?! ~- @; w. N P3 F& |
- (UBaseType_t )TASK3_PRIO,
, I+ m& D# `! Z' n - (TaskHandle_t* )&Task3Task_Handler); & c" _1 V1 x( ~8 l2 {8 x3 b8 J
- taskEXIT_CRITICAL();
* K# @" v' I+ g) X -
/ d$ E- c, C% W2 O - vTaskDelete(StartTask_Handler);
/ v5 X2 }+ v2 n - }4 K/ P3 r, ]- n! H
- $ \) Z. E4 z% Y8 [6 d6 T* ]
- //task1
4 T3 k3 H3 Q" c& ^ - void task1(void *pvParameters)
; Z5 v _$ H3 X - { - T$ }7 g6 x' A5 a; J) S
- __IO uint32_t index = 0;
( w' {" y0 m5 \) g" ? - __IO uint32_t flash_id_index = 0;
# N8 ?6 H% a0 ^" Y5 @ - 2 Q6 Q/ u; H, J C( ]& Q2 O2 J$ q
- while (1)
) A1 T& \$ O" A7 ?4 r2 N2 b0 S - {
' Q; }1 f- D4 w6 ^) b - led1_tog();6 Q8 x6 a3 n# H$ g* L& t
- printf("task1 run ...\r\n");
- u% Q( j! o9 w+ c - vTaskDelay(200);
& |) c' m }1 Z7 R& P7 N+ ^* j - }
1 R8 {( u; i& ~+ L - }
6 O! k8 O/ l; U( D- m$ d6 L' K
1 x$ K7 P# G$ k3 \- //task2
3 ^1 D/ Z7 w4 L9 G9 }0 Y3 f7 n - void task2(void *pvParameters)- z$ y+ O" k2 |( B9 T ~
- {
4 x) s6 u: v0 K2 N; x$ X) k+ V, T - while (1) r. ?' d7 m% _! X
- {
, f; R0 }- x2 {2 V% c8 C( z - led2_tog();
/ _5 O- c2 ?1 U1 c( O/ w - printf("task2 run ...\r\n");
% E- w0 m3 Z) R( Z+ Z, d - vTaskDelay(100); B& I" j% z+ A% [$ u
- }
$ U7 L% k) D$ D( ]: _ - }
2 r+ B, A2 `+ }+ i/ R - " i# h3 m$ Y6 Y# `. Z7 ~
- //task3
$ r# Z7 h" M+ }$ P - void task3(void *pvParameters)" ]% G; ^5 s# Q ?( i, i3 X1 w6 U
- {
6 m0 p3 u0 n* ]: f6 q( b - while (1)( f! i; e* A9 x$ K
- { 1 K6 I7 l- ^7 T0 w: s
- printf("task3 run ...\r\n");6 h r S. a w* u1 ~1 ]2 O3 @
- vTaskDelay(100);
2 i9 ?7 K, v; v, k - }
$ }0 A: u1 r2 ] I- q& N - }
复制代码 1 n9 E- B" D& z# W; a* |
" ?- T- A" Z2 B f; |四、运行结果: v e1 B$ D4 M1 P7 [9 ^4 s
( L+ ?& G: f3 _& p
4.1、仿真运行后,串口输出9 W8 V$ D+ G5 l W
+ t: Y5 x1 ?" L5 {7 z* U/ o9 X8 k" Q0 K0 q8 F
; c J) M4 k; c" E
7 y/ ^: k! P1 |8 L$ i) _# e8 F7 G+ F- T
7 W. v/ s, B" I! A& _7 J8 |4.2、LED灯运行' m: I4 B. r+ D3 o( x
7 ]7 B# Q# c9 I, C! c4 j/ W2 m8 }
9 L6 ~0 b6 x5 V- G
) B3 C) `* }, \2 s+ O3 O3 Y9 P7 |1 {9 D0 J$ z) O2 j# q3 \
3 f* o$ }0 d. L6 _% A1 }
! g3 T$ D* s( \" ~4 }5 ^+ @+ d8 A- O: }9 U3 l( }
1 E: V3 u y/ c2 p7 G$ [$ E2 P* j3 l( B& |0 d, a7 ]
+ U. _1 h+ e- }( a) `2 ~
1 X- y H1 H2 W l
r R! J; i: ?! u |
感谢分享