2 * FreeRTOS Kernel V10.3.1
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /*-----------------------------------------------------------
29 * Implementation of functions defined in portable.h for the NIOS2 port.
30 *----------------------------------------------------------*/
32 /* Standard Includes. */
36 /* Altera includes. */
37 #include "sys/alt_irq.h"
38 #include "altera_avalon_timer_regs.h"
39 #include "priv/alt_irq_table.h"
41 /* Scheduler includes. */
45 /* Interrupts are enabled. */
46 #define portINITIAL_ESTATUS ( StackType_t ) 0x01
48 /*-----------------------------------------------------------*/
51 * Setup the timer to generate the tick interrupts.
53 static void prvSetupTimerInterrupt( void );
56 * Call back for the alarm function.
58 void vPortSysTickHandler( void * context, alt_u32 id );
60 /*-----------------------------------------------------------*/
62 static void prvReadGp( uint32_t *ulValue )
64 asm( "stw gp, (%0)" :: "r"(ulValue) );
66 /*-----------------------------------------------------------*/
69 * See header file for description.
71 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
73 StackType_t *pxFramePointer = pxTopOfStack - 1;
74 StackType_t xGlobalPointer;
76 prvReadGp( &xGlobalPointer );
78 /* End of stack marker. */
79 *pxTopOfStack = 0xdeadbeef;
82 *pxTopOfStack = ( StackType_t ) pxFramePointer;
85 *pxTopOfStack = xGlobalPointer;
87 /* Space for R23 to R16. */
90 *pxTopOfStack = ( StackType_t ) pxCode;
93 *pxTopOfStack = portINITIAL_ESTATUS;
95 /* Space for R15 to R5. */
98 *pxTopOfStack = ( StackType_t ) pvParameters;
100 /* Space for R3 to R1, muldiv and RA. */
105 /*-----------------------------------------------------------*/
108 * See header file for description.
110 BaseType_t xPortStartScheduler( void )
112 /* Start the timer that generates the tick ISR. Interrupts are disabled
114 prvSetupTimerInterrupt();
116 /* Start the first task. */
117 asm volatile ( " movia r2, restore_sp_from_pxCurrentTCB \n"
120 /* Should not get here! */
123 /*-----------------------------------------------------------*/
125 void vPortEndScheduler( void )
127 /* It is unlikely that the NIOS2 port will require this function as there
128 is nothing to return to. */
130 /*-----------------------------------------------------------*/
133 * Setup the systick timer to generate the tick interrupts at the required
136 void prvSetupTimerInterrupt( void )
138 /* Try to register the interrupt handler. */
139 if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )
141 /* Failed to install the Interrupt Handler. */
146 /* Configure SysTick to interrupt at the requested rate. */
147 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );
148 IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );
149 IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );
150 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 );
153 /* Clear any already pending interrupts generated by the Timer. */
154 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
156 /*-----------------------------------------------------------*/
158 void vPortSysTickHandler( void * context, alt_u32 id )
160 /* Increment the kernel tick. */
161 if( xTaskIncrementTick() != pdFALSE )
163 vTaskSwitchContext();
166 /* Clear the interrupt. */
167 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
169 /*-----------------------------------------------------------*/
171 /** This function is a re-implementation of the Altera provided function.
172 * The function is re-implemented to prevent it from enabling an interrupt
173 * when it is registered. Interrupts should only be enabled after the FreeRTOS.org
174 * kernel has its scheduler started so that contexts are saved and switched
177 int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )
180 alt_irq_context status;
185 * interrupts are disabled while the handler tables are updated to ensure
186 * that an interrupt doesn't occur while the tables are in an inconsistent
190 status = alt_irq_disable_all ();
192 alt_irq[id].handler = handler;
193 alt_irq[id].context = context;
195 rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);
197 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */
202 /*-----------------------------------------------------------*/