2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the NIOS2 port.
\r
68 *----------------------------------------------------------*/
\r
70 /* Standard Includes. */
\r
74 /* Altera includes. */
\r
75 #include "sys/alt_irq.h"
\r
76 #include "altera_avalon_timer_regs.h"
\r
77 #include "priv/alt_irq_table.h"
\r
79 /* Scheduler includes. */
\r
80 #include "FreeRTOS.h"
\r
83 /* Interrupts are enabled. */
\r
84 #define portINITIAL_ESTATUS ( StackType_t ) 0x01
\r
86 /*-----------------------------------------------------------*/
\r
89 * Setup the timer to generate the tick interrupts.
\r
91 static void prvSetupTimerInterrupt( void );
\r
94 * Call back for the alarm function.
\r
96 void vPortSysTickHandler( void * context, alt_u32 id );
\r
98 /*-----------------------------------------------------------*/
\r
100 static void prvReadGp( uint32_t *ulValue )
\r
102 asm( "stw gp, (%0)" :: "r"(ulValue) );
\r
104 /*-----------------------------------------------------------*/
\r
107 * See header file for description.
\r
109 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
111 StackType_t *pxFramePointer = pxTopOfStack - 1;
\r
112 StackType_t xGlobalPointer;
\r
114 prvReadGp( &xGlobalPointer );
\r
116 /* End of stack marker. */
\r
117 *pxTopOfStack = 0xdeadbeef;
\r
120 *pxTopOfStack = ( StackType_t ) pxFramePointer;
\r
123 *pxTopOfStack = xGlobalPointer;
\r
125 /* Space for R23 to R16. */
\r
128 *pxTopOfStack = ( StackType_t ) pxCode;
\r
131 *pxTopOfStack = portINITIAL_ESTATUS;
\r
133 /* Space for R15 to R5. */
\r
134 pxTopOfStack -= 12;
\r
136 *pxTopOfStack = ( StackType_t ) pvParameters;
\r
138 /* Space for R3 to R1, muldiv and RA. */
\r
141 return pxTopOfStack;
\r
143 /*-----------------------------------------------------------*/
\r
146 * See header file for description.
\r
148 BaseType_t xPortStartScheduler( void )
\r
150 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
152 prvSetupTimerInterrupt();
\r
154 /* Start the first task. */
\r
155 asm volatile ( " movia r2, restore_sp_from_pxCurrentTCB \n"
\r
158 /* Should not get here! */
\r
161 /*-----------------------------------------------------------*/
\r
163 void vPortEndScheduler( void )
\r
165 /* It is unlikely that the NIOS2 port will require this function as there
\r
166 is nothing to return to. */
\r
168 /*-----------------------------------------------------------*/
\r
171 * Setup the systick timer to generate the tick interrupts at the required
\r
174 void prvSetupTimerInterrupt( void )
\r
176 /* Try to register the interrupt handler. */
\r
177 if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )
\r
179 /* Failed to install the Interrupt Handler. */
\r
184 /* Configure SysTick to interrupt at the requested rate. */
\r
185 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );
\r
186 IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );
\r
187 IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );
\r
188 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
191 /* Clear any already pending interrupts generated by the Timer. */
\r
192 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
\r
194 /*-----------------------------------------------------------*/
\r
196 void vPortSysTickHandler( void * context, alt_u32 id )
\r
198 /* Increment the kernel tick. */
\r
199 if( xTaskIncrementTick() != pdFALSE )
\r
201 vTaskSwitchContext();
\r
204 /* Clear the interrupt. */
\r
205 IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );
\r
207 /*-----------------------------------------------------------*/
\r
209 /** This function is a re-implementation of the Altera provided function.
\r
210 * The function is re-implemented to prevent it from enabling an interrupt
\r
211 * when it is registered. Interrupts should only be enabled after the FreeRTOS.org
\r
212 * kernel has its scheduler started so that contexts are saved and switched
\r
215 int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )
\r
218 alt_irq_context status;
\r
223 * interrupts are disabled while the handler tables are updated to ensure
\r
224 * that an interrupt doesn't occur while the tables are in an inconsistent
\r
228 status = alt_irq_disable_all ();
\r
230 alt_irq[id].handler = handler;
\r
231 alt_irq[id].context = context;
\r
233 rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);
\r
235 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */
\r
240 /*-----------------------------------------------------------*/
\r