2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
32 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
35 /*-----------------------------------------------------------
36 * Implementation of functions defined in portable.h for the PIC24 port.
37 *----------------------------------------------------------*/
39 /* Scheduler include files. */
43 /* Hardware specifics. */
45 #define portTIMER_PRESCALE 8
46 #define portINITIAL_SR 0
48 /* Defined for backward compatibility with project created prior to
49 FreeRTOS.org V4.3.0. */
50 #ifndef configKERNEL_INTERRUPT_PRIORITY
51 #define configKERNEL_INTERRUPT_PRIORITY 1
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 */
60 /* The program counter is only 23 bits. */
61 #define portUNUSED_PR_BITS 0x7f
63 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
64 UBaseType_t uxCriticalNesting = 0xef;
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 )
70 #if defined( __PIC24E__ ) || defined ( __PIC24F__ ) || defined( __PIC24FK__ ) || defined( __PIC24H__ )
73 #define portRESTORE_CONTEXT() \
74 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
76 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
77 "MOV W0, _uxCriticalNesting \n" \
82 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
92 #else /* __HAS_EDS__ */
93 #define portRESTORE_CONTEXT() \
94 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
96 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
97 "MOV W0, _uxCriticalNesting \n" \
101 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
111 #endif /* __HAS_EDS__ */
112 #endif /* defined( __PIC24E__ ) || defined ( __PIC24F__ ) || defined( __PIC24FK__ ) || defined( __PIC24H__ ) */
114 #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ )
116 #define portRESTORE_CONTEXT() \
117 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
119 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
120 "MOV W0, _uxCriticalNesting \n" \
135 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
146 #endif /* defined( __dsPIC30F__ ) || defined( __dsPIC33F__ ) */
148 #ifndef portRESTORE_CONTEXT
149 #error Unrecognised device selected
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. */
156 * Setup the timer used to generate the tick interrupt.
158 void vApplicationSetupTickTimerInterrupt( void );
161 * See header file for description.
163 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
168 const StackType_t xInitialStack[] =
187 /* dsPIC specific registers. */
188 #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ )
196 0x090a, /* DOSTARTL */
197 0x1010, /* DOSTARTH */
203 /* Setup the stack as if a yield had occurred.
205 Save the low bytes of the program counter. */
206 usCode = ( uint16_t ) pxCode;
207 *pxTopOfStack = ( StackType_t ) usCode;
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;
216 /* Status register with interrupts enabled. */
217 *pxTopOfStack = portINITIAL_SR;
220 /* Parameters are passed in W0. */
221 *pxTopOfStack = ( StackType_t ) pvParameters;
224 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( StackType_t ) ); i++ )
226 *pxTopOfStack = xInitialStack[ i ];
230 *pxTopOfStack = CORCON;
233 #if defined(__HAS_EDS__)
234 *pxTopOfStack = DSRPAG;
236 *pxTopOfStack = DSWPAG;
238 #else /* __HAS_EDS__ */
239 *pxTopOfStack = PSVPAG;
241 #endif /* __HAS_EDS__ */
243 /* Finally the critical nesting depth. */
244 *pxTopOfStack = 0x00;
249 /*-----------------------------------------------------------*/
251 BaseType_t xPortStartScheduler( void )
253 /* Setup a timer for the tick ISR. */
254 vApplicationSetupTickTimerInterrupt();
256 /* Restore the context of the first task to run. */
257 portRESTORE_CONTEXT();
259 /* Simulate the end of the yield function. */
260 asm volatile ( "return" );
262 /* Should not reach here. */
265 /*-----------------------------------------------------------*/
267 void vPortEndScheduler( void )
269 /* Not implemented in ports where there is nothing to return to.
270 Artificially force an assert. */
271 configASSERT( uxCriticalNesting == 1000UL );
273 /*-----------------------------------------------------------*/
276 * Setup a timer for a regular tick.
278 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
280 const uint32_t ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
286 PR1 = ( uint16_t ) ulCompareMatch;
288 /* Setup timer 1 interrupt priority. */
289 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
291 /* Clear the interrupt as a starting condition. */
294 /* Enable the interrupt. */
297 /* Setup the prescale value. */
298 T1CONbits.TCKPS0 = 1;
299 T1CONbits.TCKPS1 = 0;
301 /* Start the timer. */
304 /*-----------------------------------------------------------*/
306 void vPortEnterCritical( void )
308 portDISABLE_INTERRUPTS();
311 /*-----------------------------------------------------------*/
313 void vPortExitCritical( void )
315 configASSERT( uxCriticalNesting );
317 if( uxCriticalNesting == 0 )
319 portENABLE_INTERRUPTS();
322 /*-----------------------------------------------------------*/
324 void __attribute__((__interrupt__, auto_psv)) configTICK_INTERRUPT_HANDLER( void )
326 /* Clear the timer interrupt. */
329 if( xTaskIncrementTick() != pdFALSE )
334 /*-----------------------------------------------------------*/