#include "cmsis_os2.h" // CMSIS RTOS header file
/*----------------------------------------------------------------------------
* Message Queue creation & usage
*---------------------------------------------------------------------------*/
void Thread_MsgQueue1 (void *argument); // thread function 1
void Thread_MsgQueue2 (void *argument); // thread function 2
osThreadId_t tid_Thread_MsgQueue1; // thread id 1
osThreadId_t tid_Thread_MsgQueue2; // thread id 2
osEventFlagsId_t evt_id; // event flags id
#define FLAGS_MSK 0x00000001ul
#define MSGQUEUE_OBJECTS 16 // number of Message Queue Objects
typedef struct { // object data type
uint8_t Buf[32];
uint8_t Idx;
} MSGQUEUE_OBJ_t;
osMessageQueueId_t mid_MsgQueue; // message queue id
int Init_MsgQueue (void) {
//osThreadAttr_t thread_attr;
//memset(&thread_attr, 0, sizeof(thread_attr));
//thread_attr.priority = osPriorityHigh; // set initial thread priority to high
mid_MsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, sizeof(MSGQUEUE_OBJ_t), NULL);
if (!mid_MsgQueue) {
; // Message Queue object not created, handle failure
}
tid_Thread_MsgQueue1 = osThreadNew (Thread_MsgQueue1, NULL, NULL);
if (!tid_Thread_MsgQueue1) return(-1);
tid_Thread_MsgQueue2 = osThreadNew (Thread_MsgQueue2, NULL, NULL);
if (!tid_Thread_MsgQueue2) return(-1);
return(0);
}
void Thread_MsgQueue1 (void *argument) {
MSGQUEUE_OBJ_t msg;
evt_id = osEventFlagsNew(NULL);
while (1) {
; // Insert thread code here...
msg.Buf[0] = 0x55; // do some work...
msg.Idx = 0;
osMessageQueuePut (mid_MsgQueue, &msg, 0, NULL); // returns instantly
// wait for an infinite time until the message is delivered
//osMessageQueuePut (mid_MsgQueue, &msg, 0, osWaitForever);
osEventFlagsSet(evt_id, FLAGS_MSK);
//osThreadFlagsSet(tid_Thread_MsgQueue2, FLAGS_MSK);
osThreadYield (); // suspend thread
}
}
void Thread_MsgQueue2 (void *argument) {
MSGQUEUE_OBJ_t msg;
osStatus_t status;
uint32_t flags;
while (1) {
; // Insert thread code here...
flags = osEventFlagsWait (evt_id, FLAGS_MSK, osFlagsWaitAny, osWaitForever);
//flags = osThreadFlagsWait (FLAGS_MSK, osFlagsWaitAny, osWaitForever);
//handle event
status = osMessageQueueGet (mid_MsgQueue, &msg, NULL, NULL); // returns instantly
// wait for an infinite time until the message is retrieved
//status = osMessageQueueGet (mid_MsgQueue, &msg, NULL, osWaitForever);
if (status == osOK) {
; // process data
}
}
}
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
void app_main (void *argument) {
// ...
// Infinite loop
for (;;) {
osDelay(1);
}
}
// Application entry point
int main(void) {
// System Initialization
// Update SystemCoreClock variable
SystemCoreClockUpdate();
// ...
//GPIO_Init();
osKernelInitialize(); // Initialize CMSIS-RTOS for creating objects
// initialize peripherals here
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart(); // Start thread execution
for (;;) {}
}