The mbed RTOS API is a simple C++ encapsulation of the CMSIS RTOS C API.
mbed RTOS library
The Thread class allows defining, creating, and controlling thread functions in the system. The function main is a special thread function that is started at system initialization and has the initial priority osPriorityNormal.
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 DigitalOut led1(LED1); 00005 DigitalOut led2(LED2); 00006 00007 void led2_thread(void const *argument) { 00008 while (true) { 00009 led2 = !led2; 00010 Thread::wait(1000); 00011 } 00012 } 00013 00014 int main() { 00015 Thread thread(led2_thread); 00016 00017 while (true) { 00018 led1 = !led1; 00019 Thread::wait(500); 00020 } 00021 }
The main function is already the first thread scheduled by the rtos.
Public Member Functions |
|
| Thread (void(*task)(void const *argument), void *argument=NULL, osPriority priority=osPriorityNormal, uint32_t stacksize=DEFAULT_STACK_SIZE) | |
| osStatus | terminate () |
| osStatus | set_priority (osPriority priority) |
| osPriority | get_priority () |
| int32_t | signal_set (int32_t signals) |
Static Public Member Functions |
|
| static osEvent | signal_wait (int32_t signals, uint32_t millisec=osWaitForever) |
| static osStatus | wait (uint32_t millisec) |
| static osStatus | yield () |
| static osThreadId | gettid () |
A Mutex is used to synchronize the execution of threads: for example to protect the access to a shared resource.
The Mutex methods cannot be called from interrupt service routines (ISR).
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 Mutex stdio_mutex; 00005 00006 void notify(const char* name, int state) { 00007 stdio_mutex.lock(); 00008 printf("%s: %d\n\r", name, state); 00009 stdio_mutex.unlock(); 00010 } 00011 00012 void test_thread(void const *args) { 00013 while (true) { 00014 notify((const char*)args, 0); Thread::wait(1000); 00015 notify((const char*)args, 1); Thread::wait(1000); 00016 } 00017 } 00018 00019 int main() { 00020 Thread t2(test_thread, (void *)"Th 2"); 00021 Thread t3(test_thread, (void *)"Th 3"); 00022 00023 test_thread((void *)"Th 1"); 00024 }
The ARM C standard library has already mutexes in place to protect the access to stdio, therefore on the M3 mbed the above example is not necessary. On the contrary, ARM microlib (used on the M0 mbed) does not provide default stdio mutexes making the above example a necessity.
Because of the mutexes in the ARM C standard library you can not use printf, malloc and new in ISR!
A Semaphore is particularly useful to manage thread access to a pool of shared resources of a certain type.

00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 Semaphore two_slots(2); 00005 00006 void test_thread(void const *name) { 00007 while (true) { 00008 two_slots.wait(); 00009 printf("%s\n\r", (const char*)name); 00010 Thread::wait(1000); 00011 two_slots.release(); 00012 } 00013 } 00014 00015 int main (void) { 00016 Thread t2(test_thread, (void *)"Th 2"); 00017 Thread t3(test_thread, (void *)"Th 3"); 00018 00019 test_thread((void *)"Th 1"); 00020 }
Each Thread can be notified and wait for signals:
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 DigitalOut led(LED1); 00005 00006 void led_thread(void const *argument) { 00007 while (true) { 00008 // Signal flags that are reported as event are automatically cleared. 00009 Thread::signal_wait(0x1); 00010 led = !led; 00011 } 00012 } 00013 00014 int main (void) { 00015 Thread thread(led_thread); 00016 00017 while (true) { 00018 Thread::wait(1000); 00019 thread.signal_set(0x1); 00020 } 00021 }
A Queue allows you to queue pointers to data from producers threads to consumers threads:
Queue<message_t, 16> queue;
message_t *message;
queue.put(message);
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
message_t *message = (message_t*)evt.value.p;
The MemoryPool class is used to define and manage fixed-size memory pools:
MemoryPool<message_t, 16> mpool; message_t *message = mpool.alloc(); mpool.free(message);
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 typedef struct { 00005 float voltage; /* AD result of measured voltage */ 00006 float current; /* AD result of measured current */ 00007 uint32_t counter; /* A counter value */ 00008 } message_t; 00009 00010 MemoryPool<message_t, 16> mpool; 00011 Queue<message_t, 16> queue; 00012 00013 /* Send Thread */ 00014 void send_thread (void const *argument) { 00015 uint32_t i = 0; 00016 while (true) { 00017 i++; // fake data update 00018 message_t *message = mpool.alloc(); 00019 message->voltage = (i * 0.1) * 33; 00020 message->current = (i * 0.1) * 11; 00021 message->counter = i; 00022 queue.put(message); 00023 Thread::wait(1000); 00024 } 00025 } 00026 00027 int main (void) { 00028 Thread thread(send_thread); 00029 00030 while (true) { 00031 osEvent evt = queue.get(); 00032 if (evt.status == osEventMessage) { 00033 message_t *message = (message_t*)evt.value.p; 00034 printf("\nVoltage: %.2f V\n\r" , message->voltage); 00035 printf("Current: %.2f A\n\r" , message->current); 00036 printf("Number of cycles: %u\n\r", message->counter); 00037 00038 mpool.free(message); 00039 } 00040 } 00041 }
Public Member Functions |
|
| MemoryPool () | |
| T * | alloc (void) |
| T * | calloc (void) |
| osStatus | free (T *block) |
A Mail works like a queue with the added benefit of providing a memory pool for allocating messages (not only pointers).
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 /* Mail */ 00005 typedef struct { 00006 float voltage; /* AD result of measured voltage */ 00007 float current; /* AD result of measured current */ 00008 uint32_t counter; /* A counter value */ 00009 } mail_t; 00010 00011 Mail<mail_t, 16> mail_box; 00012 00013 void send_thread (void const *argument) { 00014 uint32_t i = 0; 00015 while (true) { 00016 i++; // fake data update 00017 mail_t *mail = mail_box.alloc(); 00018 mail->voltage = (i * 0.1) * 33; 00019 mail->current = (i * 0.1) * 11; 00020 mail->counter = i; 00021 mail_box.put(mail); 00022 Thread::wait(1000); 00023 } 00024 } 00025 00026 int main (void) { 00027 Thread thread(send_thread); 00028 00029 while (true) { 00030 osEvent evt = mail_box.get(); 00031 if (evt.status == osEventMail) { 00032 mail_t *mail = (mail_t*)evt.value.p; 00033 printf("\nVoltage: %.2f V\n\r" , mail->voltage); 00034 printf("Current: %.2f A\n\r" , mail->current); 00035 printf("Number of cycles: %u\n\r", mail->counter); 00036 00037 mail_box.free(mail); 00038 } 00039 } 00040 }
The RtosTimer class allows creating and and controlling of timer functions in the system. A timer function is called when a time period expires whereby both on-shot and periodic timers are possible. A timer can be started, restarted, or stopped. Timers are handled in the thread osTimerThread. Callback functions run under control of this thread and may use CMSIS-RTOS API calls.
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 DigitalOut LEDs[4] = { 00005 DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) 00006 }; 00007 00008 void blink(void const *n) { 00009 LEDs[(int)n] = !LEDs[(int)n]; 00010 } 00011 00012 int main(void) { 00013 RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0); 00014 RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1); 00015 RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2); 00016 RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3); 00017 00018 led_1_timer.start(2000); 00019 led_2_timer.start(1000); 00020 led_3_timer.start(500); 00021 led_4_timer.start(250); 00022 00023 Thread::wait(osWaitForever); 00024 }
The same RTOS API can be used in ISR. The only two warnings are:
Mutex can not be used.
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 Queue<uint32_t, 5> queue; 00005 00006 DigitalOut myled(LED1); 00007 00008 void queue_isr() { 00009 queue.put((uint32_t*)2); 00010 myled = !myled; 00011 } 00012 00013 void queue_thread(void const *argument) { 00014 while (true) { 00015 queue.put((uint32_t*)1); 00016 Thread::wait(1000); 00017 } 00018 } 00019 00020 int main (void) { 00021 Thread thread(queue_thread); 00022 00023 Ticker ticker; 00024 ticker.attach(queue_isr, 1.0); 00025 00026 while (true) { 00027 osEvent evt = queue.get(); 00028 if (evt.status != osEventMessage) { 00029 printf("queue->get() returned %02x status\n\r", evt.status); 00030 } else { 00031 printf("queue->get() returned %d\n\r", evt.value.v); 00032 } 00033 } 00034 } 00035
The mbed rtos API has made the choice of defaulting to 0 timeout (no wait) for the producer methods, and osWaitForever (infinitive wait) for the consumer methods.
A typical scenario for a producer could be a peripheral triggering an interrupt to notify an event: in the corresponding interrupt service routine you cannot wait (this would deadlock the entire system). On the other side, the consumer could be a background thread waiting for events: in this case the desired default behaviour is not using CPU cycles until this event is produced, hence the osWaitForever.
When calling an rtos object method in an ISR all the timeout parameters have to be set to 0 (no wait): waiting in ISR is not allowed.
The stack configuration is very dependent from the underling CMSIS-RTOS implementation.
As first reference implementation we are using RTX. One of the limitation of the RTX implementation is that it requires to statically configure the maximum number of threads and the size of the memory pool for their stacks.
This is our default configuration:
| mbed NXP LPC11U24 | mbed NXP LPC1768 | |
|---|---|---|
| Max number of user threads + (timer) | 3 + (1) | 7 + (1) |
| Default stack size in bytes | 0.5 Kb | 1 Kb |
To edit the configuration:
rtos library and select "Edit library...".
RTX_Conf_CM.c as needed.
The Status and Error Codes section lists all the return values that the CMSIS-RTOS functions will return:
osOK: function completed; no event occurred.
osEventSignal: function completed; signal event occurred.
osEventMessage: function completed; message event occurred.
osEventMail: function completed; mail event occurred.
osEventTimeout: function completed; timeout occurred.
osErrorParameter: parameter error: a mandatory parameter was missing or specified an incorrect object.
osErrorResource: resource not available: a specified resource was not available.
osErrorTimeoutResource: resource not available within given time: a specified resource was not available within the timeout period.
osErrorISR: not allowed in ISR context: the function cannot be called from interrupt service routines.
osErrorISRRecursive: function called multiple times from ISR with same object.
osErrorPriority: system cannot determine priority or thread has illegal priority.
osErrorNoMemory: system is out of memory: it was impossible to allocate or reserve memory for the operation.
osErrorValue: value of a parameter is out of range.
osErrorOS: unspecified RTOS error: run-time error but no other error message fits.
The osEvent data structure is returned by get methods of Queue and Mail objects.
This data structure contains both an error code and a pointer to the actual data:
Data Fields |
|
| osStatus | status |
|
status code: event or error information
|
|
| union { | |
| uint32_t v | |
|
message as 32-bit value
|
|
| void * p | |
|
message or mail as void pointer
|
|
| int32_t signals | |
|
signal flags
|
|
| } | value |
|
event value
|
|
| union { | |
| osMailQId mail_id | |
|
mail id obtained by osMailCreate
|
|
| osMessageQId message_id | |
|
message id obtained by osMessageCreate
|
|
| } | def |
|
event definition
|
|
No tags
|
32 comments
Please login to post comments.
Great work, was waiting since the beginnig of mbed,if anyone could achieve this! Really cool. Thanks a lot!