]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mail/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / rtos / mbed / mail / main.cpp
1 #include "mbed.h"
2 #include "test_env.h"
3 #include "rtos.h"
4
5 typedef struct {
6     float    voltage;   /* AD result of measured voltage */
7     float    current;   /* AD result of measured current */
8     uint32_t counter;   /* A counter value               */
9 } mail_t;
10
11 #define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33
12 #define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11
13 #define QUEUE_SIZE       16
14 #define QUEUE_PUT_DELAY  100
15
16 /*
17  * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
18  * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
19  * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
20  */
21 #if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
22     #define STACK_SIZE DEFAULT_STACK_SIZE/4
23 #else
24     #define STACK_SIZE DEFAULT_STACK_SIZE
25 #endif
26
27 Mail<mail_t, QUEUE_SIZE> mail_box;
28
29 void send_thread (void const *argument) {
30     static uint32_t i = 10;
31     while (true) {
32         i++; // fake data update
33         mail_t *mail = mail_box.alloc();
34         mail->voltage = CREATE_VOLTAGE(i);
35         mail->current = CREATE_CURRENT(i);
36         mail->counter = i;
37         mail_box.put(mail);
38         Thread::wait(QUEUE_PUT_DELAY);
39     }
40 }
41
42 int main (void) {
43     MBED_HOSTTEST_TIMEOUT(20);
44     MBED_HOSTTEST_SELECT(default_auto);
45     MBED_HOSTTEST_DESCRIPTION(Mail messaging);
46     MBED_HOSTTEST_START("RTOS_6");
47
48     Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE);
49     bool result = true;
50     int result_counter = 0;
51
52     while (true) {
53         osEvent evt = mail_box.get();
54         if (evt.status == osEventMail) {
55             mail_t *mail = (mail_t*)evt.value.p;
56             const float expected_voltage = CREATE_VOLTAGE(mail->counter);
57             const float expected_current = CREATE_CURRENT(mail->counter);
58             // Check using macros if received values correspond to values sent via queue
59             bool expected_values = (expected_voltage == mail->voltage) &&
60                                    (expected_current == mail->current);
61             result = result && expected_values;
62             const char *result_msg = expected_values ? "OK" : "FAIL";
63             printf("%3d %.2fV %.2fA ... [%s]\r\n", mail->counter,
64                                                    mail->voltage,
65                                                    mail->current,
66                                                    result_msg);
67             mail_box.free(mail);
68             if (result == false || ++result_counter == QUEUE_SIZE) {
69                 break;
70             }
71         }
72     }
73     MBED_HOSTTEST_RESULT(result);
74     return 0;
75 }