TNeoKernel  v1.03
 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  */
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  */
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 put initial CPU context to the provided stack pointer for new task
130  * and return current stack pointer.
131  *
132  * When resulting context gets restored by
133  * `_tn_arch_context_switch_now_nosave()` or `_tn_arch_context_switch_pend()`,
134  * the following conditions should be met:
135  *
136  * - Interrupts are enabled;
137  * - Return address is set to `tn_task_exit()`, so that when task body function
138  * returns, `tn_task_exit()` gets automatially called;
139  * - Argument 0 contains `param` pointer
140  *
141  * @param task_func
142  * Pointer to task body function.
143  * @param stack_top
144  * Top of the stack, returned by `_tn_arch_stack_top_get()`.
145  * @param param
146  * User-provided parameter for task body function.
147  *
148  * @return current stack pointer (top of the stack)
149  */
151  TN_TaskBody *task_func,
152  TN_UWord *stack_top,
153  void *param
154  );
155 
156 /**
157  * Should return 1 if ISR is currently running, 0 otherwise
158  */
159 int _tn_arch_inside_isr(void);
160 
161 /**
162  * Called whenever we need to switch context from one task to another.
163  *
164  * This function typically does NOT switch context; it merely pends it,
165  * that is, it sets appropriate interrupt flag. If current level is an
166  * application level, interrupt is fired immediately, and context gets
167  * switched.
168  *
169  * But, if it's hard or impossible on particular platform to use dedicated
170  * interrupt flag, this function may just switch the context on its own.
171  *
172  * **Preconditions:**
173  *
174  * - interrupts are enabled;
175  * - `tn_curr_run_task` points to currently running (preempted) task;
176  * - `tn_next_task_to_run` points to new task to run.
177  *
178  * **Actions to perform in actual context switching routine:**
179  *
180  * - save context of the preempted task to its stack;
181  * - set `tn_curr_run_task` to `tn_next_task_to_run`;
182  * - restore context of the newly activated task from its stack.
183  *
184  * @see `tn_curr_run_task`
185  * @see `tn_next_task_to_run`
186  */
188 
189 /**
190  * Called whenever we need to switch context to new task, but don't save
191  * current context. This happens:
192  * - At system start, inside `tn_sys_start()`;
193  * - At task exit, inside `tn_task_exit()`
194  *
195  * This function doesn't pend context switch, it switches context immediately.
196  *
197  * **Preconditions:**
198  *
199  * - interrupts are disabled;
200  * - `tn_next_task_to_run` is already set to needed task.
201  *
202  * **Actions to perform:**
203  *
204  * - set `tn_curr_run_task` to `tn_next_task_to_run`;
205  * - restore context of the newly activated task from its stack.
206  *
207  * @see `tn_curr_run_task`
208  * @see `tn_next_task_to_run`
209  */
211 
212 #ifdef __cplusplus
213 } /* extern "C" */
214 #endif
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 #endif /* _TN_ARCH_H */
225 
PIC32 architecture-dependent routines.
void _tn_arch_context_switch_pend(void)
Called whenever we need to switch context from one task to another.
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.
TN_UWord * _tn_arch_stack_init(TN_TaskBody *task_func, TN_UWord *stack_top, void *param)
Should put initial CPU context to the provided stack pointer for new task and return current stack po...
int _tn_arch_inside_isr(void)
Should return 1 if ISR is currently running, 0 otherwise.
void( TN_TaskBody)(void *param)
Prototype for task body function.
Definition: tn_common.h:164
TN_UWord tn_arch_sr_save_int_dis(void)
Disable interrupts and return previous value of status register, atomically.
void _tn_arch_context_switch_now_nosave(void)
Called whenever we need to switch context to new task, but don't save current context.
void tn_arch_sr_restore(TN_UWord sr)
Restore previously saved status register.
unsigned int TN_UWord
Unsigned integer type whose size is equal to the size of CPU register.