diff options
Diffstat (limited to 'tool/mbed/mbed-sdk/libraries/tests/rtos')
17 files changed, 881 insertions, 0 deletions
diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/basic/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/basic/main.cpp new file mode 100644 index 0000000000..d78a49fcf6 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/basic/main.cpp @@ -0,0 +1,22 @@ +#include "mbed.h" +#include "cmsis_os.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); + +void led2_thread(void const *argument) { + while (true) { + led2 = !led2; + osDelay(1000); + } +} +osThreadDef(led2_thread, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main() { + osThreadCreate(osThread(led2_thread), NULL); + + while (true) { + led1 = !led1; + osDelay(500); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/isr/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/isr/main.cpp new file mode 100644 index 0000000000..52c7da3d8c --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/isr/main.cpp @@ -0,0 +1,34 @@ +#include "mbed.h" +#include "rtos.h" + +Queue<uint32_t, 5> queue; + +DigitalOut myled(LED1); + +void queue_isr() { + queue.put((uint32_t*)2); + myled = !myled; +} + +void queue_thread(void const *argument) { + while (true) { + queue.put((uint32_t*)1); + Thread::wait(1000); + } +} + +int main (void) { + Thread thread(queue_thread); + + Ticker ticker; + ticker.attach(queue_isr, 1.0); + + while (true) { + osEvent evt = queue.get(); + if (evt.status != osEventMessage) { + printf("queue->get() returned %02x status\n\r", evt.status); + } else { + printf("queue->get() returned %d\n\r", evt.value.v); + } + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mail/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mail/main.cpp new file mode 100644 index 0000000000..545fca34c7 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mail/main.cpp @@ -0,0 +1,43 @@ +#include "mbed.h" +#include "cmsis_os.h" + +typedef struct { + float voltage; /* AD result of measured voltage */ + float current; /* AD result of measured current */ + uint32_t counter; /* A counter value */ +} mail_t; + +osMailQDef(mail_box, 16, mail_t); +osMailQId mail_box; + +void send_thread (void const *argument) { + uint32_t i = 0; + while (true) { + i++; // fake data update + mail_t *mail = (mail_t*)osMailAlloc(mail_box, osWaitForever); + mail->voltage = (i * 0.1) * 33; + mail->current = (i * 0.1) * 11; + mail->counter = i; + osMailPut(mail_box, mail); + osDelay(1000); + } +} + +osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main (void) { + mail_box = osMailCreate(osMailQ(mail_box), NULL); + osThreadCreate(osThread(send_thread), NULL); + + while (true) { + osEvent evt = osMailGet(mail_box, osWaitForever); + if (evt.status == osEventMail) { + mail_t *mail = (mail_t*)evt.value.p; + printf("\nVoltage: %.2f V\n\r" , mail->voltage); + printf("Current: %.2f A\n\r" , mail->current); + printf("Number of cycles: %u\n\r", mail->counter); + + osMailFree(mail_box, mail); + } + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mutex/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mutex/main.cpp new file mode 100644 index 0000000000..ccd1a603ca --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mutex/main.cpp @@ -0,0 +1,33 @@ +#include "mbed.h" +#include "cmsis_os.h" + +osMutexId stdio_mutex; +osMutexDef(stdio_mutex); + +void notify(const char* name, int state) { + osMutexWait(stdio_mutex, osWaitForever); + printf("%s: %d\n\r", name, state); + osMutexRelease(stdio_mutex); +} + +void test_thread(void const *args) { + while (true) { + notify((const char*)args, 0); osDelay(1000); + notify((const char*)args, 1); osDelay(1000); + } +} + +void t2(void const *argument) {test_thread("Th 2");} +osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE); + +void t3(void const *argument) {test_thread("Th 3");} +osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main() { + stdio_mutex = osMutexCreate(osMutex(stdio_mutex)); + + osThreadCreate(osThread(t2), NULL); + osThreadCreate(osThread(t3), NULL); + + test_thread((void *)"Th 1"); +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/queue/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/queue/main.cpp new file mode 100644 index 0000000000..1c6eab2f39 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/queue/main.cpp @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "cmsis_os.h" + +typedef struct { + float voltage; /* AD result of measured voltage */ + float current; /* AD result of measured current */ + uint32_t counter; /* A counter value */ +} message_t; + +osPoolDef(mpool, 16, message_t); +osPoolId mpool; + +osMessageQDef(queue, 16, message_t); +osMessageQId queue; + +void send_thread (void const *argument) { + uint32_t i = 0; + while (true) { + i++; // fake data update + message_t *message = (message_t*)osPoolAlloc(mpool); + message->voltage = (i * 0.1) * 33; + message->current = (i * 0.1) * 11; + message->counter = i; + osMessagePut(queue, (uint32_t)message, osWaitForever); + osDelay(1000); + } +} + +osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main (void) { + mpool = osPoolCreate(osPool(mpool)); + queue = osMessageCreate(osMessageQ(queue), NULL); + + osThreadCreate(osThread(send_thread), NULL); + + while (true) { + osEvent evt = osMessageGet(queue, osWaitForever); + if (evt.status == osEventMessage) { + message_t *message = (message_t*)evt.value.p; + printf("\nVoltage: %.2f V\n\r" , message->voltage); + printf("Current: %.2f A\n\r" , message->current); + printf("Number of cycles: %u\n\r", message->counter); + + osPoolFree(mpool, message); + } + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/semaphore/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/semaphore/main.cpp new file mode 100644 index 0000000000..d4b473c187 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/semaphore/main.cpp @@ -0,0 +1,29 @@ +#include "mbed.h" +#include "cmsis_os.h" + +osSemaphoreId two_slots; +osSemaphoreDef(two_slots); + +void test_thread(void const *name) { + while (true) { + osSemaphoreWait(two_slots, osWaitForever); + printf("%s\n\r", (const char*)name); + osDelay(1000); + osSemaphoreRelease(two_slots); + } +} + +void t2(void const *argument) {test_thread("Th 2");} +osThreadDef(t2, osPriorityNormal, DEFAULT_STACK_SIZE); + +void t3(void const *argument) {test_thread("Th 3");} +osThreadDef(t3, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main (void) { + two_slots = osSemaphoreCreate(osSemaphore(two_slots), 2); + + osThreadCreate(osThread(t2), NULL); + osThreadCreate(osThread(t3), NULL); + + test_thread((void *)"Th 1"); +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/signals/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/signals/main.cpp new file mode 100644 index 0000000000..14ce05dadb --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/signals/main.cpp @@ -0,0 +1,23 @@ +#include "mbed.h" +#include "cmsis_os.h" + +DigitalOut led(LED1); + +void led_thread(void const *argument) { + while (true) { + // Signal flags that are reported as event are automatically cleared. + osSignalWait(0x1, osWaitForever); + led = !led; + } +} + +osThreadDef(led_thread, osPriorityNormal, DEFAULT_STACK_SIZE); + +int main (void) { + osThreadId tid = osThreadCreate(osThread(led_thread), NULL); + + while (true) { + osDelay(1000); + osSignalSet(tid, 0x1); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/timer/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/timer/main.cpp new file mode 100644 index 0000000000..a0b093f946 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/timer/main.cpp @@ -0,0 +1,29 @@ +#include "mbed.h" +#include "cmsis_os.h" + +DigitalOut LEDs[4] = { + DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) +}; + +void blink(void const *n) { + LEDs[(int)n] = !LEDs[(int)n]; +} + +osTimerDef(blink_0, blink); +osTimerDef(blink_1, blink); +osTimerDef(blink_2, blink); +osTimerDef(blink_3, blink); + +int main(void) { + osTimerId timer_0 = osTimerCreate(osTimer(blink_0), osTimerPeriodic, (void *)0); + osTimerId timer_1 = osTimerCreate(osTimer(blink_1), osTimerPeriodic, (void *)1); + osTimerId timer_2 = osTimerCreate(osTimer(blink_2), osTimerPeriodic, (void *)2); + osTimerId timer_3 = osTimerCreate(osTimer(blink_3), osTimerPeriodic, (void *)3); + + osTimerStart(timer_0, 2000); + osTimerStart(timer_1, 1000); + osTimerStart(timer_2, 500); + osTimerStart(timer_3, 250); + + osDelay(osWaitForever); +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/basic/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/basic/main.cpp new file mode 100644 index 0000000000..541436786c --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/basic/main.cpp @@ -0,0 +1,44 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) +#define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else +#define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +void print_char(char c = '*') { + printf("%c", c); + fflush(stdout); +} + +DigitalOut led1(LED1); +DigitalOut led2(LED2); + +void led2_thread(void const *argument) { + while (true) { + led2 = !led2; + Thread::wait(1000); + print_char(); + } +} + +int main() { + MBED_HOSTTEST_TIMEOUT(15); + MBED_HOSTTEST_SELECT(wait_us_auto); + MBED_HOSTTEST_DESCRIPTION(Basic thread); + MBED_HOSTTEST_START("RTOS_1"); + + Thread thread(led2_thread, NULL, osPriorityNormal, STACK_SIZE); + + while (true) { + led1 = !led1; + Thread::wait(500); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/file/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/file/main.cpp new file mode 100644 index 0000000000..af32f668d6 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/file/main.cpp @@ -0,0 +1,100 @@ +#include "mbed.h" +#include "SDFileSystem.h" +#include "test_env.h" +#include "rtos.h" + +DigitalOut led2(LED2); + +#define SIZE 100 + +namespace { +// Allocate data buffers +uint8_t data_written[SIZE] = { 0 }; +uint8_t data_read[SIZE] = { 0 }; +} + +void sd_thread(void const *argument) +{ + const char *FILE_NAME = "/sd/rtos9_test.txt"; + +#if defined(TARGET_KL25Z) + SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); + +#elif defined(TARGET_KL46Z) + SDFileSystem sd(PTD6, PTD7, PTD5, PTD4, "sd"); + +#elif defined(TARGET_K64F) + SDFileSystem sd(PTD2, PTD3, PTD1, PTD0, "sd"); + +#elif defined(TARGET_RZ_A1H) + SDFileSystem sd(P8_5, P8_6, P8_3, P8_4, "sd"); + +#else + SDFileSystem sd(p11, p12, p13, p14, "sd"); +#endif + + { + // fill data_written buffer with random data + FILE *f = fopen(FILE_NAME, "w+"); + if (f) { + // write these data into the file + printf("Writing %d bytes to file:" NL, SIZE); + for (int i = 0; i < SIZE; i++) { + data_written[i] = rand() % 0xff; + fprintf(f, "%c", data_written[i]); + printf("%02X ", data_written[i]); + if (i && ((i % 20) == 19)) + printf(NL); + } + fclose(f); + printf("MBED: Done" NL); + } else { + printf("MBED: Can't open '%s'" NL, FILE_NAME); + MBED_HOSTTEST_RESULT(false); + } + } + + printf(NL); + + { + // read back the data from the file and store them in data_read + FILE *f = fopen(FILE_NAME, "r"); + if (f) { + printf("MBED: Reading %d bytes from file:" NL, SIZE); + for (int i = 0; i < SIZE; i++) { + data_read[i] = fgetc(f); + printf("%02X ", data_read[i]); + if (i && ((i % 20) == 19)) + printf(NL); + } + fclose(f); + printf("MBED: Done\r\n"); + } else { + printf("MBED: Can't open '%s'" NL, FILE_NAME); + MBED_HOSTTEST_RESULT(false); + } + } + + // check that the data written == data read + for (int i = 0; i < SIZE; i++) { + if (data_written[i] != data_read[i]) { + printf("MBED: Data index=%d: w[0x%02X] != r[0x%02X]" NL, i, data_written[i], data_read[i]); + MBED_HOSTTEST_RESULT(false); + } + } + MBED_HOSTTEST_RESULT(true); +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(SD File write read); + MBED_HOSTTEST_START("RTOS_9"); + + Thread t(sd_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25)); + + while (true) { + led2 = !led2; + Thread::wait(1000); + } +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/isr/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/isr/main.cpp new file mode 100644 index 0000000000..7168564fa9 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/isr/main.cpp @@ -0,0 +1,69 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +#define QUEUE_SIZE 5 +#define THREAD_DELAY 250 +#define QUEUE_PUT_ISR_VALUE 128 +#define QUEUE_PUT_THREAD_VALUE 127 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +Queue<uint32_t, QUEUE_SIZE> queue; + +DigitalOut myled(LED1); + +void queue_isr() { + + queue.put((uint32_t*)QUEUE_PUT_ISR_VALUE); + myled = !myled; +} + +void queue_thread(void const *argument) { + while (true) { + queue.put((uint32_t*)QUEUE_PUT_THREAD_VALUE); + Thread::wait(THREAD_DELAY); + } +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(ISR (Queue)); + MBED_HOSTTEST_START("RTOS_8"); + + Thread thread(queue_thread, NULL, osPriorityNormal, STACK_SIZE); + Ticker ticker; + ticker.attach(queue_isr, 1.0); + int isr_puts_counter = 0; + bool result = true; + + while (true) { + osEvent evt = queue.get(); + if (evt.status != osEventMessage) { + printf("QUEUE_GET: Status(0x%02X) ... [FAIL]\r\n", evt.status); + result = false; + break; + } else { + printf("QUEUE_GET: Value(%u) ... [OK]\r\n", evt.value.v); + if (evt.value.v == QUEUE_PUT_ISR_VALUE) { + isr_puts_counter++; + } + if (isr_puts_counter >= QUEUE_SIZE) { + break; + } + } + } + + MBED_HOSTTEST_RESULT(result); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mail/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mail/main.cpp new file mode 100644 index 0000000000..105d3eafbf --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mail/main.cpp @@ -0,0 +1,75 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +typedef struct { + float voltage; /* AD result of measured voltage */ + float current; /* AD result of measured current */ + uint32_t counter; /* A counter value */ +} mail_t; + +#define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33 +#define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11 +#define QUEUE_SIZE 16 +#define QUEUE_PUT_DELAY 100 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +Mail<mail_t, QUEUE_SIZE> mail_box; + +void send_thread (void const *argument) { + static uint32_t i = 10; + while (true) { + i++; // fake data update + mail_t *mail = mail_box.alloc(); + mail->voltage = CREATE_VOLTAGE(i); + mail->current = CREATE_CURRENT(i); + mail->counter = i; + mail_box.put(mail); + Thread::wait(QUEUE_PUT_DELAY); + } +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Mail messaging); + MBED_HOSTTEST_START("RTOS_6"); + + Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); + bool result = true; + int result_counter = 0; + + while (true) { + osEvent evt = mail_box.get(); + if (evt.status == osEventMail) { + mail_t *mail = (mail_t*)evt.value.p; + const float expected_voltage = CREATE_VOLTAGE(mail->counter); + const float expected_current = CREATE_CURRENT(mail->counter); + // Check using macros if received values correspond to values sent via queue + bool expected_values = (expected_voltage == mail->voltage) && + (expected_current == mail->current); + result = result && expected_values; + const char *result_msg = expected_values ? "OK" : "FAIL"; + printf("%3d %.2fV %.2fA ... [%s]\r\n", mail->counter, + mail->voltage, + mail->current, + result_msg); + mail_box.free(mail); + if (result == false || ++result_counter == QUEUE_SIZE) { + break; + } + } + } + MBED_HOSTTEST_RESULT(result); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mutex/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mutex/main.cpp new file mode 100644 index 0000000000..80d49a189c --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/mutex/main.cpp @@ -0,0 +1,87 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +#define THREAD_DELAY 50 +#define SIGNALS_TO_EMIT 100 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +void print_char(char c = '*') { + printf("%c", c); + fflush(stdout); +} + +Mutex stdio_mutex; +DigitalOut led(LED1); + +volatile int change_counter = 0; +volatile bool changing_counter = false; +volatile bool mutex_defect = false; + +bool manipulate_protected_zone(const int thread_delay) { + bool result = true; + + stdio_mutex.lock(); // LOCK + if (changing_counter == true) { + // 'e' stands for error. If changing_counter is true access is not exclusively + print_char('e'); + result = false; + mutex_defect = true; + } + changing_counter = true; + + // Some action on protected + led = !led; + change_counter++; + print_char('.'); + Thread::wait(thread_delay); + + changing_counter = false; + stdio_mutex.unlock(); // UNLOCK + return result; +} + +void test_thread(void const *args) { + const int thread_delay = int(args); + while (true) { + manipulate_protected_zone(thread_delay); + } +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default); + MBED_HOSTTEST_DESCRIPTION(Mutex resource lock); + MBED_HOSTTEST_START("RTOS_2"); + + const int t1_delay = THREAD_DELAY * 1; + const int t2_delay = THREAD_DELAY * 2; + const int t3_delay = THREAD_DELAY * 3; + Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); + Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); + + while (true) { + // Thread 1 action + Thread::wait(t1_delay); + manipulate_protected_zone(t1_delay); + if (change_counter >= SIGNALS_TO_EMIT or mutex_defect == true) { + t2.terminate(); + t3.terminate(); + break; + } + } + + fflush(stdout); + MBED_HOSTTEST_RESULT(!mutex_defect); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/queue/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/queue/main.cpp new file mode 100644 index 0000000000..4f794bc4b8 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/queue/main.cpp @@ -0,0 +1,77 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +typedef struct { + float voltage; /* AD result of measured voltage */ + float current; /* AD result of measured current */ + uint32_t counter; /* A counter value */ +} message_t; + +#define CREATE_VOLTAGE(COUNTER) (COUNTER * 0.1) * 33 +#define CREATE_CURRENT(COUNTER) (COUNTER * 0.1) * 11 +#define QUEUE_SIZE 16 +#define QUEUE_PUT_DELAY 100 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +MemoryPool<message_t, QUEUE_SIZE> mpool; +Queue<message_t, QUEUE_SIZE> queue; + +/* Send Thread */ +void send_thread (void const *argument) { + static uint32_t i = 10; + while (true) { + i++; // Fake data update + message_t *message = mpool.alloc(); + message->voltage = CREATE_VOLTAGE(i); + message->current = CREATE_CURRENT(i); + message->counter = i; + queue.put(message); + Thread::wait(QUEUE_PUT_DELAY); + } +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Queue messaging); + MBED_HOSTTEST_START("RTOS_5"); + + Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); + bool result = true; + int result_counter = 0; + + while (true) { + osEvent evt = queue.get(); + if (evt.status == osEventMessage) { + message_t *message = (message_t*)evt.value.p; + const float expected_voltage = CREATE_VOLTAGE(message->counter); + const float expected_current = CREATE_CURRENT(message->counter); + // Check using macros if received values correspond to values sent via queue + bool expected_values = (expected_voltage == message->voltage) && + (expected_current == message->current); + result = result && expected_values; + const char *result_msg = expected_values ? "OK" : "FAIL"; + printf("%3d %.2fV %.2fA ... [%s]\r\n", message->counter, + message->voltage, + message->current, + result_msg); + mpool.free(message); + if (result == false || ++result_counter == QUEUE_SIZE) { + break; + } + } + } + MBED_HOSTTEST_RESULT(result); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/semaphore/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/semaphore/main.cpp new file mode 100644 index 0000000000..1f35e9f473 --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/semaphore/main.cpp @@ -0,0 +1,75 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +#define THREAD_DELAY 75 +#define SEMAPHORE_SLOTS 2 +#define SEM_CHANGES 100 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +void print_char(char c = '*') { + printf("%c", c); + fflush(stdout); +} + +Semaphore two_slots(SEMAPHORE_SLOTS); + +volatile int change_counter = 0; +volatile int sem_counter = 0; +volatile bool sem_defect = false; + +void test_thread(void const *delay) { + const int thread_delay = int(delay); + while (true) { + two_slots.wait(); + sem_counter++; + const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS; + const char msg = sem_lock_failed ? 'e' : sem_counter + '0'; + print_char(msg); + if (sem_lock_failed) { + sem_defect = true; + } + Thread::wait(thread_delay); + print_char('.'); + sem_counter--; + change_counter++; + two_slots.release(); + } +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Semaphore resource lock); + MBED_HOSTTEST_START("RTOS_3"); + + const int t1_delay = THREAD_DELAY * 1; + const int t2_delay = THREAD_DELAY * 2; + const int t3_delay = THREAD_DELAY * 3; + Thread t1(test_thread, (void *)t1_delay, osPriorityNormal, STACK_SIZE); + Thread t2(test_thread, (void *)t2_delay, osPriorityNormal, STACK_SIZE); + Thread t3(test_thread, (void *)t3_delay, osPriorityNormal, STACK_SIZE); + + while (true) { + if (change_counter >= SEM_CHANGES or sem_defect == true) { + t1.terminate(); + t2.terminate(); + t3.terminate(); + break; + } + } + + fflush(stdout); + MBED_HOSTTEST_RESULT(!sem_defect); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/signals/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/signals/main.cpp new file mode 100644 index 0000000000..aefe72f93d --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/signals/main.cpp @@ -0,0 +1,51 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +#define SIGNALS_TO_EMIT 100 +#define SIGNAL_HANDLE_DELEY 25 +#define SIGNAL_SET_VALUE 0x01 + +/* + * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and + * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes + * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize. + */ +#if defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8) + #define STACK_SIZE DEFAULT_STACK_SIZE/4 +#else + #define STACK_SIZE DEFAULT_STACK_SIZE +#endif + +DigitalOut led(LED1); +volatile int signal_counter = 0; + +void led_thread(void const *argument) { + while (true) { + // Signal flags that are reported as event are automatically cleared. + Thread::signal_wait(SIGNAL_SET_VALUE); + led = !led; + signal_counter++; + } +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(default_auto); + MBED_HOSTTEST_DESCRIPTION(Signals messaging); + MBED_HOSTTEST_START("RTOS_4"); + + Thread thread(led_thread, NULL, osPriorityNormal, STACK_SIZE); + bool result = true; + + while (true) { + Thread::wait(2 * SIGNAL_HANDLE_DELEY); + thread.signal_set(SIGNAL_SET_VALUE); + if (signal_counter == SIGNALS_TO_EMIT) { + printf("Handled %d signals\r\n", signal_counter); + break; + } + } + MBED_HOSTTEST_RESULT(result); + return 0; +} diff --git a/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/timer/main.cpp b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/timer/main.cpp new file mode 100644 index 0000000000..3c33551b0a --- /dev/null +++ b/tool/mbed/mbed-sdk/libraries/tests/rtos/mbed/timer/main.cpp @@ -0,0 +1,42 @@ +#include "mbed.h" +#include "test_env.h" +#include "rtos.h" + +DigitalOut LEDs[4] = { + DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) +}; + +void print_char(char c = '*') +{ + printf("%c", c); + fflush(stdout); +} + +void blink(void const *n) { + static int counter = 0; + const int led_id = int(n); + LEDs[led_id] = !LEDs[led_id]; + if (++counter == 75) { + print_char(); + counter = 0; + } +} + +int main(void) { + MBED_HOSTTEST_TIMEOUT(15); + MBED_HOSTTEST_SELECT(wait_us_auto); + MBED_HOSTTEST_DESCRIPTION(Timer); + MBED_HOSTTEST_START("RTOS_7"); + + RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0); + RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1); + RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2); + RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3); + + led_1_timer.start(200); + led_2_timer.start(100); + led_3_timer.start(50); + led_4_timer.start(25); + + Thread::wait(osWaitForever); +} |