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 ) 0x1f )      /* System mode, ARM mode, interrupts enabled. */
44 #define portTHUMB_MODE_BIT         ( ( StackType_t ) 0x20 )
45 #define portINSTRUCTION_SIZE       ( ( StackType_t ) 4 )
46 
47 /* Constants required to handle critical sections. */
48 #define portNO_CRITICAL_NESTING    ( ( uint32_t ) 0 )
49 
50 /* Prescale used on the timer clock when calculating the tick period. */
51 #define portPRESCALE               20
52 
53 
54 /*-----------------------------------------------------------*/
55 
56 /* Setup the TB to generate the tick interrupts. */
57 static void prvSetupTimerInterrupt( void );
58 
59 /*-----------------------------------------------------------*/
60 
61 /*
62  * Initialise the stack of a task to look exactly as if a call to
63  * portSAVE_CONTEXT had been called.
64  *
65  * See header file for description.
66  */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)67 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
68                                      TaskFunction_t pxCode,
69                                      void * pvParameters )
70 {
71     StackType_t * pxOriginalTOS;
72 
73     pxOriginalTOS = pxTopOfStack;
74 
75     /* To ensure asserts in tasks.c don't fail, although in this case the assert
76      * is not really required. */
77     pxTopOfStack--;
78 
79     /* Setup the initial stack of the task.  The stack is set exactly as
80      * expected by the portRESTORE_CONTEXT() macro. */
81 
82     /* First on the stack is the return address - which in this case is the
83      * start of the task.  The offset is added to make the return address appear
84      * as it would within an IRQ ISR. */
85     *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
86     pxTopOfStack--;
87 
88     *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;    /* R14 */
89     pxTopOfStack--;
90     *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
91     pxTopOfStack--;
92     *pxTopOfStack = ( StackType_t ) 0x12121212;    /* R12 */
93     pxTopOfStack--;
94     *pxTopOfStack = ( StackType_t ) 0x11111111;    /* R11 */
95     pxTopOfStack--;
96     *pxTopOfStack = ( StackType_t ) 0x10101010;    /* R10 */
97     pxTopOfStack--;
98     *pxTopOfStack = ( StackType_t ) 0x09090909;    /* R9 */
99     pxTopOfStack--;
100     *pxTopOfStack = ( StackType_t ) 0x08080808;    /* R8 */
101     pxTopOfStack--;
102     *pxTopOfStack = ( StackType_t ) 0x07070707;    /* R7 */
103     pxTopOfStack--;
104     *pxTopOfStack = ( StackType_t ) 0x06060606;    /* R6 */
105     pxTopOfStack--;
106     *pxTopOfStack = ( StackType_t ) 0x05050505;    /* R5 */
107     pxTopOfStack--;
108     *pxTopOfStack = ( StackType_t ) 0x04040404;    /* R4 */
109     pxTopOfStack--;
110     *pxTopOfStack = ( StackType_t ) 0x03030303;    /* R3 */
111     pxTopOfStack--;
112     *pxTopOfStack = ( StackType_t ) 0x02020202;    /* R2 */
113     pxTopOfStack--;
114     *pxTopOfStack = ( StackType_t ) 0x01010101;    /* R1 */
115     pxTopOfStack--;
116 
117     /* When the task starts is will expect to find the function parameter in
118      * R0. */
119     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
120     pxTopOfStack--;
121 
122     /* The status register is set for system mode, with interrupts enabled. */
123     *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
124 
125     #ifdef THUMB_INTERWORK
126     {
127         /* We want the task to start in thumb mode. */
128         *pxTopOfStack |= portTHUMB_MODE_BIT;
129     }
130     #endif
131 
132     pxTopOfStack--;
133 
134     /* Interrupt flags cannot always be stored on the stack and will
135      * instead be stored in a variable, which is then saved as part of the
136      * tasks context. */
137     *pxTopOfStack = portNO_CRITICAL_NESTING;
138 
139     return pxTopOfStack;
140 }
141 /*-----------------------------------------------------------*/
142 
xPortStartScheduler(void)143 BaseType_t xPortStartScheduler( void )
144 {
145     extern void vPortISRStartFirstTask( void );
146 
147     /* Start the timer that generates the tick ISR.  Interrupts are disabled
148      * here already. */
149     prvSetupTimerInterrupt();
150 
151     /* Start the first task. */
152     vPortISRStartFirstTask();
153 
154     /* Should not get here! */
155     return 0;
156 }
157 /*-----------------------------------------------------------*/
158 
vPortEndScheduler(void)159 void vPortEndScheduler( void )
160 {
161     /* It is unlikely that the ARM port will require this function as there
162      * is nothing to return to.  */
163 }
164 /*-----------------------------------------------------------*/
165 
prvSetupTimerInterrupt(void)166 static void prvSetupTimerInterrupt( void )
167 {
168     EIC_IRQInitTypeDef EIC_IRQInitStructure;
169     TB_InitTypeDef TB_InitStructure;
170 
171     /* Setup the EIC for the TB. */
172     EIC_IRQInitStructure.EIC_IRQChannelCmd = ENABLE;
173     EIC_IRQInitStructure.EIC_IRQChannel = TB_IRQChannel;
174     EIC_IRQInitStructure.EIC_IRQChannelPriority = 1;
175     EIC_IRQInit( &EIC_IRQInitStructure );
176 
177     /* Setup the TB for the generation of the tick interrupt. */
178     TB_InitStructure.TB_Mode = TB_Mode_Timing;
179     TB_InitStructure.TB_CounterMode = TB_CounterMode_Down;
180     TB_InitStructure.TB_Prescaler = portPRESCALE - 1;
181     TB_InitStructure.TB_AutoReload = ( ( configCPU_CLOCK_HZ / portPRESCALE ) / configTICK_RATE_HZ );
182     TB_Init( &TB_InitStructure );
183 
184     /* Enable TB Update interrupt */
185     TB_ITConfig( TB_IT_Update, ENABLE );
186 
187     /* Clear TB Update interrupt pending bit */
188     TB_ClearITPendingBit( TB_IT_Update );
189 
190     /* Enable TB */
191     TB_Cmd( ENABLE );
192 }
193 /*-----------------------------------------------------------*/
194