Preliminary main mbed library for nexpaq development
libraries/tests/rtos/mbed/mail/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 "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 | typedef struct { |
nexpaq | 0:6c56fb4bc5f0 | 10 | float voltage; /* AD result of measured voltage */ |
nexpaq | 0:6c56fb4bc5f0 | 11 | float current; /* AD result of measured current */ |
nexpaq | 0:6c56fb4bc5f0 | 12 | uint32_t counter; /* A counter value */ |
nexpaq | 0:6c56fb4bc5f0 | 13 | } mail_t; |
nexpaq | 0:6c56fb4bc5f0 | 14 | |
nexpaq | 0:6c56fb4bc5f0 | 15 | #define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33 |
nexpaq | 0:6c56fb4bc5f0 | 16 | #define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11 |
nexpaq | 0:6c56fb4bc5f0 | 17 | #define QUEUE_SIZE 16 |
nexpaq | 0:6c56fb4bc5f0 | 18 | #define QUEUE_PUT_DELAY 100 |
nexpaq | 0:6c56fb4bc5f0 | 19 | |
nexpaq | 0:6c56fb4bc5f0 | 20 | /* |
nexpaq | 0:6c56fb4bc5f0 | 21 | * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and |
nexpaq | 0:6c56fb4bc5f0 | 22 | * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes |
nexpaq | 0:6c56fb4bc5f0 | 23 | * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. |
nexpaq | 0:6c56fb4bc5f0 | 24 | */ |
nexpaq | 0:6c56fb4bc5f0 | 25 | #if (defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)) && defined(TOOLCHAIN_GCC) |
nexpaq | 0:6c56fb4bc5f0 | 26 | #define STACK_SIZE DEFAULT_STACK_SIZE/4 |
nexpaq | 0:6c56fb4bc5f0 | 27 | #elif (defined(TARGET_STM32F030R8)) && defined(TOOLCHAIN_GCC) |
nexpaq | 0:6c56fb4bc5f0 | 28 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
nexpaq | 0:6c56fb4bc5f0 | 29 | #elif (defined(TARGET_STM32F030R8)) && defined(TOOLCHAIN_IAR) |
nexpaq | 0:6c56fb4bc5f0 | 30 | #define STACK_SIZE DEFAULT_STACK_SIZE/2 |
nexpaq | 0:6c56fb4bc5f0 | 31 | #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 32 | #define STACK_SIZE 512 |
nexpaq | 0:6c56fb4bc5f0 | 33 | #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 34 | #define STACK_SIZE 768 |
nexpaq | 0:6c56fb4bc5f0 | 35 | #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) |
nexpaq | 0:6c56fb4bc5f0 | 36 | #define STACK_SIZE 1536 |
nexpaq | 0:6c56fb4bc5f0 | 37 | #elif defined(TARGET_MCU_NRF51822) |
nexpaq | 0:6c56fb4bc5f0 | 38 | #define STACK_SIZE 768 |
nexpaq | 0:6c56fb4bc5f0 | 39 | #else |
nexpaq | 0:6c56fb4bc5f0 | 40 | #define STACK_SIZE DEFAULT_STACK_SIZE |
nexpaq | 0:6c56fb4bc5f0 | 41 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 42 | |
nexpaq | 0:6c56fb4bc5f0 | 43 | Mail<mail_t, QUEUE_SIZE> mail_box; |
nexpaq | 0:6c56fb4bc5f0 | 44 | |
nexpaq | 0:6c56fb4bc5f0 | 45 | void send_thread (void const *argument) { |
nexpaq | 0:6c56fb4bc5f0 | 46 | static uint32_t i = 10; |
nexpaq | 0:6c56fb4bc5f0 | 47 | while (true) { |
nexpaq | 0:6c56fb4bc5f0 | 48 | i++; // fake data update |
nexpaq | 0:6c56fb4bc5f0 | 49 | mail_t *mail = mail_box.alloc(); |
nexpaq | 0:6c56fb4bc5f0 | 50 | mail->voltage = CREATE_VOLTAGE(i); |
nexpaq | 0:6c56fb4bc5f0 | 51 | mail->current = CREATE_CURRENT(i); |
nexpaq | 0:6c56fb4bc5f0 | 52 | mail->counter = i; |
nexpaq | 0:6c56fb4bc5f0 | 53 | mail_box.put(mail); |
nexpaq | 0:6c56fb4bc5f0 | 54 | Thread::wait(QUEUE_PUT_DELAY); |
nexpaq | 0:6c56fb4bc5f0 | 55 | } |
nexpaq | 0:6c56fb4bc5f0 | 56 | } |
nexpaq | 0:6c56fb4bc5f0 | 57 | |
nexpaq | 0:6c56fb4bc5f0 | 58 | int main (void) { |
nexpaq | 0:6c56fb4bc5f0 | 59 | MBED_HOSTTEST_TIMEOUT(20); |
nexpaq | 0:6c56fb4bc5f0 | 60 | MBED_HOSTTEST_SELECT(default_auto); |
nexpaq | 0:6c56fb4bc5f0 | 61 | MBED_HOSTTEST_DESCRIPTION(Mail messaging); |
nexpaq | 0:6c56fb4bc5f0 | 62 | MBED_HOSTTEST_START("RTOS_6"); |
nexpaq | 0:6c56fb4bc5f0 | 63 | |
nexpaq | 0:6c56fb4bc5f0 | 64 | Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); |
nexpaq | 0:6c56fb4bc5f0 | 65 | bool result = true; |
nexpaq | 0:6c56fb4bc5f0 | 66 | int result_counter = 0; |
nexpaq | 0:6c56fb4bc5f0 | 67 | |
nexpaq | 0:6c56fb4bc5f0 | 68 | while (true) { |
nexpaq | 0:6c56fb4bc5f0 | 69 | osEvent evt = mail_box.get(); |
nexpaq | 0:6c56fb4bc5f0 | 70 | if (evt.status == osEventMail) { |
nexpaq | 0:6c56fb4bc5f0 | 71 | mail_t *mail = (mail_t*)evt.value.p; |
nexpaq | 0:6c56fb4bc5f0 | 72 | const float expected_voltage = CREATE_VOLTAGE(mail->counter); |
nexpaq | 0:6c56fb4bc5f0 | 73 | const float expected_current = CREATE_CURRENT(mail->counter); |
nexpaq | 0:6c56fb4bc5f0 | 74 | // Check using macros if received values correspond to values sent via queue |
nexpaq | 0:6c56fb4bc5f0 | 75 | bool expected_values = (expected_voltage == mail->voltage) && |
nexpaq | 0:6c56fb4bc5f0 | 76 | (expected_current == mail->current); |
nexpaq | 0:6c56fb4bc5f0 | 77 | result = result && expected_values; |
nexpaq | 0:6c56fb4bc5f0 | 78 | const char *result_msg = expected_values ? "OK" : "FAIL"; |
nexpaq | 0:6c56fb4bc5f0 | 79 | printf("%3d %.2fV %.2fA ... [%s]\r\n", mail->counter, |
nexpaq | 0:6c56fb4bc5f0 | 80 | mail->voltage, |
nexpaq | 0:6c56fb4bc5f0 | 81 | mail->current, |
nexpaq | 0:6c56fb4bc5f0 | 82 | result_msg); |
nexpaq | 0:6c56fb4bc5f0 | 83 | mail_box.free(mail); |
nexpaq | 0:6c56fb4bc5f0 | 84 | if (result == false || ++result_counter == QUEUE_SIZE) { |
nexpaq | 0:6c56fb4bc5f0 | 85 | break; |
nexpaq | 0:6c56fb4bc5f0 | 86 | } |
nexpaq | 0:6c56fb4bc5f0 | 87 | } |
nexpaq | 0:6c56fb4bc5f0 | 88 | } |
nexpaq | 0:6c56fb4bc5f0 | 89 | MBED_HOSTTEST_RESULT(result); |
nexpaq | 0:6c56fb4bc5f0 | 90 | return 0; |
nexpaq | 0:6c56fb4bc5f0 | 91 | } |