TNeo  v1.06
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_arch_cortex_m.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * TNeo: 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  * TNeo: copyright © 2014 Dmitry Frank.
8  *
9  * TNeo 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: TNeo.
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  *
39  * \file
40  *
41  * Cortex-M0/M0+/M3/M4/M4F architecture-dependent routines
42  *
43  */
44 
45 #ifndef _TN_ARCH_CORTEX_M_H
46 #define _TN_ARCH_CORTEX_M_H
47 
48 
49 /*******************************************************************************
50  * INCLUDED FILES
51  ******************************************************************************/
52 
53 #include "../tn_arch_detect.h"
54 #include "../../core/tn_cfg_dispatch.h"
55 
56 
57 
58 
59 #ifdef __cplusplus
60 extern "C" { /*}*/
61 #endif
62 
63 
64 /*******************************************************************************
65  * PROTECTED GLOBAL DATA
66  ******************************************************************************/
67 
68 
69 
70 
71 
72 
73 
74 #ifndef DOXYGEN_SHOULD_SKIP_THIS
75 
76 #define _TN_PIC32_INTSAVE_DATA_INVALID 0xffffffff
77 
78 #if TN_DEBUG
79 # define _TN_PIC32_INTSAVE_CHECK() \
80 { \
81  if (tn_save_status_reg == _TN_PIC32_INTSAVE_DATA_INVALID){ \
82  _TN_FATAL_ERROR(""); \
83  } \
84 }
85 #else
86 # define _TN_PIC32_INTSAVE_CHECK() /* nothing */
87 #endif
88 
89 #if defined(__TN_ARCHFEAT_CORTEX_M_ARMv7M_ISA__)
90 /**
91  * FFS - find first set bit. Used in `_find_next_task_to_run()` function.
92  * Say, for `0xa8` it should return `3`.
93  *
94  * May be not defined: in this case, naive algorithm will be used.
95  */
96 #define _TN_FFS(x) ffs_asm(x)
97 int ffs_asm(int x);
98 #endif
99 
100 /**
101  * Used by the kernel as a signal that something really bad happened.
102  * Indicates TNeo bugs as well as illegal kernel usage
103  * (e.g. sleeping in the idle task callback)
104  *
105  * Typically, set to assembler instruction that causes debugger to halt.
106  */
107 
108 #if defined(__TN_COMPILER_IAR__)
109 # define _TN_FATAL_ERROR(error_msg, ...) \
110  {asm("bkpt #0");}
111 #else
112 # define _TN_FATAL_ERROR(error_msg, ...) \
113  {__asm__ volatile("bkpt #0");}
114 #endif
115 
116 
117 
118 /**
119  * \def TN_ARCH_STK_ATTR_BEFORE
120  *
121  * Compiler-specific attribute that should be placed **before** declaration of
122  * array used for stack. It is needed because there are often additional
123  * restrictions applied to alignment of stack, so, to meet them, stack arrays
124  * need to be declared with these macros.
125  *
126  * @see TN_ARCH_STK_ATTR_AFTER
127  */
128 
129 /**
130  * \def TN_ARCH_STK_ATTR_AFTER
131  *
132  * Compiler-specific attribute that should be placed **after** declaration of
133  * array used for stack. It is needed because there are often additional
134  * restrictions applied to alignment of stack, so, to meet them, stack arrays
135  * need to be declared with these macros.
136  *
137  * @see TN_ARCH_STK_ATTR_BEFORE
138  */
139 
140 #if defined(__TN_COMPILER_ARMCC__)
141 
142 # define TN_ARCH_STK_ATTR_BEFORE __align(8)
143 # define TN_ARCH_STK_ATTR_AFTER
144 
145 #elif defined(__TN_COMPILER_GCC__) || defined(__TN_COMPILER_CLANG__)
146 
147 # define TN_ARCH_STK_ATTR_BEFORE
148 # define TN_ARCH_STK_ATTR_AFTER __attribute__((aligned(0x08)))
149 
150 #elif defined(__TN_COMPILER_IAR__)
151 
152 # define TN_ARCH_STK_ATTR_BEFORE
153 # define TN_ARCH_STK_ATTR_AFTER
154 
155 #endif
156 
157 
158 #if defined(__TN_ARCHFEAT_CORTEX_M_FPU__)
159 # define _TN_CORTEX_FPU_CONTEXT_SIZE 32 /* FPU registers: S0 .. S31 */
160 #else
161 # define _TN_CORTEX_FPU_CONTEXT_SIZE 0 /* no FPU registers */
162 #endif
163 
164 
165 /**
166  * Minimum task's stack size, in words, not in bytes; includes a space for
167  * context plus for parameters passed to task's body function.
168  */
169 #define TN_MIN_STACK_SIZE (17 /* context: 17 words */ \
170  + _TN_STACK_OVERFLOW_SIZE_ADD \
171  + _TN_CORTEX_FPU_CONTEXT_SIZE \
172  )
173 
174 /**
175  * Width of `int` type.
176  */
177 #define TN_INT_WIDTH 32
178 
179 /**
180  * Unsigned integer type whose size is equal to the size of CPU register.
181  * Typically it's plain `unsigned int`.
182  */
183 typedef unsigned int TN_UWord;
184 
185 /**
186  * Unsigned integer type that is able to store pointers.
187  * We need it because some platforms don't define `uintptr_t`.
188  * Typically it's `unsigned int`.
189  */
190 typedef unsigned int TN_UIntPtr;
191 
192 /**
193  * Maximum number of priorities available, this value usually matches
194  * `#TN_INT_WIDTH`.
195  *
196  * @see TN_PRIORITIES_CNT
197  */
198 #define TN_PRIORITIES_MAX_CNT TN_INT_WIDTH
199 
200 /**
201  * Value for infinite waiting, usually matches `ULONG_MAX`,
202  * because `#TN_TickCnt` is declared as `unsigned long`.
203  */
204 #define TN_WAIT_INFINITE (TN_TickCnt)0xFFFFFFFF
205 
206 /**
207  * Value for initializing the task's stack
208  */
209 #define TN_FILL_STACK_VAL 0xFEEDFACE
210 
211 
212 
213 
214 /**
215  * Declares variable that is used by macros `TN_INT_DIS_SAVE()` and
216  * `TN_INT_RESTORE()` for storing status register value.
217  *
218  * It is good idea to initially set it to some invalid value,
219  * and if TN_DEBUG is non-zero, check it in TN_INT_RESTORE().
220  * Then, we can catch bugs if someone tries to restore interrupts status
221  * without saving it first.
222  *
223  * @see `TN_INT_DIS_SAVE()`
224  * @see `TN_INT_RESTORE()`
225  */
226 #define TN_INTSAVE_DATA \
227  int tn_save_status_reg = _TN_PIC32_INTSAVE_DATA_INVALID;
228 
229 /**
230  * The same as `#TN_INTSAVE_DATA` but for using in ISR together with
231  * `TN_INT_IDIS_SAVE()`, `TN_INT_IRESTORE()`.
232  *
233  * @see `TN_INT_IDIS_SAVE()`
234  * @see `TN_INT_IRESTORE()`
235  */
236 #define TN_INTSAVE_DATA_INT TN_INTSAVE_DATA
237 
238 /**
239  * \def TN_INT_DIS_SAVE()
240  *
241  * Disable interrupts and return previous value of status register,
242  * atomically. Similar `tn_arch_sr_save_int_dis()`, but implemented
243  * as a macro, so it is potentially faster.
244  *
245  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
246  *
247  * @see `#TN_INTSAVE_DATA`
248  * @see `tn_arch_sr_save_int_dis()`
249  */
250 
251 /**
252  * \def TN_INT_RESTORE()
253  *
254  * Restore previously saved status register.
255  * Similar to `tn_arch_sr_restore()`, but implemented as a macro,
256  * so it is potentially faster.
257  *
258  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
259  *
260  * @see `#TN_INTSAVE_DATA`
261  * @see `tn_arch_sr_save_int_dis()`
262  */
263 
264 #define TN_INT_DIS_SAVE() tn_save_status_reg = tn_arch_sr_save_int_dis()
265 #define TN_INT_RESTORE() _TN_PIC32_INTSAVE_CHECK(); \
266  tn_arch_sr_restore(tn_save_status_reg)
267 
268 /**
269  * The same as `TN_INT_DIS_SAVE()` but for using in ISR.
270  *
271  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
272  *
273  * @see `#TN_INTSAVE_DATA_INT`
274  */
275 #define TN_INT_IDIS_SAVE() TN_INT_DIS_SAVE()
276 
277 /**
278  * The same as `TN_INT_RESTORE()` but for using in ISR.
279  *
280  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
281  *
282  * @see `#TN_INTSAVE_DATA_INT`
283  */
284 #define TN_INT_IRESTORE() TN_INT_RESTORE()
285 
286 /**
287  * Returns nonzero if interrupts are disabled, zero otherwise.
288  */
289 #define TN_IS_INT_DISABLED() (_tn_arch_is_int_disabled())
290 
291 /**
292  * Pend context switch from interrupt.
293  */
294 #define _TN_CONTEXT_SWITCH_IPEND_IF_NEEDED() \
295  _tn_context_switch_pend_if_needed()
296 
297 /**
298  * Converts size in bytes to size in `#TN_UWord`.
299  * For 32-bit platforms, we should shift it by 2 bit to the right;
300  * for 16-bit platforms, we should shift it by 1 bit to the right.
301  */
302 #define _TN_SIZE_BYTES_TO_UWORDS(size_in_bytes) ((size_in_bytes) >> 2)
303 
304 #if defined(__TN_COMPILER_ARMCC__)
305 # define _TN_INLINE __inline
306 # define _TN_VOLATILE_WORKAROUND /* nothing */
307 #elif defined(__TN_COMPILER_GCC__) || defined(__TN_COMPILER_CLANG__)
308 # define _TN_INLINE inline
309 # define _TN_VOLATILE_WORKAROUND /* nothing */
310 #elif defined(__TN_COMPILER_IAR__)
311 # define _TN_INLINE inline
312 # define _TN_VOLATILE_WORKAROUND volatile
313 #else
314 # error unknown Cortex compiler
315 #endif
316 
317 
318 #endif //-- DOXYGEN_SHOULD_SKIP_THIS
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 #ifdef __cplusplus
331 } /* extern "C" */
332 #endif
333 
334 #endif // _TN_ARCH_CORTEX_M_H
335 
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.