Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 #include "mbed.h"
switches 0:5c4d7b2438d3 2 #include "greentea-client/test_env.h"
switches 0:5c4d7b2438d3 3 #include "unity.h"
switches 0:5c4d7b2438d3 4 #include "utest.h"
switches 0:5c4d7b2438d3 5 #include "rtos.h"
switches 0:5c4d7b2438d3 6 #include "SynchronizedIntegral.h"
switches 0:5c4d7b2438d3 7 #include "LockGuard.h"
switches 0:5c4d7b2438d3 8
switches 0:5c4d7b2438d3 9 #if defined(MBED_RTOS_SINGLE_THREAD)
switches 0:5c4d7b2438d3 10 #error [NOT_SUPPORTED] test not supported
switches 0:5c4d7b2438d3 11 #endif
switches 0:5c4d7b2438d3 12
switches 0:5c4d7b2438d3 13 /*
switches 0:5c4d7b2438d3 14 * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
switches 0:5c4d7b2438d3 15 * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
switches 0:5c4d7b2438d3 16 * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
switches 0:5c4d7b2438d3 17 */
switches 0:5c4d7b2438d3 18 #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
switches 0:5c4d7b2438d3 19 #define STACK_SIZE 512
switches 0:5c4d7b2438d3 20 #elif defined(TARGET_STM32F070RB) || defined(TARGET_STM32F072RB) || defined(TARGET_STM32F103RB) || defined(TARGET_STM32F091RC)
switches 0:5c4d7b2438d3 21 #define STACK_SIZE 512
switches 0:5c4d7b2438d3 22 #elif defined(TARGET_STM32F410RB)
switches 0:5c4d7b2438d3 23 #define STACK_SIZE 512
switches 0:5c4d7b2438d3 24 #elif defined(TARGET_STM32L073RZ)
switches 0:5c4d7b2438d3 25 #define STACK_SIZE 512
switches 0:5c4d7b2438d3 26 #elif defined(TARGET_XDOT_L151CC)
switches 0:5c4d7b2438d3 27 #define STACK_SIZE 1024
switches 0:5c4d7b2438d3 28 #elif defined(TARGET_HI2110)
switches 0:5c4d7b2438d3 29 #define STACK_SIZE 512
switches 0:5c4d7b2438d3 30 #else
switches 0:5c4d7b2438d3 31 #define STACK_SIZE DEFAULT_STACK_SIZE
switches 0:5c4d7b2438d3 32 #endif
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 using namespace utest::v1;
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 // The counter type used accross all the tests
switches 0:5c4d7b2438d3 37 // It is internall ysynchronized so read
switches 0:5c4d7b2438d3 38 typedef SynchronizedIntegral<int> counter_t;
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 // Tasks with different functions to test on threads
switches 0:5c4d7b2438d3 41 void increment(counter_t* counter) {
switches 0:5c4d7b2438d3 42 (*counter)++;
switches 0:5c4d7b2438d3 43 }
switches 0:5c4d7b2438d3 44
switches 0:5c4d7b2438d3 45 void increment_with_yield(counter_t* counter) {
switches 0:5c4d7b2438d3 46 Thread::yield();
switches 0:5c4d7b2438d3 47 (*counter)++;
switches 0:5c4d7b2438d3 48 }
switches 0:5c4d7b2438d3 49
switches 0:5c4d7b2438d3 50 void increment_with_wait(counter_t* counter) {
switches 0:5c4d7b2438d3 51 Thread::wait(100);
switches 0:5c4d7b2438d3 52 (*counter)++;
switches 0:5c4d7b2438d3 53 }
switches 0:5c4d7b2438d3 54
switches 0:5c4d7b2438d3 55 void increment_with_child(counter_t* counter) {
switches 0:5c4d7b2438d3 56 Thread child(counter, increment, osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 57 child.join();
switches 0:5c4d7b2438d3 58 }
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 void increment_with_murder(counter_t* counter) {
switches 0:5c4d7b2438d3 61 {
switches 0:5c4d7b2438d3 62 // take ownership of the counter mutex so it prevent the child to
switches 0:5c4d7b2438d3 63 // modify counter.
switches 0:5c4d7b2438d3 64 LockGuard lock(counter->internal_mutex());
switches 0:5c4d7b2438d3 65 Thread child(counter, increment, osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 66 child.terminate();
switches 0:5c4d7b2438d3 67 }
switches 0:5c4d7b2438d3 68
switches 0:5c4d7b2438d3 69 (*counter)++;
switches 0:5c4d7b2438d3 70 }
switches 0:5c4d7b2438d3 71
switches 0:5c4d7b2438d3 72 void self_terminate(Thread *self) {
switches 0:5c4d7b2438d3 73 self->terminate();
switches 0:5c4d7b2438d3 74 // Code should not get here
switches 0:5c4d7b2438d3 75 TEST_ASSERT(0);
switches 0:5c4d7b2438d3 76 }
switches 0:5c4d7b2438d3 77
switches 0:5c4d7b2438d3 78 // Tests that spawn tasks in different configurations
switches 0:5c4d7b2438d3 79 template <void (*F)(counter_t *)>
switches 0:5c4d7b2438d3 80 void test_single_thread() {
switches 0:5c4d7b2438d3 81 counter_t counter(0);
switches 0:5c4d7b2438d3 82 Thread thread(&counter, F, osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 83 thread.join();
switches 0:5c4d7b2438d3 84 TEST_ASSERT_EQUAL(counter, 1);
switches 0:5c4d7b2438d3 85 }
switches 0:5c4d7b2438d3 86
switches 0:5c4d7b2438d3 87 template <int N, void (*F)(counter_t *)>
switches 0:5c4d7b2438d3 88 void test_parallel_threads() {
switches 0:5c4d7b2438d3 89 counter_t counter(0);
switches 0:5c4d7b2438d3 90 Thread *threads[N];
switches 0:5c4d7b2438d3 91
switches 0:5c4d7b2438d3 92 for (int i = 0; i < N; i++) {
switches 0:5c4d7b2438d3 93 threads[i] = new Thread(&counter, F, osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 94 }
switches 0:5c4d7b2438d3 95
switches 0:5c4d7b2438d3 96 for (int i = 0; i < N; i++) {
switches 0:5c4d7b2438d3 97 threads[i]->join();
switches 0:5c4d7b2438d3 98 delete threads[i];
switches 0:5c4d7b2438d3 99 }
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 TEST_ASSERT_EQUAL(counter, N);
switches 0:5c4d7b2438d3 102 }
switches 0:5c4d7b2438d3 103
switches 0:5c4d7b2438d3 104 template <int N, void (*F)(counter_t *)>
switches 0:5c4d7b2438d3 105 void test_serial_threads() {
switches 0:5c4d7b2438d3 106 counter_t counter(0);
switches 0:5c4d7b2438d3 107
switches 0:5c4d7b2438d3 108 for (int i = 0; i < N; i++) {
switches 0:5c4d7b2438d3 109 Thread thread(&counter, F, osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 110 thread.join();
switches 0:5c4d7b2438d3 111 }
switches 0:5c4d7b2438d3 112
switches 0:5c4d7b2438d3 113 TEST_ASSERT_EQUAL(counter, N);
switches 0:5c4d7b2438d3 114 }
switches 0:5c4d7b2438d3 115
switches 0:5c4d7b2438d3 116 void test_self_terminate() {
switches 0:5c4d7b2438d3 117 Thread *thread = new Thread(osPriorityNormal, STACK_SIZE);
switches 0:5c4d7b2438d3 118 thread->start(thread, self_terminate);
switches 0:5c4d7b2438d3 119 thread->join();
switches 0:5c4d7b2438d3 120 delete thread;
switches 0:5c4d7b2438d3 121 }
switches 0:5c4d7b2438d3 122
switches 0:5c4d7b2438d3 123 utest::v1::status_t test_setup(const size_t number_of_cases) {
switches 0:5c4d7b2438d3 124 GREENTEA_SETUP(40, "default_auto");
switches 0:5c4d7b2438d3 125 return verbose_test_setup_handler(number_of_cases);
switches 0:5c4d7b2438d3 126 }
switches 0:5c4d7b2438d3 127
switches 0:5c4d7b2438d3 128 // Test cases
switches 0:5c4d7b2438d3 129 Case cases[] = {
switches 0:5c4d7b2438d3 130 Case("Testing single thread", test_single_thread<increment>),
switches 0:5c4d7b2438d3 131 Case("Testing parallel threads", test_parallel_threads<3, increment>),
switches 0:5c4d7b2438d3 132 Case("Testing serial threads", test_serial_threads<10, increment>),
switches 0:5c4d7b2438d3 133
switches 0:5c4d7b2438d3 134 Case("Testing single thread with yield", test_single_thread<increment_with_yield>),
switches 0:5c4d7b2438d3 135 Case("Testing parallel threads with yield", test_parallel_threads<3, increment_with_yield>),
switches 0:5c4d7b2438d3 136 Case("Testing serial threads with yield", test_serial_threads<10, increment_with_yield>),
switches 0:5c4d7b2438d3 137
switches 0:5c4d7b2438d3 138 Case("Testing single thread with wait", test_single_thread<increment_with_wait>),
switches 0:5c4d7b2438d3 139 Case("Testing parallel threads with wait", test_parallel_threads<3, increment_with_wait>),
switches 0:5c4d7b2438d3 140 Case("Testing serial threads with wait", test_serial_threads<10, increment_with_wait>),
switches 0:5c4d7b2438d3 141
switches 0:5c4d7b2438d3 142 Case("Testing single thread with child", test_single_thread<increment_with_child>),
switches 0:5c4d7b2438d3 143 Case("Testing parallel threads with child", test_parallel_threads<2, increment_with_child>),
switches 0:5c4d7b2438d3 144 Case("Testing serial threads with child", test_serial_threads<10, increment_with_child>),
switches 0:5c4d7b2438d3 145
switches 0:5c4d7b2438d3 146 Case("Testing single thread with murder", test_single_thread<increment_with_murder>),
switches 0:5c4d7b2438d3 147 Case("Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>),
switches 0:5c4d7b2438d3 148 Case("Testing serial threads with murder", test_serial_threads<10, increment_with_murder>),
switches 0:5c4d7b2438d3 149
switches 0:5c4d7b2438d3 150 Case("Testing thread self terminate", test_self_terminate),
switches 0:5c4d7b2438d3 151 };
switches 0:5c4d7b2438d3 152
switches 0:5c4d7b2438d3 153 Specification specification(test_setup, cases);
switches 0:5c4d7b2438d3 154
switches 0:5c4d7b2438d3 155 int main() {
switches 0:5c4d7b2438d3 156 return !Harness::run(specification);
switches 0:5c4d7b2438d3 157 }