ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ns_event_loop.c Source File

ns_event_loop.c

00001 /*
00002  * Copyright (c) 2016 ARM Limited, All Rights Reserved
00003  */
00004 
00005 #include "cmsis.h"
00006 #include "cmsis_os.h"
00007 #include "ns_trace.h"
00008 
00009 #include "eventOS_scheduler.h"
00010 
00011 #include "ns_event_loop.h"
00012 
00013 #define TRACE_GROUP "evlp"
00014 
00015 static void event_loop_thread(const void *arg);
00016 
00017 // 1K should be enough - it's what the SAM4E port uses...
00018 // What happened to the instances parameter?
00019 static osThreadDef(event_loop_thread, osPriorityNormal, /*1,*/ MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE);
00020 static osMutexDef(event);
00021 
00022 static osThreadId event_thread_id;
00023 static osMutexId event_mutex_id;
00024 static osThreadId event_mutex_owner_id = NULL;
00025 static uint32_t owner_count = 0;
00026 
00027 void eventOS_scheduler_mutex_wait(void)
00028 {
00029     osMutexWait(event_mutex_id, osWaitForever);
00030     if (0 == owner_count) {
00031         event_mutex_owner_id = osThreadGetId();
00032     }
00033     owner_count++;
00034 }
00035 
00036 void eventOS_scheduler_mutex_release(void)
00037 {
00038     owner_count--;
00039     if (0 == owner_count) {
00040         event_mutex_owner_id = NULL;
00041     }
00042     osMutexRelease(event_mutex_id);
00043 }
00044 
00045 uint8_t eventOS_scheduler_mutex_is_owner(void)
00046 {
00047     return osThreadGetId() == event_mutex_owner_id ? 1 : 0;
00048 }
00049 
00050 void eventOS_scheduler_signal(void)
00051 {
00052     // XXX why does signal set lock if called with irqs disabled?
00053     //__enable_irq();
00054     //tr_debug("signal %p", (void*)event_thread_id);
00055     osSignalSet(event_thread_id, 1);
00056     //tr_debug("signalled %p", (void*)event_thread_id);
00057 }
00058 
00059 void eventOS_scheduler_idle(void)
00060 {
00061     //tr_debug("idle");
00062     eventOS_scheduler_mutex_release();
00063     osSignalWait(1, osWaitForever);
00064     eventOS_scheduler_mutex_wait();
00065 }
00066 
00067 static void event_loop_thread(const void *arg)
00068 {
00069     //tr_debug("event_loop_thread create");
00070     osSignalWait(2, osWaitForever);
00071 
00072     eventOS_scheduler_mutex_wait();
00073     tr_debug("event_loop_thread");
00074 
00075     // Run does not return - it calls eventOS_scheduler_idle when it's, er, idle
00076     eventOS_scheduler_run();
00077 }
00078 
00079 void ns_event_loop_thread_create(void)
00080 {
00081     event_mutex_id = osMutexCreate(osMutex(event));
00082     event_thread_id = osThreadCreate(osThread(event_loop_thread), NULL);
00083 }
00084 
00085 void ns_event_loop_thread_start(void)
00086 {
00087     osSignalSet(event_thread_id, 2);
00088 }