takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ns_event_loop_mbed.cpp Source File

ns_event_loop_mbed.cpp

00001 /*
00002  * Copyright (c) 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 "platform/arm_hal_interrupt.h"
00019 #include "cmsis.h"
00020 #include "cmsis_os2.h"
00021 #include "mbed_rtos_storage.h"
00022 #include "ns_trace.h"
00023 
00024 #include "eventOS_scheduler.h"
00025 
00026 #include "mbed_error.h"
00027 #include "mbed_shared_queues.h"
00028 #include "events/Event.h"
00029 #include "ns_event_loop_mutex.h"
00030 #include "ns_event_loop.h"
00031 
00032 #define TRACE_GROUP "evlp"
00033 
00034 #if MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS
00035 
00036 using events::EventQueue;
00037 using events::Event;
00038 
00039 static Event<void()> *event;
00040 static volatile int event_pending;
00041 static volatile bool started;
00042 
00043 void eventOS_scheduler_signal(void)
00044 {
00045     platform_enter_critical();
00046     if (started && event_pending == 0) {
00047         event_pending = event->post();
00048         MBED_ASSERT(event_pending != 0);
00049     }
00050     platform_exit_critical();
00051 }
00052 
00053 void eventOS_scheduler_idle(void)
00054 {
00055     error("Shouldn't be called");
00056 }
00057 
00058 static void do_dispatch_with_mutex_held()
00059 {
00060     platform_enter_critical();
00061     event_pending = 0;
00062     platform_exit_critical();
00063 
00064     /* Process only 1 Nanostack event at a time, to try to be nice to
00065      * others on the global queue.
00066      */
00067     eventOS_scheduler_mutex_wait();
00068     bool dispatched = eventOS_scheduler_dispatch_event();
00069     eventOS_scheduler_mutex_release();
00070 
00071     /* Go round again if (potentially) more */
00072     if (dispatched) {
00073         eventOS_scheduler_signal();
00074     }
00075 }
00076 
00077 void ns_event_loop_init(void)
00078 {
00079     ns_event_loop_mutex_init();
00080 }
00081 
00082 void ns_event_loop_thread_create(void)
00083 {
00084     EventQueue *equeue = mbed::mbed_event_queue();
00085     MBED_ASSERT(equeue != NULL);
00086 
00087     event = new Event<void()>(equeue, do_dispatch_with_mutex_held);
00088     MBED_ASSERT(event != NULL);
00089 }
00090 
00091 void ns_event_loop_thread_start(void)
00092 {
00093     started = true;
00094     eventOS_scheduler_signal();
00095 }
00096 
00097 #endif // MBED_CONF_NANOSTACK_HAL_EVENT_LOOP_USE_MBED_EVENTS