|
1 | 1 | # RT-Thread操作系统的FreeRTOS兼容层 |
2 | 2 | ## FreeRTOS Application Compatibility Layer (ACL) for RT-Thread |
3 | 3 | ## 让基于FreeRTOS开发的应用层无感地迁移到RT-Thread操作系统 |
| 4 | + |
| 5 | +# 1 概述 |
| 6 | +这是一个针对RT-Thread国产操作系统的FreeRTOS操作系统兼容层,可以让基于FreeRTOS操作系统的项目快速、无感地迁移到RT-Thread操作系统上。项目基于FreeRTOS V10.4.6版本。 |
| 7 | + |
| 8 | +# 2 FreeRTOS功能支持情况 |
| 9 | +兼容层对FreeRTOS的支持情况记录在[issue](https://github.com/RT-Thread-packages/FreeRTOS-Wrapper/issues/22)中记录。一些支持的函数在功能和使用方法上和FreeRTOS略有不同,在迁移过程中需要注意。 |
| 10 | +### vTaskSuspend |
| 11 | +`vTaskSuspend`只支持挂起当前运行的线程,在使用时`xTaskToSuspend`参数必须为`NULL`。 |
| 12 | +### xQueueSendToFront |
| 13 | +`xQueueSendToFront`不支持设置超时,使用时`xTicksToWait`参数会被忽略,消息队列没有空间时会立即返回。 |
| 14 | +### xQueueCreateStatic |
| 15 | +静态消息队列需要参考以下的例子创建,确保为消息队列分配的内存足够大: |
| 16 | +``` |
| 17 | +#define QUEUE_LENGTH 10 |
| 18 | +#define ITEM_SIZE sizeof( uint32_t ) |
| 19 | +
|
| 20 | +/* 以下是在原版FreeRTOS分配内存的方法,由于RT-Thread消息队列内部的实现与FreeRTOS不同,这样分配的内存不够存放ITEM_SIZE个消息 */ |
| 21 | +//uint8_t ucQueueStorage[ QUEUE_LENGTH * ITEM_SIZE ]; |
| 22 | +/* 要使用QUEUE_BUFFER_SIZE宏分配内存 */ |
| 23 | +uint8_t ucQueueStorage[ QUEUE_BUFFER_SIZE(QUEUE_LENGTH, ITEM_SIZE)]; |
| 24 | +StaticQueue_t xQueueBuffer; |
| 25 | +QueueHandle_t xQueue1; |
| 26 | +xQueue1 = xQueueCreate( QUEUE_LENGTH, ITEM_SIZE, &( ucQueueStorage[ 0 ] ), &xQueueBuffer ); |
| 27 | +``` |
| 28 | +### Mutex和Recursive Mutex |
| 29 | +FreeRTOS提供了两种互斥量,Mutex和Recursive Mutex。Recursive Mutex可以由同一个线程重复获取,Mutex不可以。RT-Thread提供的互斥量是可以重复获取的,因此兼容层也不对Mutex和Recursive Mutex做区分。用`xSemaphoreCreateMutex`和`xSemaphoreCreateRecursiveMutex`创建的互斥量都是可以重复获取的。 |
| 30 | +### 定时器 |
| 31 | +和FreeRTOS不同,RT-Thread不使用一个消息队列向定时器线程传递命令。使用兼容层时任何需要设置超时的定时器函数,如`xTimerStart( xTimer, xTicksToWait )`,`xTicksToWait`参数会被忽略,函数会立即完成命令并返回。 |
| 32 | +### FromISR函数 |
| 33 | +FreeRTOS为一些函数提供了在中断中使用的FromISR版本,如果这些函数唤醒了更高优先级的线程,需要手动调度,如下所示: |
| 34 | +``` |
| 35 | +BaseType_t xHigherPrioritTaskWoken = pdFALSE; |
| 36 | +xQueueSendToFrontFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken ); |
| 37 | +if( xHigherPriorityTaskWoken ) |
| 38 | +{ |
| 39 | + taskYIELD (); |
| 40 | +} |
| 41 | +``` |
| 42 | +RT-Thread不为函数提供FromISR版本,函数可以在中断调用并在内部完成调度。因此在兼容层中使用FromISR函数后不需要手动调度,`xHigherPriorityTaskWoken`总会被设置成`pdFALSE`。 |
| 43 | +### 内存堆 |
| 44 | +兼容层保留了FreeRTOS的五种内存分配算法,默认使用`heap_3`,`pvPortMalloc/vPortFree`内部调用`RT_KERNEL_MALLOC/RT_KERNEL_FREE`在RT-Thread内部的内存堆分配。若使用其他算法,需要在`FreeRTOSConfig.h`中通过`configTOTAL_HEAP_SIZE`设置内存堆大小。应用调用`pvPortMalloc/vPortFree`会在一块独立于RT-Thread,大小为`configTOTAL_HEAP_SIZE`的内存堆中分配,RT-Thread内部的内存堆仍然存在,兼容层函数内部分配内存都在RT-Thread的内存堆完成。 |
| 45 | + |
| 46 | +# 3 使用方法 |
| 47 | +通过Env工具将兼容层加入到工程中: |
| 48 | +``` |
| 49 | +RT-Thread online packages |
| 50 | + system packages ---> |
| 51 | + [*] FreeRTOS Wrapper ---> |
| 52 | + Version (latest) |
| 53 | +``` |
| 54 | +使用`scons --menuconfig`配置RT-Thread内核,以下选项会影响到FreeRTOS兼容层: |
| 55 | +``` |
| 56 | +RT_USING_TIMER_SOFT /* 使用FreeRTOS定时器时必须开启*/ |
| 57 | +RT_TIMER_THREAD_PRIO /* 定时器线程优先级。与FreeRTOS相反,该选项数值越小优先级越高 */ |
| 58 | +RT_TIMER_THREAD_STACK_SIZE /* 定时器线程栈大小 */ |
| 59 | +RT_USING_MUTEX /* 使用FreeRTOS互斥量时必须开启*/ |
| 60 | +RT_USING_SEMAPHORE /* 使用FreeRTOS信号量时必须开启*/ |
| 61 | +RT_USING_HEAP /* 使用FreeRTOS动态内存分配时必须开启*/ |
| 62 | +RT_TICK_PER_SECOND /* 相当于FreeRTOS configTICK_RATE_HZ */ |
| 63 | +RT_THREAD_PRIORITY_MAX /* 相当于FreeRTOS configMAX_PRIORITIES */ |
| 64 | +RT_NAME_MAX /* 相当于FreeRTOS configMAX_TASK_NAME_LEN */ |
| 65 | +``` |
| 66 | +在`FreeRTOS/include`提供了`FreeRTOSConfig.h`模版。大部分内容不可以修改或依赖`rtconfig.h`的配置,可以手动修改的内容如下: |
| 67 | +``` |
| 68 | +/* 可以选择不使用recursive mutex */ |
| 69 | +#ifdef RT_USING_MUTEX |
| 70 | + #define configUSE_RECURSIVE_MUTEXES 1 |
| 71 | + #define configUSE_MUTEXES 1 |
| 72 | +#endif |
| 73 | +
|
| 74 | +/* 可以选择不使用counting semaphore */ |
| 75 | +#ifdef RT_USING_SEMAPHORE |
| 76 | + #define configUSE_COUNTING_SEMAPHORES 1 |
| 77 | +#endif |
| 78 | +
|
| 79 | +/* 若不使用heap_3,可以通过configTOTAL_HEAP_SIZE配置内存堆大小 */ |
| 80 | +#define configSUPPORT_STATIC_ALLOCATION 1 |
| 81 | +#ifdef RT_USING_HEAP |
| 82 | + #define configSUPPORT_DYNAMIC_ALLOCATION 1 |
| 83 | + #define configTOTAL_HEAP_SIZE 10240 |
| 84 | + #define configAPPLICATION_ALLOCATED_HEAP 0 |
| 85 | +#endif |
| 86 | +
|
| 87 | +#define configMINIMAL_STACK_SIZE 128 |
| 88 | +
|
| 89 | +/* 可以选择的函数和功能 */ |
| 90 | +#define INCLUDE_vTaskPrioritySet 1 |
| 91 | +#define INCLUDE_uxTaskPriorityGet 1 |
| 92 | +#define INCLUDE_vTaskDelete 1 |
| 93 | +#define INCLUDE_vTaskSuspend 1 |
| 94 | +#define INCLUDE_xTaskDelayUntil 1 |
| 95 | +#define INCLUDE_vTaskDelay 1 |
| 96 | +#define INCLUDE_xTaskGetIdleTaskHandle 1 |
| 97 | +#define INCLUDE_xTaskAbortDelay 1 |
| 98 | +#define INCLUDE_xSemaphoreGetMutexHolder 1 |
| 99 | +#define INCLUDE_xTaskGetHandle 1 |
| 100 | +#define INCLUDE_uxTaskGetStackHighWaterMark 1 |
| 101 | +#define INCLUDE_uxTaskGetStackHighWaterMark2 1 |
| 102 | +#define INCLUDE_eTaskGetState 1 |
| 103 | +#define INCLUDE_xTaskResumeFromISR 1 |
| 104 | +#define INCLUDE_xTaskGetSchedulerState 1 |
| 105 | +#define INCLUDE_xTaskGetCurrentTaskHandle 1 |
| 106 | +#define configUSE_APPLICATION_TASK_TAG 1 |
| 107 | +#define configUSE_TASK_NOTIFICATIONS 1 |
| 108 | +#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3 |
| 109 | +``` |
0 commit comments