定义:
RTOS实时操作系统, (Real Time Operating System), 指的是当外界事件发生时, 能够有够快的响应速度,调度一切可利用的资源, 控制实时任务协调一致的运行。
特点:
支持多任务管理, 处理多个事件, 实现更复杂的逻辑。
与计算机操作系统的区别:
RTOS专注于 轻量级, 实时性, 稳定性, 相对于计算机主流系统, RTOS有严格的时间控制和响应速度, 成本低, 资源开销小, 所以可以用于嵌入式领域。
配置:
core设置非安全模式下的内核支持
heap:设置第四种堆区设置方式
Core: 选择 不适用安全模式
Heap: 在 FreeRTOS内, 支持五种堆空间开辟的方法:
使用过程中的状态转换关系图
任务调度的核心:
抢占式调度,时间片轮询
任务的创建:
通过cubeMX进行任务的添加和设置
相关的MX_FREERTOS_Init函数
/* 任务属性结构体:*/
typedef struct {
const char *name; ///< 任务的名字
uint32_t attr_bits; ///< 操作的标志
void *cb_mem; ///< 任务的内存地址
uint32_t cb_size; ///< 当前任务的内存大小
void *stack_mem; ///< 当前任务的栈内存地址
uint32_t stack_size; ///< 当前栈内存大小
osPriority_t priority; ///< 当前任务的优先级
TZ_ModuleId_t tz_module; ///< TrustZone module identifier
uint32_t reserved; ///< reserved (must be 0)
} osThreadAttr_t;
2.任务的优先级
typedef enum {
osPriorityNone = 0, ///< No priority (not initialized).
osPriorityIdle = 1, ///< Reserved for Idle thread.
osPriorityLow = 8, ///< Priority: low
osPriorityLow1 = 8+1, ///< Priority: low + 1
osPriorityLow2 = 8+2, ///< Priority: low + 2
osPriorityLow3 = 8+3, ///< Priority: low + 3
osPriorityLow4 = 8+4, ///< Priority: low + 4
osPriorityLow5 = 8+5, ///< Priority: low + 5
osPriorityLow6 = 8+6, ///< Priority: low + 6
osPriorityLow7 = 8+7, ///< Priority: low + 7
osPriorityBelowNormal = 16, ///< Priority: below normal
osPriorityBelowNormal1 = 16+1, ///< Priority: below normal + 1
osPriorityBelowNormal2 = 16+2, ///< Priority: below normal + 2
osPriorityBelowNormal3 = 16+3, ///< Priority: below normal + 3
osPriorityBelowNormal4 = 16+4, ///< Priority: below normal + 4
osPriorityBelowNormal5 = 16+5, ///< Priority: below normal + 5
osPriorityBelowNormal6 = 16+6, ///< Priority: below normal + 6
osPriorityBelowNormal7 = 16+7, ///< Priority: below normal + 7
osPriorityNormal = 24, ///< Priority: normal
osPriorityNormal1 = 24+1, ///< Priority: normal + 1
osPriorityNormal2 = 24+2, ///< Priority: normal + 2
osPriorityNormal3 = 24+3, ///< Priority: normal + 3
osPriorityNormal4 = 24+4, ///< Priority: normal + 4
osPriorityNormal5 = 24+5, ///< Priority: normal + 5
osPriorityNormal6 = 24+6, ///< Priority: normal + 6
osPriorityNormal7 = 24+7, ///< Priority: normal + 7
osPriorityAboveNormal = 32, ///< Priority: above normal
osPriorityAboveNormal1 = 32+1, ///< Priority: above normal + 1
osPriorityAboveNormal2 = 32+2, ///< Priority: above normal + 2
osPriorityAboveNormal3 = 32+3, ///< Priority: above normal + 3
osPriorityAboveNormal4 = 32+4, ///< Priority: above normal + 4
osPriorityAboveNormal5 = 32+5, ///< Priority: above normal + 5
osPriorityAboveNormal6 = 32+6, ///< Priority: above normal + 6
osPriorityAboveNormal7 = 32+7, ///< Priority: above normal + 7
osPriorityHigh = 40, ///< Priority: high
osPriorityHigh1 = 40+1, ///< Priority: high + 1
osPriorityHigh2 = 40+2, ///< Priority: high + 2
osPriorityHigh3 = 40+3, ///< Priority: high + 3
osPriorityHigh4 = 40+4, ///< Priority: high + 4
osPriorityHigh5 = 40+5, ///< Priority: high + 5
osPriorityHigh6 = 40+6, ///< Priority: high + 6
osPriorityHigh7 = 40+7, ///< Priority: high + 7
osPriorityRealtime = 48, ///< Priority: realtime
osPriorityRealtime1 = 48+1, ///< Priority: realtime + 1
osPriorityRealtime2 = 48+2, ///< Priority: realtime + 2
osPriorityRealtime3 = 48+3, ///< Priority: realtime + 3
osPriorityRealtime4 = 48+4, ///< Priority: realtime + 4
osPriorityRealtime5 = 48+5, ///< Priority: realtime + 5
osPriorityRealtime6 = 48+6, ///< Priority: realtime + 6
osPriorityRealtime7 = 48+7, ///< Priority: realtime + 7
osPriorityISR = 56, ///< Reserved for ISR deferred thread.
osPriorityError = -1, ///< System cannot determine priority or illegal priority.
osPriorityReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization.
} osPriority_t;
osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
void osThreadExit (void)
osStatus_t osDelay (uint32_t ticks)
osStatus_t osThreadDetach (osThreadId_t thread_id);
osStatus_t osThreadJoin (osThreadId_t thread_id)
信号量
信号量实际上就是一个值,这个值被用来解决临界区问题以及实现进程在多处理器环境下的进程同步。主要分为二值信号量和计数信号量,前者主要用于互斥访问和同步,类似于互斥信号量,不同点是二值信号量不具有优先级继承机制,这也使得其适于同步任务。而后者又称为数值信号量,数值大于1时使用的重点不在其中存储了什么数据而是通过数值去事件计数和资源管理(生产者消费者模型)
对于二值信号量的具体使用:
创建、申请or释放信号(p,v操作)
相关函数:
SemaphoreHandle_t xSemaphoreCreateBinary(void)
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore,TickType_t xBlockTime)
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
BaseType_t xSemaphoreGive(xSemaphore)
BaseType_t xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
对于计数信号量的使用:
事件计数
事件发生释放信号量数值+1,其它事件获取后数值-1,初始值为0
资源管理
信号量的数值代表着可用的资源数量,使用资源先获取,数量-1,用完之后再释放,数量+1, 初值根据资源的数量去决定。
相关函数
SemaphoreHandle_t xSemaphoreCreateCounting(UBaseType_t uxMaxCount, UBaseType_t uxInitialCount)
信号量的释放与获取与二值信号量相同:
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore,TickType_t xBlockTime)
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
BaseType_t xSemaphoreGive(xSemaphore)
BaseType_t xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
uxSemaphoreGetCount(信号量句柄 )
对于对互斥型信号量的使用:
其是一种特殊的二值信号量,特点是优先级继承机制,作用是保护临界资源(类似于互斥锁)
相关函数
SemaphoreHandle_t xSemaphoreCreateMutex(void)
信号量的释放与获取与二值信号量相同:
BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore,TickType_t xBlockTime)
BaseType_t xSemaphoreTakeFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
BaseType_t xSemaphoreGive(xSemaphore)
BaseType_t xSemaphoreGiveFromISR(SemaphoreHandle_t xSemaphore, BaseType_t* pxHigherPriorityTaskWoken)
事件标志组
为了实现多个任务或事件进行同步。
相关函数:
osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr);
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags);
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags,\ uint32_t options, uint32_t timeout);
队列:
用于任务到任务或者任务到中断再到任务的通信数据结构
相关函数:
typedef struct {
const char *name; ///< 消息队列的名称
uint32_t attr_bits; ///< 属性位
void *cb_mem; ///< 控制块(Control Block)的内存指针
uint32_t cb_size; ///< 控制块的大小
void *mq_mem; ///< 数据存储的内存指针
uint32_t mq_size; ///< 数据存储的大小
} osMessageQueueAttr_t;
osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size,\ const osMessageQueueAttr_t *attr);
osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr,\ uint8_t msg_prio, uint32_t timeout);
osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr,\ uint8_t *msg_prio, uint32_t timeout);
FREERTOS软件定时器:
可以分为一次性的和周期的即某时间点进行函数功能调用和周期执行某个函数的功能
相关函数:
osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
osStatus_t osTimerStop (osTimerId_t timer_id)
osTimerDelete (osTimerId_t timer_id)
注:修改定时器任务的优先级要尽量高一点
修改任务的优先级要尽量的低一点