Testing [Andrew L] mbos RTOS for mbed Simply by copying code for main.cpp from mbos.h-comments

Dependencies:   mbed

Committer:
chalikias
Date:
Thu May 05 07:34:12 2011 +0000
Revision:
0:a61d29450691

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chalikias 0:a61d29450691 1 /***********************************************************************************
chalikias 0:a61d29450691 2 * m b o s R T O S F O R m b e d (ARM CORTEX M3)
chalikias 0:a61d29450691 3 *
chalikias 0:a61d29450691 4 * Copyright (c) 2010 - 2011 Andrew Levido
chalikias 0:a61d29450691 5 *
chalikias 0:a61d29450691 6 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
chalikias 0:a61d29450691 7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
chalikias 0:a61d29450691 8 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
chalikias 0:a61d29450691 9 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
chalikias 0:a61d29450691 10 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
chalikias 0:a61d29450691 11 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
chalikias 0:a61d29450691 12 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
chalikias 0:a61d29450691 13 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
chalikias 0:a61d29450691 14 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
chalikias 0:a61d29450691 15 */
chalikias 0:a61d29450691 16 #ifndef MBOS_H
chalikias 0:a61d29450691 17 #define MBOS_H
chalikias 0:a61d29450691 18
chalikias 0:a61d29450691 19 typedef unsigned int uint;
chalikias 0:a61d29450691 20
chalikias 0:a61d29450691 21 /**A pre-emptive, mutlitasking real-time operating system for mbed applications.
chalikias 0:a61d29450691 22 *
chalikias 0:a61d29450691 23 * Allows the user to write their application in the form of independant tasks.
chalikias 0:a61d29450691 24 * The operating system manages a completely separate processor context for each
chalikias 0:a61d29450691 25 * task (processor registers and stack) and swaps between the tasks according to a
chalikias 0:a61d29450691 26 * simple scheduling algorithm.
chalikias 0:a61d29450691 27 *
chalikias 0:a61d29450691 28 * Tasks are allocated a priority, and the scheduler ensures that the task
chalikias 0:a61d29450691 29 * that with the highest priority that is not waiting for an event, has control of
chalikias 0:a61d29450691 30 * the processor. If more than one task shares the highest priority, they will be
chalikias 0:a61d29450691 31 * run sequentially in round-robin fashion.
chalikias 0:a61d29450691 32 *
chalikias 0:a61d29450691 33 * Each task may wait for one or more of up to 32 different events, each represented by
chalikias 0:a61d29450691 34 * one bit in the event flags word. Events may be posted by another task, by a mbos
chalikias 0:a61d29450691 35 * timer, or in response to some asynchronous event such as an peripheral interrupt.
chalikias 0:a61d29450691 36 *
chalikias 0:a61d29450691 37 * mbos implements the SysTick timer to generate a System Tick every millisecond.
chalikias 0:a61d29450691 38 * Scheduling occurs once every Tick, or whenever a task blocks or an event is posted.
chalikias 0:a61d29450691 39 * The user may create one-shot or continuous timers to post events to tasks.
chalikias 0:a61d29450691 40 *
chalikias 0:a61d29450691 41 * mbos implements a simple ceiling protocol resource locking mechanism To manage access
chalikias 0:a61d29450691 42 * to system resources. Tasks may take exclusive ownership of resources for a period.
chalikias 0:a61d29450691 43 *
chalikias 0:a61d29450691 44 * A typical simple example with two tasks, and one timer, might look like this:
chalikias 0:a61d29450691 45 * @code
chalikias 0:a61d29450691 46 * // mbos Blinky demonstration.
chalikias 0:a61d29450691 47 * // Task 1 toggles LED1 every second, under control of a timer. It then posts an event to
chalikias 0:a61d29450691 48 * // task 2 which flashed LED2 briefly.
chalikias 0:a61d29450691 49 * #include "mbed.h"
chalikias 0:a61d29450691 50 * #include "mbos.h"
chalikias 0:a61d29450691 51 *
chalikias 0:a61d29450691 52 * #define TASK1_ID 1 // Id for task 1 (idle task is 0)
chalikias 0:a61d29450691 53 * #define TASK1_PRIO 50 // priority for task 1
chalikias 0:a61d29450691 54 * #define TASK1_STACK_SZ 32 // stack size for task 1 in words
chalikias 0:a61d29450691 55 * #define TASK2_ID 2 // Id for task 2
chalikias 0:a61d29450691 56 * #define TASK2_PRIO 60 // priority for task 2
chalikias 0:a61d29450691 57 * #define TASK2_STACK_SZ 32 // stack size for task 2 in words
chalikias 0:a61d29450691 58 * #define TIMER0_ID 0 // Id for timer 0
chalikias 0:a61d29450691 59 * #define TIMER0_PERIOD 1000 // Time period in milliseconds
chalikias 0:a61d29450691 60 * #define TIMER0_EVENT 1 // Event flag (1 << 0)
chalikias 0:a61d29450691 61 * #define T1_TO_T2_EVENT 2 // Event flag (1 << 1)
chalikias 0:a61d29450691 62 *
chalikias 0:a61d29450691 63 * void task1(void); // task function prototypes
chalikias 0:a61d29450691 64 * void task2(void);
chalikias 0:a61d29450691 65 *
chalikias 0:a61d29450691 66 * DigitalOut led1(LED1);
chalikias 0:a61d29450691 67 * DigitalOut led2(LED2);
chalikias 0:a61d29450691 68 * mbos os(2, 1); // Instantiate mbos with 2 tasks & 1 timer
chalikias 0:a61d29450691 69 *
chalikias 0:a61d29450691 70 * int main(void)
chalikias 0:a61d29450691 71 * {
chalikias 0:a61d29450691 72 * // Configure tasks and timers
chalikias 0:a61d29450691 73 * os.CreateTask(TASK1_ID, TASK1_PRIO, TASK1_STACK_SZ, task1);
chalikias 0:a61d29450691 74 * os.CreateTask(TASK2_ID, TASK2_PRIO, TASK2_STACK_SZ, task2);
chalikias 0:a61d29450691 75 * os.CreateTimer(TIMER0_ID, TIMER0_EVENT, TASK1_ID);
chalikias 0:a61d29450691 76 * // Start mbos
chalikias 0:a61d29450691 77 * os.Start();
chalikias 0:a61d29450691 78 * // never return!
chalikias 0:a61d29450691 79 * }
chalikias 0:a61d29450691 80 *
chalikias 0:a61d29450691 81 * void task1(void)
chalikias 0:a61d29450691 82 * {
chalikias 0:a61d29450691 83 * os.SetTimer(TIMER0_ID, TIMER0_PERIOD, TIMER0_PERIOD);
chalikias 0:a61d29450691 84 * while(1){
chalikias 0:a61d29450691 85 * os.WaitEvent(TIMER0_EVENT);
chalikias 0:a61d29450691 86 * led1 = !led1;
chalikias 0:a61d29450691 87 * os.SetEvent(T1_TO_T2_EVENT, TASK2_ID);
chalikias 0:a61d29450691 88 * }
chalikias 0:a61d29450691 89 * }
chalikias 0:a61d29450691 90 *
chalikias 0:a61d29450691 91 * void task2(void)
chalikias 0:a61d29450691 92 * {
chalikias 0:a61d29450691 93 * while(1){
chalikias 0:a61d29450691 94 * os.WaitEvent(T1_TO_T2_EVENT);
chalikias 0:a61d29450691 95 * led2 = 1;
chalikias 0:a61d29450691 96 * wait_ms(100);
chalikias 0:a61d29450691 97 * led2 = 0;
chalikias 0:a61d29450691 98 * }
chalikias 0:a61d29450691 99 * }
chalikias 0:a61d29450691 100 * @endcode
chalikias 0:a61d29450691 101 */
chalikias 0:a61d29450691 102
chalikias 0:a61d29450691 103 class mbos {
chalikias 0:a61d29450691 104 public:
chalikias 0:a61d29450691 105 /** Create an mbos object. Instantiate mbos and define the number of tasks, timers
chalikias 0:a61d29450691 106 * and resources.
chalikias 0:a61d29450691 107 *
chalikias 0:a61d29450691 108 * @param ntasks The number of user tasks (1 .. 99).
chalikias 0:a61d29450691 109 * @param ntimers Optional number of timers (0 .. 100).
chalikias 0:a61d29450691 110 * @param nresources Optional number of resources (0 .. 100).
chalikias 0:a61d29450691 111 */
chalikias 0:a61d29450691 112 mbos(uint ntasks, uint ntimers = 0, uint nresources = 0);
chalikias 0:a61d29450691 113
chalikias 0:a61d29450691 114 /** Start mbos. Optionally specify the size of the stack for a user-written idle task.
chalikias 0:a61d29450691 115 * All tasks, timers and resources must be created before calling Start.
chalikias 0:a61d29450691 116 *
chalikias 0:a61d29450691 117 * @param idlestacksize Size in words (>= 32) of the user-written idle task if present.
chalikias 0:a61d29450691 118 * @returns Never returns
chalikias 0:a61d29450691 119 */
chalikias 0:a61d29450691 120 void Start(uint idlestacksize = 32);
chalikias 0:a61d29450691 121
chalikias 0:a61d29450691 122 /** Create an mbos task. Allocates and initialises data structures for the task.
chalikias 0:a61d29450691 123 *
chalikias 0:a61d29450691 124 * @param taskid Unique ID for the task. (1 .. ntasks).
chalikias 0:a61d29450691 125 * @param priority Priority (0 .. 99) of the task. Tasks may share the same priority.
chalikias 0:a61d29450691 126 * @param stacksize Size in words (>= 32) of the task's stack.
chalikias 0:a61d29450691 127 * @param fun Pointer to the task function. Function must be of type static void,
chalikias 0:a61d29450691 128 * and must return nothing.
chalikias 0:a61d29450691 129 */
chalikias 0:a61d29450691 130 void CreateTask(uint taskid, uint priority, uint stacksz, void (*fun)(void));
chalikias 0:a61d29450691 131
chalikias 0:a61d29450691 132 /** Get the ID of the current task.
chalikias 0:a61d29450691 133 *
chalikias 0:a61d29450691 134 * @returns The ID (0 .. 99) of the calling task.
chalikias 0:a61d29450691 135 */
chalikias 0:a61d29450691 136 uint GetTask(void);
chalikias 0:a61d29450691 137
chalikias 0:a61d29450691 138 /** Set the priority of the current task. Does nothing if called from the idle task.
chalikias 0:a61d29450691 139 *
chalikias 0:a61d29450691 140 * @param priority Priority (0 .. 99) to apply to the calling task.
chalikias 0:a61d29450691 141 */
chalikias 0:a61d29450691 142 void SetPriority(uint priority);
chalikias 0:a61d29450691 143
chalikias 0:a61d29450691 144 /** Get the priority of the current task.
chalikias 0:a61d29450691 145 *
chalikias 0:a61d29450691 146 * @returns The priority (0 .. 99) of the calling task.
chalikias 0:a61d29450691 147 */
chalikias 0:a61d29450691 148 uint GetPriority(void);
chalikias 0:a61d29450691 149
chalikias 0:a61d29450691 150 /** Wait for an event or events. Causes the current task to block, waiting for the
chalikias 0:a61d29450691 151 * specified event. Does nothing if called from the idle task, or if the event is NULL.
chalikias 0:a61d29450691 152 *
chalikias 0:a61d29450691 153 * @param event Event flag(s) to wait for.
chalikias 0:a61d29450691 154 */
chalikias 0:a61d29450691 155 void WaitEvent(uint event);
chalikias 0:a61d29450691 156
chalikias 0:a61d29450691 157 /** Post an event or events to a task.
chalikias 0:a61d29450691 158 * Posts the nominated event(s) to the specified task. Forces a context switch if the events
chalikias 0:a61d29450691 159 * are posted successfully. Does nothing if the task is not waiting, or is not waiting for
chalikias 0:a61d29450691 160 * the posted event.
chalikias 0:a61d29450691 161 *
chalikias 0:a61d29450691 162 * @param event Event flag(s) to post.
chalikias 0:a61d29450691 163 * @param task The ID of the task to which to post the event(s). May not be idle task.
chalikias 0:a61d29450691 164 */
chalikias 0:a61d29450691 165 void SetEvent(uint event, uint task);
chalikias 0:a61d29450691 166
chalikias 0:a61d29450691 167 /** Returns the event flag(s) which last caused the task to unblock.
chalikias 0:a61d29450691 168 *
chalikias 0:a61d29450691 169 * @returns The event flags.
chalikias 0:a61d29450691 170 */
chalikias 0:a61d29450691 171 uint GetEvent(void);
chalikias 0:a61d29450691 172
chalikias 0:a61d29450691 173 /** Create a mbos timer. Allocates and initialises the data structures for the timer.
chalikias 0:a61d29450691 174 *
chalikias 0:a61d29450691 175 * @param timerid Unique ID for the timer (0 .. ntimers - 1).
chalikias 0:a61d29450691 176 * @param taskid The ID of the task to which the timer will post events. May not be
chalikias 0:a61d29450691 177 * the idle task.
chalikias 0:a61d29450691 178 * @param event The event flag(s) that the timer should post on timeout. May not be NULL.
chalikias 0:a61d29450691 179 */
chalikias 0:a61d29450691 180 void CreateTimer(uint timerid, uint taskid, uint event);
chalikias 0:a61d29450691 181
chalikias 0:a61d29450691 182 /** Starts an mbos timer. If the reload time is zero or omitted, the timer will be reset
chalikias 0:a61d29450691 183 * once it has posted a single event, after time milliseconds. If the reload time is
chalikias 0:a61d29450691 184 * non-zero, this value will be loaded into the timer on time-out, and the timer will
chalikias 0:a61d29450691 185 * continue indefinitely. In this case the interval to the first event will be time,
chalikias 0:a61d29450691 186 * and the interval between subsequent events will be reloadtime.
chalikias 0:a61d29450691 187 *
chalikias 0:a61d29450691 188 * @param timerid The ID of the timer.
chalikias 0:a61d29450691 189 * @param time The period in milliseconds before the timer times out.
chalikias 0:a61d29450691 190 * @param reload The optional time to reload into the timer when it times out.
chalikias 0:a61d29450691 191 */
chalikias 0:a61d29450691 192 void SetTimer(uint timerid, uint time, uint reload = 0);
chalikias 0:a61d29450691 193
chalikias 0:a61d29450691 194 /** Redirects an mbos timer. Changes the task and event associated with a timer. If the timer
chalikias 0:a61d29450691 195 * is running, the fun ction has no effect.
chalikias 0:a61d29450691 196 *
chalikias 0:a61d29450691 197 * @param timerid The ID of the timer.
chalikias 0:a61d29450691 198 * @param taskid The ID of the task to which the timer will post events. May not be
chalikias 0:a61d29450691 199 * the idle task.
chalikias 0:a61d29450691 200 * @param event The event flag(s) that the timer should post on timeout. May not be NULL.
chalikias 0:a61d29450691 201 */
chalikias 0:a61d29450691 202 void RedirectTimer(uint timerid, uint taskid, uint event);
chalikias 0:a61d29450691 203
chalikias 0:a61d29450691 204 /** Stops and clears an mbos timer.
chalikias 0:a61d29450691 205 *
chalikias 0:a61d29450691 206 * @param timerid The ID of the timer to clear.
chalikias 0:a61d29450691 207 */
chalikias 0:a61d29450691 208 void ClearTimer(uint timerid);
chalikias 0:a61d29450691 209
chalikias 0:a61d29450691 210 /** Creates an mbos resource. Allocates and initialises the data structures for the Resource.
chalikias 0:a61d29450691 211 *
chalikias 0:a61d29450691 212 * @param resourceid Unique ID for the resource (0 .. nresource).
chalikias 0:a61d29450691 213 * @param priority Priority of the resource (normally > than that of any task using the
chalikias 0:a61d29450691 214 * resource).
chalikias 0:a61d29450691 215 */
chalikias 0:a61d29450691 216 void CreateResource(uint resourceid, uint priority);
chalikias 0:a61d29450691 217
chalikias 0:a61d29450691 218 /** Locks an mbos resource and temporarily allocates the resource's priority to the calling
chalikias 0:a61d29450691 219 * task.
chalikias 0:a61d29450691 220 * Does nothing if the resource is already locked, or if called from the idle task.
chalikias 0:a61d29450691 221 *
chalikias 0:a61d29450691 222 * @param resourceid The ID of the resource to lock.
chalikias 0:a61d29450691 223 * @returns Zero if the resource was locked successfully, or the ID of the task that is
chalikias 0:a61d29450691 224 * currently locking the resource, if not.
chalikias 0:a61d29450691 225 */
chalikias 0:a61d29450691 226 uint LockResource(uint resourceid);
chalikias 0:a61d29450691 227
chalikias 0:a61d29450691 228 /** Tests whether a resource is locked or free, without changing its state.
chalikias 0:a61d29450691 229 *
chalikias 0:a61d29450691 230 * @param resourceid The ID of the resouce to test.
chalikias 0:a61d29450691 231 * @returns Zero if the resource is free, or the ID of the task that is currently
chalikias 0:a61d29450691 232 * locking the resouce , if not.
chalikias 0:a61d29450691 233
chalikias 0:a61d29450691 234 */
chalikias 0:a61d29450691 235 uint TestResource(uint resourceid);
chalikias 0:a61d29450691 236
chalikias 0:a61d29450691 237 /** Frees a resource
chalikias 0:a61d29450691 238 * Frees an mbos resource and restores the calling task's original priority. Does nothing
chalikias 0:a61d29450691 239 * if the resource is already free, if called from the idle task, or the resource was
chalikias 0:a61d29450691 240 * locked by a different task.
chalikias 0:a61d29450691 241 *
chalikias 0:a61d29450691 242 * @param resourceid The ID of the resource t free.
chalikias 0:a61d29450691 243 * @returns Zero if the resource was freed successfully, or the ID of the task that is
chalikias 0:a61d29450691 244 * locking the resource, if not.
chalikias 0:a61d29450691 245 */
chalikias 0:a61d29450691 246 uint FreeResource(uint resource);
chalikias 0:a61d29450691 247
chalikias 0:a61d29450691 248 private:
chalikias 0:a61d29450691 249 uint* _initstack(uint *stack, void (*fun)());
chalikias 0:a61d29450691 250 };
chalikias 0:a61d29450691 251 #endif