2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the SH2A port.
\r
68 *----------------------------------------------------------*/
\r
70 /* Standard C includes. */
\r
73 /* Scheduler includes. */
\r
74 #include "FreeRTOS.h"
\r
77 /* Library includes. */
\r
80 /* Hardware specifics. */
\r
81 #include "iorx111.h"
\r
83 /*-----------------------------------------------------------*/
\r
85 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
86 PSW is set with U and I set, and PM and IPL clear. */
\r
87 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
89 /* The peripheral clock is divided by this value before being supplying the
\r
91 #if ( configUSE_TICKLESS_IDLE == 0 )
\r
92 /* If tickless idle is not used then the divisor can be fixed. */
\r
93 #define portCLOCK_DIVISOR 8UL
\r
94 #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
\r
95 #define portCLOCK_DIVISOR 512UL
\r
96 #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
\r
97 #define portCLOCK_DIVISOR 128UL
\r
98 #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
\r
99 #define portCLOCK_DIVISOR 32UL
\r
101 #define portCLOCK_DIVISOR 8UL
\r
105 /* Keys required to lock and unlock access to certain system registers
\r
107 #define portUNLOCK_KEY 0xA50B
\r
108 #define portLOCK_KEY 0xA500
\r
110 /*-----------------------------------------------------------*/
\r
113 * Function to start the first task executing - written in asm code as direct
\r
114 * access to registers is required.
\r
116 extern void prvStartFirstTask( void );
\r
119 * The tick ISR handler. The peripheral used is configured by the application
\r
120 * via a hook/callback function.
\r
122 __interrupt static void prvTickISR( void );
\r
125 * Sets up the periodic ISR used for the RTOS tick using the CMT.
\r
126 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
127 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
128 * in place of prvSetupTimerInterrupt().
\r
130 static void prvSetupTimerInterrupt( void );
\r
131 #ifndef configSETUP_TICK_INTERRUPT
\r
132 /* The user has not provided their own tick interrupt configuration so use
\r
133 the definition in this file (which uses the interval timer). */
\r
134 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
135 #endif /* configSETUP_TICK_INTERRUPT */
\r
138 * Called after the sleep mode registers have been configured, prvSleep()
\r
139 * executes the pre and post sleep macros, and actually calls the wait
\r
142 #if configUSE_TICKLESS_IDLE == 1
\r
143 static void prvSleep( TickType_t xExpectedIdleTime );
\r
144 #endif /* configUSE_TICKLESS_IDLE */
\r
146 /*-----------------------------------------------------------*/
\r
148 extern void *pxCurrentTCB;
\r
150 /*-----------------------------------------------------------*/
\r
152 /* Calculate how many clock increments make up a single tick period. */
\r
153 static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
155 #if configUSE_TICKLESS_IDLE == 1
\r
157 /* Holds the maximum number of ticks that can be suppressed - which is
\r
158 basically how far into the future an interrupt can be generated. Set
\r
159 during initialisation. This is the maximum possible value that the
\r
160 compare match register can hold divided by ulMatchValueForOneTick. */
\r
161 static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
163 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
164 sleep mode was exited because of a tick interrupt, or an interrupt
\r
165 generated by something else. */
\r
166 static volatile uint32_t ulTickFlag = pdFALSE;
\r
168 /* The CMT counter is stopped temporarily each time it is re-programmed.
\r
169 The following constant offsets the CMT counter match value by the number of
\r
170 CMT counts that would typically be missed while the counter was stopped to
\r
171 compensate for the lost time. The large difference between the divided CMT
\r
172 clock and the CPU clock means it is likely ulStoppedTimerCompensation will
\r
173 equal zero - and be optimised away. */
\r
174 static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
\r
178 /*-----------------------------------------------------------*/
\r
181 * See header file for description.
\r
183 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
185 /* Offset to end up on 8 byte boundary. */
\r
188 /* R0 is not included as it is the stack pointer. */
\r
189 *pxTopOfStack = 0x00;
\r
191 *pxTopOfStack = 0x00;
\r
193 *pxTopOfStack = portINITIAL_PSW;
\r
195 *pxTopOfStack = ( StackType_t ) pxCode;
\r
197 /* When debugging it can be useful if every register is set to a known
\r
198 value. Otherwise code space can be saved by just setting the registers
\r
199 that need to be set. */
\r
200 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
203 *pxTopOfStack = 0x12345678; /* r15. */
\r
205 *pxTopOfStack = 0xaaaabbbb;
\r
207 *pxTopOfStack = 0xdddddddd;
\r
209 *pxTopOfStack = 0xcccccccc;
\r
211 *pxTopOfStack = 0xbbbbbbbb;
\r
213 *pxTopOfStack = 0xaaaaaaaa;
\r
215 *pxTopOfStack = 0x99999999;
\r
217 *pxTopOfStack = 0x88888888;
\r
219 *pxTopOfStack = 0x77777777;
\r
221 *pxTopOfStack = 0x66666666;
\r
223 *pxTopOfStack = 0x55555555;
\r
225 *pxTopOfStack = 0x44444444;
\r
227 *pxTopOfStack = 0x33333333;
\r
229 *pxTopOfStack = 0x22222222;
\r
234 /* Leave space for the registers that will get popped from the stack
\r
235 when the task first starts executing. */
\r
236 pxTopOfStack -= 15;
\r
240 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
242 *pxTopOfStack = 0x12345678; /* Accumulator. */
\r
244 *pxTopOfStack = 0x87654321; /* Accumulator. */
\r
246 return pxTopOfStack;
\r
248 /*-----------------------------------------------------------*/
\r
250 BaseType_t xPortStartScheduler( void )
\r
252 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
253 if( pxCurrentTCB != NULL )
\r
255 /* Call an application function to set up the timer that will generate
\r
256 the tick interrupt. This way the application can decide which
\r
257 peripheral to use. If tickless mode is used then the default
\r
258 implementation defined in this file (which uses CMT0) should not be
\r
260 configSETUP_TICK_INTERRUPT();
\r
262 /* Enable the software interrupt. */
\r
263 _IEN( _ICU_SWINT ) = 1;
\r
265 /* Ensure the software interrupt is clear. */
\r
266 _IR( _ICU_SWINT ) = 0;
\r
268 /* Ensure the software interrupt is set to the kernel priority. */
\r
269 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
271 /* Start the first task. */
\r
272 prvStartFirstTask();
\r
275 /* Execution should not reach here as the tasks are now running!
\r
276 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
277 a warning about a statically declared function not being referenced in the
\r
278 case that the application writer has provided their own tick interrupt
\r
279 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
280 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
281 prvSetupTimerInterrupt();
\r
283 /* Should not get here. */
\r
286 /*-----------------------------------------------------------*/
\r
288 #pragma vector = configTICK_VECTOR
\r
289 __interrupt static void prvTickISR( void )
\r
291 /* Re-enable interrupts. */
\r
292 __enable_interrupt();
\r
294 /* Increment the tick, and perform any processing the new tick value
\r
296 __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
\r
298 if( xTaskIncrementTick() != pdFALSE )
\r
303 __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
\r
305 #if configUSE_TICKLESS_IDLE == 1
\r
307 /* The CPU woke because of a tick. */
\r
308 ulTickFlag = pdTRUE;
\r
310 /* If this is the first tick since exiting tickless mode then the CMT
\r
311 compare match value needs resetting. */
\r
312 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
316 /*-----------------------------------------------------------*/
\r
318 void vPortEndScheduler( void )
\r
320 /* Not implemented in ports where there is nothing to return to.
\r
321 Artificially force an assert. */
\r
322 configASSERT( pxCurrentTCB == NULL );
\r
324 /*-----------------------------------------------------------*/
\r
326 static void prvSetupTimerInterrupt( void )
\r
329 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
335 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
337 /* Interrupt on compare match. */
\r
338 CMT0.CMCR.BIT.CMIE = 1;
\r
340 /* Set the compare match value. */
\r
341 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
343 /* Divide the PCLK. */
\r
344 #if portCLOCK_DIVISOR == 512
\r
346 CMT0.CMCR.BIT.CKS = 3;
\r
348 #elif portCLOCK_DIVISOR == 128
\r
350 CMT0.CMCR.BIT.CKS = 2;
\r
352 #elif portCLOCK_DIVISOR == 32
\r
354 CMT0.CMCR.BIT.CKS = 1;
\r
356 #elif portCLOCK_DIVISOR == 8
\r
358 CMT0.CMCR.BIT.CKS = 0;
\r
362 #error Invalid portCLOCK_DIVISOR setting
\r
367 /* Enable the interrupt... */
\r
368 _IEN( _CMT0_CMI0 ) = 1;
\r
370 /* ...and set its priority to the application defined kernel priority. */
\r
371 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
373 /* Start the timer. */
\r
374 CMT.CMSTR0.BIT.STR0 = 1;
\r
376 /*-----------------------------------------------------------*/
\r
378 #if configUSE_TICKLESS_IDLE == 1
\r
380 static void prvSleep( TickType_t xExpectedIdleTime )
\r
382 /* Allow the application to define some pre-sleep processing. */
\r
383 configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
\r
385 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
386 means the application defined code has already executed the WAIT
\r
388 if( xExpectedIdleTime > 0 )
\r
390 __wait_for_interrupt();
\r
393 /* Allow the application to define some post sleep processing. */
\r
394 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
\r
397 #endif /* configUSE_TICKLESS_IDLE */
\r
398 /*-----------------------------------------------------------*/
\r
400 #if configUSE_TICKLESS_IDLE == 1
\r
402 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
404 uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
\r
405 eSleepModeStatus eSleepAction;
\r
407 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
409 /* Make sure the CMT reload value does not overflow the counter. */
\r
410 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
412 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
415 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
417 ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
\r
418 if( ulMatchValue > ulStoppedTimerCompensation )
\r
420 /* Compensate for the fact that the CMT is going to be stopped
\r
422 ulMatchValue -= ulStoppedTimerCompensation;
\r
425 /* Stop the CMT momentarily. The time the CMT is stopped for is
\r
426 accounted for as best it can be, but using the tickless mode will
\r
427 inevitably result in some tiny drift of the time maintained by the
\r
428 kernel with respect to calendar time. */
\r
429 CMT.CMSTR0.BIT.STR0 = 0;
\r
430 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
432 /* Nothing to do here. */
\r
435 /* Critical section using the global interrupt bit as the i bit is
\r
436 automatically reset by the WAIT instruction. */
\r
437 __disable_interrupt();
\r
439 /* The tick flag is set to false before sleeping. If it is true when
\r
440 sleep mode is exited then sleep mode was probably exited because the
\r
441 tick was suppressed for the entire xExpectedIdleTime period. */
\r
442 ulTickFlag = pdFALSE;
\r
444 /* If a context switch is pending then abandon the low power entry as
\r
445 the context switch might have been pended by an external interrupt that
\r
446 requires processing. */
\r
447 eSleepAction = eTaskConfirmSleepModeStatus();
\r
448 if( eSleepAction == eAbortSleep )
\r
450 /* Restart tick. */
\r
451 CMT.CMSTR0.BIT.STR0 = 1;
\r
452 __enable_interrupt();
\r
454 else if( eSleepAction == eNoTasksWaitingTimeout )
\r
456 /* Protection off. */
\r
457 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
459 /* Ready for software standby with all clocks stopped. */
\r
460 SYSTEM.SBYCR.BIT.SSBY = 1;
\r
462 /* Protection on. */
\r
463 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
465 /* Sleep until something happens. Calling prvSleep() will
\r
466 automatically reset the i bit in the PSW. */
\r
467 prvSleep( xExpectedIdleTime );
\r
469 /* Restart the CMT. */
\r
470 CMT.CMSTR0.BIT.STR0 = 1;
\r
474 /* Protection off. */
\r
475 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
477 /* Ready for deep sleep mode. */
\r
478 SYSTEM.MSTPCRC.BIT.DSLPE = 1;
\r
479 SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
\r
480 SYSTEM.SBYCR.BIT.SSBY = 0;
\r
482 /* Protection on. */
\r
483 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
485 /* Adjust the match value to take into account that the current
\r
486 time slice is already partially complete. */
\r
487 ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
\r
488 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
490 /* Restart the CMT to count up to the new match value. */
\r
492 CMT.CMSTR0.BIT.STR0 = 1;
\r
494 /* Sleep until something happens. Calling prvSleep() will
\r
495 automatically reset the i bit in the PSW. */
\r
496 prvSleep( xExpectedIdleTime );
\r
498 /* Stop CMT. Again, the time the SysTick is stopped for is
\r
499 accounted for as best it can be, but using the tickless mode will
\r
500 inevitably result in some tiny drift of the time maintained by the
\r
501 kernel with respect to calendar time. */
\r
502 CMT.CMSTR0.BIT.STR0 = 0;
\r
503 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
505 /* Nothing to do here. */
\r
508 ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
\r
510 if( ulTickFlag != pdFALSE )
\r
512 /* The tick interrupt has already executed, although because
\r
513 this function is called with the scheduler suspended the actual
\r
514 tick processing will not occur until after this function has
\r
515 exited. Reset the match value with whatever remains of this
\r
517 ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
\r
518 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
520 /* The tick interrupt handler will already have pended the tick
\r
521 processing in the kernel. As the pending tick will be
\r
522 processed as soon as this function exits, the tick value
\r
523 maintained by the tick is stepped forward by one less than the
\r
524 time spent sleeping. The actual stepping of the tick appears
\r
525 later in this function. */
\r
526 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
530 /* Something other than the tick interrupt ended the sleep.
\r
531 How many complete tick periods passed while the processor was
\r
533 ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
\r
535 /* The match value is set to whatever fraction of a single tick
\r
537 ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
\r
538 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
541 /* Restart the CMT so it runs up to the match value. The match value
\r
542 will get set to the value required to generate exactly one tick period
\r
543 the next time the CMT interrupt executes. */
\r
545 CMT.CMSTR0.BIT.STR0 = 1;
\r
547 /* Wind the tick forward by the number of tick periods that the CPU
\r
548 remained in a low power state. */
\r
549 vTaskStepTick( ulCompleteTickPeriods );
\r
553 #endif /* configUSE_TICKLESS_IDLE */
\r