TNeoKernel  v1.03
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_sys.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  * Kernel system routines: system start, tick processing, time slice managing.
41  *
42  */
43 
44 #ifndef _TN_SYS_H
45 #define _TN_SYS_H
46 
47 
48 
49 /*******************************************************************************
50  * INCLUDED FILES
51  ******************************************************************************/
52 
53 #include "tn_list.h"
54 #include "../arch/tn_arch.h"
55 
56 
57 
58 
59 #ifdef __cplusplus
60 extern "C" { /*}*/
61 #endif
62 
63 /*******************************************************************************
64  * EXTERNAL TYPES
65  ******************************************************************************/
66 
67 struct TN_Task;
68 struct TN_Mutex;
69 
70 
71 
72 /*******************************************************************************
73  * DEFINITIONS
74  ******************************************************************************/
75 
76 /**
77  * Convenience macro for the definition of stack array. See
78  * `tn_task_create()` for the usage example.
79  *
80  * @param name
81  * C variable name of the array
82  * @param size
83  * size of the stack array in words (`#TN_UWord`), not in bytes.
84  */
85 #define TN_STACK_ARR_DEF(name, size) \
86  TN_ARCH_STK_ATTR_BEFORE \
87  TN_UWord name[ (size) ] \
88  TN_ARCH_STK_ATTR_AFTER
89 
90 
91 
92 
93 /*******************************************************************************
94  * PUBLIC TYPES
95  ******************************************************************************/
96 
97 /**
98  * System state flags
99  */
101  ///
102  /// system is running
104  ///
105  /// deadlock is active
106  /// Note: this feature works if only `#TN_MUTEX_DEADLOCK_DETECT` is non-zero.
107  /// @see `#TN_MUTEX_DEADLOCK_DETECT`
109 };
110 
111 /**
112  * System context
113  *
114  * @see `tn_sys_context_get()`
115  */
117  ///
118  /// None: this code is possible if only system is not running
119  /// (flag (`#TN_STATE_FLAG__SYS_RUNNING` is not set in the `tn_sys_state`))
121  ///
122  /// Task context
124  ///
125  /// ISR context
127 };
128 
129 /**
130  * User-provided callback function that is called directly from
131  * `tn_sys_start()` as a part of system startup routine; it should merely
132  * create at least one (and typically just one) user's task, which should
133  * perform all the rest application initialization.
134  *
135  * When `TN_CBUserTaskCreate()` returned, the kernel performs first context
136  * switch to the task with highest priority. If there are several tasks with
137  * highest priority, context is switched to the first created one.
138  *
139  * Refer to the section \ref starting_the_kernel for details about system
140  * startup process on the whole.
141  *
142  * **Note:** Although you're able to create more than one task here, it's
143  * usually not so good idea, because many things typically should be done at
144  * startup before tasks can go on with their job: we need to initialize various
145  * on-board peripherals (displays, flash memory chips, or whatever) as well as
146  * initialize software modules used by application. So, if many tasks are
147  * created here, you have to provide some synchronization object so that tasks
148  * will wait until all the initialization is done.
149  *
150  * It's usually easier to maintain if we create just one task here, which
151  * firstly performs all the necessary initialization, **then** creates the rest
152  * of your tasks, and eventually gets to its primary job (the job for which
153  * task was created at all). For the usage example, refer to the page \ref
154  * starting_the_kernel.
155  *
156  * \attention
157  * * The only system service is allowed to call in this function is
158  * `tn_task_create()`.
159  *
160  * @see `tn_sys_start()`
161  */
162 typedef void (TN_CBUserTaskCreate)(void);
163 
164 /**
165  * User-provided callback function that is called repeatedly from the idle task
166  * loop. Make sure that idle task has enough stack space to call this function.
167  *
168  * \attention
169  * * It is illegal to sleep here, because idle task (from which this
170  * function is called) should always be runnable, by design. If `#TN_DEBUG`
171  * option is set, then sleeping in idle task is checked, so if you try to
172  * sleep here, `_TN_FATAL_ERROR()` macro will be called.
173  *
174  *
175  * @see `tn_sys_start()`
176  */
177 typedef void (TN_CBIdle)(void);
178 
179 /**
180  * User-provided callback function that is called whenever
181  * deadlock becomes active or inactive.
182  * Note: this feature works if only `#TN_MUTEX_DEADLOCK_DETECT` is non-zero.
183  *
184  * @param active if `TRUE`, deadlock becomes active, otherwise it becomes
185  * inactive (say, if task stopped waiting for mutex
186  * because of timeout)
187  * @param mutex mutex that is involved in deadlock. You may find out other
188  * mutexes involved by means of `mutex->deadlock_list`.
189  * @param task task that is involved in deadlock. You may find out other
190  * tasks involved by means of `task->deadlock_list`.
191  */
192 typedef void (TN_CBDeadlock)(
193  BOOL active,
194  struct TN_Mutex *mutex,
195  struct TN_Task *task
196  );
197 
198 
199 
200 
201 /*******************************************************************************
202  * DEFINITIONS
203  ******************************************************************************/
204 
205 /**
206  * Value to pass to `tn_sys_tslice_set()` to turn round-robin off.
207  */
208 #define TN_NO_TIME_SLICE 0
209 
210 /**
211  * Max value of time slice
212  */
213 #define TN_MAX_TIME_SLICE 0xFFFE
214 
215 
216 
217 
218 /*******************************************************************************
219  * PUBLIC FUNCTION PROTOTYPES
220  ******************************************************************************/
221 
222 /**
223  * Initial TNeoKernel system start function, never returns. Typically called
224  * from main().
225  *
226  * Refer to the \ref starting_the_kernel "Starting the kernel" section for the
227  * usage example and additional comments.
228  *
229  * $(TN_CALL_FROM_MAIN)
230  * $(TN_LEGEND_LINK)
231  *
232  * @param idle_task_stack
233  * Pointer to array for idle task stack.
234  * User must either use the macro `TN_STACK_ARR_DEF()` for the definition
235  * of stack array, or allocate it manually as an array of `#TN_UWord` with
236  * `#TN_ARCH_STK_ATTR_BEFORE` and `#TN_ARCH_STK_ATTR_AFTER` macros.
237  * @param idle_task_stack_size
238  * Size of idle task stack, in words (`#TN_UWord`)
239  * @param int_stack
240  * Pointer to array for interrupt stack.
241  * User must either use the macro `TN_STACK_ARR_DEF()` for the definition
242  * of stack array, or allocate it manually as an array of `#TN_UWord` with
243  * `#TN_ARCH_STK_ATTR_BEFORE` and `#TN_ARCH_STK_ATTR_AFTER` macros.
244  * @param int_stack_size
245  * Size of interrupt stack, in words (`#TN_UWord`)
246  * @param cb_user_task_create
247  * Callback function that should create initial user's task, see
248  * `#TN_CBUserTaskCreate` for details.
249  * @param cb_idle
250  * Callback function repeatedly called from idle task, see `#TN_CBIdle` for
251  * details.
252  */
253 void tn_sys_start(
254  TN_UWord *idle_task_stack,
255  unsigned int idle_task_stack_size,
256  TN_UWord *int_stack,
257  unsigned int int_stack_size,
258  TN_CBUserTaskCreate *cb_user_task_create,
259  TN_CBIdle *cb_idle
260  );
261 
262 /**
263  * Process system tick; should be called periodically, typically
264  * from some kind of timer ISR.
265  *
266  * The period of this timer is determined by user
267  * (typically 1 ms, but user is free to set different value)
268  *
269  * Among other things, expired \ref tn_timer.h "timers" are fired from this
270  * function.
271  *
272  * For further information, refer to \ref quick_guide "Quick guide".
273  *
274  * $(TN_CALL_FROM_ISR)
275  * $(TN_CAN_SWITCH_CONTEXT)
276  * $(TN_LEGEND_LINK)
277  *
278  * @return
279  * * `#TN_RC_OK` on success;
280  * * `#TN_RC_WCONTEXT` if called from wrong context.
281  */
283 
284 /**
285  * Set time slice ticks value for specified priority (see \ref round_robin).
286  *
287  * $(TN_CALL_FROM_TASK)
288  * $(TN_LEGEND_LINK)
289  *
290  * @param priority
291  * Priority of tasks for which time slice value should be set
292  * @param ticks
293  * Time slice value, in ticks. Set to `#TN_NO_TIME_SLICE` for no round-robin
294  * scheduling for given priority (it's default value). Value can't be
295  * higher than `#TN_MAX_TIME_SLICE`.
296  *
297  * @return
298  * * `#TN_RC_OK` on success;
299  * * `#TN_RC_WCONTEXT` if called from wrong context;
300  * * `#TN_RC_WPARAM` if given `priority` or `ticks` are invalid.
301  */
302 enum TN_RCode tn_sys_tslice_set(int priority, int ticks);
303 
304 /**
305  * Get current system ticks count.
306  *
307  * $(TN_CALL_FROM_TASK)
308  * $(TN_CALL_FROM_ISR)
309  * $(TN_LEGEND_LINK)
310  *
311  * @return
312  * Current system ticks count.
313  */
314 unsigned int tn_sys_time_get(void);
315 
316 
317 /**
318  * Set callback function that should be called whenever deadlock occurs or
319  * becomes inactive (say, if one of tasks involved in the deadlock was released
320  * from wait because of timeout)
321  *
322  * $(TN_CALL_FROM_MAIN)
323  * $(TN_LEGEND_LINK)
324  *
325  * **Note:** this function should be called from `main()`, before
326  * `tn_sys_start()`.
327  *
328  * @param cb
329  * Pointer to user-provided callback function.
330  *
331  * @see `#TN_MUTEX_DEADLOCK_DETECT`
332  * @see `#TN_CBDeadlock` for callback function prototype
333  */
335 
336 /**
337  * Returns current system state flags
338  *
339  * $(TN_CALL_FROM_TASK)
340  * $(TN_CALL_FROM_ISR)
341  * $(TN_LEGEND_LINK)
342  */
344 
345 /**
346  * Returns system context: task or ISR.
347  *
348  * $(TN_CALL_FROM_TASK)
349  * $(TN_CALL_FROM_ISR)
350  * $(TN_CALL_FROM_MAIN)
351  * $(TN_LEGEND_LINK)
352  *
353  * @see `enum #TN_Context`
354  */
355 enum TN_Context tn_sys_context_get(void);
356 
357 /**
358  * Returns whether current system context is `#TN_CONTEXT_TASK`
359  *
360  * $(TN_CALL_FROM_TASK)
361  * $(TN_CALL_FROM_ISR)
362  * $(TN_CALL_FROM_MAIN)
363  * $(TN_LEGEND_LINK)
364  *
365  * @return `TRUE` if current system context is `#TN_CONTEXT_TASK`,
366  * `FALSE` otherwise.
367  *
368  * @see `tn_sys_context_get()`
369  * @see `enum #TN_Context`
370  */
371 static inline BOOL tn_is_task_context(void)
372 {
373  return (tn_sys_context_get() == TN_CONTEXT_TASK);
374 }
375 
376 /**
377  * Returns whether current system context is `#TN_CONTEXT_ISR`
378  *
379  * $(TN_CALL_FROM_TASK)
380  * $(TN_CALL_FROM_ISR)
381  * $(TN_CALL_FROM_MAIN)
382  * $(TN_LEGEND_LINK)
383  *
384  * @return `TRUE` if current system context is `#TN_CONTEXT_ISR`,
385  * `FALSE` otherwise.
386  *
387  * @see `tn_sys_context_get()`
388  * @see `enum #TN_Context`
389  */
390 static inline BOOL tn_is_isr_context(void)
391 {
392  return (tn_sys_context_get() == TN_CONTEXT_ISR);
393 }
394 
395 /**
396  * Returns pointer to the currently running task.
397  *
398  * $(TN_CALL_FROM_TASK)
399  * $(TN_CALL_FROM_ISR)
400  * $(TN_LEGEND_LINK)
401  */
402 struct TN_Task *tn_cur_task_get(void);
403 
404 /**
405  * Returns pointer to the body function of the currently running task.
406  *
407  * $(TN_CALL_FROM_TASK)
408  * $(TN_CALL_FROM_ISR)
409  * $(TN_LEGEND_LINK)
410  */
412 
413 #ifdef __cplusplus
414 } /* extern "C" */
415 #endif
416 
417 #endif // _TN_SYS_H
418 
Mutex.
Definition: tn_mutex.h:122
TN_Context
System context.
Definition: tn_sys.h:116
#define BOOL
boolean type definition
Definition: tn_common.h:222
deadlock is active Note: this feature works if only TN_MUTEX_DEADLOCK_DETECT is non-zero.
Definition: tn_sys.h:108
Task.
Definition: tn_tasks.h:173
TN_RCode
Result code returned by kernel services.
Definition: tn_common.h:100
None: this code is possible if only system is not running (flag (TN_STATE_FLAG__SYS_RUNNING is not se...
Definition: tn_sys.h:120
TN_TaskBody * tn_cur_task_body_get(void)
Returns pointer to the body function of the currently running task.
system is running
Definition: tn_sys.h:103
void tn_callback_deadlock_set(TN_CBDeadlock *cb)
Set callback function that should be called whenever deadlock occurs or becomes inactive (say...
void tn_sys_start(TN_UWord *idle_task_stack, unsigned int idle_task_stack_size, TN_UWord *int_stack, unsigned int int_stack_size, TN_CBUserTaskCreate *cb_user_task_create, TN_CBIdle *cb_idle)
Initial TNeoKernel system start function, never returns.
void( TN_CBDeadlock)(BOOL active, struct TN_Mutex *mutex, struct TN_Task *task)
User-provided callback function that is called whenever deadlock becomes active or inactive...
Definition: tn_sys.h:192
static BOOL tn_is_task_context(void)
Returns whether current system context is TN_CONTEXT_TASK
Definition: tn_sys.h:371
enum TN_StateFlag tn_sys_state_flags_get(void)
Returns current system state flags.
void( TN_CBUserTaskCreate)(void)
User-provided callback function that is called directly from tn_sys_start() as a part of system start...
Definition: tn_sys.h:162
enum TN_Context tn_sys_context_get(void)
Returns system context: task or ISR.
int priority
current task priority
Definition: tn_tasks.h:228
enum TN_RCode tn_tick_int_processing(void)
Process system tick; should be called periodically, typically from some kind of timer ISR...
struct TN_Task * tn_cur_task_get(void)
Returns pointer to the currently running task.
Task context.
Definition: tn_sys.h:123
static BOOL tn_is_isr_context(void)
Returns whether current system context is TN_CONTEXT_ISR
Definition: tn_sys.h:390
void( TN_CBIdle)(void)
User-provided callback function that is called repeatedly from the idle task loop.
Definition: tn_sys.h:177
enum TN_RCode tn_sys_tslice_set(int priority, int ticks)
Set time slice ticks value for specified priority (see Round-robin scheduling).
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:164
unsigned int tn_sys_time_get(void)
Get current system ticks count.
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.
TN_StateFlag
System state flags.
Definition: tn_sys.h:100
ISR context.
Definition: tn_sys.h:126