2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ST STR75x ARM7
32 *----------------------------------------------------------*/
34 /* Library includes. */
38 /* Scheduler includes. */
42 /* Constants required to setup the initial stack. */
43 #define portINITIAL_SPSR ( ( StackType_t ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
44 #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
46 /* Constants required to handle critical sections. */
47 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
49 /* Prescale used on the timer clock when calculating the tick period. */
50 #define portPRESCALE 20
53 /*-----------------------------------------------------------*/
55 /* Setup the TB to generate the tick interrupts. */
56 static void prvSetupTimerInterrupt( void );
58 /* ulCriticalNesting will get set to zero when the first task starts. It
59 * cannot be initialised to 0 as this will cause interrupts to be enabled
60 * during the kernel initialisation process. */
61 uint32_t ulCriticalNesting = ( uint32_t ) 9999;
63 /* Tick interrupt routines for preemptive operation. */
64 __arm void vPortPreemptiveTick( void );
66 /*-----------------------------------------------------------*/
69 * Initialise the stack of a task to look exactly as if a call to
70 * portSAVE_CONTEXT had been called.
72 * See header file for description.
74 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
75 TaskFunction_t pxCode,
78 StackType_t * pxOriginalTOS;
80 pxOriginalTOS = pxTopOfStack;
82 /* To ensure asserts in tasks.c don't fail, although in this case the assert
83 * is not really required. */
86 /* Setup the initial stack of the task. The stack is set exactly as
87 * expected by the portRESTORE_CONTEXT() macro. */
89 /* First on the stack is the return address - which in this case is the
90 * start of the task. The offset is added to make the return address appear
91 * as it would within an IRQ ISR. */
92 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
95 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
97 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
99 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
101 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
103 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
105 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
107 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
109 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
111 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
113 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
115 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
117 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
119 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
121 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
124 /* When the task starts is will expect to find the function parameter in
126 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
129 /* The status register is set for system mode, with interrupts enabled. */
130 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
133 /* Interrupt flags cannot always be stored on the stack and will
134 * instead be stored in a variable, which is then saved as part of the
136 *pxTopOfStack = portNO_CRITICAL_NESTING;
140 /*-----------------------------------------------------------*/
142 BaseType_t xPortStartScheduler( void )
144 extern void vPortStartFirstTask( void );
146 /* Start the timer that generates the tick ISR. Interrupts are disabled
148 prvSetupTimerInterrupt();
150 /* Start the first task. */
151 vPortStartFirstTask();
153 /* Should not get here! */
156 /*-----------------------------------------------------------*/
158 void vPortEndScheduler( void )
160 /* It is unlikely that the ARM port will require this function as there
161 * is nothing to return to. */
163 /*-----------------------------------------------------------*/
165 __arm void vPortPreemptiveTick( void )
167 /* Increment the tick counter. */
168 if( xTaskIncrementTick() != pdFALSE )
170 /* Select a new task to execute. */
171 vTaskSwitchContext();
174 TB_ClearITPendingBit( TB_IT_Update );
176 /*-----------------------------------------------------------*/
178 static void prvSetupTimerInterrupt( void )
180 EIC_IRQInitTypeDef EIC_IRQInitStructure;
181 TB_InitTypeDef TB_InitStructure;
183 /* Setup the EIC for the TB. */
184 EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
185 EIC_IRQInitStructure.EIC_IRQChannel = TB_IRQChannel;
186 EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
187 EIC_IRQInit( &EIC_IRQInitStructure );
189 /* Setup the TB for the generation of the tick interrupt. */
190 TB_InitStructure.TB_Mode = TB_Mode_Timing;
191 TB_InitStructure.TB_CounterMode = TB_CounterMode_Down;
192 TB_InitStructure.TB_Prescaler = portPRESCALE - 1;
193 TB_InitStructure.TB_AutoReload = ( ( configCPU_CLOCK_HZ / portPRESCALE ) / configTICK_RATE_HZ );
194 TB_Init( &TB_InitStructure );
196 /* Enable TB Update interrupt */
197 TB_ITConfig( TB_IT_Update, ENABLE );
199 /* Clear TB Update interrupt pending bit */
200 TB_ClearITPendingBit( TB_IT_Update );
205 /*-----------------------------------------------------------*/
207 __arm __interwork void vPortEnterCritical( void )
209 /* Disable interrupts first! */
210 __disable_interrupt();
212 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
213 * directly. Increment ulCriticalNesting to keep a count of how many times
214 * portENTER_CRITICAL() has been called. */
217 /*-----------------------------------------------------------*/
219 __arm __interwork void vPortExitCritical( void )
221 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
223 /* Decrement the nesting count as we are leaving a critical section. */
226 /* If the nesting level has reached zero then interrupts should be
228 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
230 __enable_interrupt();
234 /*-----------------------------------------------------------*/