TNeoKernel  v1.04
 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  * Unsigned integer type that is able to store pointers.
108  * We need it because some platforms don't define `uintptr_t`.
109  * Typically it's `unsigned int`.
110  */
111 typedef unsigned int TN_UIntPtr;
112 
113 /**
114  * Maximum number of priorities available, this value usually matches
115  * `#TN_INT_WIDTH`.
116  *
117  * @see TN_PRIORITIES_CNT
118  */
119 #define TN_PRIORITIES_MAX_CNT TN_INT_WIDTH
120 
121 /**
122  * Value for infinite waiting, usually matches `ULONG_MAX`,
123  * because `#TN_Timeout` is declared as `unsigned long`.
124  */
125 #define TN_WAIT_INFINITE (TN_Timeout)0xFFFFFFFF
126 
127 /**
128  * Value for initializing the unused space of task's stack
129  */
130 #define TN_FILL_STACK_VAL 0xFEEDFACE
131 
132 
133 
134 
135 /**
136  * Declares variable that is used by macros `TN_INT_DIS_SAVE()` and
137  * `TN_INT_RESTORE()` for storing status register value.
138  *
139  * @see `TN_INT_DIS_SAVE()`
140  * @see `TN_INT_RESTORE()`
141  */
142 #define TN_INTSAVE_DATA int tn_save_status_reg = 0;
143 
144 /**
145  * The same as `#TN_INTSAVE_DATA` but for using in ISR together with
146  * `TN_INT_IDIS_SAVE()`, `TN_INT_IRESTORE()`.
147  *
148  * @see `TN_INT_IDIS_SAVE()`
149  * @see `TN_INT_IRESTORE()`
150  */
151 #define TN_INTSAVE_DATA_INT TN_INTSAVE_DATA
152 
153 /**
154  * Disable interrupts and return previous value of status register,
155  * atomically. Similar `tn_arch_sr_save_int_dis()`, but implemented
156  * as a macro, so it is potentially faster.
157  *
158  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
159  *
160  * @see `#TN_INTSAVE_DATA`
161  * @see `tn_arch_sr_save_int_dis()`
162  */
163 #define TN_INT_DIS_SAVE() tn_save_status_reg = tn_arch_sr_save_int_dis()
164 
165 /**
166  * Restore previously saved status register.
167  * Similar to `tn_arch_sr_restore()`, but implemented as a macro,
168  * so it is potentially faster.
169  *
170  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
171  *
172  * @see `#TN_INTSAVE_DATA`
173  * @see `tn_arch_sr_save_int_dis()`
174  */
175 #define TN_INT_RESTORE() tn_arch_sr_restore(tn_save_status_reg)
176 
177 /**
178  * The same as `TN_INT_DIS_SAVE()` but for using in ISR.
179  *
180  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
181  *
182  * @see `#TN_INTSAVE_DATA_INT`
183  */
184 #define TN_INT_IDIS_SAVE() TN_INT_DIS_SAVE()
185 
186 /**
187  * The same as `TN_INT_RESTORE()` but for using in ISR.
188  *
189  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
190  *
191  * @see `#TN_INTSAVE_DATA_INT`
192  */
193 #define TN_INT_IRESTORE() TN_INT_RESTORE()
194 
195 /**
196  * Returns nonzero if interrupts are disabled, zero otherwise.
197  */
198 #define TN_IS_INT_DISABLED() ((__builtin_mfc0(12, 0) & 1) == 0)
199 
200 /**
201  * Pend context switch from interrupt.
202  */
203 #define _TN_CONTEXT_SWITCH_IPEND_IF_NEEDED() \
204  _tn_context_switch_pend_if_needed()
205 
206 
207 /**
208  * Converts size in bytes to size in `#TN_UWord`.
209  * For 32-bit platforms, we should shift it by 2 bit to the right;
210  * for 16-bit platforms, we should shift it by 1 bit to the right.
211  */
212 #define _TN_SIZE_BYTES_TO_UWORDS(size_in_bytes) ((size_in_bytes) >> 2)
213 
214 #endif // _TN_ARCH_EXAMPLE_H
unsigned int TN_UIntPtr
Unsigned integer type that is able to store pointers.
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.