TNeoKernel  v1.01
 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 };
95 
96 /**
97  * Result code returned by kernel services.
98  */
99 enum TN_RCode {
100  ///
101  /// Successful operation
102  TN_RC_OK = 0,
103  ///
104  /// Timeout (consult `#TN_Timeout` for details).
105  /// @see `#TN_Timeout`
107  ///
108  /// This code is returned in the following cases:
109  /// * Trying to increment semaphore count more than its max count;
110  /// * Trying to return extra memory block to fixed memory pool.
111  /// @see tn_sem.h
112  /// @see tn_fmem.h
114  ///
115  /// Wrong context error: returned if function is called from
116  /// non-acceptable context. Required context suggested for every
117  /// function by badges:
118  ///
119  /// * $(TN_CALL_FROM_TASK) - function can be called from task;
120  /// * $(TN_CALL_FROM_ISR) - function can be called from ISR.
121  ///
122  /// @see `tn_sys_context_get()`
123  /// @see `enum #TN_Context`
125  ///
126  /// Wrong task state error: requested operation requires different
127  /// task state
129  ///
130  /// This code is returned by most of the kernel functions when
131  /// wrong params were given to function. This error code can be returned
132  /// if only build-time option `#TN_CHECK_PARAM` is non-zero
133  /// @see `#TN_CHECK_PARAM`
135  ///
136  /// Illegal usage. Returned in the following cases:
137  /// * task tries to unlock or delete the mutex that is locked by different
138  /// task,
139  /// * task tries to lock mutex with priority ceiling whose priority is
140  /// lower than task's priority
141  /// @see tn_mutex.h
143  ///
144  /// Returned when user tries to perform some operation on invalid object
145  /// (mutex, semaphore, etc).
146  /// Object validity is checked by comparing special `id_...` field value
147  /// with the value from `enum #TN_ObjId`
148  /// @see `#TN_CHECK_PARAM`
150  /// Object for whose event task was waiting is deleted.
152  /// Task was released from waiting forcibly because some other task
153  /// called `tn_task_release_wait()`
155  /// Internal kernel error, should never be returned by kernel services.
156  /// If it is returned, it's a bug in the kernel.
158 };
159 
160 /**
161  * Prototype for task body function.
162  */
163 typedef void (TN_TaskBody)(void *param);
164 
165 
166 /**
167  * The value representing maximum number of system ticks to wait.
168  *
169  * Assume user called some system function, and it can't perform its job
170  * immediately (say, it needs to lock mutex but it is already locked, etc).
171  *
172  * So, function can wait or return an error. There are possible `timeout`
173  * values and appropriate behavior of the function:
174  *
175  * * `timeout` is set to `0`: function doesn't wait at all, no context switch is performed,
176  * `#TN_RC_TIMEOUT` is returned immediately.
177  * * `timeout` is set to `#TN_WAIT_INFINITE`: function waits until it eventually **can** perform
178  * its job. Timeout is not taken in account, so `#TN_RC_TIMEOUT`
179  * is never returned.
180  * * `timeout` is set to other value: function waits at most specified number of system ticks.
181  * Strictly speaking, it waits from `(timeout - 1)` to `timeout` ticks. So, if
182  * you specify that timeout is 1, be aware that it might actually don't
183  * wait at all: if system timer interrupt happens just while function is
184  * putting task to wait (with interrupts disabled), then ISR will be
185  * executed right after function puts task to wait. Then
186  * `tn_tick_int_processing()` will immediately remove the task from wait
187  * queue and make it runnable again.
188  *
189  * So, to guarantee that task waits *at least* 1 system tick,
190  * you should specify timeout value of `2`.
191  *
192  * **Note** also that there are other possible ways to make task runnable:
193  *
194  * * if task waits because of call to `tn_task_sleep()`, it may be woken up
195  * by some other task, by means of `tn_task_wakeup()`. In this case,
196  * `tn_task_sleep()` returns `#TN_RC_OK`.
197  * * independently of the wait reason, task may be released from wait
198  * forcibly, by means of `tn_task_release_wait()`. It this case,
199  * `#TN_RC_FORCED` is returned by the waiting function.
200  * (the usage of the `tn_task_release_wait()` function is discouraged
201  * 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:124
Returned when user tries to perform some operation on invalid object (mutex, semaphore, etc).
Definition: tn_common.h:149
Task was released from waiting forcibly because some other task called tn_task_release_wait() ...
Definition: tn_common.h:154
id for fixed memory pools
Definition: tn_common.h:92
TN_RCode
Result code returned by kernel services.
Definition: tn_common.h:99
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:102
id for semaphores
Definition: tn_common.h:89
Illegal usage.
Definition: tn_common.h:142
This code is returned in the following cases:
Definition: tn_common.h:113
Internal kernel error, should never be returned by kernel services.
Definition: tn_common.h:157
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:134
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:163
Wrong task state error: requested operation requires different task state.
Definition: tn_common.h:128
Object for whose event task was waiting is deleted.
Definition: tn_common.h:151
Timeout (consult TN_Timeout for details).
Definition: tn_common.h:106
id for mutexes
Definition: tn_common.h:93
TNeoKernel default configuration file, to be copied as tn_cfg.h.