前言
! T; ?# T) |( N3 e$ b6 p# k4 `$ \本篇文章带大家了解FreeRTOS里面的FreeRTOSConfig.h文件,此文基于正点原子FreeRTOS手册讲解。; k% m- u& z! W. h0 r e5 i$ N
0 H$ V0 B/ }$ O4 n9 D7 U. \! Q
一、文件讲解$ l' L$ L) c& T6 I& a
源码:
6 K6 @' ^: _( z# F6 f
3 C2 y% }3 x+ M5 u$ N- /* USER CODE BEGIN Header */
6 h0 j& {2 q Q# A- s* @) N - /*$ a0 m/ Z% R& u7 Z m! g9 l
- * FreeRTOS Kernel V10.0.1
" Y+ T9 _+ j& W8 Z* }* C3 i - * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
' G* O. z- c5 z8 w - *
0 Q% U* A& A+ T$ N* J! X - * Permission is hereby granted, free of charge, to any person obtaining a copy of" G _. @7 v X! O; |1 X( x
- * this software and associated documentation files (the "Software"), to deal in$ s& n' D. I* {$ ]
- * the Software without restriction, including without limitation the rights to
# v% M+ {) Q; O% X q9 [' @# d, i - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of1 g* K9 n) i6 ?% l" V5 k
- * the Software, and to permit persons to whom the Software is furnished to do so,
5 K! U# w8 _* X/ B8 g - * subject to the following conditions:
5 d% g* N: k" O+ `7 N- N9 H, j - *" M; U3 B2 `: o8 _1 {9 i
- * The above copyright notice and this permission notice shall be included in all1 T N6 C% J# Y6 G v
- * copies or substantial portions of the Software.8 T0 s. H4 ^5 ^( z7 m" P+ D8 j
- *
; p8 S7 C- a1 t) x% S+ |! a - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" y5 ~% W) V/ c# s. H+ N - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS7 p8 w8 P% D/ \$ `7 s0 L
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR3 Z8 a, V. U- M) V+ x
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER _& q- h, W+ a
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN6 ^& @" m [$ b" H9 ~0 C
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 ~# i h' U5 X) b3 V - *
1 g4 h% s h/ m9 X L# R1 @
* U b6 e$ F6 A- *( I V, ~0 s$ V3 J, M
- * 1 tab == 4 spaces!
" \, H1 B: \! e# J j - */- v6 u! U, R! b: W ^, Q
- /* USER CODE END Header */
" \' Z( T# ]+ N$ q3 m2 W# F( A6 s - 6 { t! S8 U. N- k
- #ifndef FREERTOS_CONFIG_H
' P- A4 l$ ^5 Y* b - #define FREERTOS_CONFIG_H" N5 G/ B2 [: K6 z+ K3 }
- ! k6 c$ Y# H& z2 x6 ?
- /*-----------------------------------------------------------; g( B# P! K( D5 x9 ~
- * Application specific definitions.
7 Y9 }% `/ r0 Y1 ]8 Q6 X# W7 g - *: B! q5 Q" B" m5 i! o+ d) d+ K7 A
- * These definitions should be adjusted for your particular hardware and
$ A6 Y K4 W4 z u - * application requirements.) O4 q) C$ z9 n% i; e9 O- T
- *- }0 \/ q5 c) m2 I F* E( n8 t5 j
- * These parameters and more are described within the 'configuration' section of the
' X1 D8 y& @( _* @ - * FreeRTOS API documentation available on the FreeRTOS.org web site.( ~. F' Z: \& l8 s# C1 o# @
- *
% X4 s- x. _6 U7 Y2 R
4 Z# [. Y% u& g0 b) ` o5 j1 }- *----------------------------------------------------------*/. n- ^0 H5 a0 q8 e
- / K/ c# ^/ v3 ]$ B9 G- q- E
- /* USER CODE BEGIN Includes */( X6 K, J3 _3 H/ w( X( n1 v" a
- /* Section where include file can be added */2 q/ X$ u; f" ^3 D3 m, V
- /* USER CODE END Includes */+ X3 f% Z/ F( N0 G6 K; F
# b- ^/ l4 H/ D' l. L, H4 J6 |0 Y- /* Ensure definitions are only used by the compiler, and not by the assembler. */3 z; K) D# B* {8 ^
- #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
# \: L$ q+ m: c c& E/ A9 k - #include <stdint.h> w8 P, D$ ?: {/ W
- extern uint32_t SystemCoreClock;
5 O" S" l0 s/ p - #endif
) }. u& j6 y1 @, z0 k - #define configUSE_PREEMPTION 13 e* A4 p' H$ |2 D7 O$ o
- #define configSUPPORT_STATIC_ALLOCATION 1
0 }% P6 Y1 _% X( ]; S: F0 m9 m8 @% B - #define configSUPPORT_DYNAMIC_ALLOCATION 1 u; E8 j" v7 h; R2 \" r4 y# b
- #define configUSE_IDLE_HOOK 0
# C; E( U% Q9 A* f# k: G - #define configUSE_TICK_HOOK 0
/ A0 Q. y9 ], Z( C; X - #define configCPU_CLOCK_HZ ( SystemCoreClock )9 J- o6 z& U1 Y" I
- #define configTICK_RATE_HZ ((TickType_t)1000)4 K+ { E1 c: w9 o
- #define configMAX_PRIORITIES ( 7 )( N9 T3 t0 t1 Z6 n# d. P
- #define configMINIMAL_STACK_SIZE ((uint16_t)128)! |7 z# Z8 f3 l( h
- #define configTOTAL_HEAP_SIZE ((size_t)3072)/ X8 y7 i% F& P& U ]" |9 Q h
- #define configMAX_TASK_NAME_LEN ( 16 )
! C. Q" ^! o n# n3 A5 H& d - #define configUSE_16_BIT_TICKS 0
7 H$ \ _$ c; U- ] - #define configUSE_MUTEXES 1
% ]% X! U# A4 S3 E9 W - #define configQUEUE_REGISTRY_SIZE 8/ y5 b# N$ ^+ q' M! V: D1 N
- #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
, v* D4 j& i: l0 _; Y - ) t3 G' L0 l3 \( k$ Z( J
- /* Co-routine definitions. *// S, V. h/ [5 {8 m
- #define configUSE_CO_ROUTINES 0# w& c" ?4 j" z H% ` f
- #define configMAX_CO_ROUTINE_PRIORITIES ( 2 )7 c( M) k l! U$ u5 `" r) m
3 a& {' B- t9 H. q o- /* Set the following definitions to 1 to include the API function, or zero, `4 e* d$ U$ U3 ]
- to exclude the API function. */
) X& d3 y! p5 W' f4 o - #define INCLUDE_vTaskPrioritySet 1
. ~( [, _5 n0 ^, ? k - #define INCLUDE_uxTaskPriorityGet 1! E( f* r' ]: A" _. K, E( x+ f
- #define INCLUDE_vTaskDelete 1* B; `6 z4 ]' ~/ f! W! S
- #define INCLUDE_vTaskCleanUpResources 03 W# F3 c I T* r; a1 @6 u
- #define INCLUDE_vTaskSuspend 1
) t, u8 e X% W - #define INCLUDE_vTaskDelayUntil 0
0 ?& O5 O2 k, t9 M4 q - #define INCLUDE_vTaskDelay 1
/ x5 }. G8 |8 u; |2 e - #define INCLUDE_xTaskGetSchedulerState 1
3 g; S8 O9 M2 y$ k$ L - # `; n+ f& ]; D4 Q
- 1 B1 y+ W' a% u) n: c
- //Tickless o% }# D4 P; S; P3 v
- #define configUSE_TICKLESS_IDLE 1//启用tickless低功耗# x2 y. Z6 B, _ D3 J6 E/ G8 q, {3 V
- #define configEXPECTED_IDLE_TIME_BEFORE_SLEEP 3
, c; m2 Z9 v" _( S2 a7 A/ G1 B - 5 t t6 Y+ p6 |0 f
- /* Cortex-M specific definitions. */
, g: S' W" N. @& B: Q6 p - #ifdef __NVIC_PRIO_BITS* M* `$ U" g" F Z6 v7 D5 v+ O
- /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */! g- k% g, ^, ~6 k
- #define configPRIO_BITS __NVIC_PRIO_BITS
2 U! U0 y& F, \# ]6 B6 ~( X - #else) g, z6 T; I0 E8 C( y
- #define configPRIO_BITS 4- _& Q) \ l" d
- #endif
( e7 c3 ~( n# J - ; y* i1 X- i4 B( f. S+ C
- /* The lowest interrupt priority that can be used in a call to a "set priority"
f% C A% l1 _2 H2 E, H9 O* B - function. */
4 C9 [0 M) e2 s$ Y - #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15
* u3 L, s6 N& p1 j! {+ G. K. ~
9 _7 c/ R& u( u+ Q. b7 N- /* The highest interrupt priority that can be used by any interrupt service
9 x" ^: K5 U+ `- ^. p' J - routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
" C0 t3 J" {! h, v' m$ N* o - INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER; {! `4 `, p2 V% a( ?, G% P6 G6 `
- PRIORITY THAN THIS! (higher priorities are lower numeric values. */
; R0 C& K, `! [8 w3 S9 `# v Z5 G - #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 56 j& V" @% i0 z# I& ~. n% w- T: s
- % [0 s" T9 U7 w! p! s# q3 q7 ?1 H
- /* Interrupt priorities used by the kernel port layer itself. These are generic
( Q! D: b$ A* X' `# b2 C - to all Cortex-M ports, and do not rely on any particular library functions. */! D! o6 l* }% [7 J4 t
- #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
& D0 Y) D) I9 p5 k$ ?" t - /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
3 p! f H( A7 R; V: l - See <a href="http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html." target="_blank">http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html.</a> */
6 w6 P& A' v+ I$ M - #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )) R! |5 V% k. S* o
$ G, d* F& p( r8 R" y- /* Normal assert() semantics without relying on the provision of an assert.h7 P! M5 a& k2 ]7 g5 n3 `
- header file. */% t& _( m& z( Y! W5 x, ?
- /* USER CODE BEGIN 1 */
6 W& P9 v& \ t( G, v+ Q w - #define configASSERT( x ) if ((x) == 0) {taskDISABLE_INTERRUPTS(); for( ;; );}1 }# j: [! }8 y; q2 Q
- /* USER CODE END 1 */* v7 ~# C3 {" I( ?; A( f6 X5 A. |% L
, v+ F5 W) l. R Q- /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
6 O* R' I- o, S5 w; @2 E% G/ T - standard names. */3 ^* h4 l% x7 t/ N
- #define vPortSVCHandler SVC_Handler
. y. D( A, I6 u. ~. d6 G7 } - #define xPortPendSVHandler PendSV_Handler
3 t! g' B. N9 W) p- _ - 5 i4 c8 I) K- l3 O
- /* IMPORTANT: This define is commented when used with STM32Cube firmware, when the timebase source is SysTick,
$ b( Z) U& a4 `1 w9 N4 n+ t+ b - to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
3 z$ p' R |# n7 x1 V
3 P9 q1 E% k5 d- n- #define xPortSysTickHandler SysTick_Handler% e3 `6 D) ~6 X& s
- 7 ^6 O" L7 h( M* b4 \2 V/ u
- /* USER CODE BEGIN Defines */
8 J6 W; v$ s$ T6 V1 C f - /* Section where parameter definitions can be added (for instance, to override default ones in FreeRTOS.h) */: B# s% {5 R9 X" M5 ?5 \; z! B
- /* USER CODE END Defines */0 a- ]" z. `% q! Q) G# I
4 g- K8 C/ [9 U5 H2 |- #endif /* FREERTOS_CONFIG_H */9 ?) q ?8 l1 a8 r. f; T6 Z, j3 @; c5 T; I
复制代码 # b9 d# g* ^" J' h# _! [
FreeRTOS 的配置基本是通过在 FreeRTOSConfig.h 中使用“#define”这样的语句来定义宏定义实现的。& R8 \3 P" P, Q7 n4 t6 y! P* k
这些宏定义都有特殊的含义,我们就调其中一个讲解。- #define configGENERATE_RUN_TIME_STATS 1
8 T% ]) Y/ i. T+ O+ M, |
复制代码
" e+ W* J q& o, m. Y3 s将此宏设置为1开启时间统计功能,相应的 API 函数会被编译,为 0 时关闭时间统计功能。
9 G5 L/ X1 Q; ?; H& {其他的宏作用也和这个差不多,当设置为1时就可以使能某些函数,设置为0函数就不可用。' M: o- s3 ?% @
其他宏大家可以自己看看,一般cubeMX生成代码后都会自动配置一些宏。
; B4 b& W o: j$ y3 R. ?0 S
! C( K; f0 ^' Z( W总结
$ D5 p6 H. r6 Q2 ~FreeRTOSConfig.h文件简单解析就到这里。! L- m8 i j ^# \
( G3 y/ [/ j5 l# B
( w! A. K9 ^' S j6 V
|