]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/TriCore_1782/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / GCC / TriCore_1782 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Standard includes. */\r
30 #include <stdlib.h>\r
31 #include <string.h>\r
32 \r
33 /* TriCore specific includes. */\r
34 #include <tc1782.h>\r
35 #include <machine/intrinsics.h>\r
36 #include <machine/cint.h>\r
37 #include <machine/wdtcon.h>\r
38 \r
39 /* Kernel includes. */\r
40 #include "FreeRTOS.h"\r
41 #include "task.h"\r
42 #include "list.h"\r
43 \r
44 #if configCHECK_FOR_STACK_OVERFLOW > 0\r
45         #error "Stack checking cannot be used with this port, as, unlike most ports, the pxTopOfStack member of the TCB is consumed CSA.  CSA starvation, loosely equivalent to stack overflow, will result in a trap exception."\r
46         /* The stack pointer is accessible using portCSA_TO_ADDRESS( portCSA_TO_ADDRESS( pxCurrentTCB->pxTopOfStack )[ 0 ] )[ 2 ]; */\r
47 #endif /* configCHECK_FOR_STACK_OVERFLOW */\r
48 \r
49 \r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /* System register Definitions. */\r
53 #define portSYSTEM_PROGRAM_STATUS_WORD                                  ( 0x000008FFUL ) /* Supervisor Mode, MPU Register Set 0 and Call Depth Counting disabled. */\r
54 #define portINITIAL_PRIVILEGED_PROGRAM_STATUS_WORD              ( 0x000014FFUL ) /* IO Level 1, MPU Register Set 1 and Call Depth Counting disabled. */\r
55 #define portINITIAL_UNPRIVILEGED_PROGRAM_STATUS_WORD    ( 0x000010FFUL ) /* IO Level 0, MPU Register Set 1 and Call Depth Counting disabled. */\r
56 #define portINITIAL_PCXI_UPPER_CONTEXT_WORD                             ( 0x00C00000UL ) /* The lower 20 bits identify the CSA address. */\r
57 #define portINITIAL_SYSCON                                                              ( 0x00000000UL ) /* MPU Disable. */\r
58 \r
59 /* CSA manipulation macros. */\r
60 #define portCSA_FCX_MASK                                        ( 0x000FFFFFUL )\r
61 \r
62 /* OS Interrupt and Trap mechanisms. */\r
63 #define portRESTORE_PSW_MASK                            ( ~( 0x000000FFUL ) )\r
64 #define portSYSCALL_TRAP                                        ( 6 )\r
65 \r
66 /* Each CSA contains 16 words of data. */\r
67 #define portNUM_WORDS_IN_CSA                            ( 16 )\r
68 \r
69 /* The interrupt enable bit in the PCP_SRC register. */\r
70 #define portENABLE_CPU_INTERRUPT                        ( 1U << 12U )\r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*\r
74  * Perform any hardware configuration necessary to generate the tick interrupt.\r
75  */\r
76 static void prvSystemTickHandler( int ) __attribute__((longcall));\r
77 static void prvSetupTimerInterrupt( void );\r
78 \r
79 /*\r
80  * Trap handler for yields.\r
81  */\r
82 static void prvTrapYield( int iTrapIdentification );\r
83 \r
84 /*\r
85  * Priority 1 interrupt handler for yields pended from an interrupt.\r
86  */\r
87 static void prvInterruptYield( int iTrapIdentification );\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* This reference is required by the save/restore context macros. */\r
92 extern volatile uint32_t *pxCurrentTCB;\r
93 \r
94 /* Precalculate the compare match value at compile time. */\r
95 static const uint32_t ulCompareMatchValue = ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 StackType_t *pxPortInitialiseStack( StackType_t * pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
100 {\r
101 uint32_t *pulUpperCSA = NULL;\r
102 uint32_t *pulLowerCSA = NULL;\r
103 \r
104         /* 16 Address Registers (4 Address registers are global), 16 Data\r
105         Registers, and 3 System Registers.\r
106 \r
107         There are 3 registers that track the CSAs.\r
108                 FCX points to the head of globally free set of CSAs.\r
109                 PCX for the task needs to point to Lower->Upper->NULL arrangement.\r
110                 LCX points to the last free CSA so that corrective action can be taken.\r
111 \r
112         Need two CSAs to store the context of a task.\r
113                 The upper context contains D8-D15, A10-A15, PSW and PCXI->NULL.\r
114                 The lower context contains D0-D7, A2-A7, A11 and PCXI->UpperContext.\r
115                 The pxCurrentTCB->pxTopOfStack points to the Lower Context RSLCX matching the initial BISR.\r
116                 The Lower Context points to the Upper Context ready for the return from the interrupt handler.\r
117 \r
118          The Real stack pointer for the task is stored in the A10 which is restored\r
119          with the upper context. */\r
120 \r
121         /* Have to disable interrupts here because the CSAs are going to be\r
122         manipulated. */\r
123         portENTER_CRITICAL();\r
124         {\r
125                 /* DSync to ensure that buffering is not a problem. */\r
126                 _dsync();\r
127 \r
128                 /* Consume two free CSAs. */\r
129                 pulLowerCSA = portCSA_TO_ADDRESS( __MFCR( $FCX ) );\r
130                 if( NULL != pulLowerCSA )\r
131                 {\r
132                         /* The Lower Links to the Upper. */\r
133                         pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[ 0 ] );\r
134                 }\r
135 \r
136                 /* Check that we have successfully reserved two CSAs. */\r
137                 if( ( NULL != pulLowerCSA ) && ( NULL != pulUpperCSA ) )\r
138                 {\r
139                         /* Remove the two consumed CSAs from the free CSA list. */\r
140                         _disable();\r
141                         _dsync();\r
142                         _mtcr( $FCX, pulUpperCSA[ 0 ] );\r
143                         _isync();\r
144                         _enable();\r
145                 }\r
146                 else\r
147                 {\r
148                         /* Simply trigger a context list depletion trap. */\r
149                         _svlcx();\r
150                 }\r
151         }\r
152         portEXIT_CRITICAL();\r
153 \r
154         /* Clear the upper CSA. */\r
155         memset( pulUpperCSA, 0, portNUM_WORDS_IN_CSA * sizeof( uint32_t ) );\r
156 \r
157         /* Upper Context. */\r
158         pulUpperCSA[ 2 ] = ( uint32_t )pxTopOfStack;            /* A10; Stack Return aka Stack Pointer */\r
159         pulUpperCSA[ 1 ] = portSYSTEM_PROGRAM_STATUS_WORD;              /* PSW  */\r
160 \r
161         /* Clear the lower CSA. */\r
162         memset( pulLowerCSA, 0, portNUM_WORDS_IN_CSA * sizeof( uint32_t ) );\r
163 \r
164         /* Lower Context. */\r
165         pulLowerCSA[ 8 ] = ( uint32_t ) pvParameters;           /* A4;  Address Type Parameter Register */\r
166         pulLowerCSA[ 1 ] = ( uint32_t ) pxCode;                 /* A11; Return Address aka RA */\r
167 \r
168         /* PCXI pointing to the Upper context. */\r
169         pulLowerCSA[ 0 ] = ( portINITIAL_PCXI_UPPER_CONTEXT_WORD | ( uint32_t ) portADDRESS_TO_CSA( pulUpperCSA ) );\r
170 \r
171         /* Save the link to the CSA in the top of stack. */\r
172         pxTopOfStack = (uint32_t * ) portADDRESS_TO_CSA( pulLowerCSA );\r
173 \r
174         /* DSync to ensure that buffering is not a problem. */\r
175         _dsync();\r
176 \r
177         return pxTopOfStack;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 int32_t xPortStartScheduler( void )\r
182 {\r
183 extern void vTrapInstallHandlers( void );\r
184 uint32_t ulMFCR = 0UL;\r
185 uint32_t *pulUpperCSA = NULL;\r
186 uint32_t *pulLowerCSA = NULL;\r
187 \r
188         /* Interrupts at or below configMAX_SYSCALL_INTERRUPT_PRIORITY are disable\r
189         when this function is called. */\r
190 \r
191         /* Set-up the timer interrupt. */\r
192         prvSetupTimerInterrupt();\r
193 \r
194         /* Install the Trap Handlers. */\r
195         vTrapInstallHandlers();\r
196 \r
197         /* Install the Syscall Handler for yield calls. */\r
198         if( 0 == _install_trap_handler( portSYSCALL_TRAP, prvTrapYield ) )\r
199         {\r
200                 /* Failed to install the yield handler, force an assert. */\r
201                 configASSERT( ( ( volatile void * ) NULL ) );\r
202         }\r
203 \r
204         /* Enable then install the priority 1 interrupt for pending context\r
205         switches from an ISR.  See mod_SRC in the TriCore manual. */\r
206         CPU_SRC0.reg =  ( portENABLE_CPU_INTERRUPT ) | ( configKERNEL_YIELD_PRIORITY );\r
207         if( 0 == _install_int_handler( configKERNEL_YIELD_PRIORITY, prvInterruptYield, 0 ) )\r
208         {\r
209                 /* Failed to install the yield handler, force an assert. */\r
210                 configASSERT( ( ( volatile void * ) NULL ) );\r
211         }\r
212 \r
213         _disable();\r
214 \r
215         /* Load the initial SYSCON. */\r
216         _mtcr( $SYSCON, portINITIAL_SYSCON );\r
217         _isync();\r
218 \r
219         /* ENDINIT has already been applied in the 'cstart.c' code. */\r
220 \r
221         /* Clear the PSW.CDC to enable the use of an RFE without it generating an\r
222         exception because this code is not genuinely in an exception. */\r
223         ulMFCR = __MFCR( $PSW );\r
224         ulMFCR &= portRESTORE_PSW_MASK;\r
225         _dsync();\r
226         _mtcr( $PSW, ulMFCR );\r
227         _isync();\r
228 \r
229         /* Finally, perform the equivalent of a portRESTORE_CONTEXT() */\r
230         pulLowerCSA = portCSA_TO_ADDRESS( ( *pxCurrentTCB ) );\r
231         pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[0] );\r
232         _dsync();\r
233         _mtcr( $PCXI, *pxCurrentTCB );\r
234         _isync();\r
235         _nop();\r
236         _rslcx();\r
237         _nop();\r
238 \r
239         /* Return to the first task selected to execute. */\r
240         __asm volatile( "rfe" );\r
241 \r
242         /* Will not get here. */\r
243         return 0;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvSetupTimerInterrupt( void )\r
248 {\r
249         /* Set-up the clock divider. */\r
250         unlock_wdtcon();\r
251         {\r
252                 /* Wait until access to Endint protected register is enabled. */\r
253                 while( 0 != ( WDT_CON0.reg & 0x1UL ) );\r
254 \r
255                 /* RMC == 1 so STM Clock == FPI */\r
256                 STM_CLC.reg = ( 1UL << 8 );\r
257         }\r
258         lock_wdtcon();\r
259 \r
260     /* Determine how many bits are used without changing other bits in the CMCON register. */\r
261         STM_CMCON.reg &= ~( 0x1fUL );\r
262         STM_CMCON.reg |= ( 0x1fUL - __CLZ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) );\r
263 \r
264         /* Take into account the current time so a tick doesn't happen immediately. */\r
265         STM_CMP0.reg = ulCompareMatchValue + STM_TIM0.reg;\r
266 \r
267         if( 0 != _install_int_handler( configKERNEL_INTERRUPT_PRIORITY, prvSystemTickHandler, 0 ) )\r
268         {\r
269                 /* Set-up the interrupt. */\r
270                 STM_SRC0.reg = ( configKERNEL_INTERRUPT_PRIORITY | 0x00005000UL );\r
271 \r
272                 /* Enable the Interrupt. */\r
273                 STM_ISRR.reg &= ~( 0x03UL );\r
274                 STM_ISRR.reg |= 0x1UL;\r
275                 STM_ISRR.reg &= ~( 0x07UL );\r
276                 STM_ICR.reg |= 0x1UL;\r
277         }\r
278         else\r
279         {\r
280                 /* Failed to install the Tick Interrupt. */\r
281                 configASSERT( ( ( volatile void * ) NULL ) );\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvSystemTickHandler( int iArg )\r
287 {\r
288 uint32_t ulSavedInterruptMask;\r
289 uint32_t *pxUpperCSA = NULL;\r
290 uint32_t xUpperCSA = 0UL;\r
291 extern volatile uint32_t *pxCurrentTCB;\r
292 int32_t lYieldRequired;\r
293 \r
294         /* Just to avoid compiler warnings about unused parameters. */\r
295         ( void ) iArg;\r
296 \r
297         /* Clear the interrupt source. */\r
298         STM_ISRR.reg = 1UL;\r
299 \r
300         /* Reload the Compare Match register for X ticks into the future.\r
301 \r
302         If critical section or interrupt nesting budgets are exceeded, then\r
303         it is possible that the calculated next compare match value is in the\r
304         past.  If this occurs (unlikely), it is possible that the resulting\r
305         time slippage will exceed a single tick period.  Any adverse effect of\r
306         this is time bounded by the fact that only the first n bits of the 56 bit\r
307         STM timer are being used for a compare match, so another compare match\r
308         will occur after an overflow in just those n bits (not the entire 56 bits).\r
309         As an example, if the peripheral clock is 75MHz, and the tick rate is 1KHz,\r
310         a missed tick could result in the next tick interrupt occurring within a\r
311         time that is 1.7 times the desired period.  The fact that this is greater\r
312         than a single tick period is an effect of using a timer that cannot be\r
313         automatically reset, in hardware, by the occurrence of a tick interrupt.\r
314         Changing the tick source to a timer that has an automatic reset on compare\r
315         match (such as a GPTA timer) will reduce the maximum possible additional\r
316         period to exactly 1 times the desired period. */\r
317         STM_CMP0.reg += ulCompareMatchValue;\r
318 \r
319         /* Kernel API calls require Critical Sections. */\r
320         ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
321         {\r
322                 /* Increment the Tick. */\r
323                 lYieldRequired = xTaskIncrementTick();\r
324         }\r
325         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );\r
326 \r
327         if( lYieldRequired != pdFALSE )\r
328         {\r
329                 /* Save the context of a task.\r
330                 The upper context is automatically saved when entering a trap or interrupt.\r
331                 Need to save the lower context as well and copy the PCXI CSA ID into\r
332                 pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the\r
333                 TCB of a task.\r
334 \r
335                 Call vTaskSwitchContext to select the next task, note that this changes the\r
336                 value of pxCurrentTCB so that it needs to be reloaded.\r
337 \r
338                 Call vPortSetMPURegisterSetOne to change the MPU mapping for the task\r
339                 that has just been switched in.\r
340 \r
341                 Load the context of the task.\r
342                 Need to restore the lower context by loading the CSA from\r
343                 pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).\r
344                 In the Interrupt handler post-amble, RSLCX will restore the lower context\r
345                 of the task. RFE will restore the upper context of the task, jump to the\r
346                 return address and restore the previous state of interrupts being\r
347                 enabled/disabled. */\r
348                 _disable();\r
349                 _dsync();\r
350                 xUpperCSA = __MFCR( $PCXI );\r
351                 pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );\r
352                 *pxCurrentTCB = pxUpperCSA[ 0 ];\r
353                 vTaskSwitchContext();\r
354                 pxUpperCSA[ 0 ] = *pxCurrentTCB;\r
355                 CPU_SRC0.bits.SETR = 0;\r
356                 _isync();\r
357         }\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r
361 /*\r
362  * When a task is deleted, it is yielded permanently until the IDLE task\r
363  * has an opportunity to reclaim the memory that that task was using.\r
364  * Typically, the memory used by a task is the TCB and Stack but in the\r
365  * TriCore this includes the CSAs that were consumed as part of the Call\r
366  * Stack. These CSAs can only be returned to the Globally Free Pool when\r
367  * they are not part of the current Call Stack, hence, delaying the\r
368  * reclamation until the IDLE task is freeing the task's other resources.\r
369  * This function uses the head of the linked list of CSAs (from when the\r
370  * task yielded for the last time) and finds the tail (the very bottom of\r
371  * the call stack) and inserts this list at the head of the Free list,\r
372  * attaching the existing Free List to the tail of the reclaimed call stack.\r
373  *\r
374  * NOTE: the IDLE task needs processing time to complete this function\r
375  * and in heavily loaded systems, the Free CSAs may be consumed faster\r
376  * than they can be freed assuming that tasks are being spawned and\r
377  * deleted frequently.\r
378  */\r
379 void vPortReclaimCSA( uint32_t *pxTCB )\r
380 {\r
381 uint32_t pxHeadCSA, pxTailCSA, pxFreeCSA;\r
382 uint32_t *pulNextCSA;\r
383 \r
384         /* A pointer to the first CSA in the list of CSAs consumed by the task is\r
385         stored in the first element of the tasks TCB structure (where the stack\r
386         pointer would be on a traditional stack based architecture). */\r
387         pxHeadCSA = ( *pxTCB ) & portCSA_FCX_MASK;\r
388 \r
389         /* Mask off everything in the CSA link field other than the address.  If\r
390         the     address is NULL, then the CSA is not linking anywhere and there is\r
391         nothing to do. */\r
392         pxTailCSA = pxHeadCSA;\r
393 \r
394         /* Convert the link value to contain just a raw address and store this\r
395         in a local variable. */\r
396         pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );\r
397 \r
398         /* Iterate over the CSAs that were consumed as part of the task.  The\r
399         first field in the CSA is the pointer to then next CSA.  Mask off\r
400         everything in the pointer to the next CSA, other than the link address.\r
401         If this is NULL, then the CSA currently being pointed to is the last in\r
402         the chain. */\r
403         while( 0UL != ( pulNextCSA[ 0 ] & portCSA_FCX_MASK ) )\r
404         {\r
405                 /* Clear all bits of the pointer to the next in the chain, other\r
406                 than the address bits themselves. */\r
407                 pulNextCSA[ 0 ] = pulNextCSA[ 0 ] & portCSA_FCX_MASK;\r
408 \r
409                 /* Move the pointer to point to the next CSA in the list. */\r
410                 pxTailCSA = pulNextCSA[ 0 ];\r
411 \r
412                 /* Update the local pointer to the CSA. */\r
413                 pulNextCSA = portCSA_TO_ADDRESS( pxTailCSA );\r
414         }\r
415 \r
416         _disable();\r
417         {\r
418                 /* Look up the current free CSA head. */\r
419                 _dsync();\r
420                 pxFreeCSA = __MFCR( $FCX );\r
421 \r
422                 /* Join the current Free onto the Tail of what is being reclaimed. */\r
423                 portCSA_TO_ADDRESS( pxTailCSA )[ 0 ] = pxFreeCSA;\r
424 \r
425                 /* Move the head of the reclaimed into the Free. */\r
426                 _dsync();\r
427                 _mtcr( $FCX, pxHeadCSA );\r
428                 _isync();\r
429         }\r
430         _enable();\r
431 }\r
432 /*-----------------------------------------------------------*/\r
433 \r
434 void vPortEndScheduler( void )\r
435 {\r
436         /* Nothing to do. Unlikely to want to end. */\r
437 }\r
438 /*-----------------------------------------------------------*/\r
439 \r
440 static void prvTrapYield( int iTrapIdentification )\r
441 {\r
442 uint32_t *pxUpperCSA = NULL;\r
443 uint32_t xUpperCSA = 0UL;\r
444 extern volatile uint32_t *pxCurrentTCB;\r
445 \r
446         switch( iTrapIdentification )\r
447         {\r
448                 case portSYSCALL_TASK_YIELD:\r
449                         /* Save the context of a task.\r
450                         The upper context is automatically saved when entering a trap or interrupt.\r
451                         Need to save the lower context as well and copy the PCXI CSA ID into\r
452                         pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the\r
453                         TCB of a task.\r
454 \r
455                         Call vTaskSwitchContext to select the next task, note that this changes the\r
456                         value of pxCurrentTCB so that it needs to be reloaded.\r
457 \r
458                         Call vPortSetMPURegisterSetOne to change the MPU mapping for the task\r
459                         that has just been switched in.\r
460 \r
461                         Load the context of the task.\r
462                         Need to restore the lower context by loading the CSA from\r
463                         pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).\r
464                         In the Interrupt handler post-amble, RSLCX will restore the lower context\r
465                         of the task. RFE will restore the upper context of the task, jump to the\r
466                         return address and restore the previous state of interrupts being\r
467                         enabled/disabled. */\r
468                         _disable();\r
469                         _dsync();\r
470                         xUpperCSA = __MFCR( $PCXI );\r
471                         pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );\r
472                         *pxCurrentTCB = pxUpperCSA[ 0 ];\r
473                         vTaskSwitchContext();\r
474                         pxUpperCSA[ 0 ] = *pxCurrentTCB;\r
475                         CPU_SRC0.bits.SETR = 0;\r
476                         _isync();\r
477                         break;\r
478 \r
479                 default:\r
480                         /* Unimplemented trap called. */\r
481                         configASSERT( ( ( volatile void * ) NULL ) );\r
482                         break;\r
483         }\r
484 }\r
485 /*-----------------------------------------------------------*/\r
486 \r
487 static void prvInterruptYield( int iId )\r
488 {\r
489 uint32_t *pxUpperCSA = NULL;\r
490 uint32_t xUpperCSA = 0UL;\r
491 extern volatile uint32_t *pxCurrentTCB;\r
492 \r
493         /* Just to remove compiler warnings. */\r
494         ( void ) iId;\r
495 \r
496         /* Save the context of a task.\r
497         The upper context is automatically saved when entering a trap or interrupt.\r
498         Need to save the lower context as well and copy the PCXI CSA ID into\r
499         pxCurrentTCB->pxTopOfStack. Only Lower Context CSA IDs may be saved to the\r
500         TCB of a task.\r
501 \r
502         Call vTaskSwitchContext to select the next task, note that this changes the\r
503         value of pxCurrentTCB so that it needs to be reloaded.\r
504 \r
505         Call vPortSetMPURegisterSetOne to change the MPU mapping for the task\r
506         that has just been switched in.\r
507 \r
508         Load the context of the task.\r
509         Need to restore the lower context by loading the CSA from\r
510         pxCurrentTCB->pxTopOfStack into PCXI (effectively changing the call stack).\r
511         In the Interrupt handler post-amble, RSLCX will restore the lower context\r
512         of the task. RFE will restore the upper context of the task, jump to the\r
513         return address and restore the previous state of interrupts being\r
514         enabled/disabled. */\r
515         _disable();\r
516         _dsync();\r
517         xUpperCSA = __MFCR( $PCXI );\r
518         pxUpperCSA = portCSA_TO_ADDRESS( xUpperCSA );\r
519         *pxCurrentTCB = pxUpperCSA[ 0 ];\r
520         vTaskSwitchContext();\r
521         pxUpperCSA[ 0 ] = *pxCurrentTCB;\r
522         CPU_SRC0.bits.SETR = 0;\r
523         _isync();\r
524 }\r
525 /*-----------------------------------------------------------*/\r
526 \r
527 uint32_t uxPortSetInterruptMaskFromISR( void )\r
528 {\r
529 uint32_t uxReturn = 0UL;\r
530 \r
531         _disable();\r
532         uxReturn = __MFCR( $ICR );\r
533         _mtcr( $ICR, ( ( uxReturn & ~portCCPN_MASK ) | configMAX_SYSCALL_INTERRUPT_PRIORITY ) );\r
534         _isync();\r
535         _enable();\r
536 \r
537         /* Return just the interrupt mask bits. */\r
538         return ( uxReturn & portCCPN_MASK );\r
539 }\r
540 /*-----------------------------------------------------------*/\r
541 \r
542 \r