赞
踩
#include "cmsis_os.h" // CMSIS RTOS header file
int main(){
osKernelInitialize (); // initialize CMSIS-RTOS
..do something..
osKernelStart ();
while(1){
}
}
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){ } }
每个线程有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);
// 定义变量 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); ... }
// 定义变量 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)); ... }
// 定义变量 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); ... }
// 定义变量 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)); ... }
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); ... }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。