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