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 STR71x ARM7
31 * port.
32 *----------------------------------------------------------*/
33 
34 /* Library includes. */
35 #include "wdg.h"
36 #include "eic.h"
37 
38 /* Standard includes. */
39 #include <stdlib.h>
40 
41 /* Scheduler includes. */
42 #include "FreeRTOS.h"
43 #include "task.h"
44 
45 /* Constants required to setup the initial stack. */
46 #define portINITIAL_SPSR           ( ( StackType_t ) 0x1f )      /* System mode, ARM mode, interrupts enabled. */
47 #define portTHUMB_MODE_BIT         ( ( StackType_t ) 0x20 )
48 #define portINSTRUCTION_SIZE       ( ( StackType_t ) 4 )
49 
50 /* Constants required to handle critical sections. */
51 #define portNO_CRITICAL_NESTING    ( ( uint32_t ) 0 )
52 
53 #define portMICROS_PER_SECOND      1000000
54 
55 /*-----------------------------------------------------------*/
56 
57 /* Setup the watchdog to generate the tick interrupts. */
58 static void prvSetupTimerInterrupt( void );
59 
60 /* ulCriticalNesting will get set to zero when the first task starts.  It
61  * cannot be initialised to 0 as this will cause interrupts to be enabled
62  * during the kernel initialisation process. */
63 uint32_t ulCriticalNesting = ( uint32_t ) 9999;
64 
65 /* Tick interrupt routines for cooperative and preemptive operation
66  * respectively.  The preemptive version is not defined as __irq as it is called
67  * from an asm wrapper function. */
68 __arm __irq void vPortNonPreemptiveTick( void );
69 void vPortPreemptiveTick( void );
70 
71 /*-----------------------------------------------------------*/
72 
73 /*
74  * Initialise the stack of a task to look exactly as if a call to
75  * portSAVE_CONTEXT had been called.
76  *
77  * See header file for description.
78  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)79 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
80                                      TaskFunction_t pxCode,
81                                      void * pvParameters )
82 {
83     StackType_t * pxOriginalTOS;
84 
85     pxOriginalTOS = pxTopOfStack;
86 
87     /* To ensure asserts in tasks.c don't fail, although in this case the assert
88      * is not really required. */
89     pxTopOfStack--;
90 
91     /* Setup the initial stack of the task.  The stack is set exactly as
92      * expected by the portRESTORE_CONTEXT() macro. */
93 
94     /* First on the stack is the return address - which in this case is the
95      * start of the task.  The offset is added to make the return address appear
96      * as it would within an IRQ ISR. */
97     *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
98     pxTopOfStack--;
99 
100     *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;    /* R14 */
101     pxTopOfStack--;
102     *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
103     pxTopOfStack--;
104     *pxTopOfStack = ( StackType_t ) 0x12121212;    /* R12 */
105     pxTopOfStack--;
106     *pxTopOfStack = ( StackType_t ) 0x11111111;    /* R11 */
107     pxTopOfStack--;
108     *pxTopOfStack = ( StackType_t ) 0x10101010;    /* R10 */
109     pxTopOfStack--;
110     *pxTopOfStack = ( StackType_t ) 0x09090909;    /* R9 */
111     pxTopOfStack--;
112     *pxTopOfStack = ( StackType_t ) 0x08080808;    /* R8 */
113     pxTopOfStack--;
114     *pxTopOfStack = ( StackType_t ) 0x07070707;    /* R7 */
115     pxTopOfStack--;
116     *pxTopOfStack = ( StackType_t ) 0x06060606;    /* R6 */
117     pxTopOfStack--;
118     *pxTopOfStack = ( StackType_t ) 0x05050505;    /* R5 */
119     pxTopOfStack--;
120     *pxTopOfStack = ( StackType_t ) 0x04040404;    /* R4 */
121     pxTopOfStack--;
122     *pxTopOfStack = ( StackType_t ) 0x03030303;    /* R3 */
123     pxTopOfStack--;
124     *pxTopOfStack = ( StackType_t ) 0x02020202;    /* R2 */
125     pxTopOfStack--;
126     *pxTopOfStack = ( StackType_t ) 0x01010101;    /* R1 */
127     pxTopOfStack--;
128 
129     /* When the task starts is will expect to find the function parameter in
130      * R0. */
131     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
132     pxTopOfStack--;
133 
134     /* The status register is set for system mode, with interrupts enabled. */
135     *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
136 
137     if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
138     {
139         /* We want the task to start in thumb mode. */
140         *pxTopOfStack |= portTHUMB_MODE_BIT;
141     }
142 
143     pxTopOfStack--;
144 
145     /* Interrupt flags cannot always be stored on the stack and will
146      * instead be stored in a variable, which is then saved as part of the
147      * tasks context. */
148     *pxTopOfStack = portNO_CRITICAL_NESTING;
149 
150     return pxTopOfStack;
151 }
152 /*-----------------------------------------------------------*/
153 
xPortStartScheduler(void)154 BaseType_t xPortStartScheduler( void )
155 {
156     extern void vPortStartFirstTask( void );
157 
158     /* Start the timer that generates the tick ISR.  Interrupts are disabled
159      * here already. */
160     prvSetupTimerInterrupt();
161 
162     /* Start the first task. */
163     vPortStartFirstTask();
164 
165     /* Should not get here! */
166     return 0;
167 }
168 /*-----------------------------------------------------------*/
169 
vPortEndScheduler(void)170 void vPortEndScheduler( void )
171 {
172     /* It is unlikely that the ARM port will require this function as there
173      * is nothing to return to.  */
174 }
175 /*-----------------------------------------------------------*/
176 
177 /* The cooperative scheduler requires a normal IRQ service routine to
178  * simply increment the system tick. */
vPortNonPreemptiveTick(void)179 __arm __irq void vPortNonPreemptiveTick( void )
180 {
181     /* Increment the tick count - which may wake some tasks but as the
182      * preemptive scheduler is not being used any woken task is not given
183      * processor time no matter what its priority. */
184     xTaskIncrementTick();
185 
186     /* Clear the interrupt in the watchdog and EIC. */
187     WDG->SR = 0x0000;
188     portCLEAR_EIC();
189 }
190 /*-----------------------------------------------------------*/
191 
192 /* This function is called from an asm wrapper, so does not require the __irq
193  * keyword. */
vPortPreemptiveTick(void)194 void vPortPreemptiveTick( void )
195 {
196     /* Increment the tick counter. */
197     if( xTaskIncrementTick() != pdFALSE )
198     {
199         /* Select a new task to execute. */
200         vTaskSwitchContext();
201     }
202 
203     /* Clear the interrupt in the watchdog and EIC. */
204     WDG->SR = 0x0000;
205     portCLEAR_EIC();
206 }
207 /*-----------------------------------------------------------*/
208 
prvSetupTimerInterrupt(void)209 static void prvSetupTimerInterrupt( void )
210 {
211     /* Set the watchdog up to generate a periodic tick. */
212     WDG_ECITConfig( DISABLE );
213     WDG_CntOnOffConfig( DISABLE );
214     WDG_PeriodValueConfig( portMICROS_PER_SECOND / configTICK_RATE_HZ );
215 
216     /* Setup the tick interrupt in the EIC. */
217     EIC_IRQChannelPriorityConfig( WDG_IRQChannel, 1 );
218     EIC_IRQChannelConfig( WDG_IRQChannel, ENABLE );
219     EIC_IRQConfig( ENABLE );
220     WDG_ECITConfig( ENABLE );
221 
222     /* Start the timer - interrupts are actually disabled at this point so
223      * it is safe to do this here. */
224     WDG_CntOnOffConfig( ENABLE );
225 }
226 /*-----------------------------------------------------------*/
227 
vPortEnterCritical(void)228 __arm __interwork void vPortEnterCritical( void )
229 {
230     /* Disable interrupts first! */
231     __disable_interrupt();
232 
233     /* Now that interrupts are disabled, ulCriticalNesting can be accessed
234      * directly.  Increment ulCriticalNesting to keep a count of how many times
235      * portENTER_CRITICAL() has been called. */
236     ulCriticalNesting++;
237 }
238 /*-----------------------------------------------------------*/
239 
vPortExitCritical(void)240 __arm __interwork void vPortExitCritical( void )
241 {
242     if( ulCriticalNesting > portNO_CRITICAL_NESTING )
243     {
244         /* Decrement the nesting count as we are leaving a critical section. */
245         ulCriticalNesting--;
246 
247         /* If the nesting level has reached zero then interrupts should be
248          * re-enabled. */
249         if( ulCriticalNesting == portNO_CRITICAL_NESTING )
250         {
251             __enable_interrupt();
252         }
253     }
254 }
255 /*-----------------------------------------------------------*/
256