Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sergunb 0:8918a71cdbe9 1 /**
Sergunb 0:8918a71cdbe9 2 * @file os_port_ucos2.c
Sergunb 0:8918a71cdbe9 3 * @brief RTOS abstraction layer (Micrium uC/OS-II)
Sergunb 0:8918a71cdbe9 4 *
Sergunb 0:8918a71cdbe9 5 * @section License
Sergunb 0:8918a71cdbe9 6 *
Sergunb 0:8918a71cdbe9 7 * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
Sergunb 0:8918a71cdbe9 8 *
Sergunb 0:8918a71cdbe9 9 * This program is free software; you can redistribute it and/or
Sergunb 0:8918a71cdbe9 10 * modify it under the terms of the GNU General Public License
Sergunb 0:8918a71cdbe9 11 * as published by the Free Software Foundation; either version 2
Sergunb 0:8918a71cdbe9 12 * of the License, or (at your option) any later version.
Sergunb 0:8918a71cdbe9 13 *
Sergunb 0:8918a71cdbe9 14 * This program is distributed in the hope that it will be useful,
Sergunb 0:8918a71cdbe9 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Sergunb 0:8918a71cdbe9 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Sergunb 0:8918a71cdbe9 17 * GNU General Public License for more details.
Sergunb 0:8918a71cdbe9 18 *
Sergunb 0:8918a71cdbe9 19 * You should have received a copy of the GNU General Public License
Sergunb 0:8918a71cdbe9 20 * along with this program; if not, write to the Free Software Foundation,
Sergunb 0:8918a71cdbe9 21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Sergunb 0:8918a71cdbe9 22 *
Sergunb 0:8918a71cdbe9 23 * @author Oryx Embedded SARL (www.oryx-embedded.com)
Sergunb 0:8918a71cdbe9 24 * @version 1.7.6
Sergunb 0:8918a71cdbe9 25 **/
Sergunb 0:8918a71cdbe9 26
Sergunb 0:8918a71cdbe9 27 //Switch to the appropriate trace level
Sergunb 0:8918a71cdbe9 28 #define TRACE_LEVEL TRACE_LEVEL_OFF
Sergunb 0:8918a71cdbe9 29
Sergunb 0:8918a71cdbe9 30 //Dependencies
Sergunb 0:8918a71cdbe9 31 #include <stdio.h>
Sergunb 0:8918a71cdbe9 32 #include <stdlib.h>
Sergunb 0:8918a71cdbe9 33 #include <string.h>
Sergunb 0:8918a71cdbe9 34 #include "os_port.h"
Sergunb 0:8918a71cdbe9 35 #include "os_port_ucos2.h"
Sergunb 0:8918a71cdbe9 36 #include "debug.h"
Sergunb 0:8918a71cdbe9 37
Sergunb 0:8918a71cdbe9 38 //Variables
Sergunb 0:8918a71cdbe9 39 static OsTask tcbTable[OS_LOWEST_PRIO];
Sergunb 0:8918a71cdbe9 40
Sergunb 0:8918a71cdbe9 41
Sergunb 0:8918a71cdbe9 42 /**
Sergunb 0:8918a71cdbe9 43 * @brief Kernel initialization
Sergunb 0:8918a71cdbe9 44 **/
Sergunb 0:8918a71cdbe9 45
Sergunb 0:8918a71cdbe9 46 void osInitKernel(void)
Sergunb 0:8918a71cdbe9 47 {
Sergunb 0:8918a71cdbe9 48 //Initialize table
Sergunb 0:8918a71cdbe9 49 memset(tcbTable, 0, sizeof(tcbTable));
Sergunb 0:8918a71cdbe9 50
Sergunb 0:8918a71cdbe9 51 //Scheduler initialization
Sergunb 0:8918a71cdbe9 52 OSInit();
Sergunb 0:8918a71cdbe9 53 }
Sergunb 0:8918a71cdbe9 54
Sergunb 0:8918a71cdbe9 55
Sergunb 0:8918a71cdbe9 56 /**
Sergunb 0:8918a71cdbe9 57 * @brief Start kernel
Sergunb 0:8918a71cdbe9 58 **/
Sergunb 0:8918a71cdbe9 59
Sergunb 0:8918a71cdbe9 60 void osStartKernel(void)
Sergunb 0:8918a71cdbe9 61 {
Sergunb 0:8918a71cdbe9 62 //Start the scheduler
Sergunb 0:8918a71cdbe9 63 OSStart();
Sergunb 0:8918a71cdbe9 64 }
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66
Sergunb 0:8918a71cdbe9 67 /**
Sergunb 0:8918a71cdbe9 68 * @brief Create a static task
Sergunb 0:8918a71cdbe9 69 * @param[out] task Pointer to the task structure
Sergunb 0:8918a71cdbe9 70 * @param[in] name A name identifying the task
Sergunb 0:8918a71cdbe9 71 * @param[in] taskCode Pointer to the task entry function
Sergunb 0:8918a71cdbe9 72 * @param[in] params A pointer to a variable to be passed to the task
Sergunb 0:8918a71cdbe9 73 * @param[in] stack Pointer to the stack
Sergunb 0:8918a71cdbe9 74 * @param[in] stackSize The initial size of the stack, in words
Sergunb 0:8918a71cdbe9 75 * @param[in] priority The priority at which the task should run
Sergunb 0:8918a71cdbe9 76 * @return The function returns TRUE if the task was successfully
Sergunb 0:8918a71cdbe9 77 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 78 **/
Sergunb 0:8918a71cdbe9 79
Sergunb 0:8918a71cdbe9 80 bool_t osCreateStaticTask(OsTask *task, const char_t *name, OsTaskCode taskCode,
Sergunb 0:8918a71cdbe9 81 void *params, void *stack, size_t stackSize, int_t priority)
Sergunb 0:8918a71cdbe9 82 {
Sergunb 0:8918a71cdbe9 83 INT8U err;
Sergunb 0:8918a71cdbe9 84 OS_STK *stackTop;
Sergunb 0:8918a71cdbe9 85
Sergunb 0:8918a71cdbe9 86 //Check stack size
Sergunb 0:8918a71cdbe9 87 if(stackSize == 0)
Sergunb 0:8918a71cdbe9 88 return FALSE;
Sergunb 0:8918a71cdbe9 89
Sergunb 0:8918a71cdbe9 90 //Top of the stack
Sergunb 0:8918a71cdbe9 91 stackTop = (OS_STK *) stack + (stackSize - 1);
Sergunb 0:8918a71cdbe9 92
Sergunb 0:8918a71cdbe9 93 //Search for a free TCB
Sergunb 0:8918a71cdbe9 94 while(priority < (OS_LOWEST_PRIO - 3) && OSTCBPrioTbl[priority] != 0)
Sergunb 0:8918a71cdbe9 95 priority++;
Sergunb 0:8918a71cdbe9 96
Sergunb 0:8918a71cdbe9 97 //No more TCB available?
Sergunb 0:8918a71cdbe9 98 if(priority >= (OS_LOWEST_PRIO - 3))
Sergunb 0:8918a71cdbe9 99 return FALSE;
Sergunb 0:8918a71cdbe9 100
Sergunb 0:8918a71cdbe9 101 //Create a new task
Sergunb 0:8918a71cdbe9 102 err = OSTaskCreateExt(taskCode, params, stackTop, priority, priority,
Sergunb 0:8918a71cdbe9 103 stack, stackSize, NULL, OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
Sergunb 0:8918a71cdbe9 104
Sergunb 0:8918a71cdbe9 105 //Check whether the task was successfully created
Sergunb 0:8918a71cdbe9 106 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 107 {
Sergunb 0:8918a71cdbe9 108 //Save task priority
Sergunb 0:8918a71cdbe9 109 task->prio = priority;
Sergunb 0:8918a71cdbe9 110 //The task was successfully created
Sergunb 0:8918a71cdbe9 111 return TRUE;
Sergunb 0:8918a71cdbe9 112 }
Sergunb 0:8918a71cdbe9 113 else
Sergunb 0:8918a71cdbe9 114 {
Sergunb 0:8918a71cdbe9 115 //Report an error
Sergunb 0:8918a71cdbe9 116 return FALSE;
Sergunb 0:8918a71cdbe9 117 }
Sergunb 0:8918a71cdbe9 118 }
Sergunb 0:8918a71cdbe9 119
Sergunb 0:8918a71cdbe9 120
Sergunb 0:8918a71cdbe9 121 /**
Sergunb 0:8918a71cdbe9 122 * @brief Create a new task
Sergunb 0:8918a71cdbe9 123 * @param[in] name A name identifying the task
Sergunb 0:8918a71cdbe9 124 * @param[in] taskCode Pointer to the task entry function
Sergunb 0:8918a71cdbe9 125 * @param[in] params A pointer to a variable to be passed to the task
Sergunb 0:8918a71cdbe9 126 * @param[in] stackSize The initial size of the stack, in words
Sergunb 0:8918a71cdbe9 127 * @param[in] priority The priority at which the task should run
Sergunb 0:8918a71cdbe9 128 * @return If the function succeeds, the return value is a pointer to the
Sergunb 0:8918a71cdbe9 129 * new task. If the function fails, the return value is NULL
Sergunb 0:8918a71cdbe9 130 **/
Sergunb 0:8918a71cdbe9 131
Sergunb 0:8918a71cdbe9 132 OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode,
Sergunb 0:8918a71cdbe9 133 void *params, size_t stackSize, int_t priority)
Sergunb 0:8918a71cdbe9 134 {
Sergunb 0:8918a71cdbe9 135 //INT8U i;
Sergunb 0:8918a71cdbe9 136 OS_STK *stack;
Sergunb 0:8918a71cdbe9 137
Sergunb 0:8918a71cdbe9 138 //Allocate a memory block to hold the task's stack
Sergunb 0:8918a71cdbe9 139 stack = osAllocMem(stackSize * sizeof(OS_STK));
Sergunb 0:8918a71cdbe9 140
Sergunb 0:8918a71cdbe9 141 //Successful memory allocation?
Sergunb 0:8918a71cdbe9 142 if(stack != NULL)
Sergunb 0:8918a71cdbe9 143 {
Sergunb 0:8918a71cdbe9 144 //Create task
Sergunb 0:8918a71cdbe9 145 if(osCreateStaticTask(&tcbTable[priority], name,
Sergunb 0:8918a71cdbe9 146 taskCode, params, stack, stackSize, priority))
Sergunb 0:8918a71cdbe9 147 {
Sergunb 0:8918a71cdbe9 148 //Return a valid handle
Sergunb 0:8918a71cdbe9 149 return &tcbTable[priority];
Sergunb 0:8918a71cdbe9 150 }
Sergunb 0:8918a71cdbe9 151 else
Sergunb 0:8918a71cdbe9 152 {
Sergunb 0:8918a71cdbe9 153 //Clean up side effects
Sergunb 0:8918a71cdbe9 154 osFreeMem(stack);
Sergunb 0:8918a71cdbe9 155 //Report an error
Sergunb 0:8918a71cdbe9 156 return NULL;
Sergunb 0:8918a71cdbe9 157 }
Sergunb 0:8918a71cdbe9 158 }
Sergunb 0:8918a71cdbe9 159 else
Sergunb 0:8918a71cdbe9 160 {
Sergunb 0:8918a71cdbe9 161 //Memory allocation failed
Sergunb 0:8918a71cdbe9 162 return NULL;
Sergunb 0:8918a71cdbe9 163 }
Sergunb 0:8918a71cdbe9 164 }
Sergunb 0:8918a71cdbe9 165
Sergunb 0:8918a71cdbe9 166
Sergunb 0:8918a71cdbe9 167 /**
Sergunb 0:8918a71cdbe9 168 * @brief Delete a task
Sergunb 0:8918a71cdbe9 169 * @param[in] task Pointer to the task to be deleted
Sergunb 0:8918a71cdbe9 170 **/
Sergunb 0:8918a71cdbe9 171
Sergunb 0:8918a71cdbe9 172 void osDeleteTask(OsTask *task)
Sergunb 0:8918a71cdbe9 173 {
Sergunb 0:8918a71cdbe9 174 //Delete the specified task
Sergunb 0:8918a71cdbe9 175 OSTaskDel(task->prio);
Sergunb 0:8918a71cdbe9 176 }
Sergunb 0:8918a71cdbe9 177
Sergunb 0:8918a71cdbe9 178
Sergunb 0:8918a71cdbe9 179 /**
Sergunb 0:8918a71cdbe9 180 * @brief Delay routine
Sergunb 0:8918a71cdbe9 181 * @param[in] delay Amount of time for which the calling task should block
Sergunb 0:8918a71cdbe9 182 **/
Sergunb 0:8918a71cdbe9 183
Sergunb 0:8918a71cdbe9 184 void osDelayTask(systime_t delay)
Sergunb 0:8918a71cdbe9 185 {
Sergunb 0:8918a71cdbe9 186 INT16U n;
Sergunb 0:8918a71cdbe9 187
Sergunb 0:8918a71cdbe9 188 //Convert milliseconds to system ticks
Sergunb 0:8918a71cdbe9 189 delay = OS_MS_TO_SYSTICKS(delay);
Sergunb 0:8918a71cdbe9 190
Sergunb 0:8918a71cdbe9 191 //Delay the task for the specified duration
Sergunb 0:8918a71cdbe9 192 while(delay > 0)
Sergunb 0:8918a71cdbe9 193 {
Sergunb 0:8918a71cdbe9 194 //The maximum delay is 65535 clock ticks
Sergunb 0:8918a71cdbe9 195 n = MIN(delay, 65535);
Sergunb 0:8918a71cdbe9 196 //Wait for the specified amount of time
Sergunb 0:8918a71cdbe9 197 OSTimeDly(n);
Sergunb 0:8918a71cdbe9 198 //Decrement delay value
Sergunb 0:8918a71cdbe9 199 delay -= n;
Sergunb 0:8918a71cdbe9 200 }
Sergunb 0:8918a71cdbe9 201 }
Sergunb 0:8918a71cdbe9 202
Sergunb 0:8918a71cdbe9 203
Sergunb 0:8918a71cdbe9 204 /**
Sergunb 0:8918a71cdbe9 205 * @brief Yield control to the next task
Sergunb 0:8918a71cdbe9 206 **/
Sergunb 0:8918a71cdbe9 207
Sergunb 0:8918a71cdbe9 208 void osSwitchTask(void)
Sergunb 0:8918a71cdbe9 209 {
Sergunb 0:8918a71cdbe9 210 //Not implemented
Sergunb 0:8918a71cdbe9 211 }
Sergunb 0:8918a71cdbe9 212
Sergunb 0:8918a71cdbe9 213
Sergunb 0:8918a71cdbe9 214 /**
Sergunb 0:8918a71cdbe9 215 * @brief Suspend scheduler activity
Sergunb 0:8918a71cdbe9 216 **/
Sergunb 0:8918a71cdbe9 217
Sergunb 0:8918a71cdbe9 218 void osSuspendAllTasks(void)
Sergunb 0:8918a71cdbe9 219 {
Sergunb 0:8918a71cdbe9 220 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 221 if(OSRunning == OS_TRUE)
Sergunb 0:8918a71cdbe9 222 {
Sergunb 0:8918a71cdbe9 223 //Suspend scheduler activity
Sergunb 0:8918a71cdbe9 224 OSSchedLock();
Sergunb 0:8918a71cdbe9 225 }
Sergunb 0:8918a71cdbe9 226 }
Sergunb 0:8918a71cdbe9 227
Sergunb 0:8918a71cdbe9 228
Sergunb 0:8918a71cdbe9 229 /**
Sergunb 0:8918a71cdbe9 230 * @brief Resume scheduler activity
Sergunb 0:8918a71cdbe9 231 **/
Sergunb 0:8918a71cdbe9 232
Sergunb 0:8918a71cdbe9 233 void osResumeAllTasks(void)
Sergunb 0:8918a71cdbe9 234 {
Sergunb 0:8918a71cdbe9 235 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 236 if(OSRunning == OS_TRUE)
Sergunb 0:8918a71cdbe9 237 {
Sergunb 0:8918a71cdbe9 238 //Resume scheduler activity
Sergunb 0:8918a71cdbe9 239 OSSchedUnlock();
Sergunb 0:8918a71cdbe9 240 }
Sergunb 0:8918a71cdbe9 241 }
Sergunb 0:8918a71cdbe9 242
Sergunb 0:8918a71cdbe9 243
Sergunb 0:8918a71cdbe9 244 /**
Sergunb 0:8918a71cdbe9 245 * @brief Create an event object
Sergunb 0:8918a71cdbe9 246 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 247 * @return The function returns TRUE if the event object was successfully
Sergunb 0:8918a71cdbe9 248 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 249 **/
Sergunb 0:8918a71cdbe9 250
Sergunb 0:8918a71cdbe9 251 bool_t osCreateEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 252 {
Sergunb 0:8918a71cdbe9 253 INT8U err;
Sergunb 0:8918a71cdbe9 254
Sergunb 0:8918a71cdbe9 255 //Create an event flag group
Sergunb 0:8918a71cdbe9 256 event->p = OSFlagCreate(0, &err);
Sergunb 0:8918a71cdbe9 257
Sergunb 0:8918a71cdbe9 258 //Check whether the event flag group was successfully created
Sergunb 0:8918a71cdbe9 259 if(event->p != NULL && err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 260 return TRUE;
Sergunb 0:8918a71cdbe9 261 else
Sergunb 0:8918a71cdbe9 262 return FALSE;
Sergunb 0:8918a71cdbe9 263 }
Sergunb 0:8918a71cdbe9 264
Sergunb 0:8918a71cdbe9 265
Sergunb 0:8918a71cdbe9 266 /**
Sergunb 0:8918a71cdbe9 267 * @brief Delete an event object
Sergunb 0:8918a71cdbe9 268 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 269 **/
Sergunb 0:8918a71cdbe9 270
Sergunb 0:8918a71cdbe9 271 void osDeleteEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 272 {
Sergunb 0:8918a71cdbe9 273 INT8U err;
Sergunb 0:8918a71cdbe9 274
Sergunb 0:8918a71cdbe9 275 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 276 if(OSRunning == OS_TRUE)
Sergunb 0:8918a71cdbe9 277 {
Sergunb 0:8918a71cdbe9 278 //Properly dispose the event object
Sergunb 0:8918a71cdbe9 279 OSFlagDel(event->p, OS_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 280 }
Sergunb 0:8918a71cdbe9 281 }
Sergunb 0:8918a71cdbe9 282
Sergunb 0:8918a71cdbe9 283
Sergunb 0:8918a71cdbe9 284 /**
Sergunb 0:8918a71cdbe9 285 * @brief Set the specified event object to the signaled state
Sergunb 0:8918a71cdbe9 286 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 287 **/
Sergunb 0:8918a71cdbe9 288
Sergunb 0:8918a71cdbe9 289 void osSetEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 290 {
Sergunb 0:8918a71cdbe9 291 INT8U err;
Sergunb 0:8918a71cdbe9 292
Sergunb 0:8918a71cdbe9 293 //Set the specified event to the signaled state
Sergunb 0:8918a71cdbe9 294 OSFlagPost(event->p, 1, OS_FLAG_SET, &err);
Sergunb 0:8918a71cdbe9 295 }
Sergunb 0:8918a71cdbe9 296
Sergunb 0:8918a71cdbe9 297
Sergunb 0:8918a71cdbe9 298 /**
Sergunb 0:8918a71cdbe9 299 * @brief Set the specified event object to the nonsignaled state
Sergunb 0:8918a71cdbe9 300 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 301 **/
Sergunb 0:8918a71cdbe9 302
Sergunb 0:8918a71cdbe9 303 void osResetEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 304 {
Sergunb 0:8918a71cdbe9 305 INT8U err;
Sergunb 0:8918a71cdbe9 306
Sergunb 0:8918a71cdbe9 307 //Force the specified event to the nonsignaled state
Sergunb 0:8918a71cdbe9 308 OSFlagPost(event->p, 1, OS_FLAG_CLR, &err);
Sergunb 0:8918a71cdbe9 309 }
Sergunb 0:8918a71cdbe9 310
Sergunb 0:8918a71cdbe9 311
Sergunb 0:8918a71cdbe9 312 /**
Sergunb 0:8918a71cdbe9 313 * @brief Wait until the specified event is in the signaled state
Sergunb 0:8918a71cdbe9 314 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 315 * @param[in] timeout Timeout interval
Sergunb 0:8918a71cdbe9 316 * @return The function returns TRUE if the state of the specified object is
Sergunb 0:8918a71cdbe9 317 * signaled. FALSE is returned if the timeout interval elapsed
Sergunb 0:8918a71cdbe9 318 **/
Sergunb 0:8918a71cdbe9 319
Sergunb 0:8918a71cdbe9 320 bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Sergunb 0:8918a71cdbe9 321 {
Sergunb 0:8918a71cdbe9 322 INT8U err;
Sergunb 0:8918a71cdbe9 323 INT16U n;
Sergunb 0:8918a71cdbe9 324
Sergunb 0:8918a71cdbe9 325 //Wait until the specified event is in the signaled
Sergunb 0:8918a71cdbe9 326 //state or the timeout interval elapses
Sergunb 0:8918a71cdbe9 327 if(timeout == 0)
Sergunb 0:8918a71cdbe9 328 {
Sergunb 0:8918a71cdbe9 329 //Non-blocking call
Sergunb 0:8918a71cdbe9 330 OSFlagAccept(event->p, 1, OS_FLAG_WAIT_SET_ANY | OS_FLAG_CONSUME, &err);
Sergunb 0:8918a71cdbe9 331 }
Sergunb 0:8918a71cdbe9 332 else if(timeout == INFINITE_DELAY)
Sergunb 0:8918a71cdbe9 333 {
Sergunb 0:8918a71cdbe9 334 //Infinite timeout period
Sergunb 0:8918a71cdbe9 335 OSFlagPend(event->p, 1, OS_FLAG_WAIT_SET_ANY | OS_FLAG_CONSUME, 0, &err);
Sergunb 0:8918a71cdbe9 336 }
Sergunb 0:8918a71cdbe9 337 else
Sergunb 0:8918a71cdbe9 338 {
Sergunb 0:8918a71cdbe9 339 //Convert milliseconds to system ticks
Sergunb 0:8918a71cdbe9 340 timeout = OS_MS_TO_SYSTICKS(timeout);
Sergunb 0:8918a71cdbe9 341
Sergunb 0:8918a71cdbe9 342 //Loop until the assigned time period has elapsed
Sergunb 0:8918a71cdbe9 343 do
Sergunb 0:8918a71cdbe9 344 {
Sergunb 0:8918a71cdbe9 345 //The maximum timeout is 65535 clock ticks
Sergunb 0:8918a71cdbe9 346 n = MIN(timeout, 65535);
Sergunb 0:8918a71cdbe9 347 //Wait for the specified time interval
Sergunb 0:8918a71cdbe9 348 OSFlagPend(event->p, 1, OS_FLAG_WAIT_SET_ANY | OS_FLAG_CONSUME, n, &err);
Sergunb 0:8918a71cdbe9 349 //Decrement timeout value
Sergunb 0:8918a71cdbe9 350 timeout -= n;
Sergunb 0:8918a71cdbe9 351
Sergunb 0:8918a71cdbe9 352 //Check timeout value
Sergunb 0:8918a71cdbe9 353 } while(err == OS_ERR_TIMEOUT && timeout > 0);
Sergunb 0:8918a71cdbe9 354 }
Sergunb 0:8918a71cdbe9 355
Sergunb 0:8918a71cdbe9 356 //Check whether the specified event is set
Sergunb 0:8918a71cdbe9 357 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 358 return TRUE;
Sergunb 0:8918a71cdbe9 359 else
Sergunb 0:8918a71cdbe9 360 return FALSE;
Sergunb 0:8918a71cdbe9 361 }
Sergunb 0:8918a71cdbe9 362
Sergunb 0:8918a71cdbe9 363
Sergunb 0:8918a71cdbe9 364 /**
Sergunb 0:8918a71cdbe9 365 * @brief Set an event object to the signaled state from an interrupt service routine
Sergunb 0:8918a71cdbe9 366 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 367 * @return TRUE if setting the event to signaled state caused a task to unblock
Sergunb 0:8918a71cdbe9 368 * and the unblocked task has a priority higher than the currently running task
Sergunb 0:8918a71cdbe9 369 **/
Sergunb 0:8918a71cdbe9 370
Sergunb 0:8918a71cdbe9 371 bool_t osSetEventFromIsr(OsEvent *event)
Sergunb 0:8918a71cdbe9 372 {
Sergunb 0:8918a71cdbe9 373 INT8U err;
Sergunb 0:8918a71cdbe9 374
Sergunb 0:8918a71cdbe9 375 //Set the specified event to the signaled state
Sergunb 0:8918a71cdbe9 376 OSFlagPost(event->p, 1, OS_FLAG_SET, &err);
Sergunb 0:8918a71cdbe9 377
Sergunb 0:8918a71cdbe9 378 //The return value is not relevant
Sergunb 0:8918a71cdbe9 379 return FALSE;
Sergunb 0:8918a71cdbe9 380 }
Sergunb 0:8918a71cdbe9 381
Sergunb 0:8918a71cdbe9 382
Sergunb 0:8918a71cdbe9 383 /**
Sergunb 0:8918a71cdbe9 384 * @brief Create a semaphore object
Sergunb 0:8918a71cdbe9 385 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 386 * @param[in] count The maximum count for the semaphore object. This value
Sergunb 0:8918a71cdbe9 387 * must be greater than zero
Sergunb 0:8918a71cdbe9 388 * @return The function returns TRUE if the semaphore was successfully
Sergunb 0:8918a71cdbe9 389 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 390 **/
Sergunb 0:8918a71cdbe9 391
Sergunb 0:8918a71cdbe9 392 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Sergunb 0:8918a71cdbe9 393 {
Sergunb 0:8918a71cdbe9 394 //Create a semaphore
Sergunb 0:8918a71cdbe9 395 semaphore->p = OSSemCreate(count);
Sergunb 0:8918a71cdbe9 396
Sergunb 0:8918a71cdbe9 397 //Check whether the semaphore was successfully created
Sergunb 0:8918a71cdbe9 398 if(semaphore->p != NULL)
Sergunb 0:8918a71cdbe9 399 return TRUE;
Sergunb 0:8918a71cdbe9 400 else
Sergunb 0:8918a71cdbe9 401 return FALSE;
Sergunb 0:8918a71cdbe9 402 }
Sergunb 0:8918a71cdbe9 403
Sergunb 0:8918a71cdbe9 404
Sergunb 0:8918a71cdbe9 405 /**
Sergunb 0:8918a71cdbe9 406 * @brief Delete a semaphore object
Sergunb 0:8918a71cdbe9 407 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 408 **/
Sergunb 0:8918a71cdbe9 409
Sergunb 0:8918a71cdbe9 410 void osDeleteSemaphore(OsSemaphore *semaphore)
Sergunb 0:8918a71cdbe9 411 {
Sergunb 0:8918a71cdbe9 412 INT8U err;
Sergunb 0:8918a71cdbe9 413
Sergunb 0:8918a71cdbe9 414 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 415 if(OSRunning == OS_TRUE)
Sergunb 0:8918a71cdbe9 416 {
Sergunb 0:8918a71cdbe9 417 //Properly dispose the specified semaphore
Sergunb 0:8918a71cdbe9 418 OSSemDel(semaphore->p, OS_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 419 }
Sergunb 0:8918a71cdbe9 420 }
Sergunb 0:8918a71cdbe9 421
Sergunb 0:8918a71cdbe9 422
Sergunb 0:8918a71cdbe9 423 /**
Sergunb 0:8918a71cdbe9 424 * @brief Wait for the specified semaphore to be available
Sergunb 0:8918a71cdbe9 425 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 426 * @param[in] timeout Timeout interval
Sergunb 0:8918a71cdbe9 427 * @return The function returns TRUE if the semaphore is available. FALSE is
Sergunb 0:8918a71cdbe9 428 * returned if the timeout interval elapsed
Sergunb 0:8918a71cdbe9 429 **/
Sergunb 0:8918a71cdbe9 430
Sergunb 0:8918a71cdbe9 431 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Sergunb 0:8918a71cdbe9 432 {
Sergunb 0:8918a71cdbe9 433 INT8U err;
Sergunb 0:8918a71cdbe9 434 INT16U n;
Sergunb 0:8918a71cdbe9 435
Sergunb 0:8918a71cdbe9 436 //Wait until the semaphore is available or the timeout interval elapses
Sergunb 0:8918a71cdbe9 437 if(timeout == 0)
Sergunb 0:8918a71cdbe9 438 {
Sergunb 0:8918a71cdbe9 439 //Non-blocking call
Sergunb 0:8918a71cdbe9 440 if(OSSemAccept(semaphore->p) > 0)
Sergunb 0:8918a71cdbe9 441 err = OS_ERR_NONE;
Sergunb 0:8918a71cdbe9 442 else
Sergunb 0:8918a71cdbe9 443 err = OS_ERR_TIMEOUT;
Sergunb 0:8918a71cdbe9 444 }
Sergunb 0:8918a71cdbe9 445 else if(timeout == INFINITE_DELAY)
Sergunb 0:8918a71cdbe9 446 {
Sergunb 0:8918a71cdbe9 447 //Infinite timeout period
Sergunb 0:8918a71cdbe9 448 OSSemPend(semaphore->p, 0, &err);
Sergunb 0:8918a71cdbe9 449 }
Sergunb 0:8918a71cdbe9 450 else
Sergunb 0:8918a71cdbe9 451 {
Sergunb 0:8918a71cdbe9 452 //Convert milliseconds to system ticks
Sergunb 0:8918a71cdbe9 453 timeout = OS_MS_TO_SYSTICKS(timeout);
Sergunb 0:8918a71cdbe9 454
Sergunb 0:8918a71cdbe9 455 //Loop until the assigned time period has elapsed
Sergunb 0:8918a71cdbe9 456 do
Sergunb 0:8918a71cdbe9 457 {
Sergunb 0:8918a71cdbe9 458 //The maximum timeout is 65535 clock ticks
Sergunb 0:8918a71cdbe9 459 n = MIN(timeout, 65535);
Sergunb 0:8918a71cdbe9 460 //Wait for the specified time interval
Sergunb 0:8918a71cdbe9 461 OSSemPend(semaphore->p, n, &err);
Sergunb 0:8918a71cdbe9 462 //Decrement timeout value
Sergunb 0:8918a71cdbe9 463 timeout -= n;
Sergunb 0:8918a71cdbe9 464
Sergunb 0:8918a71cdbe9 465 //Check timeout value
Sergunb 0:8918a71cdbe9 466 } while(err == OS_ERR_TIMEOUT && timeout > 0);
Sergunb 0:8918a71cdbe9 467 }
Sergunb 0:8918a71cdbe9 468
Sergunb 0:8918a71cdbe9 469 //Check whether the specified semaphore is available
Sergunb 0:8918a71cdbe9 470 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 471 return TRUE;
Sergunb 0:8918a71cdbe9 472 else
Sergunb 0:8918a71cdbe9 473 return FALSE;
Sergunb 0:8918a71cdbe9 474 }
Sergunb 0:8918a71cdbe9 475
Sergunb 0:8918a71cdbe9 476
Sergunb 0:8918a71cdbe9 477 /**
Sergunb 0:8918a71cdbe9 478 * @brief Release the specified semaphore object
Sergunb 0:8918a71cdbe9 479 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 480 **/
Sergunb 0:8918a71cdbe9 481
Sergunb 0:8918a71cdbe9 482 void osReleaseSemaphore(OsSemaphore *semaphore)
Sergunb 0:8918a71cdbe9 483 {
Sergunb 0:8918a71cdbe9 484 //Release the semaphore
Sergunb 0:8918a71cdbe9 485 OSSemPost(semaphore->p);
Sergunb 0:8918a71cdbe9 486 }
Sergunb 0:8918a71cdbe9 487
Sergunb 0:8918a71cdbe9 488
Sergunb 0:8918a71cdbe9 489 /**
Sergunb 0:8918a71cdbe9 490 * @brief Create a mutex object
Sergunb 0:8918a71cdbe9 491 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 492 * @return The function returns TRUE if the mutex was successfully
Sergunb 0:8918a71cdbe9 493 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 494 **/
Sergunb 0:8918a71cdbe9 495
Sergunb 0:8918a71cdbe9 496 bool_t osCreateMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 497 {
Sergunb 0:8918a71cdbe9 498 #if 1
Sergunb 0:8918a71cdbe9 499 bool_t status;
Sergunb 0:8918a71cdbe9 500
Sergunb 0:8918a71cdbe9 501 //Create an event object
Sergunb 0:8918a71cdbe9 502 status = osCreateEvent((OsEvent *) mutex);
Sergunb 0:8918a71cdbe9 503
Sergunb 0:8918a71cdbe9 504 //Check whether the event object was successfully created
Sergunb 0:8918a71cdbe9 505 if(status)
Sergunb 0:8918a71cdbe9 506 {
Sergunb 0:8918a71cdbe9 507 //Set event
Sergunb 0:8918a71cdbe9 508 osSetEvent((OsEvent *) mutex);
Sergunb 0:8918a71cdbe9 509 }
Sergunb 0:8918a71cdbe9 510
Sergunb 0:8918a71cdbe9 511 //Return status
Sergunb 0:8918a71cdbe9 512 return status;
Sergunb 0:8918a71cdbe9 513 #else
Sergunb 0:8918a71cdbe9 514 INT8U err;
Sergunb 0:8918a71cdbe9 515
Sergunb 0:8918a71cdbe9 516 //Create a mutex
Sergunb 0:8918a71cdbe9 517 mutex->p = OSMutexCreate(10, &err);
Sergunb 0:8918a71cdbe9 518
Sergunb 0:8918a71cdbe9 519 //Check whether the mutex was successfully created
Sergunb 0:8918a71cdbe9 520 if(mutex->p != NULL && err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 521 return TRUE;
Sergunb 0:8918a71cdbe9 522 else
Sergunb 0:8918a71cdbe9 523 return FALSE;
Sergunb 0:8918a71cdbe9 524 #endif
Sergunb 0:8918a71cdbe9 525 }
Sergunb 0:8918a71cdbe9 526
Sergunb 0:8918a71cdbe9 527
Sergunb 0:8918a71cdbe9 528 /**
Sergunb 0:8918a71cdbe9 529 * @brief Delete a mutex object
Sergunb 0:8918a71cdbe9 530 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 531 **/
Sergunb 0:8918a71cdbe9 532
Sergunb 0:8918a71cdbe9 533 void osDeleteMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 534 {
Sergunb 0:8918a71cdbe9 535 #if 1
Sergunb 0:8918a71cdbe9 536 //Delete event object
Sergunb 0:8918a71cdbe9 537 osDeleteEvent((OsEvent *) mutex);
Sergunb 0:8918a71cdbe9 538 #else
Sergunb 0:8918a71cdbe9 539 INT8U err;
Sergunb 0:8918a71cdbe9 540
Sergunb 0:8918a71cdbe9 541 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 542 if(OSRunning == OS_TRUE)
Sergunb 0:8918a71cdbe9 543 {
Sergunb 0:8918a71cdbe9 544 //Properly dispose the specified mutex
Sergunb 0:8918a71cdbe9 545 OSMutexDel(mutex->p, OS_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 546 }
Sergunb 0:8918a71cdbe9 547 #endif
Sergunb 0:8918a71cdbe9 548 }
Sergunb 0:8918a71cdbe9 549
Sergunb 0:8918a71cdbe9 550
Sergunb 0:8918a71cdbe9 551 /**
Sergunb 0:8918a71cdbe9 552 * @brief Acquire ownership of the specified mutex object
Sergunb 0:8918a71cdbe9 553 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 554 **/
Sergunb 0:8918a71cdbe9 555
Sergunb 0:8918a71cdbe9 556 void osAcquireMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 557 {
Sergunb 0:8918a71cdbe9 558 #if 1
Sergunb 0:8918a71cdbe9 559 //Wait for event
Sergunb 0:8918a71cdbe9 560 osWaitForEvent((OsEvent *) mutex, INFINITE_DELAY);
Sergunb 0:8918a71cdbe9 561 #else
Sergunb 0:8918a71cdbe9 562 INT8U err;
Sergunb 0:8918a71cdbe9 563
Sergunb 0:8918a71cdbe9 564 //Obtain ownership of the mutex object
Sergunb 0:8918a71cdbe9 565 OSMutexPend(mutex->p, 0, &err);
Sergunb 0:8918a71cdbe9 566 #endif
Sergunb 0:8918a71cdbe9 567 }
Sergunb 0:8918a71cdbe9 568
Sergunb 0:8918a71cdbe9 569
Sergunb 0:8918a71cdbe9 570 /**
Sergunb 0:8918a71cdbe9 571 * @brief Release ownership of the specified mutex object
Sergunb 0:8918a71cdbe9 572 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 573 **/
Sergunb 0:8918a71cdbe9 574
Sergunb 0:8918a71cdbe9 575 void osReleaseMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 576 {
Sergunb 0:8918a71cdbe9 577 #if 1
Sergunb 0:8918a71cdbe9 578 //Set event
Sergunb 0:8918a71cdbe9 579 osSetEvent((OsEvent *) mutex);
Sergunb 0:8918a71cdbe9 580 #else
Sergunb 0:8918a71cdbe9 581 //Release ownership of the mutex object
Sergunb 0:8918a71cdbe9 582 OSMutexPost(mutex->p);
Sergunb 0:8918a71cdbe9 583 #endif
Sergunb 0:8918a71cdbe9 584 }
Sergunb 0:8918a71cdbe9 585
Sergunb 0:8918a71cdbe9 586
Sergunb 0:8918a71cdbe9 587 /**
Sergunb 0:8918a71cdbe9 588 * @brief Retrieve system time
Sergunb 0:8918a71cdbe9 589 * @return Number of milliseconds elapsed since the system was last started
Sergunb 0:8918a71cdbe9 590 **/
Sergunb 0:8918a71cdbe9 591
Sergunb 0:8918a71cdbe9 592 systime_t osGetSystemTime(void)
Sergunb 0:8918a71cdbe9 593 {
Sergunb 0:8918a71cdbe9 594 systime_t time;
Sergunb 0:8918a71cdbe9 595
Sergunb 0:8918a71cdbe9 596 //Get current tick count
Sergunb 0:8918a71cdbe9 597 time = OSTimeGet();
Sergunb 0:8918a71cdbe9 598
Sergunb 0:8918a71cdbe9 599 //Convert system ticks to milliseconds
Sergunb 0:8918a71cdbe9 600 return OS_SYSTICKS_TO_MS(time);
Sergunb 0:8918a71cdbe9 601 }
Sergunb 0:8918a71cdbe9 602
Sergunb 0:8918a71cdbe9 603
Sergunb 0:8918a71cdbe9 604 /**
Sergunb 0:8918a71cdbe9 605 * @brief Allocate a memory block
Sergunb 0:8918a71cdbe9 606 * @param[in] size Bytes to allocate
Sergunb 0:8918a71cdbe9 607 * @return A pointer to the allocated memory block or NULL if
Sergunb 0:8918a71cdbe9 608 * there is insufficient memory available
Sergunb 0:8918a71cdbe9 609 **/
Sergunb 0:8918a71cdbe9 610
Sergunb 0:8918a71cdbe9 611 void *osAllocMem(size_t size)
Sergunb 0:8918a71cdbe9 612 {
Sergunb 0:8918a71cdbe9 613 void *p;
Sergunb 0:8918a71cdbe9 614
Sergunb 0:8918a71cdbe9 615 //Enter critical section
Sergunb 0:8918a71cdbe9 616 osSuspendAllTasks();
Sergunb 0:8918a71cdbe9 617 //Allocate a memory block
Sergunb 0:8918a71cdbe9 618 p = malloc(size);
Sergunb 0:8918a71cdbe9 619 //Leave critical section
Sergunb 0:8918a71cdbe9 620 osResumeAllTasks();
Sergunb 0:8918a71cdbe9 621
Sergunb 0:8918a71cdbe9 622 //Debug message
Sergunb 0:8918a71cdbe9 623 TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p);
Sergunb 0:8918a71cdbe9 624
Sergunb 0:8918a71cdbe9 625 //Return a pointer to the newly allocated memory block
Sergunb 0:8918a71cdbe9 626 return p;
Sergunb 0:8918a71cdbe9 627 }
Sergunb 0:8918a71cdbe9 628
Sergunb 0:8918a71cdbe9 629
Sergunb 0:8918a71cdbe9 630 /**
Sergunb 0:8918a71cdbe9 631 * @brief Release a previously allocated memory block
Sergunb 0:8918a71cdbe9 632 * @param[in] p Previously allocated memory block to be freed
Sergunb 0:8918a71cdbe9 633 **/
Sergunb 0:8918a71cdbe9 634
Sergunb 0:8918a71cdbe9 635 void osFreeMem(void *p)
Sergunb 0:8918a71cdbe9 636 {
Sergunb 0:8918a71cdbe9 637 //Make sure the pointer is valid
Sergunb 0:8918a71cdbe9 638 if(p != NULL)
Sergunb 0:8918a71cdbe9 639 {
Sergunb 0:8918a71cdbe9 640 //Debug message
Sergunb 0:8918a71cdbe9 641 TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
Sergunb 0:8918a71cdbe9 642
Sergunb 0:8918a71cdbe9 643 //Enter critical section
Sergunb 0:8918a71cdbe9 644 osSuspendAllTasks();
Sergunb 0:8918a71cdbe9 645 //Free memory block
Sergunb 0:8918a71cdbe9 646 free(p);
Sergunb 0:8918a71cdbe9 647 //Leave critical section
Sergunb 0:8918a71cdbe9 648 osResumeAllTasks();
Sergunb 0:8918a71cdbe9 649 }
Sergunb 0:8918a71cdbe9 650 }
Sergunb 0:8918a71cdbe9 651