Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "test_env.h"
00003 #include "rtos.h"
00004 
00005 #if defined(MBED_RTOS_SINGLE_THREAD)
00006   #error [NOT_SUPPORTED] test not supported
00007 #endif
00008 
00009 #define SIGNALS_TO_EMIT     100
00010 #define SIGNAL_HANDLE_DELEY 25
00011 #define SIGNAL_SET_VALUE    0x01
00012 
00013 /*
00014  * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
00015  * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
00016  * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
00017  */
00018 #if (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO)
00019     #define STACK_SIZE 512
00020 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO)
00021     #define STACK_SIZE 768
00022 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO)
00023     #define STACK_SIZE 1536
00024 #elif defined(TARGET_MCU_NRF51822)
00025     #define STACK_SIZE 768
00026 #elif (defined(TARGET_STM32F070RB) || defined(TARGET_STM32F072RB))
00027     #define STACK_SIZE DEFAULT_STACK_SIZE/2
00028 #else
00029     #define STACK_SIZE DEFAULT_STACK_SIZE
00030 #endif
00031 
00032 DigitalOut led(LED1);
00033 volatile int signal_counter = 0;
00034 
00035 void led_thread(void const *argument) {
00036     while (true) {
00037         // Signal flags that are reported as event are automatically cleared.
00038         Thread::signal_wait(SIGNAL_SET_VALUE);
00039         led = !led;
00040         signal_counter++;
00041     }
00042 }
00043 
00044 int main (void) {
00045     MBED_HOSTTEST_TIMEOUT(20);
00046     MBED_HOSTTEST_SELECT(default_auto);
00047     MBED_HOSTTEST_DESCRIPTION(Signals messaging);
00048     MBED_HOSTTEST_START("RTOS_4");
00049 
00050     Thread thread(led_thread, NULL, osPriorityNormal, STACK_SIZE);
00051     bool result = true;
00052 
00053     while (true) {
00054         Thread::wait(2 * SIGNAL_HANDLE_DELEY);
00055         thread.signal_set(SIGNAL_SET_VALUE);
00056         if (signal_counter == SIGNALS_TO_EMIT) {
00057             printf("Handled %d signals\r\n", signal_counter);
00058             break;
00059         }
00060     }
00061     MBED_HOSTTEST_RESULT(result);
00062     return 0;
00063 }