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