xref: /Kernel-v10.6.2/portable/MPLAB/PIC32MEC14xx/port.c (revision ef7b253b56c9788077f5ecd6c9deb4021923d646)
1 /*
2  * FreeRTOS Kernel V10.6.2
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  * Implementation of functions defined in portable.h for the PIC32MEC14xx  port.
31  *----------------------------------------------------------*/
32 
33 /* Scheduler include files. */
34 #include "FreeRTOS.h"
35 #include "task.h"
36 
37 /* Microchip includes. */
38 #include <xc.h>
39 #include <cp0defs.h>
40 
41 #if !defined(__MEC__)
42     #error This port is designed to work with XC32 on MEC14xx.  Please update your C compiler version or settings.
43 #endif
44 
45 #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )
46     #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0
47 #endif
48 
49 /* Bits within various registers. */
50 #define portIE_BIT                  ( 0x00000001 )
51 #define portEXL_BIT                 ( 0x00000002 )
52 
53 /* The EXL bit is set to ensure interrupts do not occur while the context of
54 the first task is being restored.  MEC14xx does not have DSP HW. */
55 #define portINITIAL_SR              ( portIE_BIT | portEXL_BIT )
56 
57 /* MEC14xx RTOS Timer MMCR's. */
58 #define portMMCR_RTMR_PRELOAD   *((volatile uint32_t *)(0xA0007404ul))
59 #define portMMCR_RTMR_CONTROL   *((volatile uint32_t *)(0xA0007408ul))
60 
61 /* MEC14xx JTVIC external interrupt controller is mapped to M14K closely-coupled
62 peripheral space. */
63 #define portGIRQ23_RTOS_TIMER_BITPOS    ( 4 )
64 #define portGIRQ23_RTOS_TIMER_MASK      ( 1ul << ( portGIRQ23_RTOS_TIMER_BITPOS ) )
65 #define portMMCR_JTVIC_GIRQ23_SRC       *((volatile uint32_t *)(0xBFFFC0F0ul))
66 #define portMMCR_JTVIC_GIRQ23_SETEN     *((volatile uint32_t *)(0xBFFFC0F4ul))
67 #define portMMCR_JTVIC_GIRQ23_PRIA      *((volatile uint32_t *)(0xBFFFC3F0ul))
68 
69 /* MIPS Software Interrupts are routed through JTVIC GIRQ24 */
70 #define portGIRQ24_M14K_SOFTIRQ0_BITPOS ( 1 )
71 #define portGIRQ24_M14K_SOFTIRQ0_MASK   ( 1ul << ( portGIRQ24_M14K_SOFTIRQ0_BITPOS ) )
72 #define portMMCR_JTVIC_GIRQ24_SRC       *((volatile uint32_t *)(0xBFFFC100ul))
73 #define portMMCR_JTVIC_GIRQ24_SETEN     *((volatile uint32_t *)(0xBFFFC104ul))
74 #define portMMCR_JTVIC_GIRQ24_PRIA      *((volatile uint32_t *)(0xBFFFC400ul))
75 
76 /*
77 By default port.c generates its tick interrupt from the RTOS timer.  The user
78 can override this behaviour by:
79     1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
80        which is the function that configures the timer.  The function is defined
81        as a weak symbol in this file so if the same function name is used in the
82        application code then the version in the application code will be linked
83        into the application in preference to the version defined in this file.
84     2: Provide a vector implementation in port_asm.S that overrides the default
85        behaviour for the specified interrupt vector.
86     3: Specify the correct bit to clear the interrupt during the timer interrupt
87        handler.
88 */
89 #ifndef configTICK_INTERRUPT_VECTOR
90     #define configTICK_INTERRUPT_VECTOR girq23_b4
91     #define configCLEAR_TICK_TIMER_INTERRUPT() portMMCR_JTVIC_GIRQ23_SRC = portGIRQ23_RTOS_TIMER_MASK
92 #else
93     #ifndef configCLEAR_TICK_TIMER_INTERRUPT
94         #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
95     #endif
96 #endif
97 
98 /* Let the user override the pre-loading of the initial RA with the address of
99 prvTaskExitError() in case it messes up unwinding of the stack in the debugger -
100 in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
101 #ifdef configTASK_RETURN_ADDRESS
102     #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
103 #else
104     #define portTASK_RETURN_ADDRESS prvTaskExitError
105 #endif
106 
107 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
108 stack checking.  A problem in the ISR stack will trigger an assert, not call the
109 stack overflow hook function (because the stack overflow hook is specific to a
110 task stack, not the ISR stack). */
111 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
112 
113     /* Don't use 0xa5 as the stack fill bytes as that is used by the kernel for
114     the task stacks, and so will legitimately appear in many positions within
115     the ISR stack. */
116     #define portISR_STACK_FILL_BYTE 0xee
117 
118     static const uint8_t ucExpectedStackBytes[] = {
119                             portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,     \
120                             portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,     \
121                             portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,     \
122                             portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,     \
123                             portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };   \
124 
125     #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
126 #else
127     /* Define the function away. */
128     #define portCHECK_ISR_STACK()
129 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
130 
131 
132 /*-----------------------------------------------------------*/
133 
134 /*
135  * Used to catch tasks that attempt to return from their implementing function.
136  */
137 static void prvTaskExitError( void );
138 
139 /*-----------------------------------------------------------*/
140 
141 /* Records the interrupt nesting depth.  This is initialised to one as it is
142 decremented to 0 when the first task starts. */
143 volatile UBaseType_t uxInterruptNesting = 0x01;
144 
145 /* Stores the task stack pointer when a switch is made to use the system stack. */
146 UBaseType_t uxSavedTaskStackPointer = 0;
147 
148 /* The stack used by interrupt service routines that cause a context switch. */
149 StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
150 
151 /* The top of stack value ensures there is enough space to store 6 registers on
152 the callers stack, as some functions seem to want to do this. */
153 const StackType_t * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );
154 
155 /*-----------------------------------------------------------*/
156 
157 /*
158  * See header file for description.
159  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)160 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
161 {
162     /* Ensure byte alignment is maintained when leaving this function. */
163     pxTopOfStack--;
164 
165     *pxTopOfStack = (StackType_t) 0xDEADBEEF;
166     pxTopOfStack--;
167 
168     *pxTopOfStack = (StackType_t) 0x12345678;   /* Word to which the stack pointer will be left pointing after context restore. */
169     pxTopOfStack--;
170 
171     *pxTopOfStack = (StackType_t) ulPortGetCP0Cause();
172     pxTopOfStack--;
173 
174     *pxTopOfStack = (StackType_t) portINITIAL_SR;   /* CP0_STATUS */
175     pxTopOfStack--;
176 
177     *pxTopOfStack = (StackType_t) pxCode;       /* CP0_EPC */
178     pxTopOfStack--;
179 
180     *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS;  /* ra */
181     pxTopOfStack -= 15;
182 
183     *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
184     pxTopOfStack -= 15;
185 
186     return pxTopOfStack;
187 }
188 /*-----------------------------------------------------------*/
189 
prvDisableInterrupt(void)190 static __inline uint32_t prvDisableInterrupt( void )
191 {
192 uint32_t prev_state;
193 
194     __asm volatile( "di %0; ehb" : "=r" ( prev_state ) :: "memory" );
195     return prev_state;
196 }
197 /*-----------------------------------------------------------*/
198 
prvTaskExitError(void)199 static void prvTaskExitError( void )
200 {
201     /* A function that implements a task must not exit or attempt to return to
202     its caller as there is nothing to return to.  If a task wants to exit it
203     should instead call vTaskDelete( NULL ).
204 
205     Artificially force an assert() to be triggered if configASSERT() is
206     defined, then stop here so application writers can catch the error. */
207     configASSERT( uxSavedTaskStackPointer == 0UL );
208     portDISABLE_INTERRUPTS();
209     for( ;; );
210 }
211 /*-----------------------------------------------------------*/
212 
213 /*
214  * Setup a timer for a regular tick.  This function uses the RTOS timer.
215  * The function is declared weak so an application writer can use a different
216  * timer by redefining this implementation.  If a different timer is used then
217  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
218  * ensure the RTOS provided tick interrupt handler is installed on the correct
219  * vector number.
220  */
vApplicationSetupTickTimerInterrupt(void)221 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
222 {
223 /* MEC14xx RTOS Timer whose input clock is 32KHz. */
224 const uint32_t ulPreload = ( 32768ul / ( configTICK_RATE_HZ ) );
225 
226     configASSERT( ulPreload != 0UL );
227 
228     /* Configure the RTOS timer. */
229     portMMCR_RTMR_CONTROL = 0ul;
230     portMMCR_RTMR_PRELOAD = ulPreload;
231 
232     /* Configure interrupts from the RTOS timer. */
233     portMMCR_JTVIC_GIRQ23_SRC = ( portGIRQ23_RTOS_TIMER_MASK );
234     portMMCR_JTVIC_GIRQ23_PRIA &= ~( 0x0Ful << 16 );
235     portMMCR_JTVIC_GIRQ23_PRIA |= ( ( portIPL_TO_CODE( configKERNEL_INTERRUPT_PRIORITY ) ) << 16 );
236     portMMCR_JTVIC_GIRQ23_SETEN = ( portGIRQ23_RTOS_TIMER_MASK );
237 
238     /* Enable the RTOS timer. */
239     portMMCR_RTMR_CONTROL = 0x0Fu;
240 }
241 /*-----------------------------------------------------------*/
242 
vPortEndScheduler(void)243 void vPortEndScheduler(void)
244 {
245     /* Not implemented in ports where there is nothing to return to.
246     Artificially force an assert. */
247     configASSERT( uxInterruptNesting == 1000UL );
248 }
249 /*-----------------------------------------------------------*/
250 
xPortStartScheduler(void)251 BaseType_t xPortStartScheduler( void )
252 {
253 extern void vPortStartFirstTask( void );
254 extern void *pxCurrentTCB;
255 
256     #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
257     {
258         /* Fill the ISR stack to make it easy to asses how much is being used. */
259         memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
260     }
261     #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
262 
263     /* Clear the software interrupt flag. */
264     portMMCR_JTVIC_GIRQ24_SRC = (portGIRQ24_M14K_SOFTIRQ0_MASK);
265 
266     /* Set software timer priority.  Each GIRQn has one nibble containing its
267     priority */
268     portMMCR_JTVIC_GIRQ24_PRIA &= ~(0xF0ul);
269     portMMCR_JTVIC_GIRQ24_PRIA |= ( portIPL_TO_CODE( configKERNEL_INTERRUPT_PRIORITY ) << 4 );
270 
271     /* Enable software interrupt. */
272     portMMCR_JTVIC_GIRQ24_SETEN = ( portGIRQ24_M14K_SOFTIRQ0_MASK );
273 
274     /* Setup the timer to generate the tick.  Interrupts will have been disabled
275     by the time we get here. */
276     vApplicationSetupTickTimerInterrupt();
277 
278     /* Start the highest priority task that has been created so far.  Its stack
279     location is loaded into uxSavedTaskStackPointer. */
280     uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
281     vPortStartFirstTask();
282 
283     /* Should never get here as the tasks will now be executing!  Call the task
284     exit error function to prevent compiler warnings about a static function
285     not being called in the case that the application writer overrides this
286     functionality by defining configTASK_RETURN_ADDRESS. */
287     prvTaskExitError();
288 
289     return pdFALSE;
290 }
291 /*-----------------------------------------------------------*/
292 
vPortIncrementTick(void)293 void vPortIncrementTick( void )
294 {
295 UBaseType_t uxSavedStatus;
296 uint32_t ulCause;
297 
298     uxSavedStatus = uxPortSetInterruptMaskFromISR();
299     {
300         if( xTaskIncrementTick() != pdFALSE )
301         {
302             /* Pend a context switch. */
303             ulCause = ulPortGetCP0Cause();
304             ulCause |= ( 1ul << 8UL );
305             vPortSetCP0Cause( ulCause );
306         }
307     }
308     vPortClearInterruptMaskFromISR( uxSavedStatus );
309 
310     /* Look for the ISR stack getting near or past its limit. */
311     portCHECK_ISR_STACK();
312 
313     /* Clear timer interrupt. */
314     configCLEAR_TICK_TIMER_INTERRUPT();
315 }
316 /*-----------------------------------------------------------*/
317 
uxPortSetInterruptMaskFromISR(void)318 UBaseType_t uxPortSetInterruptMaskFromISR( void )
319 {
320 UBaseType_t uxSavedStatusRegister;
321 
322     prvDisableInterrupt();
323     uxSavedStatusRegister = ulPortGetCP0Status() | 0x01;
324 
325     /* This clears the IPL bits, then sets them to
326     configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called
327     from an interrupt that has a priority above
328     configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
329     can only result in the IPL being unchanged or raised, and therefore never
330     lowered. */
331     vPortSetCP0Status( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
332 
333     return uxSavedStatusRegister;
334 }
335 /*-----------------------------------------------------------*/
336 
vPortClearInterruptMaskFromISR(UBaseType_t uxSavedStatusRegister)337 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
338 {
339     vPortSetCP0Status( uxSavedStatusRegister );
340 }
341 /*-----------------------------------------------------------*/
342