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