一、安装和配置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){
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
三、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);
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
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));
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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);
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
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));
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
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);
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40