nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 1:55a6170b404f 1 #include "mbed.h"
nexpaq 1:55a6170b404f 2 #include "greentea-client/test_env.h"
nexpaq 1:55a6170b404f 3 #include "rtos.h"
nexpaq 1:55a6170b404f 4
nexpaq 1:55a6170b404f 5 #if defined(MBED_RTOS_SINGLE_THREAD)
nexpaq 1:55a6170b404f 6 #error [NOT_SUPPORTED] test not supported
nexpaq 1:55a6170b404f 7 #endif
nexpaq 1:55a6170b404f 8
nexpaq 1:55a6170b404f 9 #define THREAD_DELAY 50
nexpaq 1:55a6170b404f 10 #define SIGNALS_TO_EMIT 100
nexpaq 1:55a6170b404f 11
nexpaq 1:55a6170b404f 12 /*
nexpaq 1:55a6170b404f 13 * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
nexpaq 1:55a6170b404f 14 * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
nexpaq 1:55a6170b404f 15 * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
nexpaq 1:55a6170b404f 16 */
nexpaq 1:55a6170b404f 17 #if (defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)) && defined(TOOLCHAIN_GCC)
nexpaq 1:55a6170b404f 18 #define STACK_SIZE DEFAULT_STACK_SIZE/4
nexpaq 1:55a6170b404f 19 #elif (defined(TARGET_STM32F030R8) || defined(TARGET_STM32F070RB)) && defined(TOOLCHAIN_GCC)
nexpaq 1:55a6170b404f 20 #define STACK_SIZE DEFAULT_STACK_SIZE/4
nexpaq 1:55a6170b404f 21 #elif defined(TARGET_STM32F334R8) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 22 #define STACK_SIZE DEFAULT_STACK_SIZE/4
nexpaq 1:55a6170b404f 23 #elif defined(TARGET_STM32F030R8) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 24 #define STACK_SIZE DEFAULT_STACK_SIZE/4
nexpaq 1:55a6170b404f 25 #elif defined(TARGET_STM32F070RB) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 26 #define STACK_SIZE DEFAULT_STACK_SIZE/2
nexpaq 1:55a6170b404f 27 #elif defined(TARGET_STM32F072RB) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 28 #define STACK_SIZE DEFAULT_STACK_SIZE/2
nexpaq 1:55a6170b404f 29 #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 30 #define STACK_SIZE DEFAULT_STACK_SIZE/2
nexpaq 1:55a6170b404f 31 #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR)
nexpaq 1:55a6170b404f 32 #define STACK_SIZE DEFAULT_STACK_SIZE/2
nexpaq 1:55a6170b404f 33 #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO)
nexpaq 1:55a6170b404f 34 #define STACK_SIZE 512
nexpaq 1:55a6170b404f 35 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO)
nexpaq 1:55a6170b404f 36 #define STACK_SIZE 768
nexpaq 1:55a6170b404f 37 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO)
nexpaq 1:55a6170b404f 38 #define STACK_SIZE 1536
nexpaq 1:55a6170b404f 39 #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
nexpaq 1:55a6170b404f 40 #define STACK_SIZE 1024
nexpaq 1:55a6170b404f 41 #else
nexpaq 1:55a6170b404f 42 #define STACK_SIZE DEFAULT_STACK_SIZE
nexpaq 1:55a6170b404f 43 #endif
nexpaq 1:55a6170b404f 44
nexpaq 1:55a6170b404f 45 void print_char(char c = '*') {
nexpaq 1:55a6170b404f 46 printf("%c", c);
nexpaq 1:55a6170b404f 47 fflush(stdout);
nexpaq 1:55a6170b404f 48 }
nexpaq 1:55a6170b404f 49
nexpaq 1:55a6170b404f 50 Mutex stdio_mutex;
nexpaq 1:55a6170b404f 51 DigitalOut led(LED1);
nexpaq 1:55a6170b404f 52
nexpaq 1:55a6170b404f 53 volatile int change_counter = 0;
nexpaq 1:55a6170b404f 54 volatile bool changing_counter = false;
nexpaq 1:55a6170b404f 55 volatile bool mutex_defect = false;
nexpaq 1:55a6170b404f 56
nexpaq 1:55a6170b404f 57 bool manipulate_protected_zone(const int thread_delay) {
nexpaq 1:55a6170b404f 58 bool result = true;
nexpaq 1:55a6170b404f 59
nexpaq 1:55a6170b404f 60 stdio_mutex.lock(); // LOCK
nexpaq 1:55a6170b404f 61 if (changing_counter == true) {
nexpaq 1:55a6170b404f 62 // 'e' stands for error. If changing_counter is true access is not exclusively
nexpaq 1:55a6170b404f 63 print_char('e');
nexpaq 1:55a6170b404f 64 result = false;
nexpaq 1:55a6170b404f 65 mutex_defect = true;
nexpaq 1:55a6170b404f 66 }
nexpaq 1:55a6170b404f 67 changing_counter = true;
nexpaq 1:55a6170b404f 68
nexpaq 1:55a6170b404f 69 // Some action on protected
nexpaq 1:55a6170b404f 70 led = !led;
nexpaq 1:55a6170b404f 71 change_counter++;
nexpaq 1:55a6170b404f 72 print_char('.');
nexpaq 1:55a6170b404f 73 Thread::wait(thread_delay);
nexpaq 1:55a6170b404f 74
nexpaq 1:55a6170b404f 75 changing_counter = false;
nexpaq 1:55a6170b404f 76 stdio_mutex.unlock(); // UNLOCK
nexpaq 1:55a6170b404f 77 return result;
nexpaq 1:55a6170b404f 78 }
nexpaq 1:55a6170b404f 79
nexpaq 1:55a6170b404f 80 void test_thread(void const *args) {
nexpaq 1:55a6170b404f 81 const int thread_delay = int(args);
nexpaq 1:55a6170b404f 82 while (true) {
nexpaq 1:55a6170b404f 83 manipulate_protected_zone(thread_delay);
nexpaq 1:55a6170b404f 84 }
nexpaq 1:55a6170b404f 85 }
nexpaq 1:55a6170b404f 86
nexpaq 1:55a6170b404f 87 int main() {
nexpaq 1:55a6170b404f 88 GREENTEA_SETUP(20, "default_auto");
nexpaq 1:55a6170b404f 89
nexpaq 1:55a6170b404f 90 const int t1_delay = THREAD_DELAY * 1;
nexpaq 1:55a6170b404f 91 const int t2_delay = THREAD_DELAY * 2;
nexpaq 1:55a6170b404f 92 const int t3_delay = THREAD_DELAY * 3;
nexpaq 1:55a6170b404f 93 Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE);
nexpaq 1:55a6170b404f 94 Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE);
nexpaq 1:55a6170b404f 95
nexpaq 1:55a6170b404f 96 while (true) {
nexpaq 1:55a6170b404f 97 // Thread 1 action
nexpaq 1:55a6170b404f 98 Thread::wait(t1_delay);
nexpaq 1:55a6170b404f 99 manipulate_protected_zone(t1_delay);
nexpaq 1:55a6170b404f 100 if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) {
nexpaq 1:55a6170b404f 101 t2.terminate();
nexpaq 1:55a6170b404f 102 t3.terminate();
nexpaq 1:55a6170b404f 103 break;
nexpaq 1:55a6170b404f 104 }
nexpaq 1:55a6170b404f 105 }
nexpaq 1:55a6170b404f 106
nexpaq 1:55a6170b404f 107 fflush(stdout);
nexpaq 1:55a6170b404f 108 GREENTEA_TESTSUITE_RESULT(!mutex_defect);
nexpaq 1:55a6170b404f 109 return 0;
nexpaq 1:55a6170b404f 110 }