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