CMSIS-RTOS在stm32使用

目录:

一、安装和配置CMSIS_RTOS.

1.打开KEIL工程,点击MANAGE RUN-TIME Environment图标。

2.勾选CMSIS CORE和RTX.

3.配置RTOS 时钟频率、任务栈大小和数量, 软件定时器.

二、CMSIS_RTOS内核启动和创建线程。

1.包含头文件。

复制代码
#include "cmsis_os.h"               // CMSIS RTOS header file

2.内核初始化和启动。

复制代码
int main(){
osKernelInitialize ();                    // initialize CMSIS-RTOS
..do something..
osKernelStart (); 
	while(1){
	}
}

3.创建线程。

复制代码
osThreadId main_ID,led_ID1,led_ID2;
osThreadDef(led_thread2, osPriorityAboveNormal, 1, 0);
osThreadDef(led_thread1, osPriorityNormal, 1, 0);

void led_thread1 (void const *argument) {}
void led_thread2 (void const *argument) {}

int main(){
	osKernelInitialize ();                    // initialize CMSIS-RTOS
	
	  // create 'thread' functions that start executing,
  	led_ID1 = osThreadCreate(osThread(led_thread1), 0);
  	led_ID2 = osThreadCreate(osThread(led_thread2), 0);
  	
  	osKernelStart (); 
  		while(1){
	}
  }

三、Signals、Semaphores信号量、互斥锁、消息队列、Memory pool、消息邮箱。

1.Signals。

复制代码
每个线程有16个flag,任何线程也可以清除其它线程的信号.
int32_t osSignalSet ( osThreadId thread_id, int32_t signals);
int32_t osSignalClear ( osThreadId thread_id, int32_t signals);
复制代码
eg:
1.设置信号
osSignalSet (led_ID2,0x01);
2.等待信号触发:
osSignalWait (0x01,osWaitForever);

2.Semaphores信号量。

复制代码
	// 定义变量
	osSemaphoreId sem1;
	osSemaphoreDef(sem1);
	.....
	// 任务1
	void led_thread1 (void const *argument) {
		while(1){
			osSemaphoreRelease(sem1);
			...
		}
	}
	// 任务2
	void led_thread2 (void const *argument) {
		while(1){
			osSemaphoreWait(sem1, osWaitForever);
			....
		}
	}
	// 初始化
	int main(){
		...
		sem1 = osSemaphoreCreate(osSemaphore(sem1), 0);
		...
	}

3.互斥锁

复制代码
	// 定义变量
	osMutexId uart_mutex;
	osMutexDef (uart_mutex);
	.....
	// 任务1
	void led_thread1 (void const *argument) {
		while(1){
			osMutexWait(uart_mutex, osWaitForever);
			...do something...
			osMutexRelease(uart_mutex); 
		}
	}

	// 初始化
	int main(){
		...
		uart_mutex = osMutexCreate(osMutex(uart_mutex));
		...
	}

4.消息队列

复制代码
	// 定义变量
	osMessageQId Q_LED;
	osMessageQDef (Q_LED,16_Message_Slots,unsigned int);
	osEvent result;
	.....
	// 任务1
	void led_thread1 (void const *argument) {
		while(1){
			osMessagePut(Q_LED,0x0,osWaitForever);
			...
		}
	}
	// 任务2
	void led_thread2 (void const *argument) {
		while(1){
			result = osMessageGet(Q_LED,osWaitForever);
			LED_data = result.value.v;
			....
		}
	}
	// 初始化
	int main(){
		...
		Q_LED = osMessageCreate(osMessageQ(Q_LED),NULL);
		...
	}

5.Memory pool

复制代码
	// 定义变量
	typedef struct {
		uint8_t LED0;
		uint8_t LED1;
		uint8_t LED2;
		uint8_t  LED3;
	} memory_block_t;
	
	osPoolDef(led_pool,ten_blocks,memory_block_t);
	osPoolId( led_pool);
	
	// 任务1
	void led_thread1 (void const *argument) {
		while(1){
			*led_data = (memory_block_t *) osPoolAlloc(led_pool);
			led_data->LED0 = 0;
			led_data->LED1 = 1;
			led_data->LED2 = 2;
			led_data->LED3 = 3;
			osMessagePut(Q_LED,(uint32_t)led_data,osWaitForever);
			...
		}
	}
	// 任务2
	void led_thread2 (void const *argument) {
		osEvent event; memory_block_t * received;
		while(1){		
			event = osMessageGet(Q_LED,osWatiForever);
			*received = (memory_block *)event.value.p;
			led_on(received->LED0);
			....
		}
	}
	// 初始化
	int main(){
		...
		led_pool = osPoolCreate(osPool(led_pool));
		...
	}

6.消息邮箱

复制代码
	typedef struct {
		uint8_t LED0;
		uint8_t LED1;
		uint8_t LED2;
		uint8_t LED3;
	} mail_format;
	
	osMailQDef(mail_box, sixteen_mail_slots, mail_format);
	osMailQId mail_box;
	
	// 任务1
	void led_thread1 (void const *argument) {
		while(1){
		LEDtx = (mail_format*)osMailAlloc(mail_box, osWaitForever);
		LEDtx->LED0 = led0[index];
		LEDtx->LED1 = led1[index];
		LEDtx->LED2 = led2[index];
		LEDtx->LED3 = led3[index];
		osMailPut(mail_box, LEDtx);
			...
		}
	}
	// 任务2
	void led_thread2 (void const *argument) {

		while(1){		
			evt = osMailGet(mail_box, osWaitForever); 
			if(evt.status == osEventMail){
			LEDrx = (mail_format*)evt.value.p;
			LED_Out((LEDrx->LED0|LEDrx->LED1|LEDrx->LED2|LEDrx->LED3)<<8);
			osMailFree(mail_box, LEDrx);
			....
		}
	}
	// 初始化
	int main(){
		...
		mail_box = osMailCreate(osMailQ(mail_box), NULL);
		...
	}
相关推荐
jasonslaex2 小时前
stm32传感器通用驱动代码
驱动开发·stm32·嵌入式硬件
嵌入式仿真实验教学平台7 小时前
深入探索IIC-OLED显示技术:嵌入式仿真平台如何重塑高校教学范式——深圳航天科技创新研究院技术赋能新一代工程教育
科技·单片机·gd32·嵌入式仿真·iic-oled显示技术
十碗阳春面8 小时前
TI 毫米波雷达走读系列—— 3DFFT及测角
单片机·嵌入式硬件·毫米波雷达·mmwave radar·awr/iwr系列
CPETW8 小时前
同旺科技 USB TO SPI / I2C适配器(专业版)--EEPROM读写——C
c语言·开发语言·科技·stm32·单片机·嵌入式硬件·电子
keke109 小时前
PLC入门【6】计时器、计数器、存储器
嵌入式硬件
小智学长 | 嵌入式12 小时前
Arduino入门教程:4-1、代码基础-进阶
嵌入式硬件·物联网·arduino
国科安芯13 小时前
【AS32系列MCU调试教程】调试工具:Eclipse调试工具栏与窗口的深入分析
单片机·嵌入式硬件·eclipse
nuannuan2311a13 小时前
9N65-ASEMI照明系统应用专用9N65
单片机·嵌入式硬件
woshihonghonga13 小时前
高级定时器TIM1、TIM8
stm32·单片机·嵌入式硬件
腾飞的信仰13 小时前
举例说明单片机,主循环和中断资源访问冲突的案例
单片机·嵌入式硬件·mongodb