TNeoKernel  v1.02
 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  *
50  * May be not defined: in this case, naive algorithm will be used.
51  */
52 #define _TN_FFS(x) (32 - __builtin_clz((x) & (0 - (x))))
53 
54 /**
55  * Used by the kernel as a signal that something really bad happened.
56  * Indicates TNeoKernel bugs as well as illegal kernel usage
57  * (e.g. sleeping in the idle task callback)
58  *
59  * Typically, set to assembler instruction that causes debugger to halt.
60  */
61 #define _TN_FATAL_ERROR(error_msg, ...) \
62  {__asm__ volatile(" sdbbp 0"); __asm__ volatile ("nop");}
63 
64 
65 
66 
67 /**
68  * Compiler-specific attribute that should be placed **before** declaration of
69  * array used for stack. It is needed because there are often additional
70  * restrictions applied to alignment of stack, so, to meet them, stack arrays
71  * need to be declared with these macros.
72  *
73  * @see TN_ARCH_STK_ATTR_AFTER
74  */
75 #define TN_ARCH_STK_ATTR_BEFORE
76 
77 /**
78  * Compiler-specific attribute that should be placed **after** declaration of
79  * array used for stack. It is needed because there are often additional
80  * restrictions applied to alignment of stack, so, to meet them, stack arrays
81  * need to be declared with these macros.
82  *
83  * @see TN_ARCH_STK_ATTR_BEFORE
84  */
85 
86 #define TN_ARCH_STK_ATTR_AFTER __attribute__((aligned(0x8)))
87 
88 /**
89  * Minimum task's stack size, in words, not in bytes; includes a space for
90  * context plus for parameters passed to task's body function.
91  */
92 #define TN_MIN_STACK_SIZE 36
93 
94 /**
95  * Width of `int` type.
96  */
97 #define TN_INT_WIDTH 32
98 
99 /**
100  * Unsigned integer type whose size is equal to the size of CPU register.
101  * Typically it's plain `unsigned int`.
102  */
103 typedef unsigned int TN_UWord;
104 
105 /**
106  * Number of priorities available, this value usually matches `#TN_INT_WIDTH`.
107  * For compatibility with all platforms, it's recommended to use only values
108  * from 1 to 14, inclusive.
109  *
110  * @see `#TN_INT_WIDTH`
111  */
112 #define TN_PRIORITIES_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 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 
194 
195 #endif // _TN_ARCH_EXAMPLE_H
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.