![](/media/cache/profiles/af7fc8485f40dc495bc626d6dbe7faf1.50x50_q85.png)
Basic example showing the CMSIS-RTOS queue and memory pool API
mbed 2 and mbed OS 5
This is an mbed 2 example. mbed OS 5 has integrated the mbed library with mbed-rtos. For an mbed-os example, please see:
Import programrtos_queue
queue example
main.cpp
- Committer:
- emilmont
- Date:
- 2012-11-23
- Revision:
- 2:54e171ca91a6
- Parent:
- 1:85e3058aad0f
File content as of revision 2:54e171ca91a6:
#include "mbed.h" #include "cmsis_os.h" typedef struct { float voltage; /* AD result of measured voltage */ float current; /* AD result of measured current */ uint32_t counter; /* A counter value */ } message_t; osPoolDef(mpool, 16, message_t); osPoolId mpool; osMessageQDef(queue, 16, message_t); osMessageQId queue; void send_thread (void const *args) { uint32_t i = 0; while (true) { i++; // fake data update message_t *message = (message_t*)osPoolAlloc(mpool); message->voltage = (i * 0.1) * 33; message->current = (i * 0.1) * 11; message->counter = i; osMessagePut(queue, (uint32_t)message, osWaitForever); osDelay(1000); } } osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE); int main (void) { mpool = osPoolCreate(osPool(mpool)); queue = osMessageCreate(osMessageQ(queue), NULL); osThreadCreate(osThread(send_thread), NULL); while (true) { osEvent evt = osMessageGet(queue, osWaitForever); if (evt.status == osEventMessage) { message_t *message = (message_t*)evt.value.p; printf("\nVoltage: %.2f V\n\r" , message->voltage); printf("Current: %.2f A\n\r" , message->current); printf("Number of cycles: %u\n\r", message->counter); osPoolFree(mpool, message); } } }