TNeoKernel  v1.04
 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 #include "../arch/tn_arch_detect.h"
51 #include "tn_cfg_dispatch.h"
52 
53 #ifdef __cplusplus
54 extern "C" { /*}*/
55 #endif
56 
57 /*******************************************************************************
58  * PUBLIC TYPES
59  ******************************************************************************/
60 
61 /**
62  * Magic number for object validity verification
63  * TODO: use TN_UWord here instead of unsigned int
64  */
65 enum TN_ObjId {
66  TN_ID_TASK = (unsigned int)0x47ABCF69, //!< id for tasks
67  TN_ID_SEMAPHORE = (unsigned int)0x6FA173EB, //!< id for semaphores
68  TN_ID_EVENTGRP = (unsigned int)0x5E224F25, //!< id for event groups
69  TN_ID_DATAQUEUE = (unsigned int)0x8C8A6C89, //!< id for data queues
70  TN_ID_FSMEMORYPOOL = (unsigned int)0x26B7CE8B, //!< id for fixed memory pools
71  TN_ID_MUTEX = (unsigned int)0x17129E45, //!< id for mutexes
72  TN_ID_TIMER = (unsigned int)0x9A937FBC, //!< id for timers
73  TN_ID_EXCHANGE = (unsigned int)0x32b7c072, //!< id for exchange objects
74  TN_ID_EXCHANGE_LINK = (unsigned int)0x24d36f35, //!< id for exchange objects
75 };
76 
77 /**
78  * Result code returned by kernel services.
79  */
80 enum TN_RCode {
81  ///
82  /// Successful operation
83  TN_RC_OK = 0,
84  ///
85  /// Timeout (consult `#TN_Timeout` for details).
86  /// @see `#TN_Timeout`
88  ///
89  /// This code is returned in the following cases:
90  /// * Trying to increment semaphore count more than its max count;
91  /// * Trying to return extra memory block to fixed memory pool.
92  /// @see tn_sem.h
93  /// @see tn_fmem.h
95  ///
96  /// Wrong context error: returned if function is called from
97  /// non-acceptable context. Required context suggested for every
98  /// function by badges:
99  ///
100  /// * $(TN_CALL_FROM_TASK) - function can be called from task;
101  /// * $(TN_CALL_FROM_ISR) - function can be called from ISR.
102  ///
103  /// @see `tn_sys_context_get()`
104  /// @see `enum #TN_Context`
106  ///
107  /// Wrong task state error: requested operation requires different
108  /// task state
110  ///
111  /// This code is returned by most of the kernel functions when
112  /// wrong params were given to function. This error code can be returned
113  /// if only build-time option `#TN_CHECK_PARAM` is non-zero
114  /// @see `#TN_CHECK_PARAM`
116  ///
117  /// Illegal usage. Returned in the following cases:
118  /// * task tries to unlock or delete the mutex that is locked by different
119  /// task,
120  /// * task tries to lock mutex with priority ceiling whose priority is
121  /// lower than task's priority
122  /// @see tn_mutex.h
124  ///
125  /// Returned when user tries to perform some operation on invalid object
126  /// (mutex, semaphore, etc).
127  /// Object validity is checked by comparing special `id_...` field value
128  /// with the value from `enum #TN_ObjId`
129  /// @see `#TN_CHECK_PARAM`
131  /// Object for whose event task was waiting is deleted.
133  /// Task was released from waiting forcibly because some other task
134  /// called `tn_task_release_wait()`
136  /// Internal kernel error, should never be returned by kernel services.
137  /// If it is returned, it's a bug in the kernel.
139 };
140 
141 /**
142  * Prototype for task body function.
143  */
144 typedef void (TN_TaskBody)(void *param);
145 
146 
147 /**
148  * The value representing maximum number of system ticks to wait.
149  *
150  * Assume user called some system function, and it can't perform its job
151  * immediately (say, it needs to lock mutex but it is already locked, etc).
152  *
153  * So, function can wait or return an error. There are possible `timeout`
154  * values and appropriate behavior of the function:
155  *
156  * - `timeout` is set to `0`: function doesn't wait at all, no context switch
157  * is performed, `#TN_RC_TIMEOUT` is returned immediately.
158  * - `timeout` is set to `#TN_WAIT_INFINITE`: function waits until it
159  * eventually **can** perform its job. Timeout is not taken in account, so
160  * `#TN_RC_TIMEOUT` is never returned.
161  * - `timeout` is set to other value: function waits at most specified number
162  * of system ticks. Strictly speaking, it waits from `(timeout - 1)` to
163  * `timeout` ticks. So, if you specify that timeout is 1, be aware that it
164  * might actually don't wait at all: if $(TN_SYS_TIMER_LINK) interrupt
165  * happens just while function is putting task to wait (with interrupts
166  * disabled), then ISR will be executed right after function puts task to
167  * wait. Then `tn_tick_int_processing()` will immediately remove the task
168  * from wait queue and make it runnable again.
169  *
170  * So, to guarantee that task waits *at least* 1 system tick, you should
171  * specify timeout value of `2`.
172  *
173  * **Note** also that there are other possible ways to make task runnable:
174  *
175  * - if task waits because of call to `tn_task_sleep()`, it may be woken up by
176  * some other task, by means of `tn_task_wakeup()`. In this case,
177  * `tn_task_sleep()` returns `#TN_RC_OK`.
178  * - independently of the wait reason, task may be released from wait forcibly,
179  * by means of `tn_task_release_wait()`. It this case, `#TN_RC_FORCED` is
180  * returned by the waiting function. (the usage of the
181  * `tn_task_release_wait()` function is discouraged though)
182  */
183 typedef unsigned long TN_Timeout;
184 
185 
186 /*******************************************************************************
187  * GLOBAL VARIABLES
188  ******************************************************************************/
189 
190 /*******************************************************************************
191  * DEFINITIONS
192  ******************************************************************************/
193 
194 
195 /// NULL pointer definition
196 #ifndef TN_NULL
197 # define TN_NULL ((void *)0)
198 #endif
199 
200 /// boolean type definition
201 #ifndef TN_BOOL
202 # define TN_BOOL int
203 #endif
204 
205 /// `true` value definition for type `#TN_BOOL`
206 #ifndef TN_TRUE
207 # define TN_TRUE (1 == 1)
208 #endif
209 
210 /// `false` value definition for type `#TN_BOOL`
211 #ifndef TN_FALSE
212 # define TN_FALSE (1 == 0)
213 #endif
214 
215 /**
216  * Macro for making a number a multiple of `sizeof(#TN_UWord)`, should be used
217  * with fixed memory block pool. See `tn_fmem_create()` for usage example.
218  */
219 #define TN_MAKE_ALIG_SIZE(a) \
220  (((a) + (sizeof(TN_UWord) - 1)) & (~(sizeof(TN_UWord) - 1)))
221 
222 //-- self-checking
223 #if (!defined TN_API_MAKE_ALIG_ARG)
224 # error TN_API_MAKE_ALIG_ARG is not defined
225 #elif (!defined TN_API_MAKE_ALIG_ARG__TYPE)
226 # error TN_API_MAKE_ALIG_ARG__TYPE is not defined
227 #elif (!defined TN_API_MAKE_ALIG_ARG__SIZE)
228 # error TN_API_MAKE_ALIG_ARG__SIZE is not defined
229 #endif
230 
231 //-- define MAKE_ALIG accordingly to config
232 /**
233  * The same as `#TN_MAKE_ALIG_SIZE` but its behavior depends on the option
234  * `#TN_API_MAKE_ALIG_ARG`
235  *
236  * \attention it is recommended to use `#TN_MAKE_ALIG_SIZE` macro instead
237  * of this one, in order to avoid confusion caused by various
238  * TNKernel ports: refer to the section \ref tnkernel_diff_make_alig for details.
239  */
240 #if (TN_API_MAKE_ALIG_ARG == TN_API_MAKE_ALIG_ARG__TYPE)
241 # define TN_MAKE_ALIG(a) TN_MAKE_ALIG_SIZE(sizeof(a))
242 #elif (TN_API_MAKE_ALIG_ARG == TN_API_MAKE_ALIG_ARG__SIZE)
243 # define TN_MAKE_ALIG(a) TN_MAKE_ALIG_SIZE(a)
244 #else
245 # error wrong TN_API_MAKE_ALIG_ARG
246 #endif
247 
248 
249 
250 /*******************************************************************************
251  * PUBLIC FUNCTION PROTOTYPES
252  ******************************************************************************/
253 
254 #ifdef __cplusplus
255 } /* extern "C" */
256 #endif
257 
258 #endif // _TN_COMMON_H
259 
260 /*******************************************************************************
261  * end of file
262  ******************************************************************************/
263 
264 
id for data queues
Definition: tn_common.h:69
Wrong context error: returned if function is called from non-acceptable context.
Definition: tn_common.h:105
id for exchange objects
Definition: tn_common.h:73
Returned when user tries to perform some operation on invalid object (mutex, semaphore, etc).
Definition: tn_common.h:130
Task was released from waiting forcibly because some other task called tn_task_release_wait() ...
Definition: tn_common.h:135
id for fixed memory pools
Definition: tn_common.h:70
TN_RCode
Result code returned by kernel services.
Definition: tn_common.h:80
TN_ObjId
Magic number for object validity verification TODO: use TN_UWord here instead of unsigned int...
Definition: tn_common.h:65
unsigned long TN_Timeout
The value representing maximum number of system ticks to wait.
Definition: tn_common.h:183
Successful operation.
Definition: tn_common.h:83
id for semaphores
Definition: tn_common.h:67
Illegal usage.
Definition: tn_common.h:123
This code is returned in the following cases:
Definition: tn_common.h:94
id for exchange objects
Definition: tn_common.h:74
Dispatch configuration: set predefined options, include user-provided cfg file as well as default cfg...
Internal kernel error, should never be returned by kernel services.
Definition: tn_common.h:138
id for tasks
Definition: tn_common.h:66
id for event groups
Definition: tn_common.h:68
This code is returned by most of the kernel functions when wrong params were given to function...
Definition: tn_common.h:115
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:144
Wrong task state error: requested operation requires different task state.
Definition: tn_common.h:109
Object for whose event task was waiting is deleted.
Definition: tn_common.h:132
Timeout (consult TN_Timeout for details).
Definition: tn_common.h:87
id for mutexes
Definition: tn_common.h:71
id for timers
Definition: tn_common.h:72