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