Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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