TNeoKernel  v1.01
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_arch_pic32.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  *
39  * \file
40  *
41  * PIC32 architecture-dependent routines
42  *
43  */
44 
45 #ifndef _TN_ARCH_PIC32_H
46 #define _TN_ARCH_PIC32_H
47 
48 //-- this include is needed to get build-time configuration
49 // (TN_DEBUG is used)
50 #include "../../core/tn_common.h"
51 
52 
53 #ifdef __cplusplus
54 extern "C" { /*}*/
55 #endif
56 
57 #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 
59 #define _TN_PIC32_INTSAVE_DATA_INVALID 0xffffffff
60 
61 #if TN_DEBUG
62 # define _TN_PIC32_INTSAVE_CHECK() \
63 { \
64  if (tn_save_status_reg == _TN_PIC32_INTSAVE_DATA_INVALID){ \
65  _TN_FATAL_ERROR(""); \
66  } \
67 }
68 #else
69 # define _TN_PIC32_INTSAVE_CHECK() /* nothing */
70 #endif
71 
72 /**
73  * FFS - find first set bit. Used in `_find_next_task_to_run()` function.
74  *
75  * May be not defined: in this case, naive algorithm will be used.
76  */
77 #define _TN_FFS(x) (32 - __builtin_clz((x) & (0 - (x))))
78 
79 /**
80  * Used by the kernel as a signal that something really bad happened.
81  * Indicates TNeoKernel bugs as well as illegal kernel usage
82  * (e.g. sleeping in the idle task callback)
83  *
84  * Typically, set to assembler instruction that causes debugger to halt.
85  */
86 #define _TN_FATAL_ERROR(error_msg, ...) \
87  {__asm__ volatile(" sdbbp 0"); __asm__ volatile ("nop");}
88 
89 
90 
91 /**
92  * \def TN_ARCH_STK_ATTR_BEFORE
93  *
94  * Compiler-specific attribute that should be placed **before** declaration of
95  * array used for stack. It is needed because there are often additional
96  * restrictions applied to alignment of stack, so, to meet them, stack arrays
97  * need to be declared with these macros.
98  *
99  * @see TN_ARCH_STK_ATTR_AFTER
100  */
101 
102 /**
103  * \def TN_ARCH_STK_ATTR_AFTER
104  *
105  * Compiler-specific attribute that should be placed **after** declaration of
106  * array used for stack. It is needed because there are often additional
107  * restrictions applied to alignment of stack, so, to meet them, stack arrays
108  * need to be declared with these macros.
109  *
110  * @see TN_ARCH_STK_ATTR_BEFORE
111  */
112 
113 #if defined (__XC32)
114 # define TN_ARCH_STK_ATTR_BEFORE
115 # define TN_ARCH_STK_ATTR_AFTER __attribute__((aligned(0x8)))
116 #else
117 # error "Unknown compiler"
118 #endif
119 
120 /**
121  * Minimum task's stack size, in words, not in bytes; includes a space for
122  * context plus for parameters passed to task's body function.
123  */
124 #define TN_MIN_STACK_SIZE 36
125 
126 /**
127  * Width of `int` type.
128  */
129 #define TN_INT_WIDTH 32
130 
131 /**
132  * Unsigned integer type whose size is equal to the size of CPU register.
133  * Typically it's plain `unsigned int`.
134  */
135 typedef unsigned int TN_UWord;
136 
137 
138 /**
139  * Number of priorities available, this value usually matches `#TN_INT_WIDTH`.
140  * For compatibility with all platforms, it's recommended to use only values
141  * from 1 to 14, inclusive.
142  */
143 #define TN_PRIORITIES_CNT TN_INT_WIDTH
144 
145 /**
146  * Value for infinite waiting, usually matches `UINT_MAX`
147  */
148 #define TN_WAIT_INFINITE 0xFFFFFFFF
149 
150 /**
151  * Value for initializing the task's stack
152  */
153 #define TN_FILL_STACK_VAL 0xFEEDFACE
154 
155 
156 
157 
158 /**
159  * Declares variable that is used by macros `TN_INT_DIS_SAVE()` and
160  * `TN_INT_RESTORE()` for storing status register value.
161  *
162  * It is good idea to initially set it to some invalid value,
163  * and if TN_DEBUG is non-zero, check it in TN_INT_RESTORE().
164  * Then, we can catch bugs if someone tries to restore interrupts status
165  * without saving it first.
166  *
167  * @see `TN_INT_DIS_SAVE()`
168  * @see `TN_INT_RESTORE()`
169  */
170 #define TN_INTSAVE_DATA \
171  int tn_save_status_reg = _TN_PIC32_INTSAVE_DATA_INVALID;
172 
173 /**
174  * The same as `#TN_INTSAVE_DATA` but for using in ISR together with
175  * `TN_INT_IDIS_SAVE()`, `TN_INT_IRESTORE()`.
176  *
177  * @see `TN_INT_IDIS_SAVE()`
178  * @see `TN_INT_IRESTORE()`
179  */
180 #define TN_INTSAVE_DATA_INT TN_INTSAVE_DATA
181 
182 /**
183  * \def TN_INT_DIS_SAVE()
184  *
185  * Disable interrupts and return previous value of status register,
186  * atomically. Similar `tn_arch_sr_save_int_dis()`, but implemented
187  * as a macro, so it is potentially faster.
188  *
189  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
190  *
191  * @see `#TN_INTSAVE_DATA`
192  * @see `tn_arch_sr_save_int_dis()`
193  */
194 
195 /**
196  * \def TN_INT_RESTORE()
197  *
198  * Restore previously saved status register.
199  * Similar to `tn_arch_sr_restore()`, but implemented as a macro,
200  * so it is potentially faster.
201  *
202  * Uses `#TN_INTSAVE_DATA` as a temporary storage.
203  *
204  * @see `#TN_INTSAVE_DATA`
205  * @see `tn_arch_sr_save_int_dis()`
206  */
207 
208 #ifdef __mips16
209 # define TN_INT_DIS_SAVE() tn_save_status_reg = tn_arch_sr_save_int_dis()
210 # define TN_INT_RESTORE() _TN_PIC32_INTSAVE_CHECK(); tn_arch_sr_restore(tn_save_status_reg)
211 #else
212 # define TN_INT_DIS_SAVE() __asm__ __volatile__("di %0; ehb" : "=d" (tn_save_status_reg))
213 # define TN_INT_RESTORE() _TN_PIC32_INTSAVE_CHECK(); __builtin_mtc0(12, 0, tn_save_status_reg)
214 #endif
215 
216 /**
217  * The same as `TN_INT_DIS_SAVE()` but for using in ISR.
218  *
219  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
220  *
221  * @see `#TN_INTSAVE_DATA_INT`
222  */
223 #define TN_INT_IDIS_SAVE() TN_INT_DIS_SAVE()
224 
225 /**
226  * The same as `TN_INT_RESTORE()` but for using in ISR.
227  *
228  * Uses `#TN_INTSAVE_DATA_INT` as a temporary storage.
229  *
230  * @see `#TN_INTSAVE_DATA_INT`
231  */
232 #define TN_INT_IRESTORE() TN_INT_RESTORE()
233 
234 /**
235  * Returns nonzero if interrupts are disabled, zero otherwise.
236  */
237 #define TN_IS_INT_DISABLED() ((__builtin_mfc0(12, 0) & 1) == 0)
238 
239 
240 #endif //-- DOXYGEN_SHOULD_SKIP_THIS
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 // ---------------------------------------------------------------------------
252 
253 /**
254  * Interrupt handler wrapper macro for software context saving.
255  *
256  * Usage looks like the following:
257  *
258  * tn_soft_isr(_TIMER_1_VECTOR)
259  * {
260  * INTClearFlag(INT_T1);
261  *
262  * //-- do something useful
263  * }
264  *
265  * Note that you should not use `__ISR(_TIMER_1_VECTOR)` macro for that.
266  *
267  * @param vec interrupt vector number, such as `_TIMER_1_VECTOR`, etc.
268  */
269 #define tn_soft_isr(vec) \
270 __attribute__((__noinline__)) void _func##vec(void); \
271 void __attribute__((naked, nomips16)) \
272  __attribute__((vector(vec))) \
273  _isr##vec(void) \
274 { \
275  asm volatile(".set push"); \
276  asm volatile(".set mips32r2"); \
277  asm volatile(".set nomips16"); \
278  asm volatile(".set noreorder"); \
279  asm volatile(".set noat"); \
280  \
281  asm volatile("rdpgpr $sp, $sp"); \
282  \
283  /* Increase interrupt nesting count */ \
284  asm volatile("lui $k0, %hi(tn_int_nest_count)"); \
285  asm volatile("lw $k1, %lo(tn_int_nest_count)($k0)"); \
286  asm volatile("addiu $k1, $k1, 1"); \
287  asm volatile("sw $k1, %lo(tn_int_nest_count)($k0)"); \
288  asm volatile("ori $k0, $zero, 1"); \
289  asm volatile("bne $k1, $k0, 1f"); \
290  \
291  /* Swap stack pointers if nesting count is one */ \
292  asm volatile("lui $k0, %hi(tn_user_sp)"); \
293  asm volatile("sw $sp, %lo(tn_user_sp)($k0)"); \
294  asm volatile("lui $k0, %hi(tn_int_sp)"); \
295  asm volatile("lw $sp, %lo(tn_int_sp)($k0)"); \
296  \
297  asm volatile("1:"); \
298  /* Save context on stack */ \
299  asm volatile("addiu $sp, $sp, -92"); \
300  asm volatile("mfc0 $k1, $14"); /* c0_epc*/ \
301  asm volatile("mfc0 $k0, $12, 2"); /* c0_srsctl*/ \
302  asm volatile("sw $k1, 84($sp)"); \
303  asm volatile("sw $k0, 80($sp)"); \
304  asm volatile("mfc0 $k1, $12"); /* c0_status*/ \
305  asm volatile("sw $k1, 88($sp)"); \
306  \
307  /* Enable nested interrupts */ \
308  asm volatile("mfc0 $k0, $13"); /* c0_cause*/ \
309  asm volatile("ins $k1, $zero, 1, 15"); \
310  asm volatile("ext $k0, $k0, 10, 6"); \
311  asm volatile("ins $k1, $k0, 10, 6"); \
312  asm volatile("mtc0 $k1, $12"); /* c0_status*/ \
313  \
314  /* Save caller-save registers on stack */ \
315  asm volatile("sw $ra, 76($sp)"); \
316  asm volatile("sw $t9, 72($sp)"); \
317  asm volatile("sw $t8, 68($sp)"); \
318  asm volatile("sw $t7, 64($sp)"); \
319  asm volatile("sw $t6, 60($sp)"); \
320  asm volatile("sw $t5, 56($sp)"); \
321  asm volatile("sw $t4, 52($sp)"); \
322  asm volatile("sw $t3, 48($sp)"); \
323  asm volatile("sw $t2, 44($sp)"); \
324  asm volatile("sw $t1, 40($sp)"); \
325  asm volatile("sw $t0, 36($sp)"); \
326  asm volatile("sw $a3, 32($sp)"); \
327  asm volatile("sw $a2, 28($sp)"); \
328  asm volatile("sw $a1, 24($sp)"); \
329  asm volatile("sw $a0, 20($sp)"); \
330  asm volatile("sw $v1, 16($sp)"); \
331  asm volatile("sw $v0, 12($sp)"); \
332  asm volatile("sw $at, 8($sp)"); \
333  asm volatile("mfhi $v0"); \
334  asm volatile("mflo $v1"); \
335  asm volatile("sw $v0, 4($sp)"); \
336  \
337  /* Call ISR */ \
338  asm volatile("la $t0, _func"#vec); \
339  asm volatile("jalr $t0"); \
340  asm volatile("sw $v1, 0($sp)"); \
341  \
342  /* Pend context switch if needed */ \
343  asm volatile("lw $t0, tn_curr_run_task"); \
344  asm volatile("lw $t1, tn_next_task_to_run"); \
345  asm volatile("lw $t0, 0($t0)"); \
346  asm volatile("lw $t1, 0($t1)"); \
347  asm volatile("lui $t2, %hi(IFS0SET)"); \
348  asm volatile("beq $t0, $t1, 1f"); \
349  asm volatile("ori $t1, $zero, 2"); \
350  asm volatile("sw $t1, %lo(IFS0SET)($t2)"); \
351  \
352  asm volatile("1:"); \
353  /* Restore registers */ \
354  asm volatile("lw $v1, 0($sp)"); \
355  asm volatile("lw $v0, 4($sp)"); \
356  asm volatile("mtlo $v1"); \
357  asm volatile("mthi $v0"); \
358  asm volatile("lw $at, 8($sp)"); \
359  asm volatile("lw $v0, 12($sp)"); \
360  asm volatile("lw $v1, 16($sp)"); \
361  asm volatile("lw $a0, 20($sp)"); \
362  asm volatile("lw $a1, 24($sp)"); \
363  asm volatile("lw $a2, 28($sp)"); \
364  asm volatile("lw $a3, 32($sp)"); \
365  asm volatile("lw $t0, 36($sp)"); \
366  asm volatile("lw $t1, 40($sp)"); \
367  asm volatile("lw $t2, 44($sp)"); \
368  asm volatile("lw $t3, 48($sp)"); \
369  asm volatile("lw $t4, 52($sp)"); \
370  asm volatile("lw $t5, 56($sp)"); \
371  asm volatile("lw $t6, 60($sp)"); \
372  asm volatile("lw $t7, 64($sp)"); \
373  asm volatile("lw $t8, 68($sp)"); \
374  asm volatile("lw $t9, 72($sp)"); \
375  asm volatile("lw $ra, 76($sp)"); \
376  \
377  asm volatile("di"); \
378  asm volatile("ehb"); \
379  \
380  /* Restore context */ \
381  asm volatile("lw $k0, 84($sp)"); \
382  asm volatile("mtc0 $k0, $14"); /* c0_epc */ \
383  asm volatile("lw $k0, 80($sp)"); \
384  asm volatile("mtc0 $k0, $12, 2"); /* c0_srsctl */ \
385  asm volatile("addiu $sp, $sp, 92"); \
386  \
387  /* Decrease interrupt nesting count */ \
388  asm volatile("lui $k0, %hi(tn_int_nest_count)"); \
389  asm volatile("lw $k1, %lo(tn_int_nest_count)($k0)"); \
390  asm volatile("addiu $k1, $k1, -1"); \
391  asm volatile("sw $k1, %lo(tn_int_nest_count)($k0)"); \
392  asm volatile("bne $k1, $zero, 1f"); \
393  asm volatile("lw $k1, -4($sp)"); \
394  \
395  /* Swap stack pointers if nesting count is zero */ \
396  asm volatile("lui $k0, %hi(tn_int_sp)"); \
397  asm volatile("sw $sp, %lo(tn_int_sp)($k0)"); \
398  asm volatile("lui $k0, %hi(tn_user_sp)"); \
399  asm volatile("lw $sp, %lo(tn_user_sp)($k0)"); \
400  \
401  asm volatile("1:"); \
402  asm volatile("wrpgpr $sp, $sp"); \
403  asm volatile("mtc0 $k1, $12"); /* c0_status */ \
404  asm volatile("eret"); \
405  \
406  asm volatile(".set pop"); \
407  \
408 } __attribute((__noinline__)) void _func##vec(void)
409 
410 
411 
412 
413 /**
414  * Interrupt handler wrapper macro for shadow register context saving.
415  *
416  * Usage looks like the following:
417  *
418  * tn_srs_isr(_INT_UART_1_VECTOR)
419  * {
420  * INTClearFlag(INT_U1);
421  *
422  * //-- do something useful
423  * }
424  *
425  * Note that you should not use `__ISR(_INT_UART_1_VECTOR)` macro for that.
426  *
427  * @param vec interrupt vector number, such as `_TIMER_1_VECTOR`, etc.
428  */
429 #define tn_srs_isr(vec) \
430 __attribute__((__noinline__)) void _func##vec(void); \
431 void __attribute__((naked, nomips16)) \
432  __attribute__((vector(vec))) \
433  _isr##vec(void) \
434 { \
435  asm volatile(".set push"); \
436  asm volatile(".set mips32r2"); \
437  asm volatile(".set nomips16"); \
438  asm volatile(".set noreorder"); \
439  asm volatile(".set noat"); \
440  \
441  asm volatile("rdpgpr $sp, $sp"); \
442  \
443  /* Increase interrupt nesting count */ \
444  asm volatile("lui $k0, %hi(tn_int_nest_count)"); \
445  asm volatile("lw $k1, %lo(tn_int_nest_count)($k0)"); \
446  asm volatile("addiu $k1, $k1, 1"); \
447  asm volatile("sw $k1, %lo(tn_int_nest_count)($k0)"); \
448  asm volatile("ori $k0, $zero, 1"); \
449  asm volatile("bne $k1, $k0, 1f"); \
450  \
451  /* Swap stack pointers if nesting count is one */ \
452  asm volatile("lui $k0, %hi(tn_user_sp)"); \
453  asm volatile("sw $sp, %lo(tn_user_sp)($k0)"); \
454  asm volatile("lui $k0, %hi(tn_int_sp)"); \
455  asm volatile("lw $sp, %lo(tn_int_sp)($k0)"); \
456  \
457  asm volatile("1:"); \
458  /* Save context on stack */ \
459  asm volatile("addiu $sp, $sp, -20"); \
460  asm volatile("mfc0 $k1, $14"); /* c0_epc */ \
461  asm volatile("mfc0 $k0, $12, 2"); /* c0_srsctl */ \
462  asm volatile("sw $k1, 12($sp)"); \
463  asm volatile("sw $k0, 8($sp)"); \
464  asm volatile("mfc0 $k1, $12"); /* c0_status */ \
465  asm volatile("sw $k1, 16($sp)"); \
466  \
467  /* Enable nested interrupts */ \
468  asm volatile("mfc0 $k0, $13"); /* c0_cause */ \
469  asm volatile("ins $k1, $zero, 1, 15"); \
470  asm volatile("ext $k0, $k0, 10, 6"); \
471  asm volatile("ins $k1, $k0, 10, 6"); \
472  asm volatile("mtc0 $k1, $12"); /* c0_status */ \
473  \
474  /* Save caller-save registers on stack */ \
475  asm volatile("mfhi $v0"); \
476  asm volatile("mflo $v1"); \
477  asm volatile("sw $v0, 4($sp)"); \
478  \
479  /* Call ISR */ \
480  asm volatile("la $t0, _func"#vec); \
481  asm volatile("jalr $t0"); \
482  asm volatile("sw $v1, 0($sp)"); \
483  \
484  /* Pend context switch if needed */ \
485  asm volatile("lw $t0, tn_curr_run_task"); \
486  asm volatile("lw $t1, tn_next_task_to_run"); \
487  asm volatile("lw $t0, 0($t0)"); \
488  asm volatile("lw $t1, 0($t1)"); \
489  asm volatile("lui $t2, %hi(IFS0SET)"); \
490  asm volatile("beq $t0, $t1, 1f"); \
491  asm volatile("ori $t1, $zero, 2"); \
492  asm volatile("sw $t1, %lo(IFS0SET)($t2)"); \
493  \
494  asm volatile("1:"); \
495  /* Restore registers */ \
496  asm volatile("lw $v1, 0($sp)"); \
497  asm volatile("lw $v0, 4($sp)"); \
498  asm volatile("mtlo $v1"); \
499  asm volatile("mthi $v0"); \
500  \
501  asm volatile("di"); \
502  asm volatile("ehb"); \
503  \
504  /* Restore context */ \
505  asm volatile("lw $k0, 12($sp)"); \
506  asm volatile("mtc0 $k0, $14"); /* c0_epc */ \
507  asm volatile("lw $k0, 8($sp)"); \
508  asm volatile("mtc0 $k0, $12, 2"); /* c0_srsctl */ \
509  asm volatile("addiu $sp, $sp, 20"); \
510  \
511  /* Decrease interrupt nesting count */ \
512  asm volatile("lui $k0, %hi(tn_int_nest_count)"); \
513  asm volatile("lw $k1, %lo(tn_int_nest_count)($k0)"); \
514  asm volatile("addiu $k1, $k1, -1"); \
515  asm volatile("sw $k1, %lo(tn_int_nest_count)($k0)"); \
516  asm volatile("bne $k1, $zero, 1f"); \
517  asm volatile("lw $k1, -4($sp)"); \
518  \
519  /* Swap stack pointers if nesting count is zero */ \
520  asm volatile("lui $k0, %hi(tn_int_sp)"); \
521  asm volatile("sw $sp, %lo(tn_int_sp)($k0)"); \
522  asm volatile("lui $k0, %hi(tn_user_sp)"); \
523  asm volatile("lw $sp, %lo(tn_user_sp)($k0)"); \
524  \
525  asm volatile("1:"); \
526  asm volatile("wrpgpr $sp, $sp"); \
527  asm volatile("mtc0 $k1, $12"); /* c0_status */ \
528  asm volatile("eret"); \
529  \
530  asm volatile(".set pop"); \
531  \
532 } __attribute((__noinline__)) void _func##vec(void)
533 
534 
535 #ifdef __cplusplus
536 } /* extern "C" */
537 #endif
538 
539 #endif // _TN_ARCH_PIC32_H
540 
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.