Skip to content

Add RTOS examples #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions APIs_RTOS/Kernel_get_ms_count/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Kernel Hooks example

The example shows how to use the `Kernel::get_ms_count()` function to read the current RTOS kernel millisecond tick count.
17 changes: 17 additions & 0 deletions APIs_RTOS/Kernel_get_ms_count/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "mbed.h"

int main()
{
// 64-bit time doesn't wrap for half a billion years, at least
uint64_t now = Kernel::get_ms_count();

// wait a while
ThisThread::sleep_for(10);

uint64_t later = Kernel::get_ms_count();

//calculate millisecs elapsed
uint64_t elapsed_ms = later - now;

printf("Elapsed ms: %u \r\n", (uint32_t)elapsed_ms);
}
3 changes: 3 additions & 0 deletions APIs_RTOS/Kernel_hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Kernel Hooks example

The example shows how to use the `function attach_idle_hook()` to attach a function to be called by the RTOS idle task and the function `attach_thread_terminate_hook()` to attach a function to be called when a thread terminates.
46 changes: 46 additions & 0 deletions APIs_RTOS/Kernel_hooks/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "mbed.h"
#include "Kernel.h"

/* Entry function for test thread */
void test_thread(void)
{
printf("\ntest thread started");
ThisThread::sleep_for(2000);
printf("\ntest thread exiting");
}

/* Hook function for thread terminate */
void thread_terminate_hook(osThreadId_t id)
{
printf("\ntest thread terminated");
}

/* Hook function for idle task */
void test_idle_hook(void)
{
printf("\nidle hook invoked\n");
//Now remove this hook and re-instate the original hook by passing a NULL for the idle hook funtion pointer
Kernel::attach_idle_hook(NULL);
}

// main() runs in its own thread in the OS
int main()
{

printf("\nattach_thread_terminate_hook demo:\n");

//Attach a hook for thread terminate event
Kernel::attach_thread_terminate_hook(thread_terminate_hook);
Thread *newThread = new Thread(osPriorityNormal1, 1024, NULL, NULL);
newThread->start(callback(test_thread));
ThisThread::sleep_for(4000);

printf("\n\nattach_idle_hook demo:\n");
//Attach a hook for idle task
Kernel::attach_idle_hook(test_idle_hook);

while (1) {
ThisThread::sleep_for(3000);
}
printf("\n\n");
}
15 changes: 8 additions & 7 deletions APIs_RTOS/Queue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Queue<message_t, 16> queue;
Thread thread;

/* Send Thread */
void send_thread (void) {
void send_thread(void)
{
uint32_t i = 0;
while (true) {
i++; // fake data update
Expand All @@ -24,17 +25,17 @@ void send_thread (void) {
}
}

int main (void) {
int main(void)
{
thread.start(callback(send_thread));

while (true) {
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
message_t *message = (message_t*)evt.value.p;
printf("\r\n");
printf("Voltage: %.2f V\r\n" , message->voltage);
printf("Current: %.2f A\r\n" , message->current);
printf("Number of cycles: %u\r\n", message->counter);
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);

mpool.free(message);
}
Expand Down