]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/NiosII/port.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Source / portable / GCC / NiosII / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*-----------------------------------------------------------\r
70  * Implementation of functions defined in portable.h for the NIOS2 port.\r
71  *----------------------------------------------------------*/\r
72 \r
73 /* Standard Includes. */\r
74 #include <string.h>\r
75 #include <errno.h>\r
76 \r
77 /* Altera includes. */\r
78 #include "sys/alt_irq.h"\r
79 #include "altera_avalon_timer_regs.h"\r
80 #include "priv/alt_irq_table.h"\r
81 \r
82 /* Scheduler includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Interrupts are enabled. */\r
87 #define portINITIAL_ESTATUS     ( portSTACK_TYPE ) 0x01 \r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* \r
92  * Setup the timer to generate the tick interrupts.\r
93  */\r
94 static void prvSetupTimerInterrupt( void );\r
95 \r
96 /*\r
97  * Call back for the alarm function.\r
98  */\r
99 void vPortSysTickHandler( void * context, alt_u32 id );\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 static void prvReadGp( unsigned long *ulValue )\r
104 {\r
105         asm( "stw gp, (%0)" :: "r"(ulValue) );\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* \r
110  * See header file for description. \r
111  */\r
112 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
113 {    \r
114 portSTACK_TYPE *pxFramePointer = pxTopOfStack - 1;\r
115 portSTACK_TYPE xGlobalPointer;\r
116 \r
117     prvReadGp( &xGlobalPointer ); \r
118 \r
119     /* End of stack marker. */\r
120     *pxTopOfStack = 0xdeadbeef;\r
121     pxTopOfStack--;\r
122     \r
123     *pxTopOfStack = ( portSTACK_TYPE ) pxFramePointer; \r
124     pxTopOfStack--;\r
125     \r
126     *pxTopOfStack = xGlobalPointer; \r
127     \r
128     /* Space for R23 to R16. */\r
129     pxTopOfStack -= 9;\r
130 \r
131     *pxTopOfStack = ( portSTACK_TYPE ) pxCode; \r
132     pxTopOfStack--;\r
133 \r
134     *pxTopOfStack = portINITIAL_ESTATUS; \r
135 \r
136     /* Space for R15 to R5. */    \r
137     pxTopOfStack -= 12;\r
138     \r
139     *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; \r
140 \r
141     /* Space for R3 to R1, muldiv and RA. */\r
142     pxTopOfStack -= 5;\r
143     \r
144     return pxTopOfStack;\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 /* \r
149  * See header file for description. \r
150  */\r
151 portBASE_TYPE xPortStartScheduler( void )\r
152 {\r
153         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
154         here already. */\r
155         prvSetupTimerInterrupt();\r
156         \r
157         /* Start the first task. */\r
158     asm volatile (  " movia r2, restore_sp_from_pxCurrentTCB        \n"\r
159                     " jmp r2                                          " );\r
160 \r
161         /* Should not get here! */\r
162         return 0;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vPortEndScheduler( void )\r
167 {\r
168         /* It is unlikely that the NIOS2 port will require this function as there\r
169         is nothing to return to.  */\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /*\r
174  * Setup the systick timer to generate the tick interrupts at the required\r
175  * frequency.\r
176  */\r
177 void prvSetupTimerInterrupt( void )\r
178 {\r
179         /* Try to register the interrupt handler. */\r
180         if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )\r
181         { \r
182                 /* Failed to install the Interrupt Handler. */\r
183                 asm( "break" );\r
184         }\r
185         else\r
186         {\r
187                 /* Configure SysTick to interrupt at the requested rate. */\r
188                 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );\r
189                 IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );\r
190                 IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );\r
191                 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
192         } \r
193 \r
194         /* Clear any already pending interrupts generated by the Timer. */\r
195         IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vPortSysTickHandler( void * context, alt_u32 id )\r
200 {\r
201         /* Increment the Kernel Tick. */\r
202         vTaskIncrementTick();\r
203 \r
204         /* If using preemption, also force a context switch. */\r
205         #if configUSE_PREEMPTION == 1\r
206         vTaskSwitchContext();\r
207         #endif\r
208 \r
209         /* Clear the interrupt. */\r
210         IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 /** This function is a re-implementation of the Altera provided function.\r
215  * The function is re-implemented to prevent it from enabling an interrupt\r
216  * when it is registered. Interrupts should only be enabled after the FreeRTOS.org\r
217  * kernel has its scheduler started so that contexts are saved and switched \r
218  * correctly.\r
219  */\r
220 int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )\r
221 {\r
222         int rc = -EINVAL;  \r
223         alt_irq_context status;\r
224 \r
225         if (id < ALT_NIRQ)\r
226         {\r
227                 /* \r
228                  * interrupts are disabled while the handler tables are updated to ensure\r
229                  * that an interrupt doesn't occur while the tables are in an inconsistent\r
230                  * state.\r
231                  */\r
232         \r
233                 status = alt_irq_disable_all ();\r
234         \r
235                 alt_irq[id].handler = handler;\r
236                 alt_irq[id].context = context;\r
237         \r
238                 rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);\r
239         \r
240                 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */\r
241         }\r
242     \r
243         return rc; \r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r