TNeoKernel  v1.02
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tn_arch.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  * Architecture-dependent routines declaration.
41  */
42 
43 #ifndef _TN_ARCH_H
44 #define _TN_ARCH_H
45 
46 
47 /*******************************************************************************
48  * INCLUDED FILES
49  ******************************************************************************/
50 
51 #include "../core/tn_common.h"
52 
53 
54 
55 /*******************************************************************************
56  * ACTUAL PORT IMPLEMENTATION
57  ******************************************************************************/
58 
59 #if defined(__PIC32MX__)
60 #include "pic32/tn_arch_pic32.h"
61 #else
62 #error "unknown platform"
63 #endif
64 
65 
66 
67 #ifdef __cplusplus
68 extern "C" { /*}*/
69 #endif
70 
71 /*******************************************************************************
72  * PUBLIC FUNCTION PROTOTYPES
73  ******************************************************************************/
74 
75 
76 /**
77  * Unconditionally disable interrupts
78  */
79 void tn_arch_int_dis(void);
80 
81 /**
82  * Unconditionally enable interrupts
83  */
84 void tn_arch_int_en(void);
85 
86 /**
87  * Disable interrupts and return previous value of status register,
88  * atomically
89  *
90  * @see `tn_arch_sr_restore()`
91  */
92 unsigned int tn_arch_sr_save_int_dis(void);
93 
94 /**
95  * Restore previously saved status register
96  *
97  * @param sr status register value previously from
98  * `tn_arch_sr_save_int_dis()`
99  *
100  * @see `tn_arch_sr_save_int_dis()`
101  */
102 void tn_arch_sr_restore(unsigned int sr);
103 
104 
105 
106 
107 /**
108  * Should return top of the stack, which may be either:
109  *
110  * - `(stack_low_address - 1)`
111  * - `(stack_low_address + stack_size)`
112  *
113  * (depending on the architecture)
114  *
115  * **NOTE** that returned *top of the stack* is NOT the address which may be
116  * used for storing the new data. Instead, it is the *previous* address.
117  *
118  * @param stack_low_address
119  * start address of the stack array.
120  * @param stack_size
121  * size of the stack in `#TN_UWord`-s, not in bytes.
122  */
124  TN_UWord *stack_low_address,
125  int stack_size
126  );
127 
128 /**
129  * Should initialize stack for new task and return current stack pointer.
130  *
131  * @param task_func
132  * Pointer to task body function.
133  * @param stack_top
134  * Top of the stack, returned by `_tn_arch_stack_top_get()`.
135  * @param param
136  * User-provided parameter for task body function.
137  *
138  * @return current stack pointer (top of the stack)
139  */
140 unsigned int *_tn_arch_stack_init(
141  TN_TaskBody *task_func,
142  TN_UWord *stack_top,
143  void *param
144  );
145 
146 /**
147  * Should return 1 if ISR is currently running, 0 otherwise
148  */
149 int _tn_arch_inside_isr(void);
150 
151 /**
152  * Called whenever we need to switch context to other task.
153  *
154  * **Preconditions:**
155  *
156  * * interrupts are enabled;
157  * * `tn_curr_run_task` points to currently running (preempted) task;
158  * * `tn_next_task_to_run` points to new task to run.
159  *
160  * **Actions to perform:**
161  *
162  * * save context of the preempted task to its stack;
163  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
164  * * switch context to it.
165  *
166  * @see `tn_curr_run_task`
167  * @see `tn_next_task_to_run`
168  */
169 void _tn_arch_context_switch(void);
170 
171 /**
172  * Called when some task calls `tn_task_exit()`.
173  *
174  * **Preconditions:**
175  *
176  * * interrupts are disabled;
177  * * `tn_next_task_to_run` is already set to other task.
178  *
179  * **Actions to perform:**
180  *
181  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
182  * * switch context to it.
183  *
184  * @see `tn_curr_run_task`
185  * @see `tn_next_task_to_run`
186  */
188 
189 /**
190  * Should perform first context switch (to the task pointed to by
191  * `tn_next_task_to_run`).
192  *
193  * **Preconditions:**
194  *
195  * * no interrupts are set up yet, so, it's like interrupts disabled
196  * * `tn_next_task_to_run` is already set to idle task.
197  *
198  * **Actions to perform:**
199  *
200  * * set `#TN_STATE_FLAG__SYS_RUNNING` flag in the `tn_sys_state` variable;
201  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
202  * * switch context to it.
203  *
204  * @see `#TN_STATE_FLAG__SYS_RUNNING`
205  * @see `tn_sys_state`
206  * @see `tn_curr_run_task`
207  * @see `tn_next_task_to_run`
208  */
209 void _tn_arch_system_start(void);
210 
211 #ifdef __cplusplus
212 } /* extern "C" */
213 #endif
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 #endif /* _TN_ARCH_H */
224 
PIC32 architecture-dependent routines.
unsigned int * _tn_arch_stack_init(TN_TaskBody *task_func, TN_UWord *stack_top, void *param)
Should initialize stack for new task and return current stack pointer.
TN_UWord * _tn_arch_stack_top_get(TN_UWord *stack_low_address, int stack_size)
Should return top of the stack, which may be either:
void tn_arch_int_en(void)
Unconditionally enable interrupts.
void tn_arch_int_dis(void)
Unconditionally disable interrupts.
void tn_arch_sr_restore(unsigned int sr)
Restore previously saved status register.
void _tn_arch_context_switch(void)
Called whenever we need to switch context to other task.
void _tn_arch_context_switch_exit(void)
Called when some task calls tn_task_exit().
void _tn_arch_system_start(void)
Should perform first context switch (to the task pointed to by tn_next_task_to_run).
int _tn_arch_inside_isr(void)
Should return 1 if ISR is currently running, 0 otherwise.
unsigned int tn_arch_sr_save_int_dis(void)
Disable interrupts and return previous value of status register, atomically.
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:164
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.