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 /* Kernel includes. */
33 #include "FreeRTOS.h"
34 #include "task.h"
35 
36 /* Machine includes */
37 #include <machine/counter.h>
38 #include <machine/ic.h>
39 /*-----------------------------------------------------------*/
40 
41 /* The initial PSR has the Previous Interrupt Enabled (PIEN) flag set. */
42 #define portINITIAL_PSR    ( 0x00020000 )
43 
44 /*-----------------------------------------------------------*/
45 
46 /*
47  * Perform any hardware configuration necessary to generate the tick interrupt.
48  */
49 static void prvSetupTimerInterrupt( void );
50 /*-----------------------------------------------------------*/
51 
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)52 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
53                                      TaskFunction_t pxCode,
54                                      void * pvParameters )
55 {
56     /* Make space on the stack for the context - this leaves a couple of spaces
57      * empty.  */
58     pxTopOfStack -= 20;
59 
60     /* Fill the registers with known values to assist debugging. */
61     pxTopOfStack[ 16 ] = 0;
62     pxTopOfStack[ 15 ] = portINITIAL_PSR;
63     pxTopOfStack[ 14 ] = ( uint32_t ) pxCode;
64     pxTopOfStack[ 13 ] = 0x00000000UL; /* R15. */
65     pxTopOfStack[ 12 ] = 0x00000000UL; /* R14. */
66     pxTopOfStack[ 11 ] = 0x0d0d0d0dUL;
67     pxTopOfStack[ 10 ] = 0x0c0c0c0cUL;
68     pxTopOfStack[ 9 ] = 0x0b0b0b0bUL;
69     pxTopOfStack[ 8 ] = 0x0a0a0a0aUL;
70     pxTopOfStack[ 7 ] = 0x09090909UL;
71     pxTopOfStack[ 6 ] = 0x08080808UL;
72     pxTopOfStack[ 5 ] = 0x07070707UL;
73     pxTopOfStack[ 4 ] = 0x06060606UL;
74     pxTopOfStack[ 3 ] = 0x05050505UL;
75     pxTopOfStack[ 2 ] = 0x04040404UL;
76     pxTopOfStack[ 1 ] = 0x03030303UL;
77     pxTopOfStack[ 0 ] = ( uint32_t ) pvParameters;
78 
79     return pxTopOfStack;
80 }
81 /*-----------------------------------------------------------*/
82 
xPortStartScheduler(void)83 BaseType_t xPortStartScheduler( void )
84 {
85     /* Set-up the timer interrupt. */
86     prvSetupTimerInterrupt();
87 
88     /* Integrated Interrupt Controller: Enable all interrupts. */
89     ic->ien = 1;
90 
91     /* Restore callee saved registers. */
92     portRESTORE_CONTEXT();
93 
94     /* Should not get here. */
95     return 0;
96 }
97 /*-----------------------------------------------------------*/
98 
prvSetupTimerInterrupt(void)99 static void prvSetupTimerInterrupt( void )
100 {
101     /* Enable timer interrupts */
102     counter1->reload = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1;
103     counter1->value = counter1->reload;
104     counter1->mask = 1;
105 
106     /* Set the IRQ Handler priority and enable it. */
107     irq[ IRQ_COUNTER1 ].ien = 1;
108 }
109 /*-----------------------------------------------------------*/
110 
111 /* Trap 31 handler. */
112 void interrupt31_handler( void ) __attribute__( ( naked ) );
interrupt31_handler(void)113 void interrupt31_handler( void )
114 {
115     portSAVE_CONTEXT();
116     __asm volatile ( "call vTaskSwitchContext" );
117     portRESTORE_CONTEXT();
118 }
119 /*-----------------------------------------------------------*/
120 
121 static void prvProcessTick( void ) __attribute__( ( noinline ) );
prvProcessTick(void)122 static void prvProcessTick( void )
123 {
124     if( xTaskIncrementTick() != pdFALSE )
125     {
126         vTaskSwitchContext();
127     }
128 
129     /* Clear the Tick Interrupt. */
130     counter1->expired = 0;
131 }
132 /*-----------------------------------------------------------*/
133 
134 /* Timer 1 interrupt handler, used for tick interrupt. */
135 void interrupt7_handler( void ) __attribute__( ( naked ) );
interrupt7_handler(void)136 void interrupt7_handler( void )
137 {
138     portSAVE_CONTEXT();
139     prvProcessTick();
140     portRESTORE_CONTEXT();
141 }
142 /*-----------------------------------------------------------*/
143 
vPortEndScheduler(void)144 void vPortEndScheduler( void )
145 {
146     /* Nothing to do. Unlikely to want to end. */
147 }
148 /*-----------------------------------------------------------*/
149