diff options
Diffstat (limited to 'tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis')
8 files changed, 261 insertions, 0 deletions
diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/basic/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/basic/main.cpp new file mode 100644 index 0000000000..d78a49fcf6 --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/isr/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/isr/main.cpp new file mode 100644 index 0000000000..52c7da3d8c --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mail/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mail/main.cpp new file mode 100644 index 0000000000..545fca34c7 --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mutex/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/mutex/main.cpp new file mode 100644 index 0000000000..ccd1a603ca --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/queue/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/queue/main.cpp new file mode 100644 index 0000000000..1c6eab2f39 --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/semaphore/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/semaphore/main.cpp new file mode 100644 index 0000000000..d4b473c187 --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/signals/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/signals/main.cpp new file mode 100644 index 0000000000..14ce05dadb --- /dev/null +++ b/tmk_core/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/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/timer/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/rtos/cmsis/timer/main.cpp new file mode 100644 index 0000000000..a0b093f946 --- /dev/null +++ b/tmk_core/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); +} |