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 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the NIOS2 port.
31 *----------------------------------------------------------*/
32
33 /* Standard Includes. */
34 #include <string.h>
35 #include <errno.h>
36
37 /* Altera includes. */
38 #include "sys/alt_irq.h"
39 #include "sys/alt_exceptions.h"
40 #include "altera_avalon_timer_regs.h"
41 #include "priv/alt_irq_table.h"
42
43 /* Scheduler includes. */
44 #include "FreeRTOS.h"
45 #include "task.h"
46
47 /* Interrupts are enabled. */
48 #define portINITIAL_ESTATUS ( StackType_t ) 0x01
49
50 int _alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
51 void *isr_context, void *flags);
52 /*-----------------------------------------------------------*/
53
54 /*
55 * Setup the timer to generate the tick interrupts.
56 */
57 static void prvSetupTimerInterrupt( void );
58
59 /*
60 * Call back for the alarm function.
61 */
62 void vPortSysTickHandler( void * context);
63
64 /*-----------------------------------------------------------*/
65
prvReadGp(uint32_t * ulValue)66 static void prvReadGp( uint32_t *ulValue )
67 {
68 asm( "stw gp, (%0)" :: "r"(ulValue) );
69 }
70 /*-----------------------------------------------------------*/
71
72 /*
73 * See header file for description.
74 */
pxPortInitialiseStack(StackType_t * pxTopOfStack,TaskFunction_t pxCode,void * pvParameters)75 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
76 {
77 StackType_t *pxFramePointer = pxTopOfStack - 1;
78 StackType_t xGlobalPointer;
79
80 prvReadGp( &xGlobalPointer );
81
82 /* End of stack marker. */
83 *pxTopOfStack = 0xdeadbeef;
84 pxTopOfStack--;
85
86 *pxTopOfStack = ( StackType_t ) pxFramePointer;
87 pxTopOfStack--;
88
89 *pxTopOfStack = xGlobalPointer;
90
91 /* Space for R23 to R16. */
92 pxTopOfStack -= 9;
93
94 *pxTopOfStack = ( StackType_t ) pxCode;
95 pxTopOfStack--;
96
97 *pxTopOfStack = portINITIAL_ESTATUS;
98
99 /* Space for R15 to R5. */
100 pxTopOfStack -= 12;
101
102 *pxTopOfStack = ( StackType_t ) pvParameters;
103
104 /* Space for R3 to R1, muldiv and RA. */
105 pxTopOfStack -= 5;
106
107 return pxTopOfStack;
108 }
109 /*-----------------------------------------------------------*/
110
111 /*
112 * See header file for description.
113 */
xPortStartScheduler(void)114 BaseType_t xPortStartScheduler( void )
115 {
116 /* Start the timer that generates the tick ISR. Interrupts are disabled
117 here already. */
118 prvSetupTimerInterrupt();
119
120 /* Start the first task. */
121 asm volatile ( " movia r2, restore_sp_from_pxCurrentTCB \n"
122 " jmp r2 " );
123
124 /* Should not get here! */
125 return 0;
126 }
127 /*-----------------------------------------------------------*/
128
vPortEndScheduler(void)129 void vPortEndScheduler( void )
130 {
131 /* It is unlikely that the NIOS2 port will require this function as there
132 is nothing to return to. */
133 }
134 /*-----------------------------------------------------------*/
135
136 /*
137 * Setup the systick timer to generate the tick interrupts at the required
138 * frequency.
139 */
prvSetupTimerInterrupt(void)140 void prvSetupTimerInterrupt( void )
141 {
142 /* Try to register the interrupt handler. */
143 if ( -EINVAL == _alt_ic_isr_register( SYS_CLK_IRQ_INTERRUPT_CONTROLLER_ID, SYS_CLK_IRQ, vPortSysTickHandler, 0x0, 0x0 ) )
144 {
145 /* Failed to install the Interrupt Handler. */
146 asm( "break" );
147 }
148 else
149 {
150 /* Configure SysTick to interrupt at the requested rate. */
151 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );
152 IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );
153 IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );
154 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_CONT_MSK | ALTERA_AVALON_TIMER_CONTROL_START_MSK | ALTERA_AVALON_TIMER_CONTROL_ITO_MSK );
155 }
156
157 /* Clear any already pending interrupts generated by the Timer. */
158 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
159 }
160 /*-----------------------------------------------------------*/
161
vPortSysTickHandler(void * context)162 void vPortSysTickHandler( void * context)
163 {
164 /* Increment the kernel tick. */
165 if( xTaskIncrementTick() != pdFALSE )
166 {
167 vTaskSwitchContext();
168 }
169
170 /* Clear the interrupt. */
171 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
172 }
173 /*-----------------------------------------------------------*/
174
175 /** This function is a re-implementation of the Altera provided function.
176 * The function is re-implemented to prevent it from enabling an interrupt
177 * when it is registered. Interrupts should only be enabled after the FreeRTOS.org
178 * kernel has its scheduler started so that contexts are saved and switched
179 * correctly.
180 */
_alt_ic_isr_register(alt_u32 ic_id,alt_u32 irq,alt_isr_func isr,void * isr_context,void * flags)181 int _alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
182 void *isr_context, void *flags)
183 {
184 int rc = -EINVAL;
185 alt_irq_context status;
186 int id = irq; /* IRQ interpreted as the interrupt ID. */
187
188 if (id < ALT_NIRQ)
189 {
190 /*
191 * interrupts are disabled while the handler tables are updated to ensure
192 * that an interrupt doesn't occur while the tables are in an inconsistant
193 * state.
194 */
195
196 status = alt_irq_disable_all ();
197
198 alt_irq[id].handler = isr;
199 alt_irq[id].context = isr_context;
200
201 rc = (isr) ? alt_ic_irq_enable(ic_id, id) : alt_ic_irq_disable(ic_id, id);
202
203 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */
204 }
205
206 return rc;
207 }
208 /*-----------------------------------------------------------*/
209