![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
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 THREAD_DELAY 75 00010 #define SEMAPHORE_SLOTS 2 00011 #define SEM_CHANGES 100 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_STM32F072RB) || defined(TARGET_STM32F070RB)) 00019 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00020 #elif defined(TARGET_STM32F334R8) 00021 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00022 #elif defined(TARGET_STM32F103RB) && defined(TOOLCHAIN_IAR) 00023 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00024 #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) 00025 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00026 #elif defined(TARGET_STM32F303K8) 00027 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00028 #elif defined(TARGET_STM32F334C8) 00029 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00030 #elif defined(TARGET_STM32L073RZ) 00031 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00032 #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) 00033 #define STACK_SIZE 512 00034 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) 00035 #define STACK_SIZE 768 00036 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) 00037 #define STACK_SIZE 1536 00038 #elif defined(TARGET_MCU_NRF51822) 00039 #define STACK_SIZE 768 00040 #else 00041 #define STACK_SIZE DEFAULT_STACK_SIZE 00042 #endif 00043 00044 void print_char(char c = '*') { 00045 printf("%c", c); 00046 fflush(stdout); 00047 } 00048 00049 Semaphore two_slots(SEMAPHORE_SLOTS); 00050 00051 volatile int change_counter = 0; 00052 volatile int sem_counter = 0; 00053 volatile bool sem_defect = false; 00054 00055 void test_thread(void const *delay) { 00056 const int thread_delay = int(delay); 00057 while (true) { 00058 two_slots.wait(); 00059 sem_counter++; 00060 const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; 00061 const char msg = sem_lock_failed ? 'e' : sem_counter + '0'; 00062 print_char(msg); 00063 if (sem_lock_failed) { 00064 sem_defect = true; 00065 } 00066 Thread::wait(thread_delay); 00067 print_char('.'); 00068 sem_counter--; 00069 change_counter++; 00070 two_slots.release(); 00071 } 00072 } 00073 00074 int main (void) { 00075 MBED_HOSTTEST_TIMEOUT(20); 00076 MBED_HOSTTEST_SELECT(default_auto); 00077 MBED_HOSTTEST_DESCRIPTION(Semaphore resource lock); 00078 MBED_HOSTTEST_START("RTOS_3"); 00079 00080 const int t1_delay = THREAD_DELAY * 1; 00081 const int t2_delay = THREAD_DELAY * 2; 00082 const int t3_delay = THREAD_DELAY * 3; 00083 Thread t1(test_thread, (void *)t1_delay, osPriorityNormal, STACK_SIZE); 00084 Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); 00085 Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); 00086 00087 while (true) { 00088 if (change_counter >= SEM_CHANGES or sem_defect == true) { 00089 t1.terminate(); 00090 t2.terminate(); 00091 t3.terminate(); 00092 break; 00093 } 00094 } 00095 00096 fflush(stdout); 00097 MBED_HOSTTEST_RESULT(!sem_defect); 00098 return 0; 00099 }
Generated on Sun Jul 17 2022 08:25:26 by
![doxygen](doxygen.png)