Preliminary main mbed library for nexpaq development
TESTS/mbedmicro-rtos-mbed/semaphore/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "greentea-client/test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | #include "rtos.h" |
nexpaq | 0:6c56fb4bc5f0 | 4 | |
nexpaq | 0:6c56fb4bc5f0 | 5 | #if defined(MBED_RTOS_SINGLE_THREAD) |
nexpaq | 0:6c56fb4bc5f0 | 6 | #error [NOT_SUPPORTED] test not supported |
nexpaq | 0:6c56fb4bc5f0 | 7 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 8 | |
nexpaq | 0:6c56fb4bc5f0 | 9 | #define THREAD_DELAY 75 |
nexpaq | 0:6c56fb4bc5f0 | 10 | #define SEMAPHORE_SLOTS 2 |
nexpaq | 0:6c56fb4bc5f0 | 11 | #define SEM_CHANGES 100 |
nexpaq | 0:6c56fb4bc5f0 | 12 | |
nexpaq | 0:6c56fb4bc5f0 | 13 | /* |
nexpaq | 0:6c56fb4bc5f0 | 14 | * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and |
nexpaq | 0:6c56fb4bc5f0 | 15 | * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes |
nexpaq | 0:6c56fb4bc5f0 | 16 | * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. |
nexpaq | 0:6c56fb4bc5f0 | 17 | */ |
nexpaq | 0:6c56fb4bc5f0 | 18 | #if (defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)) && defined(TOOLCHAIN_GCC) |
nexpaq | 0:6c56fb4bc5f0 | 19 | #define STACK_SIZE DEFAULT_STACK_SIZE/16 |
nexpaq | 0:6c56fb4bc5f0 | 20 | #elif (defined(TARGET_STM32F030R8) || defined(TARGET_STM32F070RB)) && defined(TOOLCHAIN_GCC) |
nexpaq | 0:6c56fb4bc5f0 | 21 | #define STACK_SIZE DEFAULT_STACK_SIZE/8 |
nexpaq | 0:6c56fb4bc5f0 | 22 | #elif defined(TARGET_STM32F334R8) && (defined(TOOLCHAIN_GCC) || defined(TOOLCHAIN_IAR)) |
nexpaq | 0:6c56fb4bc5f0 | 23 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
nexpaq | 0:6c56fb4bc5f0 | 24 | #elif defined(TARGET_STM32F103RB) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 25 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
nexpaq | 0:6c56fb4bc5f0 | 26 | #elif defined(TARGET_STM32F030R8) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 27 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
nexpaq | 0:6c56fb4bc5f0 | 28 | #elif defined(TARGET_STM32F070RB) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 29 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
nexpaq | 0:6c56fb4bc5f0 | 30 | #elif defined(TARGET_STM32F072RB) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 31 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
nexpaq | 0:6c56fb4bc5f0 | 32 | #elif defined(TARGET_STM32F302R8) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 33 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
nexpaq | 0:6c56fb4bc5f0 | 34 | #elif defined(TARGET_STM32F303K8) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
nexpaq | 0:6c56fb4bc5f0 | 36 | #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 37 | #define STACK_SIZE 512 |
nexpaq | 0:6c56fb4bc5f0 | 38 | #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 39 | #define STACK_SIZE 768 |
nexpaq | 0:6c56fb4bc5f0 | 40 | #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 41 | #define STACK_SIZE 1536 |
nexpaq | 0:6c56fb4bc5f0 | 42 | #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) |
nexpaq | 0:6c56fb4bc5f0 | 43 | #define STACK_SIZE 768 |
nexpaq | 0:6c56fb4bc5f0 | 44 | #else |
nexpaq | 0:6c56fb4bc5f0 | 45 | #define STACK_SIZE DEFAULT_STACK_SIZE |
nexpaq | 0:6c56fb4bc5f0 | 46 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 47 | |
nexpaq | 0:6c56fb4bc5f0 | 48 | void print_char(char c = '*') { |
nexpaq | 0:6c56fb4bc5f0 | 49 | printf("%c", c); |
nexpaq | 0:6c56fb4bc5f0 | 50 | fflush(stdout); |
nexpaq | 0:6c56fb4bc5f0 | 51 | } |
nexpaq | 0:6c56fb4bc5f0 | 52 | |
nexpaq | 0:6c56fb4bc5f0 | 53 | Semaphore two_slots(SEMAPHORE_SLOTS); |
nexpaq | 0:6c56fb4bc5f0 | 54 | |
nexpaq | 0:6c56fb4bc5f0 | 55 | volatile int change_counter = 0; |
nexpaq | 0:6c56fb4bc5f0 | 56 | volatile int sem_counter = 0; |
nexpaq | 0:6c56fb4bc5f0 | 57 | volatile bool sem_defect = false; |
nexpaq | 0:6c56fb4bc5f0 | 58 | |
nexpaq | 0:6c56fb4bc5f0 | 59 | void test_thread(void const *delay) { |
nexpaq | 0:6c56fb4bc5f0 | 60 | const int thread_delay = int(delay); |
nexpaq | 0:6c56fb4bc5f0 | 61 | while (true) { |
nexpaq | 0:6c56fb4bc5f0 | 62 | two_slots.wait(); |
nexpaq | 0:6c56fb4bc5f0 | 63 | sem_counter++; |
nexpaq | 0:6c56fb4bc5f0 | 64 | const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; |
nexpaq | 0:6c56fb4bc5f0 | 65 | const char msg = sem_lock_failed ? 'e' : sem_counter + '0'; |
nexpaq | 0:6c56fb4bc5f0 | 66 | print_char(msg); |
nexpaq | 0:6c56fb4bc5f0 | 67 | if (sem_lock_failed) { |
nexpaq | 0:6c56fb4bc5f0 | 68 | sem_defect = true; |
nexpaq | 0:6c56fb4bc5f0 | 69 | } |
nexpaq | 0:6c56fb4bc5f0 | 70 | Thread::wait(thread_delay); |
nexpaq | 0:6c56fb4bc5f0 | 71 | print_char('.'); |
nexpaq | 0:6c56fb4bc5f0 | 72 | sem_counter--; |
nexpaq | 0:6c56fb4bc5f0 | 73 | change_counter++; |
nexpaq | 0:6c56fb4bc5f0 | 74 | two_slots.release(); |
nexpaq | 0:6c56fb4bc5f0 | 75 | } |
nexpaq | 0:6c56fb4bc5f0 | 76 | } |
nexpaq | 0:6c56fb4bc5f0 | 77 | |
nexpaq | 0:6c56fb4bc5f0 | 78 | int main (void) { |
nexpaq | 0:6c56fb4bc5f0 | 79 | GREENTEA_SETUP(20, "default_auto"); |
nexpaq | 0:6c56fb4bc5f0 | 80 | |
nexpaq | 0:6c56fb4bc5f0 | 81 | const int t1_delay = THREAD_DELAY * 1; |
nexpaq | 0:6c56fb4bc5f0 | 82 | const int t2_delay = THREAD_DELAY * 2; |
nexpaq | 0:6c56fb4bc5f0 | 83 | const int t3_delay = THREAD_DELAY * 3; |
nexpaq | 0:6c56fb4bc5f0 | 84 | Thread t1(test_thread, (void *)t1_delay, osPriorityNormal, STACK_SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 85 | Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 86 | Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 87 | |
nexpaq | 0:6c56fb4bc5f0 | 88 | while (true) { |
nexpaq | 0:6c56fb4bc5f0 | 89 | if (change_counter >= SEM_CHANGES or sem_defect == true) { |
nexpaq | 0:6c56fb4bc5f0 | 90 | t1.terminate(); |
nexpaq | 0:6c56fb4bc5f0 | 91 | t2.terminate(); |
nexpaq | 0:6c56fb4bc5f0 | 92 | t3.terminate(); |
nexpaq | 0:6c56fb4bc5f0 | 93 | break; |
nexpaq | 0:6c56fb4bc5f0 | 94 | } |
nexpaq | 0:6c56fb4bc5f0 | 95 | } |
nexpaq | 0:6c56fb4bc5f0 | 96 | |
nexpaq | 0:6c56fb4bc5f0 | 97 | fflush(stdout); |
nexpaq | 0:6c56fb4bc5f0 | 98 | GREENTEA_TESTSUITE_RESULT(!sem_defect); |
nexpaq | 0:6c56fb4bc5f0 | 99 | return 0; |
nexpaq | 0:6c56fb4bc5f0 | 100 | } |