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