StateOS
Loading...
Searching...
No Matches
osbase.h
1/******************************************************************************
2
3 @file StateOS: osbase.h
4 @author Rajmund Szymanski
5 @date 18.11.2025
6 @brief This file contains basic definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOSBASE_H
33#define __STATEOSBASE_H
34
35#ifndef DEBUG
36#ifndef NDEBUG
37#define NDEBUG
38#endif // NDEBUG
39#endif // DEBUG
40
41#include <stdint.h>
42#include <limits.h>
43#include <stdbool.h>
44#include <assert.h>
45#include "osport.h"
46
47/* -------------------------------------------------------------------------- */
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/* -------------------------------------------------------------------------- *
54 * FOR INTERNAL USE *
55 * -------------------------------------------------------------------------- */
56
57#ifndef OS_ATOMICS
58#define OS_ATOMICS 0
59#endif
60
61/* -------------------------------------------------------------------------- */
62
63#ifndef OS_TASK_EXIT
64#define OS_TASK_EXIT 0
65#endif
66
67/* -------------------------------------------------------------------------- */
68
69#ifndef OS_GUARD_SIZE
70#define OS_GUARD_SIZE 0
71#endif
72
73#ifndef __MPU_USED
74#define __MPU_USED 0
75#endif
76
77#if __MPU_USED
78#define __STKALIGN __ALIGNED(OS_GUARD_SIZE)
79#else
80#define __STKALIGN
81#endif
82
83#ifndef __ALIGNED
84#define __ALIGNED(x)
85#endif
86
87/* -------------------------------------------------------------------------- */
88
89#ifndef OS_TIMER_SIZE
90#define OS_TIMER_SIZE 32
91#endif
92
93/* -------------------------------------------------------------------------- */
94
95#if HW_TIMER_SIZE == 16
96typedef uint16_t hwt_t;
97#elif HW_TIMER_SIZE == 32
98typedef uint32_t hwt_t;
99#elif HW_TIMER_SIZE == 64
100typedef uint64_t hwt_t;
101#elif HW_TIMER_SIZE
102#error Invalid HW_TIMER_SIZE value!
103#endif
104
110
111#if OS_TIMER_SIZE == 16
112typedef uint16_t cnt_t;
113#define CNT_MAX 0xFFFFU
114#elif OS_TIMER_SIZE == 32
115typedef uint32_t cnt_t;
116#define CNT_MAX 0xFFFFFFFFUL
117#elif OS_TIMER_SIZE == 64
118typedef uint64_t cnt_t;
119#define CNT_MAX 0xFFFFFFFFFFFFFFFFULL
120#else
121#error Invalid OS_TIMER_SIZE value!
122#endif
123
124#define CNT_LIMIT ((CNT_MAX)-((CNT_MAX)>>((OS_TIMER_SIZE)/4)))
125
127
128/* -------------------------------------------------------------------------- */
129
130typedef struct __mtx mtx_t; // mutex
131typedef struct __tmr tmr_t; // timer
132typedef struct __tsk tsk_t; // task
136
137typedef void fun_t(void);
138typedef void fun_a(void*);
139typedef void act_t(unsigned);
140
141/* -------------------------------------------------------------------------- */
142
144enum
145{
152};
153
155
156/* -------------------------------------------------------------------------- */
157
158// object id
159
160typedef enum __tid
161{
162 ID_STOPPED = 0, // inactive object
163 ID_READY, // active task
164 ID_TIMER, // active timer
165
166} tid_t;
167
168/* -------------------------------------------------------------------------- */
169
170// object header
171
172typedef struct __obj
173{
174 tsk_t * queue; // next process in the BLOCKED queue
175 void * res; // allocated object's resource
176
177} obj_t;
178
179#define _OBJ_INIT() { NULL, NULL }
180
181/* -------------------------------------------------------------------------- */
182
183__STATIC_INLINE
184void core_obj_init( obj_t *obj, void *res )
185{
186 obj->res = res;
187}
188
189/* -------------------------------------------------------------------------- */
190
191// timer / task header
192
193typedef struct __hdr
194{
195 void * prev; // previous object (timer / task) in the READY queue
196 void * next; // next object (timer / task) in the READY queue
197 tid_t id; // timer / task id
198
199} hdr_t;
200
201#define _HDR_INIT() { NULL, NULL, ID_STOPPED }
202
203/* -------------------------------------------------------------------------- */
204
205__STATIC_INLINE
206void core_hdr_init( hdr_t *hdr )
207{
208 hdr->id = ID_STOPPED;
209}
210
211/* -------------------------------------------------------------------------- */
212
213// system data
214
215typedef struct __sys
216{
217 tsk_t * cur; // pointer to the current task control block
218 tsk_t * tsk; // task executed in system suspend mode
219#if HW_TIMER_SIZE < OS_TIMER_SIZE
220 volatile
221 cnt_t cnt; // system timer counter
222#endif
223#if OS_ROBIN && HW_TIMER_SIZE
224 tsk_t * turn; // tick-less mode: the task whose turn has started the time slice
225 bool robin; // tick-less mode: the time slice has expired, the turn ends at the next context switch
226#endif
227
228} sys_t;
229
230/* -------------------------------------------------------------------------- */
231
235
236#if (OS_FREQUENCY)/1000000 > 0 && (OS_FREQUENCY)/1000000 < (CNT_MAX)
237#define USEC (cnt_t)((OS_FREQUENCY)/1000000)
238#endif
239#if (OS_FREQUENCY)/1000 > 0 && (OS_FREQUENCY)/1000 < (CNT_MAX)
240#define MSEC (cnt_t)((OS_FREQUENCY)/1000)
241#endif
242#if (OS_FREQUENCY) < (CNT_MAX)
243#define SEC ((cnt_t)(OS_FREQUENCY))
244#endif
245#if (OS_FREQUENCY) < (CNT_MAX)/60
246#define MIN ((cnt_t)(OS_FREQUENCY)*60)
247#endif
248#if (OS_FREQUENCY) < (CNT_MAX)/3600
249#define HOUR ((cnt_t)(OS_FREQUENCY)*3600)
250#endif
251#if (OS_FREQUENCY) < (CNT_MAX)/86400
252#define DAY ((cnt_t)(OS_FREQUENCY)*86400)
253#endif
254
255/* -------------------------------------------------------------------------- */
256
257#ifndef IMMEDIATE
258#define IMMEDIATE 0
259#endif
260#ifndef INFINITE
261#define INFINITE CNT_MAX
262#endif
263
265
266/* -------------------------------------------------------------------------- */
267
268#define RELEASED ((void *)((uintptr_t)0 - 1)) // released resouces
269
270/* -------------------------------------------------------------------------- */
271
272#ifdef __cplusplus
273}
274#endif
275
276#endif//__STATEOSBASE_H
void fun_a(void *)
timer/task procedure with argument
Definition osbase.h:138
void fun_t(void)
timer/task procedure
Definition osbase.h:137
void act_t(unsigned)
signal action
Definition osbase.h:139
@ E_TIMEOUT
process was released as a result of the end of the timer countdown
Definition osbase.h:150
@ OWNERDEAD
previous owner has stopped
Definition osbase.h:151
@ E_DELETED
process was released as a result of deleting the supervising object
Definition osbase.h:149
@ E_FAILURE
process was released as a result of any failure
Definition osbase.h:147
@ E_SUCCESS
process was released as a result of taking the supervising object
Definition osbase.h:146
@ E_STOPPED
process was released as a result of reset the supervising object
Definition osbase.h:148