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_ucos3.c
Sergunb 0:8918a71cdbe9 3 * @brief RTOS abstraction layer (Micrium uC/OS-III)
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_ucos3.h"
Sergunb 0:8918a71cdbe9 36 #include "debug.h"
Sergunb 0:8918a71cdbe9 37
Sergunb 0:8918a71cdbe9 38 //Forward declaration of functions
Sergunb 0:8918a71cdbe9 39 void osIdleTaskHook(void);
Sergunb 0:8918a71cdbe9 40
Sergunb 0:8918a71cdbe9 41 //Variables
Sergunb 0:8918a71cdbe9 42 static OS_TCB *tcbTable[OS_PORT_MAX_TASKS];
Sergunb 0:8918a71cdbe9 43 static CPU_STK *stkTable[OS_PORT_MAX_TASKS];
Sergunb 0:8918a71cdbe9 44
Sergunb 0:8918a71cdbe9 45
Sergunb 0:8918a71cdbe9 46 /**
Sergunb 0:8918a71cdbe9 47 * @brief Kernel initialization
Sergunb 0:8918a71cdbe9 48 **/
Sergunb 0:8918a71cdbe9 49
Sergunb 0:8918a71cdbe9 50 void osInitKernel(void)
Sergunb 0:8918a71cdbe9 51 {
Sergunb 0:8918a71cdbe9 52 OS_ERR err;
Sergunb 0:8918a71cdbe9 53
Sergunb 0:8918a71cdbe9 54 //Initialize tables
Sergunb 0:8918a71cdbe9 55 memset(tcbTable, 0, sizeof(tcbTable));
Sergunb 0:8918a71cdbe9 56 memset(stkTable, 0, sizeof(stkTable));
Sergunb 0:8918a71cdbe9 57
Sergunb 0:8918a71cdbe9 58 //Scheduler initialization
Sergunb 0:8918a71cdbe9 59 OSInit(&err);
Sergunb 0:8918a71cdbe9 60
Sergunb 0:8918a71cdbe9 61 //Set idle task hook
Sergunb 0:8918a71cdbe9 62 OS_AppIdleTaskHookPtr = osIdleTaskHook;
Sergunb 0:8918a71cdbe9 63 }
Sergunb 0:8918a71cdbe9 64
Sergunb 0:8918a71cdbe9 65
Sergunb 0:8918a71cdbe9 66 /**
Sergunb 0:8918a71cdbe9 67 * @brief Start kernel
Sergunb 0:8918a71cdbe9 68 **/
Sergunb 0:8918a71cdbe9 69
Sergunb 0:8918a71cdbe9 70 void osStartKernel(void)
Sergunb 0:8918a71cdbe9 71 {
Sergunb 0:8918a71cdbe9 72 OS_ERR err;
Sergunb 0:8918a71cdbe9 73
Sergunb 0:8918a71cdbe9 74 //Start the scheduler
Sergunb 0:8918a71cdbe9 75 OSStart(&err);
Sergunb 0:8918a71cdbe9 76 }
Sergunb 0:8918a71cdbe9 77
Sergunb 0:8918a71cdbe9 78
Sergunb 0:8918a71cdbe9 79 /**
Sergunb 0:8918a71cdbe9 80 * @brief Create a static task
Sergunb 0:8918a71cdbe9 81 * @param[out] task Pointer to the task structure
Sergunb 0:8918a71cdbe9 82 * @param[in] name A name identifying the task
Sergunb 0:8918a71cdbe9 83 * @param[in] taskCode Pointer to the task entry function
Sergunb 0:8918a71cdbe9 84 * @param[in] params A pointer to a variable to be passed to the task
Sergunb 0:8918a71cdbe9 85 * @param[in] stack Pointer to the stack
Sergunb 0:8918a71cdbe9 86 * @param[in] stackSize The initial size of the stack, in words
Sergunb 0:8918a71cdbe9 87 * @param[in] priority The priority at which the task should run
Sergunb 0:8918a71cdbe9 88 * @return The function returns TRUE if the task was successfully
Sergunb 0:8918a71cdbe9 89 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 90 **/
Sergunb 0:8918a71cdbe9 91
Sergunb 0:8918a71cdbe9 92 bool_t osCreateStaticTask(OsTask *task, const char_t *name, OsTaskCode taskCode,
Sergunb 0:8918a71cdbe9 93 void *params, void *stack, size_t stackSize, int_t priority)
Sergunb 0:8918a71cdbe9 94 {
Sergunb 0:8918a71cdbe9 95 OS_ERR err;
Sergunb 0:8918a71cdbe9 96 CPU_STK stackLimit;
Sergunb 0:8918a71cdbe9 97
Sergunb 0:8918a71cdbe9 98 //The watermark limit is used to monitor and ensure that the stack does not overflow
Sergunb 0:8918a71cdbe9 99 stackLimit = stackSize / 10;
Sergunb 0:8918a71cdbe9 100
Sergunb 0:8918a71cdbe9 101 //Create a new task
Sergunb 0:8918a71cdbe9 102 OSTaskCreate(task, (CPU_CHAR *) name, taskCode, params,
Sergunb 0:8918a71cdbe9 103 priority, stack, stackLimit, stackSize, 0, 1, NULL,
Sergunb 0:8918a71cdbe9 104 OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err);
Sergunb 0:8918a71cdbe9 105
Sergunb 0:8918a71cdbe9 106 //Check whether the task was successfully created
Sergunb 0:8918a71cdbe9 107 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 108 return TRUE;
Sergunb 0:8918a71cdbe9 109 else
Sergunb 0:8918a71cdbe9 110 return FALSE;
Sergunb 0:8918a71cdbe9 111 }
Sergunb 0:8918a71cdbe9 112
Sergunb 0:8918a71cdbe9 113
Sergunb 0:8918a71cdbe9 114 /**
Sergunb 0:8918a71cdbe9 115 * @brief Create a new task
Sergunb 0:8918a71cdbe9 116 * @param[in] name A name identifying the task
Sergunb 0:8918a71cdbe9 117 * @param[in] taskCode Pointer to the task entry function
Sergunb 0:8918a71cdbe9 118 * @param[in] params A pointer to a variable to be passed to the task
Sergunb 0:8918a71cdbe9 119 * @param[in] stackSize The initial size of the stack, in words
Sergunb 0:8918a71cdbe9 120 * @param[in] priority The priority at which the task should run
Sergunb 0:8918a71cdbe9 121 * @return If the function succeeds, the return value is a pointer to the
Sergunb 0:8918a71cdbe9 122 * new task. If the function fails, the return value is NULL
Sergunb 0:8918a71cdbe9 123 **/
Sergunb 0:8918a71cdbe9 124
Sergunb 0:8918a71cdbe9 125 OsTask *osCreateTask(const char_t *name, OsTaskCode taskCode,
Sergunb 0:8918a71cdbe9 126 void *params, size_t stackSize, int_t priority)
Sergunb 0:8918a71cdbe9 127 {
Sergunb 0:8918a71cdbe9 128 OS_ERR err;
Sergunb 0:8918a71cdbe9 129 CPU_INT32U i;
Sergunb 0:8918a71cdbe9 130 CPU_STK stackLimit;
Sergunb 0:8918a71cdbe9 131 OS_TCB *task;
Sergunb 0:8918a71cdbe9 132 CPU_STK *stack;
Sergunb 0:8918a71cdbe9 133
Sergunb 0:8918a71cdbe9 134 //The watermark limit is used to monitor and ensure that the stack does not overflow
Sergunb 0:8918a71cdbe9 135 stackLimit = stackSize / 10;
Sergunb 0:8918a71cdbe9 136
Sergunb 0:8918a71cdbe9 137 //Enter critical section
Sergunb 0:8918a71cdbe9 138 osSuspendAllTasks();
Sergunb 0:8918a71cdbe9 139
Sergunb 0:8918a71cdbe9 140 //Loop through TCB table
Sergunb 0:8918a71cdbe9 141 for(i = 0; i < OS_PORT_MAX_TASKS; i++)
Sergunb 0:8918a71cdbe9 142 {
Sergunb 0:8918a71cdbe9 143 //Check whether the current entry is free
Sergunb 0:8918a71cdbe9 144 if(tcbTable[i] == NULL)
Sergunb 0:8918a71cdbe9 145 break;
Sergunb 0:8918a71cdbe9 146 }
Sergunb 0:8918a71cdbe9 147
Sergunb 0:8918a71cdbe9 148 //Any entry available in the table?
Sergunb 0:8918a71cdbe9 149 if(i < OS_PORT_MAX_TASKS)
Sergunb 0:8918a71cdbe9 150 {
Sergunb 0:8918a71cdbe9 151 //Allocate a memory block to hold the task's control block
Sergunb 0:8918a71cdbe9 152 task = osAllocMem(sizeof(OS_TCB));
Sergunb 0:8918a71cdbe9 153
Sergunb 0:8918a71cdbe9 154 //Successful memory allocation?
Sergunb 0:8918a71cdbe9 155 if(task != NULL)
Sergunb 0:8918a71cdbe9 156 {
Sergunb 0:8918a71cdbe9 157 //Allocate a memory block to hold the task's stack
Sergunb 0:8918a71cdbe9 158 stack = osAllocMem(stackSize * sizeof(CPU_STK));
Sergunb 0:8918a71cdbe9 159
Sergunb 0:8918a71cdbe9 160 //Successful memory allocation?
Sergunb 0:8918a71cdbe9 161 if(stack != NULL)
Sergunb 0:8918a71cdbe9 162 {
Sergunb 0:8918a71cdbe9 163 //Create a new task
Sergunb 0:8918a71cdbe9 164 OSTaskCreate(task, (CPU_CHAR *) name, taskCode, params,
Sergunb 0:8918a71cdbe9 165 priority, stack, stackLimit, stackSize, 0, 1, NULL,
Sergunb 0:8918a71cdbe9 166 OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR, &err);
Sergunb 0:8918a71cdbe9 167
Sergunb 0:8918a71cdbe9 168 //Check the return value
Sergunb 0:8918a71cdbe9 169 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 170 {
Sergunb 0:8918a71cdbe9 171 //Save TCB base address
Sergunb 0:8918a71cdbe9 172 tcbTable[i] = task;
Sergunb 0:8918a71cdbe9 173 //Save stack base address
Sergunb 0:8918a71cdbe9 174 stkTable[i] = stack;
Sergunb 0:8918a71cdbe9 175 }
Sergunb 0:8918a71cdbe9 176 else
Sergunb 0:8918a71cdbe9 177 {
Sergunb 0:8918a71cdbe9 178 //Clean up side effects
Sergunb 0:8918a71cdbe9 179 osFreeMem(task);
Sergunb 0:8918a71cdbe9 180 osFreeMem(stack);
Sergunb 0:8918a71cdbe9 181 }
Sergunb 0:8918a71cdbe9 182 }
Sergunb 0:8918a71cdbe9 183 else
Sergunb 0:8918a71cdbe9 184 {
Sergunb 0:8918a71cdbe9 185 //Memory allocation failed
Sergunb 0:8918a71cdbe9 186 err = OS_ERR_MEM_FULL;
Sergunb 0:8918a71cdbe9 187 //Clean up side effects
Sergunb 0:8918a71cdbe9 188 osFreeMem(task);
Sergunb 0:8918a71cdbe9 189 }
Sergunb 0:8918a71cdbe9 190 }
Sergunb 0:8918a71cdbe9 191 else
Sergunb 0:8918a71cdbe9 192 {
Sergunb 0:8918a71cdbe9 193 //Memory allocation failed
Sergunb 0:8918a71cdbe9 194 err = OS_ERR_MEM_FULL;
Sergunb 0:8918a71cdbe9 195 }
Sergunb 0:8918a71cdbe9 196 }
Sergunb 0:8918a71cdbe9 197 else
Sergunb 0:8918a71cdbe9 198 {
Sergunb 0:8918a71cdbe9 199 //No entry available in the table
Sergunb 0:8918a71cdbe9 200 err = OS_ERR_MEM_FULL;
Sergunb 0:8918a71cdbe9 201 }
Sergunb 0:8918a71cdbe9 202
Sergunb 0:8918a71cdbe9 203 //Leave critical section
Sergunb 0:8918a71cdbe9 204 osResumeAllTasks();
Sergunb 0:8918a71cdbe9 205
Sergunb 0:8918a71cdbe9 206 //Check whether the task was successfully created
Sergunb 0:8918a71cdbe9 207 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 208 return task;
Sergunb 0:8918a71cdbe9 209 else
Sergunb 0:8918a71cdbe9 210 return NULL;
Sergunb 0:8918a71cdbe9 211 }
Sergunb 0:8918a71cdbe9 212
Sergunb 0:8918a71cdbe9 213
Sergunb 0:8918a71cdbe9 214 /**
Sergunb 0:8918a71cdbe9 215 * @brief Delete a task
Sergunb 0:8918a71cdbe9 216 * @param[in] task Pointer to the task to be deleted
Sergunb 0:8918a71cdbe9 217 **/
Sergunb 0:8918a71cdbe9 218
Sergunb 0:8918a71cdbe9 219 void osDeleteTask(OsTask *task)
Sergunb 0:8918a71cdbe9 220 {
Sergunb 0:8918a71cdbe9 221 OS_ERR err;
Sergunb 0:8918a71cdbe9 222
Sergunb 0:8918a71cdbe9 223 //Delete the specified task
Sergunb 0:8918a71cdbe9 224 OSTaskDel(task, &err);
Sergunb 0:8918a71cdbe9 225 }
Sergunb 0:8918a71cdbe9 226
Sergunb 0:8918a71cdbe9 227
Sergunb 0:8918a71cdbe9 228 /**
Sergunb 0:8918a71cdbe9 229 * @brief Delay routine
Sergunb 0:8918a71cdbe9 230 * @param[in] delay Amount of time for which the calling task should block
Sergunb 0:8918a71cdbe9 231 **/
Sergunb 0:8918a71cdbe9 232
Sergunb 0:8918a71cdbe9 233 void osDelayTask(systime_t delay)
Sergunb 0:8918a71cdbe9 234 {
Sergunb 0:8918a71cdbe9 235 OS_ERR err;
Sergunb 0:8918a71cdbe9 236
Sergunb 0:8918a71cdbe9 237 //Delay the task for the specified duration
Sergunb 0:8918a71cdbe9 238 OSTimeDly(OS_MS_TO_SYSTICKS(delay), OS_OPT_TIME_DLY, &err);
Sergunb 0:8918a71cdbe9 239 }
Sergunb 0:8918a71cdbe9 240
Sergunb 0:8918a71cdbe9 241
Sergunb 0:8918a71cdbe9 242 /**
Sergunb 0:8918a71cdbe9 243 * @brief Yield control to the next task
Sergunb 0:8918a71cdbe9 244 **/
Sergunb 0:8918a71cdbe9 245
Sergunb 0:8918a71cdbe9 246 void osSwitchTask(void)
Sergunb 0:8918a71cdbe9 247 {
Sergunb 0:8918a71cdbe9 248 //Force a context switch
Sergunb 0:8918a71cdbe9 249 OSSched();
Sergunb 0:8918a71cdbe9 250 }
Sergunb 0:8918a71cdbe9 251
Sergunb 0:8918a71cdbe9 252
Sergunb 0:8918a71cdbe9 253 /**
Sergunb 0:8918a71cdbe9 254 * @brief Suspend scheduler activity
Sergunb 0:8918a71cdbe9 255 **/
Sergunb 0:8918a71cdbe9 256
Sergunb 0:8918a71cdbe9 257 void osSuspendAllTasks(void)
Sergunb 0:8918a71cdbe9 258 {
Sergunb 0:8918a71cdbe9 259 OS_ERR err;
Sergunb 0:8918a71cdbe9 260
Sergunb 0:8918a71cdbe9 261 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 262 if(OSRunning == OS_STATE_OS_RUNNING)
Sergunb 0:8918a71cdbe9 263 {
Sergunb 0:8918a71cdbe9 264 //Suspend scheduler activity
Sergunb 0:8918a71cdbe9 265 OSSchedLock(&err);
Sergunb 0:8918a71cdbe9 266 }
Sergunb 0:8918a71cdbe9 267 }
Sergunb 0:8918a71cdbe9 268
Sergunb 0:8918a71cdbe9 269
Sergunb 0:8918a71cdbe9 270 /**
Sergunb 0:8918a71cdbe9 271 * @brief Resume scheduler activity
Sergunb 0:8918a71cdbe9 272 **/
Sergunb 0:8918a71cdbe9 273
Sergunb 0:8918a71cdbe9 274 void osResumeAllTasks(void)
Sergunb 0:8918a71cdbe9 275 {
Sergunb 0:8918a71cdbe9 276 OS_ERR err;
Sergunb 0:8918a71cdbe9 277
Sergunb 0:8918a71cdbe9 278 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 279 if(OSRunning == OS_STATE_OS_RUNNING)
Sergunb 0:8918a71cdbe9 280 {
Sergunb 0:8918a71cdbe9 281 //Resume scheduler activity
Sergunb 0:8918a71cdbe9 282 OSSchedUnlock(&err);
Sergunb 0:8918a71cdbe9 283 }
Sergunb 0:8918a71cdbe9 284 }
Sergunb 0:8918a71cdbe9 285
Sergunb 0:8918a71cdbe9 286
Sergunb 0:8918a71cdbe9 287 /**
Sergunb 0:8918a71cdbe9 288 * @brief Create an event object
Sergunb 0:8918a71cdbe9 289 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 290 * @return The function returns TRUE if the event object was successfully
Sergunb 0:8918a71cdbe9 291 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 292 **/
Sergunb 0:8918a71cdbe9 293
Sergunb 0:8918a71cdbe9 294 bool_t osCreateEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 295 {
Sergunb 0:8918a71cdbe9 296 OS_ERR err;
Sergunb 0:8918a71cdbe9 297
Sergunb 0:8918a71cdbe9 298 //Create an event flag group
Sergunb 0:8918a71cdbe9 299 OSFlagCreate(event, "EVENT", 0, &err);
Sergunb 0:8918a71cdbe9 300
Sergunb 0:8918a71cdbe9 301 //Check whether the event flag group was successfully created
Sergunb 0:8918a71cdbe9 302 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 303 return TRUE;
Sergunb 0:8918a71cdbe9 304 else
Sergunb 0:8918a71cdbe9 305 return FALSE;
Sergunb 0:8918a71cdbe9 306 }
Sergunb 0:8918a71cdbe9 307
Sergunb 0:8918a71cdbe9 308
Sergunb 0:8918a71cdbe9 309 /**
Sergunb 0:8918a71cdbe9 310 * @brief Delete an event object
Sergunb 0:8918a71cdbe9 311 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 312 **/
Sergunb 0:8918a71cdbe9 313
Sergunb 0:8918a71cdbe9 314 void osDeleteEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 315 {
Sergunb 0:8918a71cdbe9 316 OS_ERR err;
Sergunb 0:8918a71cdbe9 317
Sergunb 0:8918a71cdbe9 318 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 319 if(OSRunning == OS_STATE_OS_RUNNING)
Sergunb 0:8918a71cdbe9 320 {
Sergunb 0:8918a71cdbe9 321 //Properly dispose the event object
Sergunb 0:8918a71cdbe9 322 OSFlagDel(event, OS_OPT_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 323 }
Sergunb 0:8918a71cdbe9 324 }
Sergunb 0:8918a71cdbe9 325
Sergunb 0:8918a71cdbe9 326
Sergunb 0:8918a71cdbe9 327 /**
Sergunb 0:8918a71cdbe9 328 * @brief Set the specified event object to the signaled state
Sergunb 0:8918a71cdbe9 329 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 330 **/
Sergunb 0:8918a71cdbe9 331
Sergunb 0:8918a71cdbe9 332 void osSetEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 333 {
Sergunb 0:8918a71cdbe9 334 OS_ERR err;
Sergunb 0:8918a71cdbe9 335
Sergunb 0:8918a71cdbe9 336 //Set the specified event to the signaled state
Sergunb 0:8918a71cdbe9 337 OSFlagPost(event, 1, OS_OPT_POST_FLAG_SET, &err);
Sergunb 0:8918a71cdbe9 338 }
Sergunb 0:8918a71cdbe9 339
Sergunb 0:8918a71cdbe9 340
Sergunb 0:8918a71cdbe9 341 /**
Sergunb 0:8918a71cdbe9 342 * @brief Set the specified event object to the nonsignaled state
Sergunb 0:8918a71cdbe9 343 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 344 **/
Sergunb 0:8918a71cdbe9 345
Sergunb 0:8918a71cdbe9 346 void osResetEvent(OsEvent *event)
Sergunb 0:8918a71cdbe9 347 {
Sergunb 0:8918a71cdbe9 348 OS_ERR err;
Sergunb 0:8918a71cdbe9 349
Sergunb 0:8918a71cdbe9 350 //Force the specified event to the nonsignaled state
Sergunb 0:8918a71cdbe9 351 OSFlagPost(event, 1, OS_OPT_POST_FLAG_CLR, &err);
Sergunb 0:8918a71cdbe9 352 }
Sergunb 0:8918a71cdbe9 353
Sergunb 0:8918a71cdbe9 354
Sergunb 0:8918a71cdbe9 355 /**
Sergunb 0:8918a71cdbe9 356 * @brief Wait until the specified event is in the signaled state
Sergunb 0:8918a71cdbe9 357 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 358 * @param[in] timeout Timeout interval
Sergunb 0:8918a71cdbe9 359 * @return The function returns TRUE if the state of the specified object is
Sergunb 0:8918a71cdbe9 360 * signaled. FALSE is returned if the timeout interval elapsed
Sergunb 0:8918a71cdbe9 361 **/
Sergunb 0:8918a71cdbe9 362
Sergunb 0:8918a71cdbe9 363 bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Sergunb 0:8918a71cdbe9 364 {
Sergunb 0:8918a71cdbe9 365 OS_ERR err;
Sergunb 0:8918a71cdbe9 366
Sergunb 0:8918a71cdbe9 367 //Wait until the specified event is in the signaled
Sergunb 0:8918a71cdbe9 368 //state or the timeout interval elapses
Sergunb 0:8918a71cdbe9 369 if(timeout == 0)
Sergunb 0:8918a71cdbe9 370 {
Sergunb 0:8918a71cdbe9 371 //Non-blocking call
Sergunb 0:8918a71cdbe9 372 OSFlagPend(event, 1, 0, OS_OPT_PEND_FLAG_SET_ANY |
Sergunb 0:8918a71cdbe9 373 OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_NON_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 374 }
Sergunb 0:8918a71cdbe9 375 else if(timeout == INFINITE_DELAY)
Sergunb 0:8918a71cdbe9 376 {
Sergunb 0:8918a71cdbe9 377 //Infinite timeout period
Sergunb 0:8918a71cdbe9 378 OSFlagPend(event, 1, 0, OS_OPT_PEND_FLAG_SET_ANY |
Sergunb 0:8918a71cdbe9 379 OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 380 }
Sergunb 0:8918a71cdbe9 381 else
Sergunb 0:8918a71cdbe9 382 {
Sergunb 0:8918a71cdbe9 383 //Wait until the specified event becomes set
Sergunb 0:8918a71cdbe9 384 OSFlagPend(event, 1, OS_MS_TO_SYSTICKS(timeout), OS_OPT_PEND_FLAG_SET_ANY |
Sergunb 0:8918a71cdbe9 385 OS_OPT_PEND_FLAG_CONSUME | OS_OPT_PEND_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 386 }
Sergunb 0:8918a71cdbe9 387
Sergunb 0:8918a71cdbe9 388 //Check whether the specified event is set
Sergunb 0:8918a71cdbe9 389 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 390 return TRUE;
Sergunb 0:8918a71cdbe9 391 else
Sergunb 0:8918a71cdbe9 392 return FALSE;
Sergunb 0:8918a71cdbe9 393 }
Sergunb 0:8918a71cdbe9 394
Sergunb 0:8918a71cdbe9 395
Sergunb 0:8918a71cdbe9 396 /**
Sergunb 0:8918a71cdbe9 397 * @brief Set an event object to the signaled state from an interrupt service routine
Sergunb 0:8918a71cdbe9 398 * @param[in] event Pointer to the event object
Sergunb 0:8918a71cdbe9 399 * @return TRUE if setting the event to signaled state caused a task to unblock
Sergunb 0:8918a71cdbe9 400 * and the unblocked task has a priority higher than the currently running task
Sergunb 0:8918a71cdbe9 401 **/
Sergunb 0:8918a71cdbe9 402
Sergunb 0:8918a71cdbe9 403 bool_t osSetEventFromIsr(OsEvent *event)
Sergunb 0:8918a71cdbe9 404 {
Sergunb 0:8918a71cdbe9 405 OS_ERR err;
Sergunb 0:8918a71cdbe9 406
Sergunb 0:8918a71cdbe9 407 //Set the specified event to the signaled state
Sergunb 0:8918a71cdbe9 408 OSFlagPost(event, 1, OS_OPT_POST_FLAG_SET, &err);
Sergunb 0:8918a71cdbe9 409
Sergunb 0:8918a71cdbe9 410 //The return value is not relevant
Sergunb 0:8918a71cdbe9 411 return FALSE;
Sergunb 0:8918a71cdbe9 412 }
Sergunb 0:8918a71cdbe9 413
Sergunb 0:8918a71cdbe9 414
Sergunb 0:8918a71cdbe9 415 /**
Sergunb 0:8918a71cdbe9 416 * @brief Create a semaphore object
Sergunb 0:8918a71cdbe9 417 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 418 * @param[in] count The maximum count for the semaphore object. This value
Sergunb 0:8918a71cdbe9 419 * must be greater than zero
Sergunb 0:8918a71cdbe9 420 * @return The function returns TRUE if the semaphore was successfully
Sergunb 0:8918a71cdbe9 421 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 422 **/
Sergunb 0:8918a71cdbe9 423
Sergunb 0:8918a71cdbe9 424 bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Sergunb 0:8918a71cdbe9 425 {
Sergunb 0:8918a71cdbe9 426 OS_ERR err;
Sergunb 0:8918a71cdbe9 427
Sergunb 0:8918a71cdbe9 428 //Create a semaphore
Sergunb 0:8918a71cdbe9 429 OSSemCreate(semaphore, "SEMAPHORE", count, &err);
Sergunb 0:8918a71cdbe9 430
Sergunb 0:8918a71cdbe9 431 //Check whether the semaphore was successfully created
Sergunb 0:8918a71cdbe9 432 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 433 return TRUE;
Sergunb 0:8918a71cdbe9 434 else
Sergunb 0:8918a71cdbe9 435 return FALSE;
Sergunb 0:8918a71cdbe9 436 }
Sergunb 0:8918a71cdbe9 437
Sergunb 0:8918a71cdbe9 438
Sergunb 0:8918a71cdbe9 439 /**
Sergunb 0:8918a71cdbe9 440 * @brief Delete a semaphore object
Sergunb 0:8918a71cdbe9 441 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 442 **/
Sergunb 0:8918a71cdbe9 443
Sergunb 0:8918a71cdbe9 444 void osDeleteSemaphore(OsSemaphore *semaphore)
Sergunb 0:8918a71cdbe9 445 {
Sergunb 0:8918a71cdbe9 446 OS_ERR err;
Sergunb 0:8918a71cdbe9 447
Sergunb 0:8918a71cdbe9 448 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 449 if(OSRunning == OS_STATE_OS_RUNNING)
Sergunb 0:8918a71cdbe9 450 {
Sergunb 0:8918a71cdbe9 451 //Properly dispose the specified semaphore
Sergunb 0:8918a71cdbe9 452 OSSemDel(semaphore, OS_OPT_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 453 }
Sergunb 0:8918a71cdbe9 454 }
Sergunb 0:8918a71cdbe9 455
Sergunb 0:8918a71cdbe9 456
Sergunb 0:8918a71cdbe9 457 /**
Sergunb 0:8918a71cdbe9 458 * @brief Wait for the specified semaphore to be available
Sergunb 0:8918a71cdbe9 459 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 460 * @param[in] timeout Timeout interval
Sergunb 0:8918a71cdbe9 461 * @return The function returns TRUE if the semaphore is available. FALSE is
Sergunb 0:8918a71cdbe9 462 * returned if the timeout interval elapsed
Sergunb 0:8918a71cdbe9 463 **/
Sergunb 0:8918a71cdbe9 464
Sergunb 0:8918a71cdbe9 465 bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Sergunb 0:8918a71cdbe9 466 {
Sergunb 0:8918a71cdbe9 467 OS_ERR err;
Sergunb 0:8918a71cdbe9 468
Sergunb 0:8918a71cdbe9 469 //Wait until the semaphore is available or the timeout interval elapses
Sergunb 0:8918a71cdbe9 470 if(timeout == 0)
Sergunb 0:8918a71cdbe9 471 {
Sergunb 0:8918a71cdbe9 472 //Non-blocking call
Sergunb 0:8918a71cdbe9 473 OSSemPend(semaphore, 0, OS_OPT_PEND_NON_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 474 }
Sergunb 0:8918a71cdbe9 475 else if(timeout == INFINITE_DELAY)
Sergunb 0:8918a71cdbe9 476 {
Sergunb 0:8918a71cdbe9 477 //Infinite timeout period
Sergunb 0:8918a71cdbe9 478 OSSemPend(semaphore, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 479 }
Sergunb 0:8918a71cdbe9 480 else
Sergunb 0:8918a71cdbe9 481 {
Sergunb 0:8918a71cdbe9 482 //Wait until the specified semaphore becomes available
Sergunb 0:8918a71cdbe9 483 OSSemPend(semaphore, OS_MS_TO_SYSTICKS(timeout),
Sergunb 0:8918a71cdbe9 484 OS_OPT_PEND_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 485 }
Sergunb 0:8918a71cdbe9 486
Sergunb 0:8918a71cdbe9 487 //Check whether the specified semaphore is available
Sergunb 0:8918a71cdbe9 488 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 489 return TRUE;
Sergunb 0:8918a71cdbe9 490 else
Sergunb 0:8918a71cdbe9 491 return FALSE;
Sergunb 0:8918a71cdbe9 492 }
Sergunb 0:8918a71cdbe9 493
Sergunb 0:8918a71cdbe9 494
Sergunb 0:8918a71cdbe9 495 /**
Sergunb 0:8918a71cdbe9 496 * @brief Release the specified semaphore object
Sergunb 0:8918a71cdbe9 497 * @param[in] semaphore Pointer to the semaphore object
Sergunb 0:8918a71cdbe9 498 **/
Sergunb 0:8918a71cdbe9 499
Sergunb 0:8918a71cdbe9 500 void osReleaseSemaphore(OsSemaphore *semaphore)
Sergunb 0:8918a71cdbe9 501 {
Sergunb 0:8918a71cdbe9 502 OS_ERR err;
Sergunb 0:8918a71cdbe9 503
Sergunb 0:8918a71cdbe9 504 //Release the semaphore
Sergunb 0:8918a71cdbe9 505 OSSemPost(semaphore, OS_OPT_POST_1, &err);
Sergunb 0:8918a71cdbe9 506 }
Sergunb 0:8918a71cdbe9 507
Sergunb 0:8918a71cdbe9 508
Sergunb 0:8918a71cdbe9 509 /**
Sergunb 0:8918a71cdbe9 510 * @brief Create a mutex object
Sergunb 0:8918a71cdbe9 511 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 512 * @return The function returns TRUE if the mutex was successfully
Sergunb 0:8918a71cdbe9 513 * created. Otherwise, FALSE is returned
Sergunb 0:8918a71cdbe9 514 **/
Sergunb 0:8918a71cdbe9 515
Sergunb 0:8918a71cdbe9 516 bool_t osCreateMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 517 {
Sergunb 0:8918a71cdbe9 518 OS_ERR err;
Sergunb 0:8918a71cdbe9 519
Sergunb 0:8918a71cdbe9 520 //Create a mutex
Sergunb 0:8918a71cdbe9 521 OSMutexCreate(mutex, "MUTEX", &err);
Sergunb 0:8918a71cdbe9 522
Sergunb 0:8918a71cdbe9 523 //Check whether the mutex was successfully created
Sergunb 0:8918a71cdbe9 524 if(err == OS_ERR_NONE)
Sergunb 0:8918a71cdbe9 525 return TRUE;
Sergunb 0:8918a71cdbe9 526 else
Sergunb 0:8918a71cdbe9 527 return FALSE;
Sergunb 0:8918a71cdbe9 528 }
Sergunb 0:8918a71cdbe9 529
Sergunb 0:8918a71cdbe9 530
Sergunb 0:8918a71cdbe9 531 /**
Sergunb 0:8918a71cdbe9 532 * @brief Delete a mutex object
Sergunb 0:8918a71cdbe9 533 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 534 **/
Sergunb 0:8918a71cdbe9 535
Sergunb 0:8918a71cdbe9 536 void osDeleteMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 537 {
Sergunb 0:8918a71cdbe9 538 OS_ERR err;
Sergunb 0:8918a71cdbe9 539
Sergunb 0:8918a71cdbe9 540 //Make sure the operating system is running
Sergunb 0:8918a71cdbe9 541 if(OSRunning == OS_STATE_OS_RUNNING)
Sergunb 0:8918a71cdbe9 542 {
Sergunb 0:8918a71cdbe9 543 //Properly dispose the specified mutex
Sergunb 0:8918a71cdbe9 544 OSMutexDel(mutex, OS_OPT_DEL_ALWAYS, &err);
Sergunb 0:8918a71cdbe9 545 }
Sergunb 0:8918a71cdbe9 546 }
Sergunb 0:8918a71cdbe9 547
Sergunb 0:8918a71cdbe9 548
Sergunb 0:8918a71cdbe9 549 /**
Sergunb 0:8918a71cdbe9 550 * @brief Acquire ownership of the specified mutex object
Sergunb 0:8918a71cdbe9 551 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 552 **/
Sergunb 0:8918a71cdbe9 553
Sergunb 0:8918a71cdbe9 554 void osAcquireMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 555 {
Sergunb 0:8918a71cdbe9 556 OS_ERR err;
Sergunb 0:8918a71cdbe9 557
Sergunb 0:8918a71cdbe9 558 //Obtain ownership of the mutex object
Sergunb 0:8918a71cdbe9 559 OSMutexPend(mutex, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
Sergunb 0:8918a71cdbe9 560 }
Sergunb 0:8918a71cdbe9 561
Sergunb 0:8918a71cdbe9 562
Sergunb 0:8918a71cdbe9 563 /**
Sergunb 0:8918a71cdbe9 564 * @brief Release ownership of the specified mutex object
Sergunb 0:8918a71cdbe9 565 * @param[in] mutex Pointer to the mutex object
Sergunb 0:8918a71cdbe9 566 **/
Sergunb 0:8918a71cdbe9 567
Sergunb 0:8918a71cdbe9 568 void osReleaseMutex(OsMutex *mutex)
Sergunb 0:8918a71cdbe9 569 {
Sergunb 0:8918a71cdbe9 570 OS_ERR err;
Sergunb 0:8918a71cdbe9 571
Sergunb 0:8918a71cdbe9 572 //Release ownership of the mutex object
Sergunb 0:8918a71cdbe9 573 OSMutexPost(mutex, OS_OPT_POST_NONE, &err);
Sergunb 0:8918a71cdbe9 574 }
Sergunb 0:8918a71cdbe9 575
Sergunb 0:8918a71cdbe9 576
Sergunb 0:8918a71cdbe9 577 /**
Sergunb 0:8918a71cdbe9 578 * @brief Retrieve system time
Sergunb 0:8918a71cdbe9 579 * @return Number of milliseconds elapsed since the system was last started
Sergunb 0:8918a71cdbe9 580 **/
Sergunb 0:8918a71cdbe9 581
Sergunb 0:8918a71cdbe9 582 systime_t osGetSystemTime(void)
Sergunb 0:8918a71cdbe9 583 {
Sergunb 0:8918a71cdbe9 584 OS_ERR err;
Sergunb 0:8918a71cdbe9 585 systime_t time;
Sergunb 0:8918a71cdbe9 586
Sergunb 0:8918a71cdbe9 587 //Get current tick count
Sergunb 0:8918a71cdbe9 588 time = OSTimeGet(&err);
Sergunb 0:8918a71cdbe9 589
Sergunb 0:8918a71cdbe9 590 //Convert system ticks to milliseconds
Sergunb 0:8918a71cdbe9 591 return OS_SYSTICKS_TO_MS(time);
Sergunb 0:8918a71cdbe9 592 }
Sergunb 0:8918a71cdbe9 593
Sergunb 0:8918a71cdbe9 594
Sergunb 0:8918a71cdbe9 595 /**
Sergunb 0:8918a71cdbe9 596 * @brief Allocate a memory block
Sergunb 0:8918a71cdbe9 597 * @param[in] size Bytes to allocate
Sergunb 0:8918a71cdbe9 598 * @return A pointer to the allocated memory block or NULL if
Sergunb 0:8918a71cdbe9 599 * there is insufficient memory available
Sergunb 0:8918a71cdbe9 600 **/
Sergunb 0:8918a71cdbe9 601
Sergunb 0:8918a71cdbe9 602 void *osAllocMem(size_t size)
Sergunb 0:8918a71cdbe9 603 {
Sergunb 0:8918a71cdbe9 604 void *p;
Sergunb 0:8918a71cdbe9 605
Sergunb 0:8918a71cdbe9 606 //Enter critical section
Sergunb 0:8918a71cdbe9 607 osSuspendAllTasks();
Sergunb 0:8918a71cdbe9 608 //Allocate a memory block
Sergunb 0:8918a71cdbe9 609 p = malloc(size);
Sergunb 0:8918a71cdbe9 610 //Leave critical section
Sergunb 0:8918a71cdbe9 611 osResumeAllTasks();
Sergunb 0:8918a71cdbe9 612
Sergunb 0:8918a71cdbe9 613 //Debug message
Sergunb 0:8918a71cdbe9 614 TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n", size, (uintptr_t) p);
Sergunb 0:8918a71cdbe9 615
Sergunb 0:8918a71cdbe9 616 //Return a pointer to the newly allocated memory block
Sergunb 0:8918a71cdbe9 617 return p;
Sergunb 0:8918a71cdbe9 618 }
Sergunb 0:8918a71cdbe9 619
Sergunb 0:8918a71cdbe9 620
Sergunb 0:8918a71cdbe9 621 /**
Sergunb 0:8918a71cdbe9 622 * @brief Release a previously allocated memory block
Sergunb 0:8918a71cdbe9 623 * @param[in] p Previously allocated memory block to be freed
Sergunb 0:8918a71cdbe9 624 **/
Sergunb 0:8918a71cdbe9 625
Sergunb 0:8918a71cdbe9 626 void osFreeMem(void *p)
Sergunb 0:8918a71cdbe9 627 {
Sergunb 0:8918a71cdbe9 628 //Make sure the pointer is valid
Sergunb 0:8918a71cdbe9 629 if(p != NULL)
Sergunb 0:8918a71cdbe9 630 {
Sergunb 0:8918a71cdbe9 631 //Debug message
Sergunb 0:8918a71cdbe9 632 TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
Sergunb 0:8918a71cdbe9 633
Sergunb 0:8918a71cdbe9 634 //Enter critical section
Sergunb 0:8918a71cdbe9 635 osSuspendAllTasks();
Sergunb 0:8918a71cdbe9 636 //Free memory block
Sergunb 0:8918a71cdbe9 637 free(p);
Sergunb 0:8918a71cdbe9 638 //Leave critical section
Sergunb 0:8918a71cdbe9 639 osResumeAllTasks();
Sergunb 0:8918a71cdbe9 640 }
Sergunb 0:8918a71cdbe9 641 }
Sergunb 0:8918a71cdbe9 642
Sergunb 0:8918a71cdbe9 643
Sergunb 0:8918a71cdbe9 644 /**
Sergunb 0:8918a71cdbe9 645 * @brief Idle task hook
Sergunb 0:8918a71cdbe9 646 **/
Sergunb 0:8918a71cdbe9 647
Sergunb 0:8918a71cdbe9 648 void osIdleTaskHook(void)
Sergunb 0:8918a71cdbe9 649 {
Sergunb 0:8918a71cdbe9 650 uint_t i;
Sergunb 0:8918a71cdbe9 651
Sergunb 0:8918a71cdbe9 652 //Loop through TCB table
Sergunb 0:8918a71cdbe9 653 for(i = 0; i < OS_PORT_MAX_TASKS; i++)
Sergunb 0:8918a71cdbe9 654 {
Sergunb 0:8918a71cdbe9 655 //Check whether current entry is used
Sergunb 0:8918a71cdbe9 656 if(tcbTable[i] != NULL)
Sergunb 0:8918a71cdbe9 657 {
Sergunb 0:8918a71cdbe9 658 //Wait for task termination
Sergunb 0:8918a71cdbe9 659 if(tcbTable[i]->TaskState == OS_TASK_STATE_DEL)
Sergunb 0:8918a71cdbe9 660 {
Sergunb 0:8918a71cdbe9 661 //Free previously allocated resources
Sergunb 0:8918a71cdbe9 662 osFreeMem(stkTable[i]);
Sergunb 0:8918a71cdbe9 663 osFreeMem(tcbTable[i]);
Sergunb 0:8918a71cdbe9 664
Sergunb 0:8918a71cdbe9 665 //Mark the entry as free
Sergunb 0:8918a71cdbe9 666 stkTable[i] = NULL;
Sergunb 0:8918a71cdbe9 667 tcbTable[i] = NULL;
Sergunb 0:8918a71cdbe9 668 }
Sergunb 0:8918a71cdbe9 669 }
Sergunb 0:8918a71cdbe9 670 }
Sergunb 0:8918a71cdbe9 671 }
Sergunb 0:8918a71cdbe9 672