Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TESTS/mbedmicro-rtos-mbed/queue/main.cpp@0:0e018d759a2a, 2016-11-08 (annotated)
- Committer:
- switches
- Date:
- Tue Nov 08 18:27:11 2016 +0000
- Revision:
- 0:0e018d759a2a
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:0e018d759a2a | 1 | #include "mbed.h" |
switches | 0:0e018d759a2a | 2 | #include "greentea-client/test_env.h" |
switches | 0:0e018d759a2a | 3 | #include "rtos.h" |
switches | 0:0e018d759a2a | 4 | |
switches | 0:0e018d759a2a | 5 | #if defined(MBED_RTOS_SINGLE_THREAD) |
switches | 0:0e018d759a2a | 6 | #error [NOT_SUPPORTED] test not supported |
switches | 0:0e018d759a2a | 7 | #endif |
switches | 0:0e018d759a2a | 8 | |
switches | 0:0e018d759a2a | 9 | typedef struct { |
switches | 0:0e018d759a2a | 10 | float voltage; /* AD result of measured voltage */ |
switches | 0:0e018d759a2a | 11 | float current; /* AD result of measured current */ |
switches | 0:0e018d759a2a | 12 | uint32_t counter; /* A counter value */ |
switches | 0:0e018d759a2a | 13 | } message_t; |
switches | 0:0e018d759a2a | 14 | |
switches | 0:0e018d759a2a | 15 | #define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33 |
switches | 0:0e018d759a2a | 16 | #define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11 |
switches | 0:0e018d759a2a | 17 | #define QUEUE_SIZE 16 |
switches | 0:0e018d759a2a | 18 | #define QUEUE_PUT_DELAY 100 |
switches | 0:0e018d759a2a | 19 | |
switches | 0:0e018d759a2a | 20 | /* |
switches | 0:0e018d759a2a | 21 | * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and |
switches | 0:0e018d759a2a | 22 | * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes |
switches | 0:0e018d759a2a | 23 | * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. |
switches | 0:0e018d759a2a | 24 | */ |
switches | 0:0e018d759a2a | 25 | #if (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:0e018d759a2a | 26 | #define STACK_SIZE 512 |
switches | 0:0e018d759a2a | 27 | #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:0e018d759a2a | 28 | #define STACK_SIZE 768 |
switches | 0:0e018d759a2a | 29 | #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO) |
switches | 0:0e018d759a2a | 30 | #define STACK_SIZE 1536 |
switches | 0:0e018d759a2a | 31 | #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832) |
switches | 0:0e018d759a2a | 32 | #define STACK_SIZE 768 |
switches | 0:0e018d759a2a | 33 | #elif defined(TARGET_XDOT_L151CC) |
switches | 0:0e018d759a2a | 34 | #define STACK_SIZE 1024 |
switches | 0:0e018d759a2a | 35 | #else |
switches | 0:0e018d759a2a | 36 | #define STACK_SIZE DEFAULT_STACK_SIZE |
switches | 0:0e018d759a2a | 37 | #endif |
switches | 0:0e018d759a2a | 38 | |
switches | 0:0e018d759a2a | 39 | MemoryPool<message_t, QUEUE_SIZE> mpool; |
switches | 0:0e018d759a2a | 40 | Queue<message_t, QUEUE_SIZE> queue; |
switches | 0:0e018d759a2a | 41 | |
switches | 0:0e018d759a2a | 42 | /* Send Thread */ |
switches | 0:0e018d759a2a | 43 | void send_thread (void const *argument) { |
switches | 0:0e018d759a2a | 44 | static uint32_t i = 10; |
switches | 0:0e018d759a2a | 45 | while (true) { |
switches | 0:0e018d759a2a | 46 | i++; // Fake data update |
switches | 0:0e018d759a2a | 47 | message_t *message = mpool.alloc(); |
switches | 0:0e018d759a2a | 48 | message->voltage = CREATE_VOLTAGE(i); |
switches | 0:0e018d759a2a | 49 | message->current = CREATE_CURRENT(i); |
switches | 0:0e018d759a2a | 50 | message->counter = i; |
switches | 0:0e018d759a2a | 51 | queue.put(message); |
switches | 0:0e018d759a2a | 52 | Thread::wait(QUEUE_PUT_DELAY); |
switches | 0:0e018d759a2a | 53 | } |
switches | 0:0e018d759a2a | 54 | } |
switches | 0:0e018d759a2a | 55 | |
switches | 0:0e018d759a2a | 56 | int main (void) { |
switches | 0:0e018d759a2a | 57 | GREENTEA_SETUP(20, "default_auto"); |
switches | 0:0e018d759a2a | 58 | |
switches | 0:0e018d759a2a | 59 | Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); |
switches | 0:0e018d759a2a | 60 | bool result = true; |
switches | 0:0e018d759a2a | 61 | int result_counter = 0; |
switches | 0:0e018d759a2a | 62 | |
switches | 0:0e018d759a2a | 63 | while (true) { |
switches | 0:0e018d759a2a | 64 | osEvent evt = queue.get(); |
switches | 0:0e018d759a2a | 65 | if (evt.status == osEventMessage) { |
switches | 0:0e018d759a2a | 66 | message_t *message = (message_t*)evt.value.p; |
switches | 0:0e018d759a2a | 67 | const float expected_voltage = CREATE_VOLTAGE(message->counter); |
switches | 0:0e018d759a2a | 68 | const float expected_current = CREATE_CURRENT(message->counter); |
switches | 0:0e018d759a2a | 69 | // Check using macros if received values correspond to values sent via queue |
switches | 0:0e018d759a2a | 70 | bool expected_values = (expected_voltage == message->voltage) && |
switches | 0:0e018d759a2a | 71 | (expected_current == message->current); |
switches | 0:0e018d759a2a | 72 | result = result && expected_values; |
switches | 0:0e018d759a2a | 73 | const char *result_msg = expected_values ? "OK" : "FAIL"; |
switches | 0:0e018d759a2a | 74 | printf("%3d %.2fV %.2fA ... [%s]\r\n", message->counter, |
switches | 0:0e018d759a2a | 75 | message->voltage, |
switches | 0:0e018d759a2a | 76 | message->current, |
switches | 0:0e018d759a2a | 77 | result_msg); |
switches | 0:0e018d759a2a | 78 | mpool.free(message); |
switches | 0:0e018d759a2a | 79 | if (result == false || ++result_counter == QUEUE_SIZE) { |
switches | 0:0e018d759a2a | 80 | break; |
switches | 0:0e018d759a2a | 81 | } |
switches | 0:0e018d759a2a | 82 | } |
switches | 0:0e018d759a2a | 83 | } |
switches | 0:0e018d759a2a | 84 | GREENTEA_TESTSUITE_RESULT(result); |
switches | 0:0e018d759a2a | 85 | return 0; |
switches | 0:0e018d759a2a | 86 | } |