ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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_EFR32)) && !defined(TOOLCHAIN_ARM_MICRO)
00036     #define STACK_SIZE 768
00037 #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
00038     #define STACK_SIZE 1024
00039 #elif defined(TARGET_XDOT_L151CC)
00040     #define STACK_SIZE 1024
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(int const *thread_delay) {
00081     while (true) {
00082         manipulate_protected_zone(*thread_delay);
00083     }
00084 }
00085 
00086 int main() {
00087     GREENTEA_SETUP(20, "default_auto");
00088 
00089     const int t1_delay = THREAD_DELAY * 1;
00090     const int t2_delay = THREAD_DELAY * 2;
00091     const int t3_delay = THREAD_DELAY * 3;
00092     Thread t2(osPriorityNormal, STACK_SIZE);
00093     Thread t3(osPriorityNormal, STACK_SIZE);
00094 
00095     t2.start(callback(test_thread, &t2_delay));
00096     t3.start(callback(test_thread, &t3_delay));
00097 
00098     while (true) {
00099         // Thread 1 action
00100         Thread::wait(t1_delay);
00101         manipulate_protected_zone(t1_delay);
00102         if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) {
00103             t2.terminate();
00104             t3.terminate();
00105             break;
00106         }
00107     }
00108 
00109     fflush(stdout);
00110     GREENTEA_TESTSUITE_RESULT(!mutex_defect);
00111     return 0;
00112 }