1414- RT-Thread操作系统的Arduino生态兼容层:https://github.com/RTduino/RTduino
1515
1616## 2 FreeRTOS的API支持情况
17- 兼容层对FreeRTOS的支持情况记录在[ issue] ( https://github.com/RT-Thread-packages/FreeRTOS-Wrapper/discussions/31 ) 中记录。一些支持的函数在功能和使用方法上和FreeRTOS略有不同,在迁移过程中需要注意。
17+ 兼容层对FreeRTOS API的支持情况如下:
18+ ### 任务控制
19+ - [x] xTaskCreate
20+ - [x] xTaskCreateStatic
21+ - [ ] [ xTaskCreateRestrictedStatic] ( https://www.freertos.org/xtaskcreaterestrictedstaticfreertos-mpu-specific.html )
22+ - [x] vTaskDelete
23+ - [x] vTaskDelay
24+ - [x] vTaskDelayUntil
25+ - [x] xTaskDelayUntil
26+ - [x] uxTaskPriorityGet
27+ - [x] vTaskPrioritySet
28+ - [x] vTaskSuspend (只支持挂起当前线程)
29+ - [x] vTaskResume
30+ - [x] xTaskResumeFromISR
31+ - [x] xTaskAbortDelay
32+ - [ ] [ uxTaskGetSystemState] ( https://www.freertos.org/uxTaskGetSystemState.html )
33+ - [ ] [ vTaskGetInfo] ( https://www.freertos.org/vTaskGetInfo.html )
34+ - [ ] [ vTaskList] ( https://www.freertos.org/a00021.html#vTaskList )
35+ - [ ] [ vTaskGetRunTimeStats] ( https://www.freertos.org/a00021.html#vTaskGetRunTimeStats )
36+ - [ ] [ vTaskStartTrace] ( https://www.freertos.org/a00021.html#vTaskStartTrace )
37+ - [ ] [ ulTaskEndTrace] ( https://www.freertos.org/a00021.html#usTaskEndTrace )
38+ - [ ] [ SetThreadLocalStoragePointer] ( https://www.freertos.org/vTaskSetThreadLocalStoragePointer.html )
39+ - [ ] [ GetThreadLocalStoragePointer] ( https://www.freertos.org/pvTaskGetThreadLocalStoragePointer.html )
40+ - [x] xTaskGetApplicationTaskTag
41+ - [x] xTaskGetCurrentTaskHandle
42+ - [x] xTaskGetIdleTaskHandle
43+ - [x] uxTaskGetStackHighWaterMark
44+ - [x] eTaskGetState
45+ - [x] pcTaskGetName
46+ - [x] xTaskGetTickCount
47+ - [x] xTaskGetTickCountFromISR
48+ - [x] xTaskGetSchedulerState
49+ - [x] uxTaskGetNumberOfTasks
50+ - [x] vTaskSetApplicationTaskTag
51+ - [x] xTaskCallApplicationTaskTag
52+ - [x] vTaskSetTimeoutState
53+ - [x] xTaskGetCheckForTimeout
54+ ### 内核控制
55+ - [x] [ taskYIELD] ( https://www.freertos.org/a00020.html#taskYIELD )
56+ - [x] [ taskENTER_CRITICAL] ( https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html )
57+ - [x] [ taskEXIT_CRITICAL] ( https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html )
58+ - [x] [ taskENTER_CRITICAL_FROM_ISR] ( https://www.freertos.org/taskENTER_CRITICAL_FROM_ISR_taskEXIT_CRITICAL_FROM_ISR.html )
59+ - [x] [ taskEXIT_CRITICAL_FROM_ISR] ( https://www.freertos.org/taskENTER_CRITICAL_FROM_ISR_taskEXIT_CRITICAL_FROM_ISR.html )
60+ - [x] [ taskDISABLE_INTERRUPTS] ( https://www.freertos.org/a00020.html#taskDISABLE_INTERRUPTS )
61+ - [x] [ taskENABLE_INTERRUPTS] ( https://www.freertos.org/a00020.html#taskENABLE_INTERRUPTS )
62+ - [x] [ vTaskStartScheduler] ( https://www.freertos.org/a00132.html )
63+ - [x] [ vTaskEndScheduler] ( https://www.freertos.org/a00133.html )
64+ - [x] [ vTaskSuspendAll] ( https://www.freertos.org/a00134.html )
65+ - [x] [ xTaskResumeAll] ( https://www.freertos.org/a00135.html )
66+ - [ ] [ vTaskStepTick] ( https://www.freertos.org/vTaskStepTick.html )
67+ - [ ] [ xTaskCatchUpTicks] ( https://www.freertos.org/xTaskCatchUpTicks.html )
68+ ### 任务通知
69+ - [x] [ xTaskNotifyGive] ( https://www.freertos.org/xTaskNotifyGive.html )
70+ - [x] [ vTaskNotifyGiveFromISR] ( https://www.freertos.org/vTaskNotifyGiveFromISR.html )
71+ - [x] [ ulTaskNotifyTake] ( https://www.freertos.org/ulTaskNotifyTake.html )
72+ - [x] [ xTaskNotify] ( https://www.freertos.org/xTaskNotify.html )
73+ - [x] [ xTaskNotifyAndQuery] ( https://www.freertos.org/xTaskNotifyAndQuery.html )
74+ - [x] [ xTaskNotifyAndQueryFromISR] ( https://www.freertos.org/xTaskNotifyAndQueryFromISR.html )
75+ - [x] [ xTaskNotifyFromISR] ( https://www.freertos.org/xTaskNotifyFromISR.html )
76+ - [x] [ xTaskNotifyWait] ( https://www.freertos.org/xTaskNotifyWait.html )
77+ - [x] [ xTaskNotifyStateClear] ( https://www.freertos.org/xTaskNotifyStateClear.html )
78+ - [x] [ ulTaskNotifyValueClear] ( https://www.freertos.org/ulTasknotifyValueClear.html )
79+ ### 消息队列
80+ - [x] [ xQueueCreate] ( https://www.freertos.org/a00116.html )
81+ - [x] [ xQueueCreateStatic] ( https://www.freertos.org/xQueueCreateStatic.html )
82+ - [x] [ vQueueDelete] ( https://www.freertos.org/a00018.html#vQueueDelete )
83+ - [x] [ xQueueSend] ( https://www.freertos.org/a00117.html )
84+ - [x] [ xQueueSendFromISR] ( https://www.freertos.org/a00119.html )
85+ - [x] [ xQueueSendToBack] ( https://www.freertos.org/xQueueSendToBack.html )
86+ - [x] [ xQueueSendToBackFromISR] ( https://www.freertos.org/xQueueSendToBackFromISR.html )
87+ - [x] [ xQueueSendToFront] ( https://www.freertos.org/xQueueSendToFront.html ) (不支持设置超时)
88+ - [x] [ xQueueSendToFrontFromISR] ( https://www.freertos.org/xQueueSendToFrontFromISR.html )
89+ - [x] [ xQueueReceive] ( https://www.freertos.org/a00118.html )
90+ - [x] [ xQueueReceiveFromISR] ( https://www.freertos.org/a00120.html )
91+ - [x] [ uxQueueMessagesWaiting] ( https://www.freertos.org/a00018.html#ucQueueMessagesWaiting )
92+ - [x] [ uxQueueMessagesWaitingFromISR] ( https://www.freertos.org/a00018.html#ucQueueMessagesWaitingFromISR )
93+ - [x] [ uxQueueSpacesAvailable] ( https://www.freertos.org/a00018.html#uxQueueSpacesAvailable )
94+ - [x] [ xQueueReset] ( https://www.freertos.org/a00018.html#xQueueReset )
95+ - [ ] [ xQueueOverwrite] ( https://www.freertos.org/xQueueOverwrite.html )
96+ - [ ] [ xQueueOverwriteFromISR] ( https://www.freertos.org/xQueueOverwriteFromISR.html )
97+ - [ ] [ xQueuePeek] ( https://www.freertos.org/xQueuePeek.html )
98+ - [ ] [ xQueuePeekFromISR] ( https://www.freertos.org/xQueuePeekFromISR.html )
99+ - [x] [ xQueueIsQueueFullFromISR] ( https://www.freertos.org/a00018.html#xQueueIsQueueFullFromISR )
100+ - [x] [ xQueueIsQueueEmptyFromISR] ( https://www.freertos.org/a00018.html#xQueueIsQueueEmptyFromISR )
101+ - [ ] [ vQueueAddToRegistry] ( https://www.freertos.org/vQueueAddToRegistry.html )
102+ - [ ] [ vQueueUnregisterQueue] ( https://www.freertos.org/vQueueUnregisterQueue.html )
103+ - [ ] [ pcQueueGetName] ( https://www.freertos.org/pcQueueGetName.html )
104+ ### 信号量/互斥量
105+ - [x] [ xSemaphoreCreateBinary] ( https://www.freertos.org/xSemaphoreCreateBinary.html )
106+ - [x] [ xSemaphoreCreateBinaryStatic] ( https://www.freertos.org/xSemaphoreCreateBinaryStatic.html )
107+ - [x] [ vSemaphoreCreateBinary] ( https://www.freertos.org/a00121.html )
108+ - [x] [ xSemaphoreCreateCounting] ( https://www.freertos.org/CreateCounting.html )
109+ - [x] [ xSemaphoreCreateCountingStatic] ( https://www.freertos.org/xSemaphoreCreateCountingStatic.html )
110+ - [x] [ xSemaphoreCreateMutex] ( https://www.freertos.org/CreateMutex.html )
111+ - [x] [ xSemaphoreCreateMutexStatic] ( https://www.freertos.org/xSemaphoreCreateMutexStatic.html )
112+ - [x] [ xSem'CreateRecursiveMutex] ( https://www.freertos.org/xSemaphoreCreateRecursiveMutex.html )
113+ - [x] [ xSem'CreateRecursiveMutexStatic] ( https://www.freertos.org/xSemaphoreCreateRecursiveMutexStatic.html )
114+ - [x] [ vSemaphoreDelete] ( https://www.freertos.org/a00113.html#vSemaphoreDelete )
115+ - [x] [ xSemaphoreGetMutexHolder] ( https://www.freertos.org/xSemaphoreGetMutexHolder.html )
116+ - [x] [ uxSemaphoreGetCount] ( https://www.freertos.org/uxSemaphoreGetCount.html )
117+ - [x] [ xSemaphoreTake] ( https://www.freertos.org/a00122.html )
118+ - [x] [ xSemaphoreTakeFromISR] ( https://www.freertos.org/xSemaphoreTakeFromISR.html )
119+ - [x] [ xSemaphoreTakeRecursive] ( https://www.freertos.org/xSemaphoreTakeRecursive.html )
120+ - [x] [ xSemaphoreGive] ( https://www.freertos.org/a00123.html )
121+ - [x] [ xSemaphoreGiveRecursive] ( https://www.freertos.org/xSemaphoreGiveRecursive.html )
122+ - [x] [ xSemaphoreGiveFromISR] ( https://www.freertos.org/a00124.html )
123+ ### 定时器
124+ - [x] [ xTimerCreate] ( https://www.freertos.org/FreeRTOS-timers-xTimerCreate.html )
125+ - [x] [ xTimerCreateStatic] ( https://www.freertos.org/xTimerCreateStatic.html )
126+ - [x] [ xTimerIsTimerActive] ( https://www.freertos.org/FreeRTOS-timers-xTimerIsTimerActive.html )
127+ - [x] [ xTimerStart] ( https://www.freertos.org/FreeRTOS-timers-xTimerStart.html )
128+ - [x] [ xTimerStop] ( https://www.freertos.org/FreeRTOS-timers-xTimerStop.html )
129+ - [x] [ xTimerChangePeriod] ( https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriod.html )
130+ - [x] [ xTimerDelete] ( https://www.freertos.org/FreeRTOS-timers-xTimerDelete.html )
131+ - [x] [ xTimerReset] ( https://www.freertos.org/FreeRTOS-timers-xTimerReset.html )
132+ - [x] [ xTimerStartFromISR] ( https://www.freertos.org/FreeRTOS-timers-xTimerStartFromISR.html )
133+ - [x] [ xTimerStopFromISR] ( https://www.freertos.org/FreeRTOS-timers-xTimerStopFromISR.html )
134+ - [x] [ xTimerChangePeriodFromISR] ( https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriodFromISR.html )
135+ - [x] [ xTimerResetFromISR] ( https://www.freertos.org/FreeRTOS-timers-xTimerResetFromISR.html )
136+ - [x] [ pvTimerGetTimerID] ( https://www.freertos.org/FreeRTOS-timers-pvTimerGetTimerID.html )
137+ - [x] [ vTimerSetReloadMode] ( https://www.freertos.org/FreeRTOS-Timers-vTimerSetReloadMode.html )
138+ - [x] [ vTimerSetTimerID] ( https://www.freertos.org/FreeRTOS-timers-vTimerSetTimerID.html )
139+ - [x] [ xTimerGetTimerDaemonTaskHandle] ( https://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html#xTimerGetTimerDaemonTaskHandle )
140+ - [ ] [ xTimerPendFunctionCall] ( https://www.freertos.org/xTimerPendFunctionCall.html )
141+ - [ ] [ xTimerPendFunctionCallFromISR] ( https://www.freertos.org/xTimerPendFunctionCallFromISR.html )
142+ - [x] [ pcTimerGetName] ( https://www.freertos.org/FreeRTOS-timers-pcTimerGetName.html )
143+ - [x] [ xTimerGetPeriod] ( https://www.freertos.org/FreeRTOS-timers-xTimerGetPeriod.html )
144+ - [x] [ xTimerGetExpiryTime] ( https://www.freertos.org/FreeRTOS-timers-xTimerGetExpiryTime.html )
145+ - [x] [ uxTimerGetReloadMode] ( https://www.freertos.org/uxTimerGetReloadMode.html )
146+ ### 事件集
147+ - [x] [ xEventGroupCreate] ( https://www.freertos.org/xEventGroupCreate.html )
148+ - [x] [ xEventGroupCreateStatic] ( https://www.freertos.org/xEventGroupCreateStatic.html )
149+ - [x] [ vEventGroupDelete] ( https://www.freertos.org/vEventGroupDelete.html )
150+ - [x] [ xEventGroupWaitBits] ( https://www.freertos.org/xEventGroupWaitBits.html )
151+ - [x] [ xEventGroupSetBits] ( https://www.freertos.org/xEventGroupSetBits.html )
152+ - [x] [ xEventGroupSetBitsFromISR] ( https://www.freertos.org/xEventGroupSetBitsFromISR.html )
153+ - [x] [ xEventGroupClearBits] ( https://www.freertos.org/xEventGroupClearBits.html )
154+ - [x] [ xEventGroupClearBitsFromISR] ( https://www.freertos.org/xEventGroupClearBitsFromISR.html )
155+ - [x] [ xEventGroupGetBits] ( https://www.freertos.org/xEventGroupGetBits.html )
156+ - [x] [ xEventGroupGetBitsFromISR] ( https://www.freertos.org/xEventGroupGetBitsFromISR.html )
157+ - [ ] [ xEventGroupSync] ( https://www.freertos.org/xEventGroupSync.html )
158+ ### 不支持的功能
159+ - [ ] [ 消息队列集] ( https://www.freertos.org/RTOS-queue-sets.html )
160+ - [ ] [ 流缓冲区] ( https://www.freertos.org/RTOS-stream-buffer-API.html )
161+ - [ ] [ 消息缓冲区] ( https://www.freertos.org/RTOS-message-buffer-API.html )
162+ - [ ] [ MPU] ( https://www.freertos.org/FreeRTOS-MPU-specific.html )
163+ - [ ] [ 协程] ( https://www.freertos.org/croutineapi.html )
164+ - [ ] [ 钩子函数] ( https://www.freertos.org/a00016.html )
165+ - [ ] [ 跟踪功能] ( https://www.freertos.org/rtos-trace-macros.html )
18166
19- ### 2.1线程、消息队列与互斥量
167+ ## 3 使用注意事项
168+ 一些函数在功能和使用方法上和FreeRTOS略有不同,在迁移过程中需要注意。
20169
21- #### 2.1.1 vTaskSuspend
170+ ### 3.1线程、消息队列与互斥量
171+
172+ #### 3.1.1 vTaskSuspend
22173` vTaskSuspend ` 只支持挂起当前运行的线程,在使用时` xTaskToSuspend ` 参数必须为` NULL ` 。否则会触发断言。
23174
24- #### 2 .1.2 xQueueSendToFront
175+ #### 3 .1.2 xQueueSendToFront
25176` xQueueSendToFront ` 不支持设置超时,使用时` xTicksToWait ` 参数会被忽略,消息队列没有空间时会立即返回` errQUEUE_FULL ` 。
26177
27- #### 2 .1.3 xQueueCreateStatic
178+ #### 3 .1.3 xQueueCreateStatic
28179静态消息队列需要参考以下的例子创建,确保为消息队列分配的内存足够大:
29180``` c
30181#define QUEUE_LENGTH 10
@@ -38,11 +189,11 @@ StaticQueue_t xQueueBuffer;
38189QueueHandle_t xQueue1;
39190xQueue1 = xQueueCreate( QUEUE_LENGTH, ITEM_SIZE, &( ucQueueStorage[ 0 ] ), &xQueueBuffer );
40191```
41- #### 2 .1.4 Mutex和Recursive Mutex
192+ #### 3 .1.4 Mutex和Recursive Mutex
42193FreeRTOS提供了两种互斥量,Mutex和Recursive Mutex。Recursive Mutex可以由同一个线程重复获取,Mutex不可以。RT-Thread提供的互斥量是可以重复获取的,因此兼容层也不对Mutex和Recursive Mutex做区分。用` xSemaphoreCreateMutex ` 和` xSemaphoreCreateRecursiveMutex ` 创建的互斥量都是可以重复获取的。
43- ### 2 .2 定时器
194+ ### 3 .2 定时器
44195和FreeRTOS不同,RT-Thread不使用一个消息队列向定时器线程传递命令。使用兼容层时任何需要设置超时的定时器函数,如` xTimerStart( xTimer, xTicksToWait ) ` ,` xTicksToWait ` 参数会被忽略,函数会立即完成命令并返回。
45- ### 2 .3 FromISR函数
196+ ### 3 .3 FromISR函数
46197FreeRTOS为一些函数提供了在中断中使用的FromISR版本,如果这些函数唤醒了更高优先级的线程,需要手动调度,如下所示:
47198``` c
48199BaseType_t xHigherPrioritTaskWoken = pdFALSE;
@@ -53,7 +204,7 @@ if( xHigherPriorityTaskWoken )
53204}
54205```
55206RT-Thread不为函数提供FromISR版本,函数可以在中断调用并在内部完成调度。因此在兼容层中使用FromISR函数后不需要手动调度,`xHigherPriorityTaskWoken`总会被设置成`pdFALSE`。
56- ### 2 .4 内存堆
207+ ### 3 .4 内存堆
57208兼容层保留了FreeRTOS的五种内存分配算法,默认使用`heap_3`,`pvPortMalloc/vPortFree`内部调用`RT_KERNEL_MALLOC/RT_KERNEL_FREE`在RT-Thread内部的内存堆分配。这种情况下内存堆的大小由RT-Thread BSP配置决定,无法在`FreeRTOSConfig.h`中通过`configTOTAL_HEAP_SIZE`设置。
58209若使用其他算法,需要修改`FreeRTOS/sSConscript`,选择相应的源文件
59210
@@ -63,17 +214,17 @@ src += Glob(os.path.join("portable", "MemMang", "heap_3.c"))
63214```
64215在` FreeRTOS/portable/rt-thread/FreeRTOSConfig.h ` 中通过` configTOTAL_HEAP_SIZE ` 设置内存堆大小。应用调用` pvPortMalloc/vPortFree ` 会在一块独立于RT-Thread,大小为` configTOTAL_HEAP_SIZE ` 的内存堆中分配,RT-Thread内部的内存堆仍然存在,兼容层函数内部分配内存都在RT-Thread的内存堆完成。
65216
66- ### 2 .5 线程优先级
217+ ### 3 .5 线程优先级
67218RT-Threa线程优先级数值越小时优先级越高,而FreeRTOS线程优先级数值越大优先级越高。在使用兼容层的FreeRTOS API,如` xTaskCreate ` ,使用FreeRTOS的规则为线程指定优先级即可。若在应用中将RT-Thread和FreeRTOS API混合使用,在指定线程优先级时要特别注意。可以使用以下两个宏对RT-Thread和FreeRTOS线程优先级做转换:
68219``` c
69220#define FREERTOS_PRIORITY_TO_RTTHREAD (priority ) ( configMAX_PRIORITIES - 1 - ( priority ) )
70221#define RTTHREAD_PRIORITY_TO_FREERTOS (priority ) ( RT_THREAD_PRIORITY_MAX - 1 - ( priority ) )
71222```
72223
73- ### 2 .6 线程堆栈大小
224+ ### 3 .6 线程堆栈大小
74225FreeRTOS线程堆栈大小的单位为` sizeof(StackType_t) ` ,RT-Thread线程堆栈大小为` sizeof(rt_uint8_t) ` 。使用FreeRTOS API创建线程时一定要遵守FreeRTOS的规则,切勿混淆。
75226
76- ### 2 .7 vTaskStartScheduler
227+ ### 3 .7 vTaskStartScheduler
77228由于RT-Thread和FreeRTOS的内核启动流程不同,使用兼容层时,` main ` 函数是在一个线程中运行,该线程优先级为` CONFIG_RT_MAIN_THREAD_PRIORITY ` 。(此选项通过SCons配置,数值越小优先级越高。),此时调度器已经开启。一般的FreeRTOS应用采用以下的方式创建线程:
78229
79230``` c
@@ -85,7 +236,7 @@ vTaskStartScheduler();
85236
86237使用兼容层时,任何使用`xTaskCreate`创建的线程若优先级比`CONFIG_RT_MAIN_THREAD_PRIORITY`更高,会立即开始执行。`vTaskStartScheduler`只是为了提供对应用的兼容,没有任何实际效果。在使用兼容层时,创建线程要特别注意,确保在调用`xTaskCreate`时,该线程所需的所有资源已经完成初始化,可以正常运行。
87238
88- ## 3 使用方法
239+ ## 4 使用方法
89240通过Env工具将兼容层加入到工程中:
90241```shell
91242RT-Thread online packages
@@ -169,7 +320,7 @@ Task 1 receive data 9 from queue
169320Task 1 receive data 10 from queue
170321```
171322
172- ## 4 参考资料
323+ ## 5 参考资料
173324RT-Thread文档 [https:// www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/README](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/README)
174325
175326FreeRTOS文档 [https:// www.freertos.org/a00106.html](https://www.freertos.org/a00106.html)
0 commit comments