dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
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 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_STM32L053R8) || defined(TARGET_STM32L053C8)) && defined(TOOLCHAIN_GCC) 00018 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00019 #elif (defined(TARGET_STM32F030R8) || defined(TARGET_STM32F070RB)) && defined(TOOLCHAIN_GCC) 00020 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00021 #elif defined(TARGET_STM32F334R8) && defined(TOOLCHAIN_IAR) 00022 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00023 #elif defined(TARGET_STM32F030R8) && defined(TOOLCHAIN_IAR) 00024 #define STACK_SIZE DEFAULT_STACK_SIZE/4 00025 #elif defined(TARGET_STM32F070RB) && defined(TOOLCHAIN_IAR) 00026 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00027 #elif defined(TARGET_STM32F072RB) && defined(TOOLCHAIN_IAR) 00028 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00029 #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) 00030 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00031 #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR) 00032 #define STACK_SIZE DEFAULT_STACK_SIZE/2 00033 #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) 00034 #define STACK_SIZE 512 00035 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) 00036 #define STACK_SIZE 768 00037 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) 00038 #define STACK_SIZE 1536 00039 #elif defined(TARGET_MCU_NRF51822) 00040 #define STACK_SIZE 768 00041 #else 00042 #define STACK_SIZE DEFAULT_STACK_SIZE 00043 #endif 00044 00045 void print_char(char c = '*') { 00046 printf("%c", c); 00047 fflush(stdout); 00048 } 00049 00050 Mutex stdio_mutex; 00051 DigitalOut led(LED1); 00052 00053 volatile int change_counter = 0; 00054 volatile bool changing_counter = false; 00055 volatile bool mutex_defect = false; 00056 00057 bool manipulate_protected_zone(const int thread_delay) { 00058 bool result = true; 00059 00060 stdio_mutex.lock(); // LOCK 00061 if (changing_counter == true) { 00062 // 'e' stands for error. If changing_counter is true access is not exclusively 00063 print_char('e'); 00064 result = false; 00065 mutex_defect = true; 00066 } 00067 changing_counter = true; 00068 00069 // Some action on protected 00070 led = !led; 00071 change_counter++; 00072 print_char('.'); 00073 Thread::wait(thread_delay); 00074 00075 changing_counter = false; 00076 stdio_mutex.unlock(); // UNLOCK 00077 return result; 00078 } 00079 00080 void test_thread(void const *args) { 00081 const int thread_delay = int(args); 00082 while (true) { 00083 manipulate_protected_zone(thread_delay); 00084 } 00085 } 00086 00087 int main() { 00088 MBED_HOSTTEST_TIMEOUT(20); 00089 MBED_HOSTTEST_SELECT(default); 00090 MBED_HOSTTEST_DESCRIPTION(Mutex resource lock); 00091 MBED_HOSTTEST_START("RTOS_2"); 00092 00093 const int t1_delay = THREAD_DELAY * 1; 00094 const int t2_delay = THREAD_DELAY * 2; 00095 const int t3_delay = THREAD_DELAY * 3; 00096 Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); 00097 Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); 00098 00099 while (true) { 00100 // Thread 1 action 00101 Thread::wait(t1_delay); 00102 manipulate_protected_zone(t1_delay); 00103 if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) { 00104 t2.terminate(); 00105 t3.terminate(); 00106 break; 00107 } 00108 } 00109 00110 fflush(stdout); 00111 MBED_HOSTTEST_RESULT(!mutex_defect); 00112 return 0; 00113 }
Generated on Tue Jul 12 2022 11:01:54 by
1.7.2
