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