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