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.
Fork of mbed-os by
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 50 00010 #define SIGNALS_TO_EMIT 100 00011 00012 /* 00013 * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and 00014 * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes 00015 * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. 00016 */ 00017 #if defined(TARGET_STM32F334R8) && defined(TOOLCHAIN_IAR) 00018 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00019 #elif defined(TARGET_STM32F070RB) 00020 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00021 #elif defined(TARGET_STM32F072RB) 00022 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00023 #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) 00024 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00025 #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR) 00026 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00027 #elif defined(TARGET_STM32L073RZ) 00028 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00029 #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) 00030 #define STACK_SIZE 512 00031 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) 00032 #define STACK_SIZE 768 00033 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) 00034 #define STACK_SIZE 1536 00035 #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) 00036 #define STACK_SIZE 1024 00037 #elif defined(TARGET_XDOT_L151CC) 00038 #define STACK_SIZE 1024 00039 #else 00040 #define STACK_SIZE DEFAULT_STACK_SIZE 00041 #endif 00042 00043 void print_char(char c = '*') { 00044 printf("%c", c); 00045 fflush(stdout); 00046 } 00047 00048 Mutex stdio_mutex; 00049 DigitalOut led(LED1); 00050 00051 volatile int change_counter = 0; 00052 volatile bool changing_counter = false; 00053 volatile bool mutex_defect = false; 00054 00055 bool manipulate_protected_zone(const int thread_delay) { 00056 bool result = true; 00057 00058 stdio_mutex.lock(); // LOCK 00059 if (changing_counter == true) { 00060 // 'e' stands for error. If changing_counter is true access is not exclusively 00061 print_char('e'); 00062 result = false; 00063 mutex_defect = true; 00064 } 00065 changing_counter = true; 00066 00067 // Some action on protected 00068 led = !led; 00069 change_counter++; 00070 print_char('.'); 00071 Thread::wait(thread_delay); 00072 00073 changing_counter = false; 00074 stdio_mutex.unlock(); // UNLOCK 00075 return result; 00076 } 00077 00078 void test_thread(void const *args) { 00079 const int thread_delay = int(args); 00080 while (true) { 00081 manipulate_protected_zone(thread_delay); 00082 } 00083 } 00084 00085 int main() { 00086 GREENTEA_SETUP(20, "default_auto"); 00087 00088 const int t1_delay = THREAD_DELAY * 1; 00089 const int t2_delay = THREAD_DELAY * 2; 00090 const int t3_delay = THREAD_DELAY * 3; 00091 Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); 00092 Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); 00093 00094 while (true) { 00095 // Thread 1 action 00096 Thread::wait(t1_delay); 00097 manipulate_protected_zone(t1_delay); 00098 if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) { 00099 t2.terminate(); 00100 t3.terminate(); 00101 break; 00102 } 00103 } 00104 00105 fflush(stdout); 00106 GREENTEA_TESTSUITE_RESULT(!mutex_defect); 00107 return 0; 00108 }
Generated on Tue Jul 12 2022 13:15:58 by
