nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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