os_port_none.c
Go to the documentation of this file.
1 /**
2  * @file os_port_none.c
3  * @brief RTOS-less environment
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 2.4.4
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL TRACE_LEVEL_OFF
31 
32 //Dependencies
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "os_port.h"
36 #include "os_port_none.h"
37 #include "debug.h"
38 
39 //Platform-specific dependencies
40 #if defined(__linux__) || defined(__FreeBSD__)
41  #include <sys/time.h>
42 #elif defined(_WIN32)
43  #include <windows.h>
44 #endif
45 
46 //Tick count
47 volatile systime_t systemTicks = 0;
48 
49 //Default task parameters
51 {
52  0, //Size of the stack
53  0 //Task priority
54 };
55 
56 
57 /**
58  * @brief Kernel initialization
59  **/
60 
61 void osInitKernel(void)
62 {
63  //Initialize tick count
64  systemTicks = 0;
65 }
66 
67 
68 /**
69  * @brief Start kernel
70  **/
71 
72 void osStartKernel(void)
73 {
74  //Not implemented
75 }
76 
77 
78 /**
79  * @brief Create a task
80  * @param[in] name NULL-terminated string identifying the task
81  * @param[in] taskCode Pointer to the task entry function
82  * @param[in] arg Argument passed to the task function
83  * @param[in] params Task parameters
84  * @return Task identifier referencing the newly created task
85  **/
86 
87 OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg,
88  const OsTaskParameters *params)
89 {
90  //Return a dummy task identifier
91  return (OsTaskId) 1;
92 }
93 
94 
95 /**
96  * @brief Delete a task
97  * @param[in] taskId Task identifier referencing the task to be deleted
98  **/
99 
100 void osDeleteTask(OsTaskId taskId)
101 {
102  //Not implemented
103 }
104 
105 
106 /**
107  * @brief Delay routine
108  * @param[in] delay Amount of time for which the calling task should block
109  **/
110 
112 {
113  //Not implemented
114 }
115 
116 
117 /**
118  * @brief Yield control to the next task
119  **/
120 
121 void osSwitchTask(void)
122 {
123  //Not implemented
124 }
125 
126 
127 /**
128  * @brief Suspend scheduler activity
129  **/
130 
132 {
133  //Not implemented
134 }
135 
136 
137 /**
138  * @brief Resume scheduler activity
139  **/
140 
142 {
143  //Not implemented
144 }
145 
146 
147 /**
148  * @brief Create an event object
149  * @param[in] event Pointer to the event object
150  * @return The function returns TRUE if the event object was successfully
151  * created. Otherwise, FALSE is returned
152  **/
153 
155 {
156  //Force the event to the nonsignaled state
157  *event = FALSE;
158  //Event successfully created
159  return TRUE;
160 }
161 
162 
163 /**
164  * @brief Delete an event object
165  * @param[in] event Pointer to the event object
166  **/
167 
168 void osDeleteEvent(OsEvent *event)
169 {
170  //Not implemented
171 }
172 
173 
174 /**
175  * @brief Set the specified event object to the signaled state
176  * @param[in] event Pointer to the event object
177  **/
178 
179 void osSetEvent(OsEvent *event)
180 {
181  //Set the specified event to the signaled state
182  *event = TRUE;
183 }
184 
185 
186 /**
187  * @brief Set the specified event object to the nonsignaled state
188  * @param[in] event Pointer to the event object
189  **/
190 
191 void osResetEvent(OsEvent *event)
192 {
193  //Force the specified event to the nonsignaled state
194  *event = FALSE;
195 }
196 
197 
198 /**
199  * @brief Wait until the specified event is in the signaled state
200  * @param[in] event Pointer to the event object
201  * @param[in] timeout Timeout interval
202  * @return The function returns TRUE if the state of the specified object is
203  * signaled. FALSE is returned if the timeout interval elapsed
204  **/
205 
207 {
208  //Check whether the specified event is set
209  if(*event)
210  {
211  //Clear event
212  *event = FALSE;
213  //The event is in the signaled state
214  return TRUE;
215  }
216  else
217  {
218  //The event is not in the signaled state
219  return FALSE;
220  }
221 }
222 
223 
224 /**
225  * @brief Set an event object to the signaled state from an interrupt service routine
226  * @param[in] event Pointer to the event object
227  * @return TRUE if setting the event to signaled state caused a task to unblock
228  * and the unblocked task has a priority higher than the currently running task
229  **/
230 
232 {
233  //Set the specified event to the signaled state
234  *event = TRUE;
235  //A higher priority task has been woken?
236  return FALSE;
237 }
238 
239 
240 /**
241  * @brief Create a semaphore object
242  * @param[in] semaphore Pointer to the semaphore object
243  * @param[in] count The maximum count for the semaphore object. This value
244  * must be greater than zero
245  * @return The function returns TRUE if the semaphore was successfully
246  * created. Otherwise, FALSE is returned
247  **/
248 
250 {
251  //Create a semaphore
252  *semaphore = count;
253  //The semaphore was successfully created
254  return TRUE;
255 }
256 
257 
258 /**
259  * @brief Delete a semaphore object
260  * @param[in] semaphore Pointer to the semaphore object
261  **/
262 
264 {
265 }
266 
267 
268 /**
269  * @brief Wait for the specified semaphore to be available
270  * @param[in] semaphore Pointer to the semaphore object
271  * @param[in] timeout Timeout interval
272  * @return The function returns TRUE if the semaphore is available. FALSE is
273  * returned if the timeout interval elapsed
274  **/
275 
277 {
278  //Check whether the specified semaphore is available
279  if(*semaphore > 0)
280  {
281  //Decrement semaphore value
282  *semaphore -= 1;
283  //The semaphore is available
284  return TRUE;
285  }
286  else
287  {
288  //The semaphore is not available
289  return FALSE;
290  }
291 }
292 
293 
294 /**
295  * @brief Release the specified semaphore object
296  * @param[in] semaphore Pointer to the semaphore object
297  **/
298 
300 {
301  //Release the semaphore
302  *semaphore += 1;
303 }
304 
305 
306 /**
307  * @brief Create a mutex object
308  * @param[in] mutex Pointer to the mutex object
309  * @return The function returns TRUE if the mutex was successfully
310  * created. Otherwise, FALSE is returned
311  **/
312 
314 {
315  //The mutex was successfully created
316  return TRUE;
317 }
318 
319 
320 /**
321  * @brief Delete a mutex object
322  * @param[in] mutex Pointer to the mutex object
323  **/
324 
325 void osDeleteMutex(OsMutex *mutex)
326 {
327  //Not implemented
328 }
329 
330 
331 /**
332  * @brief Acquire ownership of the specified mutex object
333  * @param[in] mutex Pointer to the mutex object
334  **/
335 
337 {
338  //Not implemented
339 }
340 
341 
342 /**
343  * @brief Release ownership of the specified mutex object
344  * @param[in] mutex Pointer to the mutex object
345  **/
346 
348 {
349  //Not implemented
350 }
351 
352 
353 /**
354  * @brief Retrieve system time
355  * @return Number of milliseconds elapsed since the system was last started
356  **/
357 
359 {
360  systime_t time;
361 
362 #if defined(__linux__) || defined(__FreeBSD__)
363  struct timeval tv;
364  //Get current time
365  gettimeofday(&tv, NULL);
366  //Convert resulting value to milliseconds
367  time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
368 #elif defined(_WIN32)
369  //Get current tick count
370  time = GetTickCount();
371 #else
372  //Get current tick count
373  time = systemTicks;
374 #endif
375 
376  //Convert system ticks to milliseconds
377  return OS_SYSTICKS_TO_MS(time);
378 }
379 
380 
381 /**
382  * @brief Allocate a memory block
383  * @param[in] size Bytes to allocate
384  * @return A pointer to the allocated memory block or NULL if
385  * there is insufficient memory available
386  **/
387 
388 __weak_func void *osAllocMem(size_t size)
389 {
390  void *p;
391 
392  //Allocate a memory block
393  p = malloc(size);
394 
395  //Debug message
396  TRACE_DEBUG("Allocating %" PRIuSIZE " bytes at 0x%08" PRIXPTR "\r\n",
397  size, (uintptr_t) p);
398 
399  //Return a pointer to the newly allocated memory block
400  return p;
401 }
402 
403 
404 /**
405  * @brief Release a previously allocated memory block
406  * @param[in] p Previously allocated memory block to be freed
407  **/
408 
409 __weak_func void osFreeMem(void *p)
410 {
411  //Make sure the pointer is valid
412  if(p != NULL)
413  {
414  //Debug message
415  TRACE_DEBUG("Freeing memory at 0x%08" PRIXPTR "\r\n", (uintptr_t) p);
416 
417  //Free memory block
418  free(p);
419  }
420 }
int bool_t
Definition: compiler_port.h:53
void osDeleteEvent(OsEvent *event)
Delete an event object.
Definition: os_port_none.c:168
uint8_t p
Definition: ndp.h:300
void osDeleteSemaphore(OsSemaphore *semaphore)
Delete a semaphore object.
Definition: os_port_none.c:263
void osDeleteTask(OsTaskId taskId)
Delete a task.
Definition: os_port_none.c:100
#define TRUE
Definition: os_port.h:50
Event object.
void osResumeAllTasks(void)
Resume scheduler activity.
Definition: os_port_none.c:141
systime_t osGetSystemTime(void)
Retrieve system time.
Definition: os_port_none.c:358
char_t name[]
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
Definition: os_port_none.c:347
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
Definition: os_port_none.c:206
Semaphore object.
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
Definition: os_port_none.c:191
#define FALSE
Definition: os_port.h:46
int32_t tv_sec
Definition: bsd_socket.h:573
void osSwitchTask(void)
Yield control to the next task.
Definition: os_port_none.c:121
void osReleaseSemaphore(OsSemaphore *semaphore)
Release the specified semaphore object.
Definition: os_port_none.c:299
void(* OsTaskCode)(void *arg)
Task routine.
bool_t osWaitForSemaphore(OsSemaphore *semaphore, systime_t timeout)
Wait for the specified semaphore to be available.
Definition: os_port_none.c:276
__weak_func void osFreeMem(void *p)
Release a previously allocated memory block.
Definition: os_port_none.c:409
void osInitKernel(void)
Kernel initialization.
Definition: os_port_none.c:61
void osDeleteMutex(OsMutex *mutex)
Delete a mutex object.
Definition: os_port_none.c:325
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
Definition: os_port_none.c:336
#define OS_SYSTICKS_TO_MS(n)
void osSuspendAllTasks(void)
Suspend scheduler activity.
Definition: os_port_none.c:131
Task parameters.
void osStartKernel(void)
Start kernel.
Definition: os_port_none.c:72
bool_t osSetEventFromIsr(OsEvent *event)
Set an event object to the signaled state from an interrupt service routine.
Definition: os_port_none.c:231
int32_t tv_usec
Definition: bsd_socket.h:574
volatile systime_t systemTicks
Definition: os_port_none.c:47
Mutex object.
uint32_t systime_t
System time.
#define TRACE_DEBUG(...)
Definition: debug.h:107
char char_t
Definition: compiler_port.h:48
RTOS-less environment.
uint32_t time
Timeout structure.
Definition: bsd_socket.h:572
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
Definition: os_port_none.c:388
thread_t * OsTaskId
Task identifier.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
Definition: os_port_none.c:154
#define PRIuSIZE
OsTaskId osCreateTask(const char_t *name, OsTaskCode taskCode, void *arg, const OsTaskParameters *params)
Create a task.
Definition: os_port_none.c:87
unsigned int uint_t
Definition: compiler_port.h:50
RTOS abstraction layer.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Definition: os_port_none.c:179
Debugging facilities.
bool_t osCreateSemaphore(OsSemaphore *semaphore, uint_t count)
Create a semaphore object.
Definition: os_port_none.c:249
void osDelayTask(systime_t delay)
Delay routine.
Definition: os_port_none.c:111
const OsTaskParameters OS_TASK_DEFAULT_PARAMS
Definition: os_port_none.c:50
bool_t osCreateMutex(OsMutex *mutex)
Create a mutex object.
Definition: os_port_none.c:313