TNeoKernel  v1.03
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_arch_example.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  * Example of architecture-dependent routines
41  */
42 
43 
44 #ifndef _TN_ARCH_EXAMPLE_H
45 #define _TN_ARCH_EXAMPLE_H
46 
47 /**
48  * FFS - find first set bit. Used in `_find_next_task_to_run()` function.
49  * Say, for `0xa8` it should return `3`.
50  *
51  * May be not defined: in this case, naive algorithm will be used.
52  */
53 #define _TN_FFS(x) (32 - __builtin_clz((x) & (0 - (x))))
54 
55 /**
56  * Used by the kernel as a signal that something really bad happened.
57  * Indicates TNeoKernel bugs as well as illegal kernel usage
58  * (e.g. sleeping in the idle task callback)
59  *
60  * Typically, set to assembler instruction that causes debugger to halt.
61  */
62 #define _TN_FATAL_ERROR(error_msg, ...) \
63  {__asm__ volatile(" sdbbp 0"); __asm__ volatile ("nop");}
64 
65 
66 
67 
68 /**
69  * Compiler-specific attribute that should be placed **before** declaration of
70  * array used for stack. It is needed because there are often additional
71  * restrictions applied to alignment of stack, so, to meet them, stack arrays
72  * need to be declared with these macros.
73  *
74  * @see TN_ARCH_STK_ATTR_AFTER
75  */
76 #define TN_ARCH_STK_ATTR_BEFORE
77 
78 /**
79  * Compiler-specific attribute that should be placed **after** declaration of
80  * array used for stack. It is needed because there are often additional
81  * restrictions applied to alignment of stack, so, to meet them, stack arrays
82  * need to be declared with these macros.
83  *
84  * @see TN_ARCH_STK_ATTR_BEFORE
85  */
86 
87 #define TN_ARCH_STK_ATTR_AFTER __attribute__((aligned(0x8)))
88 
89 /**
90  * Minimum task's stack size, in words, not in bytes; includes a space for
91  * context plus for parameters passed to task's body function.
92  */
93 #define TN_MIN_STACK_SIZE 36
94 
95 /**
96  * Width of `int` type.
97  */
98 #define TN_INT_WIDTH 32
99 
100 /**
101  * Unsigned integer type whose size is equal to the size of CPU register.
102  * Typically it's plain `unsigned int`.
103  */
104 typedef unsigned int TN_UWord;
105 
106 /**
107  * Maximum number of priorities available, this value usually matches
108  * `#TN_INT_WIDTH`.
109  *
110  * @see TN_PRIORITIES_CNT
111  */
112 #define TN_PRIORITIES_MAX_CNT TN_INT_WIDTH
113 
114 /**
115  * Value for infinite waiting, usually matches `UINT_MAX`
116  */
117 #define TN_WAIT_INFINITE 0xFFFFFFFF
118 
119 /**
120  * Value for initializing the unused space of task's stack
121  */
122 #define TN_FILL_STACK_VAL 0xFEEDFACE
123 
124 
125 
126 
127 /**
128  * Declares variable that is used by macros `TN_INT_DIS_SAVE()` and
129  * `TN_INT_RESTORE()` for storing status register value.
130  *
131  * @see `TN_INT_DIS_SAVE()`
132  * @see `TN_INT_RESTORE()`
133  */
134 #define TN_INTSAVE_DATA int tn_save_status_reg = 0;
135 
136 /**
137  * The same as `#TN_INTSAVE_DATA` but for using in ISR together with
138  * `TN_INT_IDIS_SAVE()`, `TN_INT_IRESTORE()`.
139  *
140  * @see `TN_INT_IDIS_SAVE()`
141  * @see `TN_INT_IRESTORE()`
142  */
143 #define TN_INTSAVE_DATA_INT TN_INTSAVE_DATA
144 
145 /**
146  * Disable interrupts and return previous value of status register,
147  * atomically. Similar `tn_arch_sr_save_int_dis()`, but implemented
148  * as a macro, so it is potentially faster.
149  *
150  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
151  *
152  * @see `#TN_INTSAVE_DATA`
153  * @see `tn_arch_sr_save_int_dis()`
154  */
155 #define TN_INT_DIS_SAVE() tn_save_status_reg = tn_arch_sr_save_int_dis()
156 
157 /**
158  * Restore previously saved status register.
159  * Similar to `tn_arch_sr_restore()`, but implemented as a macro,
160  * so it is potentially faster.
161  *
162  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
163  *
164  * @see `#TN_INTSAVE_DATA`
165  * @see `tn_arch_sr_save_int_dis()`
166  */
167 #define TN_INT_RESTORE() tn_arch_sr_restore(tn_save_status_reg)
168 
169 /**
170  * The same as `TN_INT_DIS_SAVE()` but for using in ISR.
171  *
172  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
173  *
174  * @see `#TN_INTSAVE_DATA_INT`
175  */
176 #define TN_INT_IDIS_SAVE() TN_INT_DIS_SAVE()
177 
178 /**
179  * The same as `TN_INT_RESTORE()` but for using in ISR.
180  *
181  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
182  *
183  * @see `#TN_INTSAVE_DATA_INT`
184  */
185 #define TN_INT_IRESTORE() TN_INT_RESTORE()
186 
187 /**
188  * Returns nonzero if interrupts are disabled, zero otherwise.
189  */
190 #define TN_IS_INT_DISABLED() ((__builtin_mfc0(12, 0) & 1) == 0)
191 
192 /**
193  * Pend context switch from interrupt.
194  */
195 #define _TN_CONTEXT_SWITCH_IPEND_IF_NEEDED() \
196  _tn_context_switch_pend_if_needed()
197 
198 
199 
200 #endif // _TN_ARCH_EXAMPLE_H
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.