1 /*
2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*
30 Changes from V4.2.1
31
32 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
33 */
34
35 /*-----------------------------------------------------------
36 * Implementation of functions defined in portable.h for the PIC24 port.
37 *----------------------------------------------------------*/
38
39 /* Scheduler include files. */
40 #include "FreeRTOS.h"
41 #include "task.h"
42
43 /* Hardware specifics. */
44 #define portBIT_SET 1
45 #define portTIMER_PRESCALE 8
46 #define portINITIAL_SR 0
47
48 /* Defined for backward compatability with project created prior to
49 FreeRTOS.org V4.3.0. */
50 #ifndef configKERNEL_INTERRUPT_PRIORITY
51 #define configKERNEL_INTERRUPT_PRIORITY 1
52 #endif
53
54 /* Use _T1Interrupt as the interrupt handler name if the application writer has
55 not provided their own. */
56 #ifndef configTICK_INTERRUPT_HANDLER
57 #define configTICK_INTERRUPT_HANDLER _T1Interrupt
58 #endif /* configTICK_INTERRUPT_HANDLER */
59
60 /* The program counter is only 23 bits. */
61 #define portUNUSED_PR_BITS 0x7f
62
63 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
64 UBaseType_t uxCriticalNesting = 0xef;
65
66 #if configKERNEL_INTERRUPT_PRIORITY != 1
67 #error If configKERNEL_INTERRUPT_PRIORITY is not 1 then the #32 in the following macros needs changing to equal the portINTERRUPT_BITS value, which is ( configKERNEL_INTERRUPT_PRIORITY << 5 )
68 #endif
69
70 #if defined( __PIC24E__ ) || defined ( __PIC24F__ ) || defined( __PIC24FK__ ) || defined( __PIC24H__ )
71
72 #ifdef __HAS_EDS__
73 #define portRESTORE_CONTEXT() \
74 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
75 "MOV [W0], W15 \n" \
76 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
77 "MOV W0, _uxCriticalNesting \n" \
78 "POP DSWPAG \n" \
79 "POP DSRPAG \n" \
80 "POP CORCON \n" \
81 "POP TBLPAG \n" \
82 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
83 "POP W14 \n" \
84 "POP.D W12 \n" \
85 "POP.D W10 \n" \
86 "POP.D W8 \n" \
87 "POP.D W6 \n" \
88 "POP.D W4 \n" \
89 "POP.D W2 \n" \
90 "POP.D W0 \n" \
91 "POP SR " );
92 #else /* __HAS_EDS__ */
93 #define portRESTORE_CONTEXT() \
94 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
95 "MOV [W0], W15 \n" \
96 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
97 "MOV W0, _uxCriticalNesting \n" \
98 "POP PSVPAG \n" \
99 "POP CORCON \n" \
100 "POP TBLPAG \n" \
101 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
102 "POP W14 \n" \
103 "POP.D W12 \n" \
104 "POP.D W10 \n" \
105 "POP.D W8 \n" \
106 "POP.D W6 \n" \
107 "POP.D W4 \n" \
108 "POP.D W2 \n" \
109 "POP.D W0 \n" \
110 "POP SR " );
111 #endif /* __HAS_EDS__ */
112 #endif /* defined( __PIC24E__ ) || defined ( __PIC24F__ ) || defined( __PIC24FK__ ) || defined( __PIC24H__ ) */
113
114 #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ )
115
116 #define portRESTORE_CONTEXT() \
117 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
118 "MOV [W0], W15 \n" \
119 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
120 "MOV W0, _uxCriticalNesting \n" \
121 "POP PSVPAG \n" \
122 "POP CORCON \n" \
123 "POP DOENDH \n" \
124 "POP DOENDL \n" \
125 "POP DOSTARTH \n" \
126 "POP DOSTARTL \n" \
127 "POP DCOUNT \n" \
128 "POP ACCBU \n" \
129 "POP ACCBH \n" \
130 "POP ACCBL \n" \
131 "POP ACCAU \n" \
132 "POP ACCAH \n" \
133 "POP ACCAL \n" \
134 "POP TBLPAG \n" \
135 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
136 "POP W14 \n" \
137 "POP.D W12 \n" \
138 "POP.D W10 \n" \
139 "POP.D W8 \n" \
140 "POP.D W6 \n" \
141 "POP.D W4 \n" \
142 "POP.D W2 \n" \
143 "POP.D W0 \n" \
144 "POP SR " );
145
146 #endif /* defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) */
147
148 #ifndef portRESTORE_CONTEXT
149 #error Unrecognised device selected
150
151 /* Note: dsPIC parts with EDS are not supported as there is no easy way to
152 recover the hardware stacked copies for DOCOUNT, DOHIGH, DOLOW. */
153 #endif
154
155 /*
156 * Setup the timer used to generate the tick interrupt.
157 */
158 void vApplicationSetupTickTimerInterrupt( void );
159
160 /*
161 * See header file for description.
162 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)163 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
164 {
165 uint16_t usCode;
166 UBaseType_t i;
167
168 const StackType_t xInitialStack[] =
169 {
170 0x1111, /* W1 */
171 0x2222, /* W2 */
172 0x3333, /* W3 */
173 0x4444, /* W4 */
174 0x5555, /* W5 */
175 0x6666, /* W6 */
176 0x7777, /* W7 */
177 0x8888, /* W8 */
178 0x9999, /* W9 */
179 0xaaaa, /* W10 */
180 0xbbbb, /* W11 */
181 0xcccc, /* W12 */
182 0xdddd, /* W13 */
183 0xeeee, /* W14 */
184 0xcdce, /* RCOUNT */
185 0xabac, /* TBLPAG */
186
187 /* dsPIC specific registers. */
188 #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ )
189 0x0202, /* ACCAL */
190 0x0303, /* ACCAH */
191 0x0404, /* ACCAU */
192 0x0505, /* ACCBL */
193 0x0606, /* ACCBH */
194 0x0707, /* ACCBU */
195 0x0808, /* DCOUNT */
196 0x090a, /* DOSTARTL */
197 0x1010, /* DOSTARTH */
198 0x1110, /* DOENDL */
199 0x1212, /* DOENDH */
200 #endif
201 };
202
203 /* Setup the stack as if a yield had occurred.
204
205 Save the low bytes of the program counter. */
206 usCode = ( uint16_t ) pxCode;
207 *pxTopOfStack = ( StackType_t ) usCode;
208 pxTopOfStack++;
209
210 /* Save the high byte of the program counter. This will always be zero
211 here as it is passed in a 16bit pointer. If the address is greater than
212 16 bits then the pointer will point to a jump table. */
213 *pxTopOfStack = ( StackType_t ) 0;
214 pxTopOfStack++;
215
216 /* Status register with interrupts enabled. */
217 *pxTopOfStack = portINITIAL_SR;
218 pxTopOfStack++;
219
220 /* Parameters are passed in W0. */
221 *pxTopOfStack = ( StackType_t ) pvParameters;
222 pxTopOfStack++;
223
224 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( StackType_t ) ); i++ )
225 {
226 *pxTopOfStack = xInitialStack[ i ];
227 pxTopOfStack++;
228 }
229
230 *pxTopOfStack = CORCON;
231 pxTopOfStack++;
232
233 #if defined(__HAS_EDS__)
234 *pxTopOfStack = DSRPAG;
235 pxTopOfStack++;
236 *pxTopOfStack = DSWPAG;
237 pxTopOfStack++;
238 #else /* __HAS_EDS__ */
239 *pxTopOfStack = PSVPAG;
240 pxTopOfStack++;
241 #endif /* __HAS_EDS__ */
242
243 /* Finally the critical nesting depth. */
244 *pxTopOfStack = 0x00;
245 pxTopOfStack++;
246
247 return pxTopOfStack;
248 }
249 /*-----------------------------------------------------------*/
250
xPortStartScheduler(void)251 BaseType_t xPortStartScheduler( void )
252 {
253 /* Setup a timer for the tick ISR. */
254 vApplicationSetupTickTimerInterrupt();
255
256 /* Restore the context of the first task to run. */
257 portRESTORE_CONTEXT();
258
259 /* Simulate the end of the yield function. */
260 asm volatile ( "return" );
261
262 /* Should not reach here. */
263 return pdTRUE;
264 }
265 /*-----------------------------------------------------------*/
266
vPortEndScheduler(void)267 void vPortEndScheduler( void )
268 {
269 /* Not implemented in ports where there is nothing to return to.
270 Artificially force an assert. */
271 configASSERT( uxCriticalNesting == 1000UL );
272 }
273 /*-----------------------------------------------------------*/
274
275 /*
276 * Setup a timer for a regular tick.
277 */
vApplicationSetupTickTimerInterrupt(void)278 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
279 {
280 const uint32_t ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
281
282 /* Prescale of 8. */
283 T1CON = 0;
284 TMR1 = 0;
285
286 PR1 = ( uint16_t ) ulCompareMatch;
287
288 /* Setup timer 1 interrupt priority. */
289 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
290
291 /* Clear the interrupt as a starting condition. */
292 IFS0bits.T1IF = 0;
293
294 /* Enable the interrupt. */
295 IEC0bits.T1IE = 1;
296
297 /* Setup the prescale value. */
298 T1CONbits.TCKPS0 = 1;
299 T1CONbits.TCKPS1 = 0;
300
301 /* Start the timer. */
302 T1CONbits.TON = 1;
303 }
304 /*-----------------------------------------------------------*/
305
vPortEnterCritical(void)306 void vPortEnterCritical( void )
307 {
308 portDISABLE_INTERRUPTS();
309 uxCriticalNesting++;
310 }
311 /*-----------------------------------------------------------*/
312
vPortExitCritical(void)313 void vPortExitCritical( void )
314 {
315 configASSERT( uxCriticalNesting );
316 uxCriticalNesting--;
317 if( uxCriticalNesting == 0 )
318 {
319 portENABLE_INTERRUPTS();
320 }
321 }
322 /*-----------------------------------------------------------*/
323
configTICK_INTERRUPT_HANDLER(void)324 void __attribute__((__interrupt__, auto_psv)) configTICK_INTERRUPT_HANDLER( void )
325 {
326 /* Clear the timer interrupt. */
327 IFS0bits.T1IF = 0;
328
329 if( xTaskIncrementTick() != pdFALSE )
330 {
331 portYIELD();
332 }
333 }
334 /*-----------------------------------------------------------*/
335