Example code displaying how to use and implement the mbed RTOS along with a simple state machine used to capture button presses.
Fork of mbed-rtos by
rtos/Thread.cpp@35:b8555a5966f6, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 11:15:19 2014 +0100
- Revision:
- 35:b8555a5966f6
- Parent:
- 25:420a92812cd1
- Child:
- 36:512640f00564
Synchronized with git revision 3197042b65f8d28e856e1a7812d45e2fbe80e3f1
Full URL: https://github.com/mbedmicro/mbed/commit/3197042b65f8d28e856e1a7812d45e2fbe80e3f1/
error.h -> mbed_error.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 8:88a1a9c26ae3 | 1 | /* mbed Microcontroller Library |
emilmont | 8:88a1a9c26ae3 | 2 | * Copyright (c) 2006-2012 ARM Limited |
emilmont | 8:88a1a9c26ae3 | 3 | * |
emilmont | 8:88a1a9c26ae3 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 8:88a1a9c26ae3 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 8:88a1a9c26ae3 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 8:88a1a9c26ae3 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 8:88a1a9c26ae3 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 8:88a1a9c26ae3 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 8:88a1a9c26ae3 | 10 | * |
emilmont | 8:88a1a9c26ae3 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 8:88a1a9c26ae3 | 12 | * all copies or substantial portions of the Software. |
emilmont | 8:88a1a9c26ae3 | 13 | * |
emilmont | 8:88a1a9c26ae3 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 8:88a1a9c26ae3 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 8:88a1a9c26ae3 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 8:88a1a9c26ae3 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 8:88a1a9c26ae3 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 8:88a1a9c26ae3 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 8:88a1a9c26ae3 | 20 | * SOFTWARE. |
emilmont | 8:88a1a9c26ae3 | 21 | */ |
emilmont | 8:88a1a9c26ae3 | 22 | #include "Thread.h" |
emilmont | 8:88a1a9c26ae3 | 23 | |
mbed_official | 35:b8555a5966f6 | 24 | #include "mbed_error.h" |
emilmont | 8:88a1a9c26ae3 | 25 | |
emilmont | 8:88a1a9c26ae3 | 26 | namespace rtos { |
emilmont | 8:88a1a9c26ae3 | 27 | |
emilmont | 8:88a1a9c26ae3 | 28 | Thread::Thread(void (*task)(void const *argument), void *argument, |
emilmont | 8:88a1a9c26ae3 | 29 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
emilmont | 8:88a1a9c26ae3 | 30 | #ifdef CMSIS_OS_RTX |
emilmont | 8:88a1a9c26ae3 | 31 | _thread_def.pthread = task; |
emilmont | 8:88a1a9c26ae3 | 32 | _thread_def.tpriority = priority; |
emilmont | 8:88a1a9c26ae3 | 33 | _thread_def.stacksize = stack_size; |
emilmont | 8:88a1a9c26ae3 | 34 | if (stack_pointer != NULL) { |
emilmont | 8:88a1a9c26ae3 | 35 | _thread_def.stack_pointer = stack_pointer; |
emilmont | 8:88a1a9c26ae3 | 36 | _dynamic_stack = false; |
emilmont | 8:88a1a9c26ae3 | 37 | } else { |
emilmont | 8:88a1a9c26ae3 | 38 | _thread_def.stack_pointer = new unsigned char[stack_size]; |
emilmont | 8:88a1a9c26ae3 | 39 | if (_thread_def.stack_pointer == NULL) |
mbed_official | 25:420a92812cd1 | 40 | error("Error allocating the stack memory\n"); |
emilmont | 8:88a1a9c26ae3 | 41 | _dynamic_stack = true; |
emilmont | 8:88a1a9c26ae3 | 42 | } |
emilmont | 8:88a1a9c26ae3 | 43 | #endif |
emilmont | 8:88a1a9c26ae3 | 44 | _tid = osThreadCreate(&_thread_def, argument); |
emilmont | 8:88a1a9c26ae3 | 45 | } |
emilmont | 8:88a1a9c26ae3 | 46 | |
emilmont | 8:88a1a9c26ae3 | 47 | osStatus Thread::terminate() { |
emilmont | 8:88a1a9c26ae3 | 48 | return osThreadTerminate(_tid); |
emilmont | 8:88a1a9c26ae3 | 49 | } |
emilmont | 8:88a1a9c26ae3 | 50 | |
emilmont | 8:88a1a9c26ae3 | 51 | osStatus Thread::set_priority(osPriority priority) { |
emilmont | 8:88a1a9c26ae3 | 52 | return osThreadSetPriority(_tid, priority); |
emilmont | 8:88a1a9c26ae3 | 53 | } |
emilmont | 8:88a1a9c26ae3 | 54 | |
emilmont | 8:88a1a9c26ae3 | 55 | osPriority Thread::get_priority() { |
emilmont | 8:88a1a9c26ae3 | 56 | return osThreadGetPriority(_tid); |
emilmont | 8:88a1a9c26ae3 | 57 | } |
emilmont | 8:88a1a9c26ae3 | 58 | |
emilmont | 8:88a1a9c26ae3 | 59 | int32_t Thread::signal_set(int32_t signals) { |
emilmont | 8:88a1a9c26ae3 | 60 | return osSignalSet(_tid, signals); |
emilmont | 8:88a1a9c26ae3 | 61 | } |
emilmont | 8:88a1a9c26ae3 | 62 | |
emilmont | 8:88a1a9c26ae3 | 63 | Thread::State Thread::get_state() { |
emilmont | 8:88a1a9c26ae3 | 64 | return ((State)_thread_def.tcb.state); |
emilmont | 8:88a1a9c26ae3 | 65 | } |
emilmont | 8:88a1a9c26ae3 | 66 | |
emilmont | 8:88a1a9c26ae3 | 67 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 68 | return osSignalWait(signals, millisec); |
emilmont | 8:88a1a9c26ae3 | 69 | } |
emilmont | 8:88a1a9c26ae3 | 70 | |
emilmont | 8:88a1a9c26ae3 | 71 | osStatus Thread::wait(uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 72 | return osDelay(millisec); |
emilmont | 8:88a1a9c26ae3 | 73 | } |
emilmont | 8:88a1a9c26ae3 | 74 | |
emilmont | 8:88a1a9c26ae3 | 75 | osStatus Thread::yield() { |
emilmont | 8:88a1a9c26ae3 | 76 | return osThreadYield(); |
emilmont | 8:88a1a9c26ae3 | 77 | } |
emilmont | 8:88a1a9c26ae3 | 78 | |
emilmont | 8:88a1a9c26ae3 | 79 | osThreadId Thread::gettid() { |
emilmont | 8:88a1a9c26ae3 | 80 | return osThreadGetId(); |
emilmont | 8:88a1a9c26ae3 | 81 | } |
emilmont | 8:88a1a9c26ae3 | 82 | |
emilmont | 8:88a1a9c26ae3 | 83 | Thread::~Thread() { |
emilmont | 8:88a1a9c26ae3 | 84 | terminate(); |
emilmont | 8:88a1a9c26ae3 | 85 | if (_dynamic_stack) { |
emilmont | 8:88a1a9c26ae3 | 86 | delete[] (_thread_def.stack_pointer); |
emilmont | 8:88a1a9c26ae3 | 87 | } |
emilmont | 8:88a1a9c26ae3 | 88 | } |
emilmont | 8:88a1a9c26ae3 | 89 | |
emilmont | 8:88a1a9c26ae3 | 90 | } |