]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RX100/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / IAR / RX100 / 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 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the SH2A port.\r
31  *----------------------------------------------------------*/\r
32 \r
33 /* Standard C includes. */\r
34 #include "limits.h"\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 /* Library includes. */\r
41 #include "string.h"\r
42 \r
43 /* Hardware specifics. */\r
44 #include "machine.h"\r
45 \r
46 /*-----------------------------------------------------------*/\r
47 \r
48 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore\r
49 PSW is set with U and I set, and PM and IPL clear. */\r
50 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )\r
51 \r
52 /* The peripheral clock is divided by this value before being supplying the\r
53 CMT. */\r
54 #if ( configUSE_TICKLESS_IDLE == 0 )\r
55         /* If tickless idle is not used then the divisor can be fixed. */\r
56         #define portCLOCK_DIVISOR       8UL\r
57 #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )\r
58         #define portCLOCK_DIVISOR       512UL\r
59 #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )\r
60         #define portCLOCK_DIVISOR       128UL\r
61 #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )\r
62         #define portCLOCK_DIVISOR       32UL\r
63 #else\r
64         #define portCLOCK_DIVISOR       8UL\r
65 #endif\r
66 \r
67 \r
68 /* Keys required to lock and unlock access to certain system registers\r
69 respectively. */\r
70 #define portUNLOCK_KEY          0xA50B\r
71 #define portLOCK_KEY            0xA500\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /*\r
76  * Function to start the first task executing - written in asm code as direct\r
77  * access to registers is required.\r
78  */\r
79 extern void prvStartFirstTask( void );\r
80 \r
81 /*\r
82  * The tick ISR handler.  The peripheral used is configured by the application\r
83  * via a hook/callback function.\r
84  */\r
85 __interrupt static void prvTickISR( void );\r
86 \r
87 /*\r
88  * Sets up the periodic ISR used for the RTOS tick using the CMT.\r
89  * The application writer can define configSETUP_TICK_INTERRUPT() (in\r
90  * FreeRTOSConfig.h) such that their own tick interrupt configuration is used\r
91  * in place of prvSetupTimerInterrupt().\r
92  */\r
93 static void prvSetupTimerInterrupt( void );\r
94 #ifndef configSETUP_TICK_INTERRUPT\r
95         /* The user has not provided their own tick interrupt configuration so use\r
96     the definition in this file (which uses the interval timer). */\r
97         #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()\r
98 #endif /* configSETUP_TICK_INTERRUPT */\r
99 \r
100 /*\r
101  * Called after the sleep mode registers have been configured, prvSleep()\r
102  * executes the pre and post sleep macros, and actually calls the wait\r
103  * instruction.\r
104  */\r
105 #if configUSE_TICKLESS_IDLE == 1\r
106         static void prvSleep( TickType_t xExpectedIdleTime );\r
107 #endif /* configUSE_TICKLESS_IDLE */\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 extern void *pxCurrentTCB;\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /* Calculate how many clock increments make up a single tick period. */\r
116 static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );\r
117 \r
118 #if configUSE_TICKLESS_IDLE == 1\r
119 \r
120         /* Holds the maximum number of ticks that can be suppressed - which is\r
121         basically how far into the future an interrupt can be generated. Set\r
122         during initialisation.  This is the maximum possible value that the\r
123         compare match register can hold divided by ulMatchValueForOneTick. */\r
124         static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );\r
125 \r
126         /* Flag set from the tick interrupt to allow the sleep processing to know if\r
127         sleep mode was exited because of a tick interrupt, or an interrupt\r
128         generated by something else. */\r
129         static volatile uint32_t ulTickFlag = pdFALSE;\r
130 \r
131         /* The CMT counter is stopped temporarily each time it is re-programmed.\r
132         The following constant offsets the CMT counter match value by the number of\r
133         CMT     counts that would typically be missed while the counter was stopped to\r
134         compensate for the lost time.  The large difference between the divided CMT\r
135         clock and the CPU clock means it is likely ulStoppedTimerCompensation will\r
136         equal zero - and be optimised away. */\r
137         static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );\r
138 \r
139 #endif\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /*\r
144  * See header file for description.\r
145  */\r
146 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
147 {\r
148         /* Offset to end up on 8 byte boundary. */\r
149         pxTopOfStack--;\r
150 \r
151         /* R0 is not included as it is the stack pointer. */\r
152         *pxTopOfStack = 0x00;\r
153         pxTopOfStack--;\r
154     *pxTopOfStack = 0x00;\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = portINITIAL_PSW;\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = ( StackType_t ) pxCode;\r
159 \r
160         /* When debugging it can be useful if every register is set to a known\r
161         value.  Otherwise code space can be saved by just setting the registers\r
162         that need to be set. */\r
163         #ifdef USE_FULL_REGISTER_INITIALISATION\r
164         {\r
165                 pxTopOfStack--;\r
166                 *pxTopOfStack = 0x12345678;     /* r15. */\r
167                 pxTopOfStack--;\r
168                 *pxTopOfStack = 0xaaaabbbb;\r
169                 pxTopOfStack--;\r
170                 *pxTopOfStack = 0xdddddddd;\r
171                 pxTopOfStack--;\r
172                 *pxTopOfStack = 0xcccccccc;\r
173                 pxTopOfStack--;\r
174                 *pxTopOfStack = 0xbbbbbbbb;\r
175                 pxTopOfStack--;\r
176                 *pxTopOfStack = 0xaaaaaaaa;\r
177                 pxTopOfStack--;\r
178                 *pxTopOfStack = 0x99999999;\r
179                 pxTopOfStack--;\r
180                 *pxTopOfStack = 0x88888888;\r
181                 pxTopOfStack--;\r
182                 *pxTopOfStack = 0x77777777;\r
183                 pxTopOfStack--;\r
184                 *pxTopOfStack = 0x66666666;\r
185                 pxTopOfStack--;\r
186                 *pxTopOfStack = 0x55555555;\r
187                 pxTopOfStack--;\r
188                 *pxTopOfStack = 0x44444444;\r
189                 pxTopOfStack--;\r
190                 *pxTopOfStack = 0x33333333;\r
191                 pxTopOfStack--;\r
192                 *pxTopOfStack = 0x22222222;\r
193                 pxTopOfStack--;\r
194         }\r
195         #else\r
196         {\r
197                 /* Leave space for the registers that will get popped from the stack\r
198                 when the task first starts executing. */\r
199                 pxTopOfStack -= 15;\r
200         }\r
201         #endif\r
202 \r
203         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = 0x12345678; /* Accumulator. */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = 0x87654321; /* Accumulator. */\r
208 \r
209         return pxTopOfStack;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 BaseType_t xPortStartScheduler( void )\r
214 {\r
215         /* Use pxCurrentTCB just so it does not get optimised away. */\r
216         if( pxCurrentTCB != NULL )\r
217         {\r
218                 /* Call an application function to set up the timer that will generate\r
219                 the tick interrupt.  This way the application can decide which\r
220                 peripheral to use.  If tickless mode is used then the default\r
221                 implementation defined in this file (which uses CMT0) should not be\r
222                 overridden. */\r
223                 configSETUP_TICK_INTERRUPT();\r
224 \r
225                 /* Enable the software interrupt. */\r
226                 _IEN( _ICU_SWINT ) = 1;\r
227 \r
228                 /* Ensure the software interrupt is clear. */\r
229                 _IR( _ICU_SWINT ) = 0;\r
230 \r
231                 /* Ensure the software interrupt is set to the kernel priority. */\r
232                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
233 \r
234                 /* Start the first task. */\r
235                 prvStartFirstTask();\r
236         }\r
237 \r
238         /* Execution should not reach here as the tasks are now running!\r
239         prvSetupTimerInterrupt() is called here to prevent the compiler outputting\r
240         a warning about a statically declared function not being referenced in the\r
241         case that the application writer has provided their own tick interrupt\r
242         configuration routine (and defined configSETUP_TICK_INTERRUPT() such that\r
243         their own routine will be called in place of prvSetupTimerInterrupt()). */\r
244         prvSetupTimerInterrupt();\r
245 \r
246         /* Should not get here. */\r
247         return pdFAIL;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 #pragma vector = configTICK_VECTOR\r
252 __interrupt static void prvTickISR( void )\r
253 {\r
254         /* Re-enable interrupts. */\r
255         __enable_interrupt();\r
256 \r
257         /* Increment the tick, and perform any processing the new tick value\r
258         necessitates. */\r
259         __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );\r
260         {\r
261                 if( xTaskIncrementTick() != pdFALSE )\r
262                 {\r
263                         taskYIELD();\r
264                 }\r
265         }\r
266         __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );\r
267 \r
268         #if configUSE_TICKLESS_IDLE == 1\r
269         {\r
270                 /* The CPU woke because of a tick. */\r
271                 ulTickFlag = pdTRUE;\r
272 \r
273                 /* If this is the first tick since exiting tickless mode then the CMT\r
274                 compare match value needs resetting. */\r
275                 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;\r
276         }\r
277         #endif\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 void vPortEndScheduler( void )\r
282 {\r
283         /* Not implemented in ports where there is nothing to return to.\r
284         Artificially force an assert. */\r
285         configASSERT( pxCurrentTCB == NULL );\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 static void prvSetupTimerInterrupt( void )\r
290 {\r
291         /* Unlock. */\r
292         SYSTEM.PRCR.WORD = portUNLOCK_KEY;\r
293 \r
294         /* Enable CMT0. */\r
295         MSTP( CMT0 ) = 0;\r
296 \r
297         /* Lock again. */\r
298         SYSTEM.PRCR.WORD = portLOCK_KEY;\r
299 \r
300         /* Interrupt on compare match. */\r
301         CMT0.CMCR.BIT.CMIE = 1;\r
302 \r
303         /* Set the compare match value. */\r
304         CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;\r
305 \r
306         /* Divide the PCLK. */\r
307         #if portCLOCK_DIVISOR == 512\r
308         {\r
309                 CMT0.CMCR.BIT.CKS = 3;\r
310         }\r
311         #elif portCLOCK_DIVISOR == 128\r
312         {\r
313                 CMT0.CMCR.BIT.CKS = 2;\r
314         }\r
315         #elif portCLOCK_DIVISOR == 32\r
316         {\r
317                 CMT0.CMCR.BIT.CKS = 1;\r
318         }\r
319         #elif portCLOCK_DIVISOR == 8\r
320         {\r
321                 CMT0.CMCR.BIT.CKS = 0;\r
322         }\r
323         #else\r
324         {\r
325                 #error Invalid portCLOCK_DIVISOR setting\r
326         }\r
327         #endif\r
328 \r
329 \r
330         /* Enable the interrupt... */\r
331         _IEN( _CMT0_CMI0 ) = 1;\r
332 \r
333         /* ...and set its priority to the application defined kernel priority. */\r
334         _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;\r
335 \r
336         /* Start the timer. */\r
337         CMT.CMSTR0.BIT.STR0 = 1;\r
338 }\r
339 /*-----------------------------------------------------------*/\r
340 \r
341 #if configUSE_TICKLESS_IDLE == 1\r
342 \r
343         static void prvSleep( TickType_t xExpectedIdleTime )\r
344         {\r
345                 /* Allow the application to define some pre-sleep processing. */\r
346                 configPRE_SLEEP_PROCESSING( xExpectedIdleTime );\r
347 \r
348                 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()\r
349                 means the application defined code has already executed the WAIT\r
350                 instruction. */\r
351                 if( xExpectedIdleTime > 0 )\r
352                 {\r
353                         __wait_for_interrupt();\r
354                 }\r
355 \r
356                 /* Allow the application to define some post sleep processing. */\r
357                 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
358         }\r
359 \r
360 #endif /* configUSE_TICKLESS_IDLE */\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 #if configUSE_TICKLESS_IDLE == 1\r
364 \r
365         void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
366         {\r
367         uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;\r
368         eSleepModeStatus eSleepAction;\r
369 \r
370                 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */\r
371 \r
372                 /* Make sure the CMT reload value does not overflow the counter. */\r
373                 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
374                 {\r
375                         xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
376                 }\r
377 \r
378                 /* Calculate the reload value required to wait xExpectedIdleTime tick\r
379                 periods. */\r
380                 ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;\r
381                 if( ulMatchValue > ulStoppedTimerCompensation )\r
382                 {\r
383                         /* Compensate for the fact that the CMT is going to be stopped\r
384                         momentarily. */\r
385                         ulMatchValue -= ulStoppedTimerCompensation;\r
386                 }\r
387 \r
388                 /* Stop the CMT momentarily.  The time the CMT is stopped for is\r
389                 accounted for as best it can be, but using the tickless mode will\r
390                 inevitably result in some tiny drift of the time maintained by the\r
391                 kernel with respect to calendar time. */\r
392                 CMT.CMSTR0.BIT.STR0 = 0;\r
393                 while( CMT.CMSTR0.BIT.STR0 == 1 )\r
394                 {\r
395                         /* Nothing to do here. */\r
396                 }\r
397 \r
398                 /* Critical section using the global interrupt bit as the i bit is\r
399                 automatically reset by the WAIT instruction. */\r
400                 __disable_interrupt();\r
401 \r
402                 /* The tick flag is set to false before sleeping.  If it is true when\r
403                 sleep mode is exited then sleep mode was probably exited because the\r
404                 tick was suppressed for the entire xExpectedIdleTime period. */\r
405                 ulTickFlag = pdFALSE;\r
406 \r
407                 /* If a context switch is pending then abandon the low power entry as\r
408                 the context switch might have been pended by an external interrupt that\r
409                 requires processing. */\r
410                 eSleepAction = eTaskConfirmSleepModeStatus();\r
411                 if( eSleepAction == eAbortSleep )\r
412                 {\r
413                         /* Restart tick. */\r
414                         CMT.CMSTR0.BIT.STR0 = 1;\r
415                         __enable_interrupt();\r
416                 }\r
417                 else if( eSleepAction == eNoTasksWaitingTimeout )\r
418                 {\r
419                     /* Protection off. */\r
420                     SYSTEM.PRCR.WORD = portUNLOCK_KEY;\r
421 \r
422                     /* Ready for software standby with all clocks stopped. */\r
423                         SYSTEM.SBYCR.BIT.SSBY = 1;\r
424 \r
425                     /* Protection on. */\r
426                     SYSTEM.PRCR.WORD = portLOCK_KEY;\r
427 \r
428                         /* Sleep until something happens.  Calling prvSleep() will\r
429                         automatically reset the i bit in the PSW. */\r
430                         prvSleep( xExpectedIdleTime );\r
431 \r
432                         /* Restart the CMT. */\r
433                         CMT.CMSTR0.BIT.STR0 = 1;\r
434                 }\r
435                 else\r
436                 {\r
437                     /* Protection off. */\r
438                     SYSTEM.PRCR.WORD = portUNLOCK_KEY;\r
439 \r
440                     /* Ready for deep sleep mode. */\r
441                         SYSTEM.MSTPCRC.BIT.DSLPE = 1;\r
442                         SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;\r
443                         SYSTEM.SBYCR.BIT.SSBY = 0;\r
444 \r
445                     /* Protection on. */\r
446                     SYSTEM.PRCR.WORD = portLOCK_KEY;\r
447 \r
448                     /* Adjust the match value to take into account that the current\r
449                         time slice is already partially complete. */\r
450                         ulMatchValue -= ( uint32_t ) CMT0.CMCNT;\r
451                         CMT0.CMCOR = ( uint16_t ) ulMatchValue;\r
452 \r
453                         /* Restart the CMT to count up to the new match value. */\r
454                         CMT0.CMCNT = 0;\r
455                         CMT.CMSTR0.BIT.STR0 = 1;\r
456 \r
457                         /* Sleep until something happens.  Calling prvSleep() will\r
458                         automatically reset the i bit in the PSW. */\r
459                         prvSleep( xExpectedIdleTime );\r
460 \r
461                         /* Stop CMT.  Again, the time the SysTick is stopped for is\r
462                         accounted for as best it can be, but using the tickless mode will\r
463                         inevitably result in some tiny drift of the time maintained by the\r
464                         kernel with     respect to calendar time. */\r
465                         CMT.CMSTR0.BIT.STR0 = 0;\r
466                         while( CMT.CMSTR0.BIT.STR0 == 1 )\r
467                         {\r
468                                 /* Nothing to do here. */\r
469                         }\r
470 \r
471                         ulCurrentCount = ( uint32_t ) CMT0.CMCNT;\r
472 \r
473                         if( ulTickFlag != pdFALSE )\r
474                         {\r
475                                 /* The tick interrupt has already executed, although because\r
476                                 this function is called with the scheduler suspended the actual\r
477                                 tick processing will not occur until after this function has\r
478                                 exited.  Reset the match value with whatever remains of this\r
479                                 tick period. */\r
480                                 ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;\r
481                                 CMT0.CMCOR = ( uint16_t ) ulMatchValue;\r
482 \r
483                                 /* The tick interrupt handler will already have pended the tick\r
484                                 processing in the kernel.  As the pending tick will be\r
485                                 processed as soon as this function exits, the tick value\r
486                                 maintained by the tick is stepped forward by one less than the\r
487                                 time spent sleeping.  The actual stepping of the tick appears\r
488                                 later in this function. */\r
489                                 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
490                         }\r
491                         else\r
492                         {\r
493                                 /* Something other than the tick interrupt ended the sleep.\r
494                                 How     many complete tick periods passed while the processor was\r
495                                 sleeping? */\r
496                                 ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;\r
497 \r
498                                 /* The match value is set to whatever fraction of a single tick\r
499                                 period remains. */\r
500                                 ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );\r
501                                 CMT0.CMCOR = ( uint16_t ) ulMatchValue;\r
502                         }\r
503 \r
504                         /* Restart the CMT so it runs up to the match value.  The match value\r
505                         will get set to the value required to generate exactly one tick period\r
506                         the next time the CMT interrupt executes. */\r
507                         CMT0.CMCNT = 0;\r
508                         CMT.CMSTR0.BIT.STR0 = 1;\r
509 \r
510                         /* Wind the tick forward by the number of tick periods that the CPU\r
511                         remained in a low power state. */\r
512                         vTaskStepTick( ulCompleteTickPeriods );\r
513                 }\r
514         }\r
515 \r
516 #endif /* configUSE_TICKLESS_IDLE */\r
517 \r