一.CMISIS-RTOS中thread相关API概览
模块 | 宏定义 | 描述 |
线程定义 | osThreadDef | 定义一个线程 |
osThread | 访问一个线程定义 |
模块 | API | 描述 |
线程管理: 定义,创建和控制线程 | osThreadCreate | 开始执行一个线程 |
osThreadTerminate | 停止线程的运行 | |
osThreadYield | 运行处于就绪态的线程 | |
osThreadGetId | 获取线程的ID | |
osThreadSetPriority | 改变线程运行的优先级 | |
osThreadGetPriority | 获取线程的优先级 |
优先级(priority)相关枚举:
typedef enum {
osPriorityIdle = -3, ///< priority: idle (lowest)
osPriorityLow = -2, ///< priority: low
osPriorityBelowNormal = -1, ///< priority: below normal
osPriorityNormal = 0, ///< priority: normal (default)
osPriorityAboveNormal = +1, ///< priority: above normal
osPriorityHigh = +2, ///< priority: high
osPriorityRealtime = +3, ///< priority: realtime (highest)
osPriorityError = 0x84 ///< system cannot determine priority or thread has illegal priority
} osPriority;
二.线程创建并执行
在STM32CubMx下,已经自动生成了一个线程,可以看看其定义
MX_FREERTOS_Init()在freertos.c的文件中,
osThreadDef参数含义:
//#define osThreadDef (名字(该线程的名字),线程函数名,优先级,该线程可以实例化的个数,线程的栈大小 )
void MX_FREERTOS_Init(void) {
/* Create the thread(s) */
/* definition and creation of deviceTask*/
osThreadDef(deviceTask, StartDefaultTask, osPriorityNormal, 0, STACKSIZE);
defaultTaskHandle = osThreadCreate(osThread(deviceTask), NULL);
}
//线程函数
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN StartDeviceTask */
/* Infinite loop */
for(;;)
{
}
/* USER CODE END StartDeviceTask */
}
现在就可以在无限循环中,添加自己的应用了
三.LED闪烁
创建一个LED线程(修改默认的线程)
osThreadDef(ledThreadHandler, ledThread, osPriorityNormal, 0, STACKSIZE);
this->led_thread = osThreadCreate(osThread(ledThreadHandler), NULL);
在LED线程中
void startLedTask(void const * argument)
{
led_t led0 , led1;
led0 = led_get_instance(LED_ID0);
led1 = led_get_instance(LED_ID1);
for(;;)
{
led_on(led0);
led_off(led1);
osDelay(1000);
led_off(led0);
led_on(led1);
osDelay(1000);
}
}
这样LED闪烁应用就完成了(代码已上传)
ref:
file:///D:/Keil_v5/ARM/PACK/ARM/CMSIS/4.2.0/CMSIS/Documentation/RTOS/html/group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849a