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 /* Kernel includes. */
30 #include "FreeRTOS.h"
31 #include "task.h"
32 
33 
34 #define portINITIAL_FORMAT_VECTOR       ( ( StackType_t ) 0x4000 )
35 
36 /* Supervisor mode set. */
37 #define portINITIAL_STATUS_REGISTER     ( ( StackType_t ) 0x2000)
38 
39 /* The clock prescale into the timer peripheral. */
40 #define portPRESCALE_VALUE              ( ( uint8_t ) 10 )
41 
42 /* The clock frequency into the RTC. */
43 #define portRTC_CLOCK_HZ                ( ( uint32_t ) 1000 )
44 
45 asm void interrupt VectorNumber_VL1swi vPortYieldISR( void );
46 static void prvSetupTimerInterrupt( void );
47 
48 /* Used to keep track of the number of nested calls to taskENTER_CRITICAL().  This
49 will be set to 0 prior to the first task being started. */
50 static uint32_t ulCriticalNesting = 0x9999UL;
51 
52 /*-----------------------------------------------------------*/
53 
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)54 StackType_t *pxPortInitialiseStack( StackType_t * pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
55 {
56 
57 uint32_t ulOriginalA5;
58 
59     __asm{ MOVE.L A5, ulOriginalA5 };
60 
61 
62     *pxTopOfStack = (StackType_t) 0xDEADBEEF;
63     pxTopOfStack--;
64 
65     /* Exception stack frame starts with the return address. */
66     *pxTopOfStack = ( StackType_t ) pxCode;
67     pxTopOfStack--;
68 
69     *pxTopOfStack = ( portINITIAL_FORMAT_VECTOR << 16UL ) | ( portINITIAL_STATUS_REGISTER );
70     pxTopOfStack--;
71 
72     *pxTopOfStack = ( StackType_t ) 0x0; /*FP*/
73     pxTopOfStack -= 14; /* A5 to D0. */
74 
75     /* Parameter in A0. */
76     *( pxTopOfStack + 8 ) = ( StackType_t ) pvParameters;
77 
78     /* A5 must be maintained as it is reserved by the compiler. */
79     *( pxTopOfStack + 13 ) = ulOriginalA5;
80 
81     return pxTopOfStack;
82 }
83 /*-----------------------------------------------------------*/
84 
xPortStartScheduler(void)85 BaseType_t xPortStartScheduler( void )
86 {
87 extern void vPortStartFirstTask( void );
88 
89     ulCriticalNesting = 0UL;
90 
91     /* Configure a timer to generate the tick interrupt. */
92     prvSetupTimerInterrupt();
93 
94     /* Start the first task executing. */
95     vPortStartFirstTask();
96 
97     return pdFALSE;
98 }
99 /*-----------------------------------------------------------*/
100 
prvSetupTimerInterrupt(void)101 static void prvSetupTimerInterrupt( void )
102 {
103     /* Prescale by 1 - ie no prescale. */
104     RTCSC |= 8;
105 
106     /* Compare match value. */
107     RTCMOD = portRTC_CLOCK_HZ / configTICK_RATE_HZ;
108 
109     /* Enable the RTC to generate interrupts - interrupts are already disabled
110     when this code executes. */
111     RTCSC_RTIE = 1;
112 }
113 /*-----------------------------------------------------------*/
114 
vPortEndScheduler(void)115 void vPortEndScheduler( void )
116 {
117     /* Not implemented as there is nothing to return to. */
118 }
119 /*-----------------------------------------------------------*/
120 
vPortEnterCritical(void)121 void vPortEnterCritical( void )
122 {
123     if( ulCriticalNesting == 0UL )
124     {
125         /* Guard against context switches being pended simultaneously with a
126         critical section being entered. */
127         do
128         {
129             portDISABLE_INTERRUPTS();
130             if( INTC_FRC == 0UL )
131             {
132                 break;
133             }
134 
135             portENABLE_INTERRUPTS();
136 
137         } while( 1 );
138     }
139     ulCriticalNesting++;
140 }
141 /*-----------------------------------------------------------*/
142 
vPortExitCritical(void)143 void vPortExitCritical( void )
144 {
145     ulCriticalNesting--;
146     if( ulCriticalNesting == 0 )
147     {
148         portENABLE_INTERRUPTS();
149     }
150 }
151 /*-----------------------------------------------------------*/
152 
vPortYieldHandler(void)153 void vPortYieldHandler( void )
154 {
155 uint32_t ulSavedInterruptMask;
156 
157     ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
158     {
159         /* Note this will clear all forced interrupts - this is done for speed. */
160         INTC_CFRC = 0x3E;
161         vTaskSwitchContext();
162     }
163     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
164 }
165 /*-----------------------------------------------------------*/
166 
vPortTickISR(void)167 void interrupt VectorNumber_Vrtc vPortTickISR( void )
168 {
169 uint32_t ulSavedInterruptMask;
170 
171     /* Clear the interrupt. */
172     RTCSC |= RTCSC_RTIF_MASK;
173 
174     /* Increment the RTOS tick. */
175     ulSavedInterruptMask = portSET_INTERRUPT_MASK_FROM_ISR();
176     {
177         if( xTaskIncrementTick() != pdFALSE )
178         {
179             taskYIELD();
180         }
181     }
182     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulSavedInterruptMask );
183 }
184