takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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-2018 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed_assert.h"
00018 #include "cmsis.h"
00019 #include "cmsis_os2.h"
00020 #include "mbed_rtos_storage.h"
00021 #include "ns_trace.h"
00022 
00023 #include "eventOS_scheduler.h"
00024 
00025 #include "ns_event_loop_mutex.h"
00026 #include "ns_event_loop.h"
00027 
00028 #define TRACE_GROUP "evlp"
00029 
00030 #if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
00031 
00032 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00033 
00034 static mbed_rtos_storage_event_flags_t event_flag_cb;
00035 static const osEventFlagsAttr_t event_flags_attr = {
00036     .name = "nanostack_event_flags",
00037     .cb_mem = &event_flag_cb,
00038     .cb_size = sizeof event_flag_cb
00039 };
00040 static osEventFlagsId_t event_flag_id;
00041 
00042 #else
00043 
00044 #ifndef MBED_TZ_DEFAULT_ACCESS
00045 #define MBED_TZ_DEFAULT_ACCESS   0
00046 #endif    
00047     
00048 static void event_loop_thread(void *arg);
00049 
00050 static uint64_t event_thread_stk[MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_THREAD_STACK_SIZE/8];
00051 static mbed_rtos_storage_thread_t event_thread_tcb;
00052 static const osThreadAttr_t event_thread_attr = {
00053     .name = "nanostack_event_thread",
00054     .priority = osPriorityNormal,
00055     .stack_mem = &event_thread_stk[0],
00056     .stack_size = sizeof event_thread_stk,
00057     .cb_mem = &event_thread_tcb,
00058     .cb_size = sizeof event_thread_tcb,
00059     .tz_module = MBED_TZ_DEFAULT_ACCESS,
00060 };
00061 #endif
00062 
00063 #if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00064 static osThreadId_t event_thread_id;
00065 #endif
00066 
00067 void eventOS_scheduler_signal(void)
00068 {
00069     // XXX why does signal set lock if called with irqs disabled?
00070     //__enable_irq();
00071     //tr_debug("signal %p", (void*)event_thread_id);
00072 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00073     osEventFlagsSet(event_flag_id, 1);
00074 #else
00075     osThreadFlagsSet(event_thread_id, 1);
00076 #endif
00077     //tr_debug("signalled %p", (void*)event_thread_id);
00078 }
00079 
00080 void eventOS_scheduler_idle(void)
00081 {
00082     //tr_debug("idle");
00083     eventOS_scheduler_mutex_release();
00084 
00085 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00086     osEventFlagsWait(event_flag_id, 1, osFlagsWaitAny, osWaitForever);
00087 #else
00088     osThreadFlagsWait(1, 0, osWaitForever);
00089 #endif
00090 
00091     eventOS_scheduler_mutex_wait();
00092 }
00093 
00094 #if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00095 static void event_loop_thread(void *arg)
00096 {
00097     (void)arg;
00098     eventOS_scheduler_mutex_wait();
00099     eventOS_scheduler_run(); //Does not return
00100 }
00101 #endif
00102 
00103 // This is used to initialize the lock used by event loop even
00104 // if it is not ran in a separate thread.
00105 void ns_event_loop_init(void)
00106 {
00107     ns_event_loop_mutex_init();
00108 
00109     // If a separate event loop thread is not used, the signaling
00110     // happens via event flags instead of thread flags. This allows one to
00111     // perform the initialization from any thread and removes need to know the id
00112     // of event loop dispatch thread.
00113 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00114     event_flag_id  = osEventFlagsNew(&event_flags_attr);
00115     MBED_ASSERT(event_flag_id != NULL);
00116 #endif
00117 }
00118 
00119 #if !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_DISPATCH_FROM_APPLICATION
00120 void ns_event_loop_thread_create(void)
00121 {
00122     event_thread_id = osThreadNew(event_loop_thread, NULL, &event_thread_attr);
00123     MBED_ASSERT(event_thread_id != NULL);
00124 }
00125 
00126 void ns_event_loop_thread_start(void)
00127 {
00128 }
00129 #endif
00130 
00131 #endif // !MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS