2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*-----------------------------------------------------------
30 * Implementation of functions defined in portable.h for the NIOS2 port.
31 *----------------------------------------------------------*/
33 /* Standard Includes. */
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"
43 /* Scheduler includes. */
47 /* Interrupts are enabled. */
48 #define portINITIAL_ESTATUS ( StackType_t ) 0x01
50 int _alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
51 void *isr_context, void *flags);
52 /*-----------------------------------------------------------*/
55 * Setup the timer to generate the tick interrupts.
57 static void prvSetupTimerInterrupt( void );
60 * Call back for the alarm function.
62 void vPortSysTickHandler( void * context);
64 /*-----------------------------------------------------------*/
66 static void prvReadGp( uint32_t *ulValue )
68 asm( "stw gp, (%0)" :: "r"(ulValue) );
70 /*-----------------------------------------------------------*/
73 * See header file for description.
75 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
77 StackType_t *pxFramePointer = pxTopOfStack - 1;
78 StackType_t xGlobalPointer;
80 prvReadGp( &xGlobalPointer );
82 /* End of stack marker. */
83 *pxTopOfStack = 0xdeadbeef;
86 *pxTopOfStack = ( StackType_t ) pxFramePointer;
89 *pxTopOfStack = xGlobalPointer;
91 /* Space for R23 to R16. */
94 *pxTopOfStack = ( StackType_t ) pxCode;
97 *pxTopOfStack = portINITIAL_ESTATUS;
99 /* Space for R15 to R5. */
102 *pxTopOfStack = ( StackType_t ) pvParameters;
104 /* Space for R3 to R1, muldiv and RA. */
109 /*-----------------------------------------------------------*/
112 * See header file for description.
114 BaseType_t xPortStartScheduler( void )
116 /* Start the timer that generates the tick ISR. Interrupts are disabled
118 prvSetupTimerInterrupt();
120 /* Start the first task. */
121 asm volatile ( " movia r2, restore_sp_from_pxCurrentTCB \n"
124 /* Should not get here! */
127 /*-----------------------------------------------------------*/
129 void vPortEndScheduler( void )
131 /* It is unlikely that the NIOS2 port will require this function as there
132 is nothing to return to. */
134 /*-----------------------------------------------------------*/
137 * Setup the systick timer to generate the tick interrupts at the required
140 void prvSetupTimerInterrupt( void )
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 ) )
145 /* Failed to install the Interrupt Handler. */
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 );
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 );
160 /*-----------------------------------------------------------*/
162 void vPortSysTickHandler( void * context)
164 /* Increment the kernel tick. */
165 if( xTaskIncrementTick() != pdFALSE )
167 vTaskSwitchContext();
170 /* Clear the interrupt. */
171 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
173 /*-----------------------------------------------------------*/
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
181 int _alt_ic_isr_register(alt_u32 ic_id, alt_u32 irq, alt_isr_func isr,
182 void *isr_context, void *flags)
185 alt_irq_context status;
186 int id = irq; /* IRQ interpreted as the interrupt ID. */
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
196 status = alt_irq_disable_all ();
198 alt_irq[id].handler = isr;
199 alt_irq[id].context = isr_context;
201 rc = (isr) ? alt_ic_irq_enable(ic_id, id) : alt_ic_irq_disable(ic_id, id);
203 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */
208 /*-----------------------------------------------------------*/