1 /*
2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
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.
23 *
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
26 *
27 */
28
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the ST STR75x ARM7
31 * port.
32 *----------------------------------------------------------*/
33
34 /* Library includes. */
35 #include "75x_tb.h"
36 #include "75x_eic.h"
37
38 /* Scheduler includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41
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 )
45
46 /* Constants required to handle critical sections. */
47 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
48
49 /* Prescale used on the timer clock when calculating the tick period. */
50 #define portPRESCALE 20
51
52
53 /*-----------------------------------------------------------*/
54
55 /* Setup the TB to generate the tick interrupts. */
56 static void prvSetupTimerInterrupt( void );
57
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;
62
63 /* Tick interrupt routines for preemptive operation. */
64 __arm void vPortPreemptiveTick( void );
65
66 /*-----------------------------------------------------------*/
67
68 /*
69 * Initialise the stack of a task to look exactly as if a call to
70 * portSAVE_CONTEXT had been called.
71 *
72 * See header file for description.
73 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)74 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
75 TaskFunction_t pxCode,
76 void * pvParameters )
77 {
78 StackType_t * pxOriginalTOS;
79
80 pxOriginalTOS = pxTopOfStack;
81
82 /* To ensure asserts in tasks.c don't fail, although in this case the assert
83 * is not really required. */
84 pxTopOfStack--;
85
86 /* Setup the initial stack of the task. The stack is set exactly as
87 * expected by the portRESTORE_CONTEXT() macro. */
88
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;
93 pxTopOfStack--;
94
95 *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
96 pxTopOfStack--;
97 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
98 pxTopOfStack--;
99 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
100 pxTopOfStack--;
101 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
102 pxTopOfStack--;
103 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
104 pxTopOfStack--;
105 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
106 pxTopOfStack--;
107 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
108 pxTopOfStack--;
109 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
110 pxTopOfStack--;
111 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
112 pxTopOfStack--;
113 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
114 pxTopOfStack--;
115 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
116 pxTopOfStack--;
117 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
118 pxTopOfStack--;
119 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
120 pxTopOfStack--;
121 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
122 pxTopOfStack--;
123
124 /* When the task starts is will expect to find the function parameter in
125 * R0. */
126 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
127 pxTopOfStack--;
128
129 /* The status register is set for system mode, with interrupts enabled. */
130 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
131 pxTopOfStack--;
132
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
135 * tasks context. */
136 *pxTopOfStack = portNO_CRITICAL_NESTING;
137
138 return pxTopOfStack;
139 }
140 /*-----------------------------------------------------------*/
141
xPortStartScheduler(void)142 BaseType_t xPortStartScheduler( void )
143 {
144 extern void vPortStartFirstTask( void );
145
146 /* Start the timer that generates the tick ISR. Interrupts are disabled
147 * here already. */
148 prvSetupTimerInterrupt();
149
150 /* Start the first task. */
151 vPortStartFirstTask();
152
153 /* Should not get here! */
154 return 0;
155 }
156 /*-----------------------------------------------------------*/
157
vPortEndScheduler(void)158 void vPortEndScheduler( void )
159 {
160 /* It is unlikely that the ARM port will require this function as there
161 * is nothing to return to. */
162 }
163 /*-----------------------------------------------------------*/
164
vPortPreemptiveTick(void)165 __arm void vPortPreemptiveTick( void )
166 {
167 /* Increment the tick counter. */
168 if( xTaskIncrementTick() != pdFALSE )
169 {
170 /* Select a new task to execute. */
171 vTaskSwitchContext();
172 }
173
174 TB_ClearITPendingBit( TB_IT_Update );
175 }
176 /*-----------------------------------------------------------*/
177
prvSetupTimerInterrupt(void)178 static void prvSetupTimerInterrupt( void )
179 {
180 EIC_IRQInitTypeDef EIC_IRQInitStructure;
181 TB_InitTypeDef TB_InitStructure;
182
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 );
188
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 );
195
196 /* Enable TB Update interrupt */
197 TB_ITConfig( TB_IT_Update, ENABLE );
198
199 /* Clear TB Update interrupt pending bit */
200 TB_ClearITPendingBit( TB_IT_Update );
201
202 /* Enable TB */
203 TB_Cmd( ENABLE );
204 }
205 /*-----------------------------------------------------------*/
206
vPortEnterCritical(void)207 __arm __interwork void vPortEnterCritical( void )
208 {
209 /* Disable interrupts first! */
210 __disable_interrupt();
211
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. */
215 ulCriticalNesting++;
216 }
217 /*-----------------------------------------------------------*/
218
vPortExitCritical(void)219 __arm __interwork void vPortExitCritical( void )
220 {
221 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
222 {
223 /* Decrement the nesting count as we are leaving a critical section. */
224 ulCriticalNesting--;
225
226 /* If the nesting level has reached zero then interrupts should be
227 * re-enabled. */
228 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
229 {
230 __enable_interrupt();
231 }
232 }
233 }
234 /*-----------------------------------------------------------*/
235