mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

UserRevisionLine numberNew contents of line
be_bryan 0:b74591d5ab33 1 /* events
be_bryan 0:b74591d5ab33 2 * Copyright (c) 2017 ARM Limited
be_bryan 0:b74591d5ab33 3 *
be_bryan 0:b74591d5ab33 4 * Licensed under the Apache License, Version 2.0 (the "License");
be_bryan 0:b74591d5ab33 5 * you may not use this file except in compliance with the License.
be_bryan 0:b74591d5ab33 6 * You may obtain a copy of the License at
be_bryan 0:b74591d5ab33 7 *
be_bryan 0:b74591d5ab33 8 * http://www.apache.org/licenses/LICENSE-2.0
be_bryan 0:b74591d5ab33 9 *
be_bryan 0:b74591d5ab33 10 * Unless required by applicable law or agreed to in writing, software
be_bryan 0:b74591d5ab33 11 * distributed under the License is distributed on an "AS IS" BASIS,
be_bryan 0:b74591d5ab33 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
be_bryan 0:b74591d5ab33 13 * See the License for the specific language governing permissions and
be_bryan 0:b74591d5ab33 14 * limitations under the License.
be_bryan 0:b74591d5ab33 15 */
be_bryan 0:b74591d5ab33 16
be_bryan 0:b74591d5ab33 17 #include "events/mbed_shared_queues.h"
be_bryan 0:b74591d5ab33 18 #include "mbed.h"
be_bryan 0:b74591d5ab33 19
be_bryan 0:b74591d5ab33 20 using namespace events;
be_bryan 0:b74591d5ab33 21
be_bryan 0:b74591d5ab33 22 namespace mbed {
be_bryan 0:b74591d5ab33 23
be_bryan 0:b74591d5ab33 24 #ifdef MBED_CONF_RTOS_PRESENT
be_bryan 0:b74591d5ab33 25 /* Create an event queue, and start the thread that dispatches it. Static
be_bryan 0:b74591d5ab33 26 * variables mean this happens once the first time each template instantiation
be_bryan 0:b74591d5ab33 27 * is called. This is currently instantiated no more than twice.
be_bryan 0:b74591d5ab33 28 */
be_bryan 0:b74591d5ab33 29 template
be_bryan 0:b74591d5ab33 30 <osPriority Priority, size_t QueueSize, size_t StackSize>
be_bryan 0:b74591d5ab33 31 EventQueue *do_shared_event_queue_with_thread()
be_bryan 0:b74591d5ab33 32 {
be_bryan 0:b74591d5ab33 33 static uint64_t queue_buffer[QueueSize / sizeof(uint64_t)];
be_bryan 0:b74591d5ab33 34 static EventQueue queue(sizeof queue_buffer, (unsigned char *) queue_buffer);
be_bryan 0:b74591d5ab33 35
be_bryan 0:b74591d5ab33 36 static uint64_t stack[StackSize / sizeof(uint64_t)];
be_bryan 0:b74591d5ab33 37 static Thread thread(Priority, StackSize, (unsigned char *) stack);
be_bryan 0:b74591d5ab33 38
be_bryan 0:b74591d5ab33 39 Thread::State state = thread.get_state();
be_bryan 0:b74591d5ab33 40 if (state == Thread::Inactive || state == Thread::Deleted) {
be_bryan 0:b74591d5ab33 41 osStatus status = thread.start(callback(&queue, &EventQueue::dispatch_forever));
be_bryan 0:b74591d5ab33 42 MBED_ASSERT(status == osOK);
be_bryan 0:b74591d5ab33 43 if (status != osOK) {
be_bryan 0:b74591d5ab33 44 return NULL;
be_bryan 0:b74591d5ab33 45 }
be_bryan 0:b74591d5ab33 46 }
be_bryan 0:b74591d5ab33 47
be_bryan 0:b74591d5ab33 48 return &queue;
be_bryan 0:b74591d5ab33 49 }
be_bryan 0:b74591d5ab33 50 #endif
be_bryan 0:b74591d5ab33 51
be_bryan 0:b74591d5ab33 52 EventQueue *mbed_event_queue()
be_bryan 0:b74591d5ab33 53 {
be_bryan 0:b74591d5ab33 54 #if MBED_CONF_EVENTS_SHARED_DISPATCH_FROM_APPLICATION || !defined MBED_CONF_RTOS_PRESENT
be_bryan 0:b74591d5ab33 55 /* Only create the EventQueue, but no dispatching thread */
be_bryan 0:b74591d5ab33 56 static unsigned char queue_buffer[MBED_CONF_EVENTS_SHARED_EVENTSIZE];
be_bryan 0:b74591d5ab33 57 static EventQueue queue(sizeof queue_buffer, queue_buffer);
be_bryan 0:b74591d5ab33 58
be_bryan 0:b74591d5ab33 59 return &queue;
be_bryan 0:b74591d5ab33 60 #else
be_bryan 0:b74591d5ab33 61 return do_shared_event_queue_with_thread<osPriorityNormal, MBED_CONF_EVENTS_SHARED_EVENTSIZE, MBED_CONF_EVENTS_SHARED_STACKSIZE>();
be_bryan 0:b74591d5ab33 62 #endif
be_bryan 0:b74591d5ab33 63 }
be_bryan 0:b74591d5ab33 64
be_bryan 0:b74591d5ab33 65 #ifdef MBED_CONF_RTOS_PRESENT
be_bryan 0:b74591d5ab33 66 EventQueue *mbed_highprio_event_queue()
be_bryan 0:b74591d5ab33 67 {
be_bryan 0:b74591d5ab33 68 return do_shared_event_queue_with_thread<osPriorityHigh, MBED_CONF_EVENTS_SHARED_HIGHPRIO_EVENTSIZE, MBED_CONF_EVENTS_SHARED_HIGHPRIO_STACKSIZE>();
be_bryan 0:b74591d5ab33 69 }
be_bryan 0:b74591d5ab33 70 #endif
be_bryan 0:b74591d5ab33 71
be_bryan 0:b74591d5ab33 72 }