TNeoKernel  v1.03
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_common.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * TNeoKernel: real-time kernel initially based on TNKernel
4  *
5  * TNKernel: copyright © 2004, 2013 Yuri Tiomkin.
6  * PIC32-specific routines: copyright © 2013, 2014 Anders Montonen.
7  * TNeoKernel: copyright © 2014 Dmitry Frank.
8  *
9  * TNeoKernel was born as a thorough review and re-implementation of
10  * TNKernel. The new kernel has well-formed code, inherited bugs are fixed
11  * as well as new features being added, and it is tested carefully with
12  * unit-tests.
13  *
14  * API is changed somewhat, so it's not 100% compatible with TNKernel,
15  * hence the new name: TNeoKernel.
16  *
17  * Permission to use, copy, modify, and distribute this software in source
18  * and binary forms and its documentation for any purpose and without fee
19  * is hereby granted, provided that the above copyright notice appear
20  * in all copies and that both that copyright notice and this permission
21  * notice appear in supporting documentation.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE DMITRY FRANK AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DMITRY FRANK OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
36 
37 /**
38  * \file
39  *
40  * Definitions used through the whole kernel.
41  */
42 
43 #ifndef _TN_COMMON_H
44 #define _TN_COMMON_H
45 
46 /*******************************************************************************
47  * INCLUDED FILES
48  ******************************************************************************/
49 
50 //-- Configuration constants
51 /**
52  * In this case, you should use macro like this: `#TN_MAKE_ALIG(struct my_struct)`.
53  * This way is used in the majority of TNKernel ports. (actually, in all ports
54  * except the one by AlexB)
55  */
56 #define TN_API_MAKE_ALIG_ARG__TYPE 1
57 
58 /**
59  * In this case, you should use macro like this: `#TN_MAKE_ALIG(sizeof(struct
60  * my_struct))`. This way is stated in TNKernel docs and used in the port for
61  * dsPIC/PIC24/PIC32 by AlexB.
62  */
63 #define TN_API_MAKE_ALIG_ARG__SIZE 2
64 
65 //--- As a starting point, you might want to copy tn_cfg_default.h -> tn_cfg.h,
66 // and then edit it if you want to change default configuration.
67 // NOTE: the file tn_cfg.h is specified in .hgignore file, in order to not include
68 // project-specific configuration in the common TNKernel repository.
69 #include "tn_cfg.h"
70 
71 //--- default cfg file is included too, so that you are free to not set
72 // all available options in your tn_cfg.h file.
73 #include "tn_cfg_default.h"
74 
75 
76 #ifdef __cplusplus
77 extern "C" { /*}*/
78 #endif
79 
80 /*******************************************************************************
81  * PUBLIC TYPES
82  ******************************************************************************/
83 
84 /**
85  * Magic number for object validity verification
86  */
87 enum TN_ObjId {
88  TN_ID_TASK = 0x47ABCF69, //!< id for tasks
89  TN_ID_SEMAPHORE = 0x6FA173EB, //!< id for semaphores
90  TN_ID_EVENTGRP = 0x5E224F25, //!< id for event groups
91  TN_ID_DATAQUEUE = 0x8C8A6C89, //!< id for data queues
92  TN_ID_FSMEMORYPOOL = 0x26B7CE8B, //!< id for fixed memory pools
93  TN_ID_MUTEX = 0x17129E45, //!< id for mutexes
94  TN_ID_TIMER = 0x9A937FBC, //!< id for timers
95 };
96 
97 /**
98  * Result code returned by kernel services.
99  */
100 enum TN_RCode {
101  ///
102  /// Successful operation
103  TN_RC_OK = 0,
104  ///
105  /// Timeout (consult `#TN_Timeout` for details).
106  /// @see `#TN_Timeout`
108  ///
109  /// This code is returned in the following cases:
110  /// * Trying to increment semaphore count more than its max count;
111  /// * Trying to return extra memory block to fixed memory pool.
112  /// @see tn_sem.h
113  /// @see tn_fmem.h
115  ///
116  /// Wrong context error: returned if function is called from
117  /// non-acceptable context. Required context suggested for every
118  /// function by badges:
119  ///
120  /// * $(TN_CALL_FROM_TASK) - function can be called from task;
121  /// * $(TN_CALL_FROM_ISR) - function can be called from ISR.
122  ///
123  /// @see `tn_sys_context_get()`
124  /// @see `enum #TN_Context`
126  ///
127  /// Wrong task state error: requested operation requires different
128  /// task state
130  ///
131  /// This code is returned by most of the kernel functions when
132  /// wrong params were given to function. This error code can be returned
133  /// if only build-time option `#TN_CHECK_PARAM` is non-zero
134  /// @see `#TN_CHECK_PARAM`
136  ///
137  /// Illegal usage. Returned in the following cases:
138  /// * task tries to unlock or delete the mutex that is locked by different
139  /// task,
140  /// * task tries to lock mutex with priority ceiling whose priority is
141  /// lower than task's priority
142  /// @see tn_mutex.h
144  ///
145  /// Returned when user tries to perform some operation on invalid object
146  /// (mutex, semaphore, etc).
147  /// Object validity is checked by comparing special `id_...` field value
148  /// with the value from `enum #TN_ObjId`
149  /// @see `#TN_CHECK_PARAM`
151  /// Object for whose event task was waiting is deleted.
153  /// Task was released from waiting forcibly because some other task
154  /// called `tn_task_release_wait()`
156  /// Internal kernel error, should never be returned by kernel services.
157  /// If it is returned, it's a bug in the kernel.
159 };
160 
161 /**
162  * Prototype for task body function.
163  */
164 typedef void (TN_TaskBody)(void *param);
165 
166 
167 /**
168  * The value representing maximum number of system ticks to wait.
169  *
170  * Assume user called some system function, and it can't perform its job
171  * immediately (say, it needs to lock mutex but it is already locked, etc).
172  *
173  * So, function can wait or return an error. There are possible `timeout`
174  * values and appropriate behavior of the function:
175  *
176  * - `timeout` is set to `0`: function doesn't wait at all, no context switch
177  * is performed, `#TN_RC_TIMEOUT` is returned immediately.
178  * - `timeout` is set to `#TN_WAIT_INFINITE`: function waits until it
179  * eventually **can** perform its job. Timeout is not taken in account, so
180  * `#TN_RC_TIMEOUT` is never returned.
181  * - `timeout` is set to other value: function waits at most specified number
182  * of system ticks. Strictly speaking, it waits from `(timeout - 1)` to
183  * `timeout` ticks. So, if you specify that timeout is 1, be aware that it
184  * might actually don't wait at all: if $(TN_SYS_TIMER_LINK) interrupt
185  * happens just while function is putting task to wait (with interrupts
186  * disabled), then ISR will be executed right after function puts task to
187  * wait. Then `tn_tick_int_processing()` will immediately remove the task
188  * from wait queue and make it runnable again.
189  *
190  * So, to guarantee that task waits *at least* 1 system tick, you should
191  * specify timeout value of `2`.
192  *
193  * **Note** also that there are other possible ways to make task runnable:
194  *
195  * - if task waits because of call to `tn_task_sleep()`, it may be woken up by
196  * some other task, by means of `tn_task_wakeup()`. In this case,
197  * `tn_task_sleep()` returns `#TN_RC_OK`.
198  * - independently of the wait reason, task may be released from wait forcibly,
199  * by means of `tn_task_release_wait()`. It this case, `#TN_RC_FORCED` is
200  * returned by the waiting function. (the usage of the
201  * `tn_task_release_wait()` function is discouraged though)
202  */
203 typedef unsigned long TN_Timeout;
204 
205 
206 /*******************************************************************************
207  * GLOBAL VARIABLES
208  ******************************************************************************/
209 
210 /*******************************************************************************
211  * DEFINITIONS
212  ******************************************************************************/
213 
214 
215 /// NULL pointer definition
216 #ifndef NULL
217 # define NULL ((void *)0)
218 #endif
219 
220 /// boolean type definition
221 #ifndef BOOL
222 # define BOOL int
223 #endif
224 
225 /// `true` value definition for type `#BOOL`
226 #ifndef TRUE
227 # define TRUE (1 == 1)
228 #endif
229 
230 /// `false` value definition for type `#BOOL`
231 #ifndef FALSE
232 # define FALSE (1 == 0)
233 #endif
234 
235 /**
236  * Macro for making a number a multiple of `sizeof(#TN_UWord)`, should be used
237  * with fixed memory block pool. See `tn_fmem_create()` for usage example.
238  */
239 #define TN_MAKE_ALIG_SIZE(a) \
240  (((a) + (sizeof(TN_UWord) - 1)) & (~(sizeof(TN_UWord) - 1)))
241 
242 //-- self-checking
243 #if (!defined TN_API_MAKE_ALIG_ARG)
244 # error TN_API_MAKE_ALIG_ARG is not defined
245 #elif (!defined TN_API_MAKE_ALIG_ARG__TYPE)
246 # error TN_API_MAKE_ALIG_ARG__TYPE is not defined
247 #elif (!defined TN_API_MAKE_ALIG_ARG__SIZE)
248 # error TN_API_MAKE_ALIG_ARG__SIZE is not defined
249 #endif
250 
251 //-- define MAKE_ALIG accordingly to config
252 /**
253  * The same as `#TN_MAKE_ALIG_SIZE` but its behavior depends on the option
254  * `#TN_API_MAKE_ALIG_ARG`
255  *
256  * \attention it is recommended to use `#TN_MAKE_ALIG_SIZE` macro instead
257  * of this one, in order to avoid confusion caused by various
258  * TNKernel ports: refer to the section \ref tnkernel_diff_make_alig for details.
259  */
260 #if (TN_API_MAKE_ALIG_ARG == TN_API_MAKE_ALIG_ARG__TYPE)
261 # define TN_MAKE_ALIG(a) TN_MAKE_ALIG_SIZE(sizeof(a))
262 #elif (TN_API_MAKE_ALIG_ARG == TN_API_MAKE_ALIG_ARG__SIZE)
263 # define TN_MAKE_ALIG(a) TN_MAKE_ALIG_SIZE(a)
264 #else
265 # error wrong TN_API_MAKE_ALIG_ARG
266 #endif
267 
268 
269 
270 /*******************************************************************************
271  * PUBLIC FUNCTION PROTOTYPES
272  ******************************************************************************/
273 
274 #ifdef __cplusplus
275 } /* extern "C" */
276 #endif
277 
278 #endif // _TN_COMMON_H
279 
280 /*******************************************************************************
281  * end of file
282  ******************************************************************************/
283 
284 
id for data queues
Definition: tn_common.h:91
Wrong context error: returned if function is called from non-acceptable context.
Definition: tn_common.h:125
Returned when user tries to perform some operation on invalid object (mutex, semaphore, etc).
Definition: tn_common.h:150
Task was released from waiting forcibly because some other task called tn_task_release_wait() ...
Definition: tn_common.h:155
id for fixed memory pools
Definition: tn_common.h:92
TN_RCode
Result code returned by kernel services.
Definition: tn_common.h:100
TN_ObjId
Magic number for object validity verification.
Definition: tn_common.h:87
unsigned long TN_Timeout
The value representing maximum number of system ticks to wait.
Definition: tn_common.h:203
Successful operation.
Definition: tn_common.h:103
id for semaphores
Definition: tn_common.h:89
Illegal usage.
Definition: tn_common.h:143
This code is returned in the following cases:
Definition: tn_common.h:114
Internal kernel error, should never be returned by kernel services.
Definition: tn_common.h:158
id for tasks
Definition: tn_common.h:88
id for event groups
Definition: tn_common.h:90
This code is returned by most of the kernel functions when wrong params were given to function...
Definition: tn_common.h:135
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:164
Wrong task state error: requested operation requires different task state.
Definition: tn_common.h:129
Object for whose event task was waiting is deleted.
Definition: tn_common.h:152
Timeout (consult TN_Timeout for details).
Definition: tn_common.h:107
id for mutexes
Definition: tn_common.h:93
TNeoKernel default configuration file, to be copied as tn_cfg.h.
id for timers
Definition: tn_common.h:94