WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "rtos.h"
00004 
00005 #if defined(MBED_RTOS_SINGLE_THREAD)
00006   #error [NOT_SUPPORTED] test not supported
00007 #endif
00008 
00009 typedef struct {
00010     float    voltage;   /* AD result of measured voltage */
00011     float    current;   /* AD result of measured current */
00012     uint32_t counter;   /* A counter value               */
00013 } mail_t;
00014 
00015 #define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33
00016 #define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11
00017 #define QUEUE_SIZE       16
00018 #define QUEUE_PUT_DELAY  100
00019 
00020 /*
00021  * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
00022  * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
00023  * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
00024  */
00025 #if (defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)) && defined(TOOLCHAIN_GCC)
00026     #define STACK_SIZE DEFAULT_STACK_SIZE/4
00027 #elif (defined(TARGET_STM32F030R8)) && defined(TOOLCHAIN_GCC)
00028     #define STACK_SIZE DEFAULT_STACK_SIZE/2
00029 #elif (defined(TARGET_STM32F030R8)) && defined(TOOLCHAIN_IAR)
00030     #define STACK_SIZE DEFAULT_STACK_SIZE/2
00031 #elif (defined(TARGET_EFM32HG_STK3400)) && !defined(TOOLCHAIN_ARM_MICRO)
00032     #define STACK_SIZE 512
00033 #elif (defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)) && !defined(TOOLCHAIN_ARM_MICRO)
00034     #define STACK_SIZE 768
00035 #elif (defined(TARGET_EFM32GG_STK3700)) && !defined(TOOLCHAIN_ARM_MICRO)
00036     #define STACK_SIZE 1536
00037 #elif defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
00038     #define STACK_SIZE 768
00039 #else
00040     #define STACK_SIZE DEFAULT_STACK_SIZE
00041 #endif
00042 
00043 Mail<mail_t, QUEUE_SIZE> mail_box;
00044 
00045 void send_thread (void const *argument) {
00046     static uint32_t i = 10;
00047     while (true) {
00048         i++; // fake data update
00049         mail_t *mail = mail_box.alloc();
00050         mail->voltage = CREATE_VOLTAGE(i);
00051         mail->current = CREATE_CURRENT(i);
00052         mail->counter = i;
00053         mail_box.put(mail);
00054         Thread::wait(QUEUE_PUT_DELAY);
00055     }
00056 }
00057 
00058 int main (void) {
00059     GREENTEA_SETUP(20, "default_auto");
00060 
00061     Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE);
00062     bool result = true;
00063     int result_counter = 0;
00064 
00065     while (true) {
00066         osEvent evt = mail_box.get();
00067         if (evt.status == osEventMail) {
00068             mail_t *mail = (mail_t*)evt.value.p;
00069             const float expected_voltage = CREATE_VOLTAGE(mail->counter);
00070             const float expected_current = CREATE_CURRENT(mail->counter);
00071             // Check using macros if received values correspond to values sent via queue
00072             bool expected_values = (expected_voltage == mail->voltage) &&
00073                                    (expected_current == mail->current);
00074             result = result && expected_values;
00075             const char *result_msg = expected_values ? "OK" : "FAIL";
00076             printf("%3d %.2fV %.2fA ... [%s]\r\n", mail->counter,
00077                                                    mail->voltage,
00078                                                    mail->current,
00079                                                    result_msg);
00080             mail_box.free(mail);
00081             if (result == false || ++result_counter == QUEUE_SIZE) {
00082                 break;
00083             }
00084         }
00085     }
00086     GREENTEA_TESTSUITE_RESULT(result);
00087     return 0;
00088 }