2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /*-----------------------------------------------------------
\r
71 * Implementation of functions defined in portable.h for the SH2A port.
\r
72 *----------------------------------------------------------*/
\r
74 /* Standard C includes. */
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
81 /* Library includes. */
\r
84 /* Hardware specifics. */
\r
85 #warning Used to include the chip specific header here.
\r
86 #include "machine.h"
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
91 PSW is set with U and I set, and PM and IPL clear. */
\r
92 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
94 /* The peripheral clock is divided by this value before being supplying the
\r
96 #if ( configUSE_TICKLESS_IDLE == 0 )
\r
97 /* If tickless idle is not used then the divisor can be fixed. */
\r
98 #define portCLOCK_DIVISOR 8UL
\r
99 #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
\r
100 #define portCLOCK_DIVISOR 512UL
\r
101 #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
\r
102 #define portCLOCK_DIVISOR 128UL
\r
103 #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
\r
104 #define portCLOCK_DIVISOR 32UL
\r
106 #define portCLOCK_DIVISOR 8UL
\r
110 /* Keys required to lock and unlock access to certain system registers
\r
112 #define portUNLOCK_KEY 0xA50B
\r
113 #define portLOCK_KEY 0xA500
\r
115 /*-----------------------------------------------------------*/
\r
118 * Function to start the first task executing - written in asm code as direct
\r
119 * access to registers is required.
\r
121 extern void prvStartFirstTask( void );
\r
124 * The tick ISR handler. The peripheral used is configured by the application
\r
125 * via a hook/callback function.
\r
127 __interrupt static void prvTickISR( void );
\r
130 * Sets up the periodic ISR used for the RTOS tick using the CMT.
\r
131 * The application writer can define configSETUP_TICK_INTERRUPT() (in
\r
132 * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
\r
133 * in place of prvSetupTimerInterrupt().
\r
135 static void prvSetupTimerInterrupt( void );
\r
136 #ifndef configSETUP_TICK_INTERRUPT
\r
137 /* The user has not provided their own tick interrupt configuration so use
\r
138 the definition in this file (which uses the interval timer). */
\r
139 #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
\r
140 #endif /* configSETUP_TICK_INTERRUPT */
\r
143 * Called after the sleep mode registers have been configured, prvSleep()
\r
144 * executes the pre and post sleep macros, and actually calls the wait
\r
147 #if configUSE_TICKLESS_IDLE == 1
\r
148 static void prvSleep( TickType_t xExpectedIdleTime );
\r
149 #endif /* configUSE_TICKLESS_IDLE */
\r
151 /*-----------------------------------------------------------*/
\r
153 extern void *pxCurrentTCB;
\r
155 /*-----------------------------------------------------------*/
\r
157 /* Calculate how many clock increments make up a single tick period. */
\r
158 static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
160 #if configUSE_TICKLESS_IDLE == 1
\r
162 /* Holds the maximum number of ticks that can be suppressed - which is
\r
163 basically how far into the future an interrupt can be generated. Set
\r
164 during initialisation. This is the maximum possible value that the
\r
165 compare match register can hold divided by ulMatchValueForOneTick. */
\r
166 static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
\r
168 /* Flag set from the tick interrupt to allow the sleep processing to know if
\r
169 sleep mode was exited because of a tick interrupt, or an interrupt
\r
170 generated by something else. */
\r
171 static volatile uint32_t ulTickFlag = pdFALSE;
\r
173 /* The CMT counter is stopped temporarily each time it is re-programmed.
\r
174 The following constant offsets the CMT counter match value by the number of
\r
175 CMT counts that would typically be missed while the counter was stopped to
\r
176 compensate for the lost time. The large difference between the divided CMT
\r
177 clock and the CPU clock means it is likely ulStoppedTimerCompensation will
\r
178 equal zero - and be optimised away. */
\r
179 static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
\r
183 /*-----------------------------------------------------------*/
\r
186 * See header file for description.
\r
188 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
190 /* Offset to end up on 8 byte boundary. */
\r
193 /* R0 is not included as it is the stack pointer. */
\r
194 *pxTopOfStack = 0x00;
\r
196 *pxTopOfStack = 0x00;
\r
198 *pxTopOfStack = portINITIAL_PSW;
\r
200 *pxTopOfStack = ( StackType_t ) pxCode;
\r
202 /* When debugging it can be useful if every register is set to a known
\r
203 value. Otherwise code space can be saved by just setting the registers
\r
204 that need to be set. */
\r
205 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
208 *pxTopOfStack = 0x12345678; /* r15. */
\r
210 *pxTopOfStack = 0xaaaabbbb;
\r
212 *pxTopOfStack = 0xdddddddd;
\r
214 *pxTopOfStack = 0xcccccccc;
\r
216 *pxTopOfStack = 0xbbbbbbbb;
\r
218 *pxTopOfStack = 0xaaaaaaaa;
\r
220 *pxTopOfStack = 0x99999999;
\r
222 *pxTopOfStack = 0x88888888;
\r
224 *pxTopOfStack = 0x77777777;
\r
226 *pxTopOfStack = 0x66666666;
\r
228 *pxTopOfStack = 0x55555555;
\r
230 *pxTopOfStack = 0x44444444;
\r
232 *pxTopOfStack = 0x33333333;
\r
234 *pxTopOfStack = 0x22222222;
\r
239 /* Leave space for the registers that will get popped from the stack
\r
240 when the task first starts executing. */
\r
241 pxTopOfStack -= 15;
\r
245 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
247 *pxTopOfStack = 0x12345678; /* Accumulator. */
\r
249 *pxTopOfStack = 0x87654321; /* Accumulator. */
\r
251 return pxTopOfStack;
\r
253 /*-----------------------------------------------------------*/
\r
255 BaseType_t xPortStartScheduler( void )
\r
257 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
258 if( pxCurrentTCB != NULL )
\r
260 /* Call an application function to set up the timer that will generate
\r
261 the tick interrupt. This way the application can decide which
\r
262 peripheral to use. If tickless mode is used then the default
\r
263 implementation defined in this file (which uses CMT0) should not be
\r
265 configSETUP_TICK_INTERRUPT();
\r
267 /* Enable the software interrupt. */
\r
268 _IEN( _ICU_SWINT ) = 1;
\r
270 /* Ensure the software interrupt is clear. */
\r
271 _IR( _ICU_SWINT ) = 0;
\r
273 /* Ensure the software interrupt is set to the kernel priority. */
\r
274 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
276 /* Start the first task. */
\r
277 prvStartFirstTask();
\r
280 /* Execution should not reach here as the tasks are now running!
\r
281 prvSetupTimerInterrupt() is called here to prevent the compiler outputting
\r
282 a warning about a statically declared function not being referenced in the
\r
283 case that the application writer has provided their own tick interrupt
\r
284 configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
\r
285 their own routine will be called in place of prvSetupTimerInterrupt()). */
\r
286 prvSetupTimerInterrupt();
\r
288 /* Should not get here. */
\r
291 /*-----------------------------------------------------------*/
\r
293 #pragma vector = configTICK_VECTOR
\r
294 __interrupt static void prvTickISR( void )
\r
296 /* Re-enable interrupts. */
\r
297 __enable_interrupt();
\r
299 /* Increment the tick, and perform any processing the new tick value
\r
301 __set_interrupt_level( configMAX_SYSCALL_INTERRUPT_PRIORITY );
\r
303 if( xTaskIncrementTick() != pdFALSE )
\r
308 __set_interrupt_level( configKERNEL_INTERRUPT_PRIORITY );
\r
310 #if configUSE_TICKLESS_IDLE == 1
\r
312 /* The CPU woke because of a tick. */
\r
313 ulTickFlag = pdTRUE;
\r
315 /* If this is the first tick since exiting tickless mode then the CMT
\r
316 compare match value needs resetting. */
\r
317 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
321 /*-----------------------------------------------------------*/
\r
323 void vPortEndScheduler( void )
\r
325 /* Not implemented in ports where there is nothing to return to.
\r
326 Artificially force an assert. */
\r
327 configASSERT( pxCurrentTCB == NULL );
\r
329 /*-----------------------------------------------------------*/
\r
331 static void prvSetupTimerInterrupt( void )
\r
334 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
340 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
342 /* Interrupt on compare match. */
\r
343 CMT0.CMCR.BIT.CMIE = 1;
\r
345 /* Set the compare match value. */
\r
346 CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
\r
348 /* Divide the PCLK. */
\r
349 #if portCLOCK_DIVISOR == 512
\r
351 CMT0.CMCR.BIT.CKS = 3;
\r
353 #elif portCLOCK_DIVISOR == 128
\r
355 CMT0.CMCR.BIT.CKS = 2;
\r
357 #elif portCLOCK_DIVISOR == 32
\r
359 CMT0.CMCR.BIT.CKS = 1;
\r
361 #elif portCLOCK_DIVISOR == 8
\r
363 CMT0.CMCR.BIT.CKS = 0;
\r
367 #error Invalid portCLOCK_DIVISOR setting
\r
372 /* Enable the interrupt... */
\r
373 _IEN( _CMT0_CMI0 ) = 1;
\r
375 /* ...and set its priority to the application defined kernel priority. */
\r
376 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
378 /* Start the timer. */
\r
379 CMT.CMSTR0.BIT.STR0 = 1;
\r
381 /*-----------------------------------------------------------*/
\r
383 #if configUSE_TICKLESS_IDLE == 1
\r
385 static void prvSleep( TickType_t xExpectedIdleTime )
\r
387 /* Allow the application to define some pre-sleep processing. */
\r
388 configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
\r
390 /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
\r
391 means the application defined code has already executed the WAIT
\r
393 if( xExpectedIdleTime > 0 )
\r
395 __wait_for_interrupt();
\r
398 /* Allow the application to define some post sleep processing. */
\r
399 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
\r
402 #endif /* configUSE_TICKLESS_IDLE */
\r
403 /*-----------------------------------------------------------*/
\r
405 #if configUSE_TICKLESS_IDLE == 1
\r
407 void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
\r
409 uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
\r
410 eSleepModeStatus eSleepAction;
\r
412 /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
\r
414 /* Make sure the CMT reload value does not overflow the counter. */
\r
415 if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
\r
417 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
\r
420 /* Calculate the reload value required to wait xExpectedIdleTime tick
\r
422 ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
\r
423 if( ulMatchValue > ulStoppedTimerCompensation )
\r
425 /* Compensate for the fact that the CMT is going to be stopped
\r
427 ulMatchValue -= ulStoppedTimerCompensation;
\r
430 /* Stop the CMT momentarily. The time the CMT is stopped for is
\r
431 accounted for as best it can be, but using the tickless mode will
\r
432 inevitably result in some tiny drift of the time maintained by the
\r
433 kernel with respect to calendar time. */
\r
434 CMT.CMSTR0.BIT.STR0 = 0;
\r
435 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
437 /* Nothing to do here. */
\r
440 /* Critical section using the global interrupt bit as the i bit is
\r
441 automatically reset by the WAIT instruction. */
\r
442 __disable_interrupt();
\r
444 /* The tick flag is set to false before sleeping. If it is true when
\r
445 sleep mode is exited then sleep mode was probably exited because the
\r
446 tick was suppressed for the entire xExpectedIdleTime period. */
\r
447 ulTickFlag = pdFALSE;
\r
449 /* If a context switch is pending then abandon the low power entry as
\r
450 the context switch might have been pended by an external interrupt that
\r
451 requires processing. */
\r
452 eSleepAction = eTaskConfirmSleepModeStatus();
\r
453 if( eSleepAction == eAbortSleep )
\r
455 /* Restart tick. */
\r
456 CMT.CMSTR0.BIT.STR0 = 1;
\r
457 __enable_interrupt();
\r
459 else if( eSleepAction == eNoTasksWaitingTimeout )
\r
461 /* Protection off. */
\r
462 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
464 /* Ready for software standby with all clocks stopped. */
\r
465 SYSTEM.SBYCR.BIT.SSBY = 1;
\r
467 /* Protection on. */
\r
468 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
470 /* Sleep until something happens. Calling prvSleep() will
\r
471 automatically reset the i bit in the PSW. */
\r
472 prvSleep( xExpectedIdleTime );
\r
474 /* Restart the CMT. */
\r
475 CMT.CMSTR0.BIT.STR0 = 1;
\r
479 /* Protection off. */
\r
480 SYSTEM.PRCR.WORD = portUNLOCK_KEY;
\r
482 /* Ready for deep sleep mode. */
\r
483 SYSTEM.MSTPCRC.BIT.DSLPE = 1;
\r
484 SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
\r
485 SYSTEM.SBYCR.BIT.SSBY = 0;
\r
487 /* Protection on. */
\r
488 SYSTEM.PRCR.WORD = portLOCK_KEY;
\r
490 /* Adjust the match value to take into account that the current
\r
491 time slice is already partially complete. */
\r
492 ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
\r
493 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
495 /* Restart the CMT to count up to the new match value. */
\r
497 CMT.CMSTR0.BIT.STR0 = 1;
\r
499 /* Sleep until something happens. Calling prvSleep() will
\r
500 automatically reset the i bit in the PSW. */
\r
501 prvSleep( xExpectedIdleTime );
\r
503 /* Stop CMT. Again, the time the SysTick is stopped for is
\r
504 accounted for as best it can be, but using the tickless mode will
\r
505 inevitably result in some tiny drift of the time maintained by the
\r
506 kernel with respect to calendar time. */
\r
507 CMT.CMSTR0.BIT.STR0 = 0;
\r
508 while( CMT.CMSTR0.BIT.STR0 == 1 )
\r
510 /* Nothing to do here. */
\r
513 ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
\r
515 if( ulTickFlag != pdFALSE )
\r
517 /* The tick interrupt has already executed, although because
\r
518 this function is called with the scheduler suspended the actual
\r
519 tick processing will not occur until after this function has
\r
520 exited. Reset the match value with whatever remains of this
\r
522 ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
\r
523 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
525 /* The tick interrupt handler will already have pended the tick
\r
526 processing in the kernel. As the pending tick will be
\r
527 processed as soon as this function exits, the tick value
\r
528 maintained by the tick is stepped forward by one less than the
\r
529 time spent sleeping. The actual stepping of the tick appears
\r
530 later in this function. */
\r
531 ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
\r
535 /* Something other than the tick interrupt ended the sleep.
\r
536 How many complete tick periods passed while the processor was
\r
538 ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
\r
540 /* The match value is set to whatever fraction of a single tick
\r
542 ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
\r
543 CMT0.CMCOR = ( uint16_t ) ulMatchValue;
\r
546 /* Restart the CMT so it runs up to the match value. The match value
\r
547 will get set to the value required to generate exactly one tick period
\r
548 the next time the CMT interrupt executes. */
\r
550 CMT.CMSTR0.BIT.STR0 = 1;
\r
552 /* Wind the tick forward by the number of tick periods that the CPU
\r
553 remained in a low power state. */
\r
554 vTaskStepTick( ulCompleteTickPeriods );
\r
558 #endif /* configUSE_TICKLESS_IDLE */
\r