|
|
@@ -14,17 +14,168 @@
|
|
|
- RT-Thread操作系统的Arduino生态兼容层:https://github.com/RTduino/RTduino
|
|
|
|
|
|
## 2 FreeRTOS的API支持情况
|
|
|
-兼容层对FreeRTOS的支持情况记录在[issue](https://github.com/RT-Thread-packages/FreeRTOS-Wrapper/discussions/31)中记录。一些支持的函数在功能和使用方法上和FreeRTOS略有不同,在迁移过程中需要注意。
|
|
|
+兼容层对FreeRTOS API的支持情况如下:
|
|
|
+### 任务控制
|
|
|
+- [x] xTaskCreate
|
|
|
+- [x] xTaskCreateStatic
|
|
|
+- [ ] [xTaskCreateRestrictedStatic](https://www.freertos.org/xtaskcreaterestrictedstaticfreertos-mpu-specific.html)
|
|
|
+- [x] vTaskDelete
|
|
|
+- [x] vTaskDelay
|
|
|
+- [x] vTaskDelayUntil
|
|
|
+- [x] xTaskDelayUntil
|
|
|
+- [x] uxTaskPriorityGet
|
|
|
+- [x] vTaskPrioritySet
|
|
|
+- [x] vTaskSuspend (只支持挂起当前线程)
|
|
|
+- [x] vTaskResume
|
|
|
+- [x] xTaskResumeFromISR
|
|
|
+- [x] xTaskAbortDelay
|
|
|
+- [ ] [uxTaskGetSystemState](https://www.freertos.org/uxTaskGetSystemState.html)
|
|
|
+- [ ] [vTaskGetInfo](https://www.freertos.org/vTaskGetInfo.html)
|
|
|
+- [ ] [vTaskList](https://www.freertos.org/a00021.html#vTaskList)
|
|
|
+- [ ] [vTaskGetRunTimeStats](https://www.freertos.org/a00021.html#vTaskGetRunTimeStats)
|
|
|
+- [ ] [vTaskStartTrace](https://www.freertos.org/a00021.html#vTaskStartTrace)
|
|
|
+- [ ] [ulTaskEndTrace](https://www.freertos.org/a00021.html#usTaskEndTrace)
|
|
|
+- [ ] [SetThreadLocalStoragePointer](https://www.freertos.org/vTaskSetThreadLocalStoragePointer.html)
|
|
|
+- [ ] [GetThreadLocalStoragePointer](https://www.freertos.org/pvTaskGetThreadLocalStoragePointer.html)
|
|
|
+- [x] xTaskGetApplicationTaskTag
|
|
|
+- [x] xTaskGetCurrentTaskHandle
|
|
|
+- [x] xTaskGetIdleTaskHandle
|
|
|
+- [x] uxTaskGetStackHighWaterMark
|
|
|
+- [x] eTaskGetState
|
|
|
+- [x] pcTaskGetName
|
|
|
+- [x] xTaskGetTickCount
|
|
|
+- [x] xTaskGetTickCountFromISR
|
|
|
+- [x] xTaskGetSchedulerState
|
|
|
+- [x] uxTaskGetNumberOfTasks
|
|
|
+- [x] vTaskSetApplicationTaskTag
|
|
|
+- [x] xTaskCallApplicationTaskTag
|
|
|
+- [x] vTaskSetTimeoutState
|
|
|
+- [x] xTaskGetCheckForTimeout
|
|
|
+### 内核控制
|
|
|
+- [x] [taskYIELD](https://www.freertos.org/a00020.html#taskYIELD)
|
|
|
+- [x] [taskENTER_CRITICAL](https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html)
|
|
|
+- [x] [taskEXIT_CRITICAL](https://www.freertos.org/taskENTER_CRITICAL_taskEXIT_CRITICAL.html)
|
|
|
+- [x] [taskENTER_CRITICAL_FROM_ISR](https://www.freertos.org/taskENTER_CRITICAL_FROM_ISR_taskEXIT_CRITICAL_FROM_ISR.html)
|
|
|
+- [x] [taskEXIT_CRITICAL_FROM_ISR](https://www.freertos.org/taskENTER_CRITICAL_FROM_ISR_taskEXIT_CRITICAL_FROM_ISR.html)
|
|
|
+- [x] [taskDISABLE_INTERRUPTS](https://www.freertos.org/a00020.html#taskDISABLE_INTERRUPTS)
|
|
|
+- [x] [taskENABLE_INTERRUPTS](https://www.freertos.org/a00020.html#taskENABLE_INTERRUPTS)
|
|
|
+- [x] [vTaskStartScheduler](https://www.freertos.org/a00132.html)
|
|
|
+- [x] [vTaskEndScheduler](https://www.freertos.org/a00133.html)
|
|
|
+- [x] [vTaskSuspendAll](https://www.freertos.org/a00134.html)
|
|
|
+- [x] [xTaskResumeAll](https://www.freertos.org/a00135.html)
|
|
|
+- [ ] [vTaskStepTick](https://www.freertos.org/vTaskStepTick.html)
|
|
|
+- [ ] [xTaskCatchUpTicks](https://www.freertos.org/xTaskCatchUpTicks.html)
|
|
|
+### 任务通知
|
|
|
+- [x] [xTaskNotifyGive](https://www.freertos.org/xTaskNotifyGive.html)
|
|
|
+- [x] [vTaskNotifyGiveFromISR](https://www.freertos.org/vTaskNotifyGiveFromISR.html)
|
|
|
+- [x] [ulTaskNotifyTake](https://www.freertos.org/ulTaskNotifyTake.html)
|
|
|
+- [x] [xTaskNotify](https://www.freertos.org/xTaskNotify.html)
|
|
|
+- [x] [xTaskNotifyAndQuery](https://www.freertos.org/xTaskNotifyAndQuery.html)
|
|
|
+- [x] [xTaskNotifyAndQueryFromISR](https://www.freertos.org/xTaskNotifyAndQueryFromISR.html)
|
|
|
+- [x] [xTaskNotifyFromISR](https://www.freertos.org/xTaskNotifyFromISR.html)
|
|
|
+- [x] [xTaskNotifyWait](https://www.freertos.org/xTaskNotifyWait.html)
|
|
|
+- [x] [xTaskNotifyStateClear](https://www.freertos.org/xTaskNotifyStateClear.html)
|
|
|
+- [x] [ulTaskNotifyValueClear](https://www.freertos.org/ulTasknotifyValueClear.html)
|
|
|
+### 消息队列
|
|
|
+- [x] [xQueueCreate](https://www.freertos.org/a00116.html)
|
|
|
+- [x] [xQueueCreateStatic](https://www.freertos.org/xQueueCreateStatic.html)
|
|
|
+- [x] [vQueueDelete](https://www.freertos.org/a00018.html#vQueueDelete)
|
|
|
+- [x] [xQueueSend](https://www.freertos.org/a00117.html)
|
|
|
+- [x] [xQueueSendFromISR](https://www.freertos.org/a00119.html)
|
|
|
+- [x] [xQueueSendToBack](https://www.freertos.org/xQueueSendToBack.html)
|
|
|
+- [x] [xQueueSendToBackFromISR](https://www.freertos.org/xQueueSendToBackFromISR.html)
|
|
|
+- [x] [xQueueSendToFront](https://www.freertos.org/xQueueSendToFront.html) (不支持设置超时)
|
|
|
+- [x] [xQueueSendToFrontFromISR](https://www.freertos.org/xQueueSendToFrontFromISR.html)
|
|
|
+- [x] [xQueueReceive](https://www.freertos.org/a00118.html)
|
|
|
+- [x] [xQueueReceiveFromISR](https://www.freertos.org/a00120.html)
|
|
|
+- [x] [uxQueueMessagesWaiting](https://www.freertos.org/a00018.html#ucQueueMessagesWaiting)
|
|
|
+- [x] [uxQueueMessagesWaitingFromISR](https://www.freertos.org/a00018.html#ucQueueMessagesWaitingFromISR)
|
|
|
+- [x] [uxQueueSpacesAvailable](https://www.freertos.org/a00018.html#uxQueueSpacesAvailable)
|
|
|
+- [x] [xQueueReset](https://www.freertos.org/a00018.html#xQueueReset)
|
|
|
+- [ ] [xQueueOverwrite](https://www.freertos.org/xQueueOverwrite.html)
|
|
|
+- [ ] [xQueueOverwriteFromISR](https://www.freertos.org/xQueueOverwriteFromISR.html)
|
|
|
+- [ ] [xQueuePeek](https://www.freertos.org/xQueuePeek.html)
|
|
|
+- [ ] [xQueuePeekFromISR](https://www.freertos.org/xQueuePeekFromISR.html)
|
|
|
+- [x] [xQueueIsQueueFullFromISR](https://www.freertos.org/a00018.html#xQueueIsQueueFullFromISR)
|
|
|
+- [x] [xQueueIsQueueEmptyFromISR](https://www.freertos.org/a00018.html#xQueueIsQueueEmptyFromISR)
|
|
|
+- [ ] [vQueueAddToRegistry](https://www.freertos.org/vQueueAddToRegistry.html)
|
|
|
+- [ ] [vQueueUnregisterQueue](https://www.freertos.org/vQueueUnregisterQueue.html)
|
|
|
+- [ ] [pcQueueGetName](https://www.freertos.org/pcQueueGetName.html)
|
|
|
+### 信号量/互斥量
|
|
|
+- [x] [xSemaphoreCreateBinary](https://www.freertos.org/xSemaphoreCreateBinary.html)
|
|
|
+- [x] [xSemaphoreCreateBinaryStatic](https://www.freertos.org/xSemaphoreCreateBinaryStatic.html)
|
|
|
+- [x] [vSemaphoreCreateBinary](https://www.freertos.org/a00121.html)
|
|
|
+- [x] [xSemaphoreCreateCounting](https://www.freertos.org/CreateCounting.html)
|
|
|
+- [x] [xSemaphoreCreateCountingStatic](https://www.freertos.org/xSemaphoreCreateCountingStatic.html)
|
|
|
+- [x] [xSemaphoreCreateMutex](https://www.freertos.org/CreateMutex.html)
|
|
|
+- [x] [xSemaphoreCreateMutexStatic](https://www.freertos.org/xSemaphoreCreateMutexStatic.html)
|
|
|
+- [x] [xSem'CreateRecursiveMutex](https://www.freertos.org/xSemaphoreCreateRecursiveMutex.html)
|
|
|
+- [x] [xSem'CreateRecursiveMutexStatic](https://www.freertos.org/xSemaphoreCreateRecursiveMutexStatic.html)
|
|
|
+- [x] [vSemaphoreDelete](https://www.freertos.org/a00113.html#vSemaphoreDelete)
|
|
|
+- [x] [xSemaphoreGetMutexHolder](https://www.freertos.org/xSemaphoreGetMutexHolder.html)
|
|
|
+- [x] [uxSemaphoreGetCount](https://www.freertos.org/uxSemaphoreGetCount.html)
|
|
|
+- [x] [xSemaphoreTake](https://www.freertos.org/a00122.html)
|
|
|
+- [x] [xSemaphoreTakeFromISR](https://www.freertos.org/xSemaphoreTakeFromISR.html)
|
|
|
+- [x] [xSemaphoreTakeRecursive](https://www.freertos.org/xSemaphoreTakeRecursive.html)
|
|
|
+- [x] [xSemaphoreGive](https://www.freertos.org/a00123.html)
|
|
|
+- [x] [xSemaphoreGiveRecursive](https://www.freertos.org/xSemaphoreGiveRecursive.html)
|
|
|
+- [x] [xSemaphoreGiveFromISR](https://www.freertos.org/a00124.html)
|
|
|
+### 定时器
|
|
|
+- [x] [xTimerCreate](https://www.freertos.org/FreeRTOS-timers-xTimerCreate.html)
|
|
|
+- [x] [xTimerCreateStatic](https://www.freertos.org/xTimerCreateStatic.html)
|
|
|
+- [x] [xTimerIsTimerActive](https://www.freertos.org/FreeRTOS-timers-xTimerIsTimerActive.html)
|
|
|
+- [x] [xTimerStart](https://www.freertos.org/FreeRTOS-timers-xTimerStart.html)
|
|
|
+- [x] [xTimerStop](https://www.freertos.org/FreeRTOS-timers-xTimerStop.html)
|
|
|
+- [x] [xTimerChangePeriod](https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriod.html)
|
|
|
+- [x] [xTimerDelete](https://www.freertos.org/FreeRTOS-timers-xTimerDelete.html)
|
|
|
+- [x] [xTimerReset](https://www.freertos.org/FreeRTOS-timers-xTimerReset.html)
|
|
|
+- [x] [xTimerStartFromISR](https://www.freertos.org/FreeRTOS-timers-xTimerStartFromISR.html)
|
|
|
+- [x] [xTimerStopFromISR](https://www.freertos.org/FreeRTOS-timers-xTimerStopFromISR.html)
|
|
|
+- [x] [xTimerChangePeriodFromISR](https://www.freertos.org/FreeRTOS-timers-xTimerChangePeriodFromISR.html)
|
|
|
+- [x] [xTimerResetFromISR](https://www.freertos.org/FreeRTOS-timers-xTimerResetFromISR.html)
|
|
|
+- [x] [pvTimerGetTimerID](https://www.freertos.org/FreeRTOS-timers-pvTimerGetTimerID.html)
|
|
|
+- [x] [vTimerSetReloadMode](https://www.freertos.org/FreeRTOS-Timers-vTimerSetReloadMode.html)
|
|
|
+- [x] [vTimerSetTimerID](https://www.freertos.org/FreeRTOS-timers-vTimerSetTimerID.html)
|
|
|
+- [x] [xTimerGetTimerDaemonTaskHandle](https://www.freertos.org/FreeRTOS-Software-Timer-API-Functions.html#xTimerGetTimerDaemonTaskHandle)
|
|
|
+- [ ] [xTimerPendFunctionCall](https://www.freertos.org/xTimerPendFunctionCall.html)
|
|
|
+- [ ] [xTimerPendFunctionCallFromISR](https://www.freertos.org/xTimerPendFunctionCallFromISR.html)
|
|
|
+- [x] [pcTimerGetName](https://www.freertos.org/FreeRTOS-timers-pcTimerGetName.html)
|
|
|
+- [x] [xTimerGetPeriod](https://www.freertos.org/FreeRTOS-timers-xTimerGetPeriod.html)
|
|
|
+- [x] [xTimerGetExpiryTime](https://www.freertos.org/FreeRTOS-timers-xTimerGetExpiryTime.html)
|
|
|
+- [x] [uxTimerGetReloadMode](https://www.freertos.org/uxTimerGetReloadMode.html)
|
|
|
+### 事件集
|
|
|
+- [x] [xEventGroupCreate](https://www.freertos.org/xEventGroupCreate.html)
|
|
|
+- [x] [xEventGroupCreateStatic](https://www.freertos.org/xEventGroupCreateStatic.html)
|
|
|
+- [x] [vEventGroupDelete](https://www.freertos.org/vEventGroupDelete.html)
|
|
|
+- [x] [xEventGroupWaitBits](https://www.freertos.org/xEventGroupWaitBits.html)
|
|
|
+- [x] [xEventGroupSetBits](https://www.freertos.org/xEventGroupSetBits.html)
|
|
|
+- [x] [xEventGroupSetBitsFromISR](https://www.freertos.org/xEventGroupSetBitsFromISR.html)
|
|
|
+- [x] [xEventGroupClearBits](https://www.freertos.org/xEventGroupClearBits.html)
|
|
|
+- [x] [xEventGroupClearBitsFromISR](https://www.freertos.org/xEventGroupClearBitsFromISR.html)
|
|
|
+- [x] [xEventGroupGetBits](https://www.freertos.org/xEventGroupGetBits.html)
|
|
|
+- [x] [xEventGroupGetBitsFromISR](https://www.freertos.org/xEventGroupGetBitsFromISR.html)
|
|
|
+- [ ] [xEventGroupSync](https://www.freertos.org/xEventGroupSync.html)
|
|
|
+### 不支持的功能
|
|
|
+- [ ] [消息队列集](https://www.freertos.org/RTOS-queue-sets.html)
|
|
|
+- [ ] [流缓冲区](https://www.freertos.org/RTOS-stream-buffer-API.html)
|
|
|
+- [ ] [消息缓冲区](https://www.freertos.org/RTOS-message-buffer-API.html)
|
|
|
+- [ ] [MPU](https://www.freertos.org/FreeRTOS-MPU-specific.html)
|
|
|
+- [ ] [协程](https://www.freertos.org/croutineapi.html)
|
|
|
+- [ ] [钩子函数](https://www.freertos.org/a00016.html)
|
|
|
+- [ ] [跟踪功能](https://www.freertos.org/rtos-trace-macros.html)
|
|
|
|
|
|
-### 2.1线程、消息队列与互斥量
|
|
|
+## 3 使用注意事项
|
|
|
+一些函数在功能和使用方法上和FreeRTOS略有不同,在迁移过程中需要注意。
|
|
|
|
|
|
-#### 2.1.1 vTaskSuspend
|
|
|
+### 3.1线程、消息队列与互斥量
|
|
|
+
|
|
|
+#### 3.1.1 vTaskSuspend
|
|
|
`vTaskSuspend`只支持挂起当前运行的线程,在使用时`xTaskToSuspend`参数必须为`NULL`。否则会触发断言。
|
|
|
|
|
|
-#### 2.1.2 xQueueSendToFront
|
|
|
+#### 3.1.2 xQueueSendToFront
|
|
|
`xQueueSendToFront`不支持设置超时,使用时`xTicksToWait`参数会被忽略,消息队列没有空间时会立即返回`errQUEUE_FULL`。
|
|
|
|
|
|
-#### 2.1.3 xQueueCreateStatic
|
|
|
+#### 3.1.3 xQueueCreateStatic
|
|
|
静态消息队列需要参考以下的例子创建,确保为消息队列分配的内存足够大:
|
|
|
```c
|
|
|
#define QUEUE_LENGTH 10
|
|
|
@@ -38,11 +189,11 @@ StaticQueue_t xQueueBuffer;
|
|
|
QueueHandle_t xQueue1;
|
|
|
xQueue1 = xQueueCreate( QUEUE_LENGTH, ITEM_SIZE, &( ucQueueStorage[ 0 ] ), &xQueueBuffer );
|
|
|
```
|
|
|
-#### 2.1.4 Mutex和Recursive Mutex
|
|
|
+#### 3.1.4 Mutex和Recursive Mutex
|
|
|
FreeRTOS提供了两种互斥量,Mutex和Recursive Mutex。Recursive Mutex可以由同一个线程重复获取,Mutex不可以。RT-Thread提供的互斥量是可以重复获取的,因此兼容层也不对Mutex和Recursive Mutex做区分。用`xSemaphoreCreateMutex`和`xSemaphoreCreateRecursiveMutex`创建的互斥量都是可以重复获取的。
|
|
|
-### 2.2 定时器
|
|
|
+### 3.2 定时器
|
|
|
和FreeRTOS不同,RT-Thread不使用一个消息队列向定时器线程传递命令。使用兼容层时任何需要设置超时的定时器函数,如`xTimerStart( xTimer, xTicksToWait )`,`xTicksToWait`参数会被忽略,函数会立即完成命令并返回。
|
|
|
-### 2.3 FromISR函数
|
|
|
+### 3.3 FromISR函数
|
|
|
FreeRTOS为一些函数提供了在中断中使用的FromISR版本,如果这些函数唤醒了更高优先级的线程,需要手动调度,如下所示:
|
|
|
```c
|
|
|
BaseType_t xHigherPrioritTaskWoken = pdFALSE;
|
|
|
@@ -53,7 +204,7 @@ if( xHigherPriorityTaskWoken )
|
|
|
}
|
|
|
```
|
|
|
RT-Thread不为函数提供FromISR版本,函数可以在中断调用并在内部完成调度。因此在兼容层中使用FromISR函数后不需要手动调度,`xHigherPriorityTaskWoken`总会被设置成`pdFALSE`。
|
|
|
-### 2.4 内存堆
|
|
|
+### 3.4 内存堆
|
|
|
兼容层保留了FreeRTOS的五种内存分配算法,默认使用`heap_3`,`pvPortMalloc/vPortFree`内部调用`RT_KERNEL_MALLOC/RT_KERNEL_FREE`在RT-Thread内部的内存堆分配。这种情况下内存堆的大小由RT-Thread BSP配置决定,无法在`FreeRTOSConfig.h`中通过`configTOTAL_HEAP_SIZE`设置。
|
|
|
若使用其他算法,需要修改`FreeRTOS/sSConscript`,选择相应的源文件
|
|
|
|
|
|
@@ -63,17 +214,17 @@ src += Glob(os.path.join("portable", "MemMang", "heap_3.c"))
|
|
|
```
|
|
|
在`FreeRTOS/portable/rt-thread/FreeRTOSConfig.h`中通过`configTOTAL_HEAP_SIZE`设置内存堆大小。应用调用`pvPortMalloc/vPortFree`会在一块独立于RT-Thread,大小为`configTOTAL_HEAP_SIZE`的内存堆中分配,RT-Thread内部的内存堆仍然存在,兼容层函数内部分配内存都在RT-Thread的内存堆完成。
|
|
|
|
|
|
-### 2.5 线程优先级
|
|
|
+### 3.5 线程优先级
|
|
|
RT-Threa线程优先级数值越小时优先级越高,而FreeRTOS线程优先级数值越大优先级越高。在使用兼容层的FreeRTOS API,如`xTaskCreate`,使用FreeRTOS的规则为线程指定优先级即可。若在应用中将RT-Thread和FreeRTOS API混合使用,在指定线程优先级时要特别注意。可以使用以下两个宏对RT-Thread和FreeRTOS线程优先级做转换:
|
|
|
```c
|
|
|
#define FREERTOS_PRIORITY_TO_RTTHREAD(priority) ( configMAX_PRIORITIES - 1 - ( priority ) )
|
|
|
#define RTTHREAD_PRIORITY_TO_FREERTOS(priority) ( RT_THREAD_PRIORITY_MAX - 1 - ( priority ) )
|
|
|
```
|
|
|
|
|
|
-### 2.6 线程堆栈大小
|
|
|
+### 3.6 线程堆栈大小
|
|
|
FreeRTOS线程堆栈大小的单位为`sizeof(StackType_t)`,RT-Thread线程堆栈大小为`sizeof(rt_uint8_t)`。使用FreeRTOS API创建线程时一定要遵守FreeRTOS的规则,切勿混淆。
|
|
|
|
|
|
-### 2.7 vTaskStartScheduler
|
|
|
+### 3.7 vTaskStartScheduler
|
|
|
由于RT-Thread和FreeRTOS的内核启动流程不同,使用兼容层时,`main`函数是在一个线程中运行,该线程优先级为`CONFIG_RT_MAIN_THREAD_PRIORITY`。(此选项通过SCons配置,数值越小优先级越高。),此时调度器已经开启。一般的FreeRTOS应用采用以下的方式创建线程:
|
|
|
|
|
|
```c
|
|
|
@@ -85,7 +236,7 @@ vTaskStartScheduler();
|
|
|
|
|
|
使用兼容层时,任何使用`xTaskCreate`创建的线程若优先级比`CONFIG_RT_MAIN_THREAD_PRIORITY`更高,会立即开始执行。`vTaskStartScheduler`只是为了提供对应用的兼容,没有任何实际效果。在使用兼容层时,创建线程要特别注意,确保在调用`xTaskCreate`时,该线程所需的所有资源已经完成初始化,可以正常运行。
|
|
|
|
|
|
-## 3 使用方法
|
|
|
+## 4 使用方法
|
|
|
通过Env工具将兼容层加入到工程中:
|
|
|
```shell
|
|
|
RT-Thread online packages
|
|
|
@@ -169,7 +320,7 @@ Task 1 receive data 9 from queue
|
|
|
Task 1 receive data 10 from queue
|
|
|
```
|
|
|
|
|
|
-## 4 参考资料
|
|
|
+## 5 参考资料
|
|
|
RT-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)
|
|
|
|
|
|
FreeRTOS文档 [https://www.freertos.org/a00106.html](https://www.freertos.org/a00106.html)
|