Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
main.cpp
00001 #include "mbed.h" 00002 #include "greentea-client/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_STM32F334R8) && (defined(TOOLCHAIN_GCC) || defined(TOOLCHAIN_IAR)) 00019 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00020 #elif defined(TARGET_STM32F103RB) 00021 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00022 #elif defined(TARGET_STM32F070RB) 00023 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00024 #elif defined(TARGET_STM32F072RB) 00025 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00026 #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) 00027 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00028 #elif defined(TARGET_STM32L073RZ) 00029 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00030 #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR) 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_EFR32)) && !defined(TOOLCHAIN_ARM_MICRO) 00039 #define STACK_SIZE 768 00040 #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) 00041 #define STACK_SIZE 768 00042 #elif defined(TARGET_XDOT_L151CC) 00043 #define STACK_SIZE 1024 00044 #else 00045 #define STACK_SIZE DEFAULT_STACK_SIZE 00046 #endif 00047 00048 void print_char(char c = '*') { 00049 printf("%c", c); 00050 fflush(stdout); 00051 } 00052 00053 Semaphore two_slots(SEMAPHORE_SLOTS); 00054 00055 volatile int change_counter = 0; 00056 volatile int sem_counter = 0; 00057 volatile bool sem_defect = false; 00058 00059 void test_thread(int const *delay) { 00060 const int thread_delay = *delay; 00061 while (true) { 00062 two_slots.wait(); 00063 sem_counter++; 00064 const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; 00065 const char msg = sem_lock_failed ? 'e' : sem_counter + '0'; 00066 print_char(msg); 00067 if (sem_lock_failed) { 00068 sem_defect = true; 00069 } 00070 Thread::wait(thread_delay); 00071 print_char('.'); 00072 sem_counter--; 00073 change_counter++; 00074 two_slots.release(); 00075 } 00076 } 00077 00078 int main (void) { 00079 GREENTEA_SETUP(20, "default_auto"); 00080 00081 const int t1_delay = THREAD_DELAY * 1; 00082 const int t2_delay = THREAD_DELAY * 2; 00083 const int t3_delay = THREAD_DELAY * 3; 00084 Thread t1(osPriorityNormal, STACK_SIZE); 00085 Thread t2(osPriorityNormal, STACK_SIZE); 00086 Thread t3(osPriorityNormal, STACK_SIZE); 00087 00088 t1.start(callback(test_thread, &t1_delay)); 00089 t2.start(callback(test_thread, &t2_delay)); 00090 t3.start(callback(test_thread, &t3_delay)); 00091 00092 while (true) { 00093 if (change_counter >= SEM_CHANGES or sem_defect == true) { 00094 t1.terminate(); 00095 t2.terminate(); 00096 t3.terminate(); 00097 break; 00098 } 00099 } 00100 00101 fflush(stdout); 00102 GREENTEA_TESTSUITE_RESULT(!sem_defect); 00103 return 0; 00104 }
Generated on Tue Jul 12 2022 11:02:46 by
1.7.2