TNeoKernel  v1.0
 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 start stack address, which may be either the lowest address of
109  * the stack array or the highest one, depending on the architecture.
110  *
111  * @param stack_low_address start address of the stack array.
112  * @param stack_size size of the stack in `int`-s, not in bytes.
113  */
115  TN_UWord *stack_low_address,
116  int stack_size
117  );
118 
119 /**
120  * Should initialize stack for new task and return current stack pointer.
121  *
122  * @param task_func
123  * Pointer to task body function.
124  * @param stack_start
125  * Start address of the stack, returned by `_tn_arch_stack_start_get()`.
126  * @param param
127  * User-provided parameter for task body function.
128  *
129  * @return current stack pointer (top of the stack)
130  */
131 unsigned int *_tn_arch_stack_init(
132  TN_TaskBody *task_func,
133  TN_UWord *stack_start,
134  void *param
135  );
136 
137 /**
138  * Should return 1 if ISR is currently running, 0 otherwise
139  */
140 int _tn_arch_inside_isr(void);
141 
142 /**
143  * Called whenever we need to switch context to other task.
144  *
145  * **Preconditions:**
146  *
147  * * interrupts are enabled;
148  * * `tn_curr_run_task` points to currently running (preempted) task;
149  * * `tn_next_task_to_run` points to new task to run.
150  *
151  * **Actions to perform:**
152  *
153  * * save context of the preempted task to its stack;
154  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
155  * * switch context to it.
156  *
157  * @see `tn_curr_run_task`
158  * @see `tn_next_task_to_run`
159  */
160 void _tn_arch_context_switch(void);
161 
162 /**
163  * Called when some task calls `tn_task_exit()`.
164  *
165  * **Preconditions:**
166  *
167  * * interrupts are disabled;
168  * * `tn_next_task_to_run` is already set to other task.
169  *
170  * **Actions to perform:**
171  *
172  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
173  * * switch context to it.
174  *
175  * @see `tn_curr_run_task`
176  * @see `tn_next_task_to_run`
177  */
179 
180 /**
181  * Should perform first context switch (to the task pointed to by
182  * `tn_next_task_to_run`).
183  *
184  * **Preconditions:**
185  *
186  * * no interrupts are set up yet, so, it's like interrupts disabled
187  * * `tn_next_task_to_run` is already set to idle task.
188  *
189  * **Actions to perform:**
190  *
191  * * set `#TN_STATE_FLAG__SYS_RUNNING` flag in the `tn_sys_state` variable;
192  * * set `tn_curr_run_task` to `tn_next_task_to_run`;
193  * * switch context to it.
194  *
195  * @see `#TN_STATE_FLAG__SYS_RUNNING`
196  * @see `tn_sys_state`
197  * @see `tn_curr_run_task`
198  * @see `tn_next_task_to_run`
199  */
200 void _tn_arch_system_start(void);
201 
202 #ifdef __cplusplus
203 } /* extern "C" */
204 #endif
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 #endif /* _TN_ARCH_H */
215 
PIC32 architecture-dependent routines.
TN_UWord * _tn_arch_stack_start_get(TN_UWord *stack_low_address, int stack_size)
Should return start stack address, which may be either the lowest address of the stack array or the h...
unsigned int * _tn_arch_stack_init(TN_TaskBody *task_func, TN_UWord *stack_start, void *param)
Should initialize stack for new task and return current stack pointer.
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:163
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.