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 /* Standard includes. */
30 #include <stdlib.h>
31
32 /* Scheduler includes. */
33 #include "FreeRTOS.h"
34 #include "task.h"
35
36 /* Critical nesting should be initialised to a non zero value so interrupts don't
37 * accidentally get enabled before the scheduler is started. */
38 #define portINITIAL_CRITICAL_NESTING ( ( StackType_t ) 10 )
39
40 /* The PSW value assigned to tasks when they start to run for the first time. */
41 #define portPSW ( ( StackType_t ) 0x00000000 )
42
43 /* We require the address of the pxCurrentTCB variable, but don't want to know
44 * any details of its type. */
45 typedef void TCB_t;
46 extern volatile TCB_t * volatile pxCurrentTCB;
47
48 /* Keeps track of the nesting level of critical sections. */
49 volatile StackType_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;
50 /*-----------------------------------------------------------*/
51
52 /* Sets up the timer to generate the tick interrupt. */
53 static void prvSetupTimerInterrupt( void );
54
55 /*-----------------------------------------------------------*/
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)56 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
57 TaskFunction_t pxCode,
58 void * pvParameters )
59 {
60 *pxTopOfStack = ( StackType_t ) pxCode; /* Task function start address */
61 pxTopOfStack--;
62 *pxTopOfStack = ( StackType_t ) pxCode; /* Task function start address */
63 pxTopOfStack--;
64 *pxTopOfStack = portPSW; /* Initial PSW value */
65 pxTopOfStack--;
66 *pxTopOfStack = ( StackType_t ) 0x20202020; /* Initial Value of R20 */
67 pxTopOfStack--;
68 *pxTopOfStack = ( StackType_t ) 0x21212121; /* Initial Value of R21 */
69 pxTopOfStack--;
70 *pxTopOfStack = ( StackType_t ) 0x22222222; /* Initial Value of R22 */
71 pxTopOfStack--;
72 *pxTopOfStack = ( StackType_t ) 0x23232323; /* Initial Value of R23 */
73 pxTopOfStack--;
74 *pxTopOfStack = ( StackType_t ) 0x24242424; /* Initial Value of R24 */
75 pxTopOfStack--;
76 #if ( __DATA_MODEL__ == 0 ) || ( __DATA_MODEL__ == 1 )
77 *pxTopOfStack = ( StackType_t ) 0x25252525; /* Initial Value of R25 */
78 pxTopOfStack--;
79 #endif /* configDATA_MODE */
80 *pxTopOfStack = ( StackType_t ) 0x26262626; /* Initial Value of R26 */
81 pxTopOfStack--;
82 *pxTopOfStack = ( StackType_t ) 0x27272727; /* Initial Value of R27 */
83 pxTopOfStack--;
84 *pxTopOfStack = ( StackType_t ) 0x28282828; /* Initial Value of R28 */
85 pxTopOfStack--;
86 *pxTopOfStack = ( StackType_t ) 0x29292929; /* Initial Value of R29 */
87 pxTopOfStack--;
88 *pxTopOfStack = ( StackType_t ) 0x30303030; /* Initial Value of R30 */
89 pxTopOfStack--;
90 *pxTopOfStack = ( StackType_t ) 0x19191919; /* Initial Value of R19 */
91 pxTopOfStack--;
92 *pxTopOfStack = ( StackType_t ) 0x18181818; /* Initial Value of R18 */
93 pxTopOfStack--;
94 *pxTopOfStack = ( StackType_t ) 0x17171717; /* Initial Value of R17 */
95 pxTopOfStack--;
96 *pxTopOfStack = ( StackType_t ) 0x16161616; /* Initial Value of R16 */
97 pxTopOfStack--;
98 *pxTopOfStack = ( StackType_t ) 0x15151515; /* Initial Value of R15 */
99 pxTopOfStack--;
100 *pxTopOfStack = ( StackType_t ) 0x14141414; /* Initial Value of R14 */
101 pxTopOfStack--;
102 *pxTopOfStack = ( StackType_t ) 0x13131313; /* Initial Value of R13 */
103 pxTopOfStack--;
104 *pxTopOfStack = ( StackType_t ) 0x12121212; /* Initial Value of R12 */
105 pxTopOfStack--;
106 *pxTopOfStack = ( StackType_t ) 0x11111111; /* Initial Value of R11 */
107 pxTopOfStack--;
108 *pxTopOfStack = ( StackType_t ) 0x10101010; /* Initial Value of R10 */
109 pxTopOfStack--;
110 *pxTopOfStack = ( StackType_t ) 0x99999999; /* Initial Value of R09 */
111 pxTopOfStack--;
112 *pxTopOfStack = ( StackType_t ) 0x88888888; /* Initial Value of R08 */
113 pxTopOfStack--;
114 *pxTopOfStack = ( StackType_t ) 0x77777777; /* Initial Value of R07 */
115 pxTopOfStack--;
116 *pxTopOfStack = ( StackType_t ) 0x66666666; /* Initial Value of R06 */
117 pxTopOfStack--;
118 *pxTopOfStack = ( StackType_t ) 0x55555555; /* Initial Value of R05 */
119 pxTopOfStack--;
120 #if __DATA_MODEL__ == 0 || __DATA_MODEL__ == 1
121 *pxTopOfStack = ( StackType_t ) 0x44444444; /* Initial Value of R04 */
122 pxTopOfStack--;
123 #endif /* configDATA_MODE */
124 *pxTopOfStack = ( StackType_t ) 0x22222222; /* Initial Value of R02 */
125 pxTopOfStack--;
126 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 is expected to hold the function parameter*/
127 pxTopOfStack--;
128 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
129
130 /*
131 * Return a pointer to the top of the stack we have generated so this can
132 * be stored in the task control block for the task.
133 */
134 return pxTopOfStack;
135 }
136 /*-----------------------------------------------------------*/
137
xPortStartScheduler(void)138 BaseType_t xPortStartScheduler( void )
139 {
140 /* Setup the hardware to generate the tick. Interrupts are disabled when
141 * this function is called. */
142 prvSetupTimerInterrupt();
143
144 /* Restore the context of the first task that is going to run. */
145 vPortStart();
146
147 /* Should not get here as the tasks are now running! */
148 return pdTRUE;
149 }
150 /*-----------------------------------------------------------*/
151
vPortEndScheduler(void)152 void vPortEndScheduler( void )
153 {
154 /* It is unlikely that the V850ES/Fx3 port will get stopped. If required simply
155 * disable the tick interrupt here. */
156 }
157 /*-----------------------------------------------------------*/
158
159 /*
160 * Hardware initialisation to generate the RTOS tick. This uses
161 */
prvSetupTimerInterrupt(void)162 static void prvSetupTimerInterrupt( void )
163 {
164 TM0CE = 0; /* TMM0 operation disable */
165 TM0EQMK0 = 1; /* INTTM0EQ0 interrupt disable */
166 TM0EQIF0 = 0; /* clear INTTM0EQ0 interrupt flag */
167
168 #ifdef __IAR_V850ES_Fx3__
169 {
170 TM0CMP0 = ( ( ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / 2 ) - 1 ); /* divided by 2 because peripherals only run at CPU_CLOCK/2 */
171 }
172 #else
173 {
174 TM0CMP0 = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
175 }
176 #endif
177
178 TM0EQIC0 &= 0xF8;
179 TM0CTL0 = 0x00;
180 TM0EQIF0 = 0; /* clear INTTM0EQ0 interrupt flag */
181 TM0EQMK0 = 0; /* INTTM0EQ0 interrupt enable */
182 TM0CE = 1; /* TMM0 operation enable */
183 }
184 /*-----------------------------------------------------------*/
185