]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/NiosII/port.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Source / portable / GCC / NiosII / port.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*-----------------------------------------------------------\r
67  * Implementation of functions defined in portable.h for the NIOS2 port.\r
68  *----------------------------------------------------------*/\r
69 \r
70 /* Standard Includes. */\r
71 #include <string.h>\r
72 #include <errno.h>\r
73 \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
78 \r
79 /* Scheduler includes. */\r
80 #include "FreeRTOS.h"\r
81 #include "task.h"\r
82 \r
83 /* Interrupts are enabled. */\r
84 #define portINITIAL_ESTATUS     ( StackType_t ) 0x01 \r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* \r
89  * Setup the timer to generate the tick interrupts.\r
90  */\r
91 static void prvSetupTimerInterrupt( void );\r
92 \r
93 /*\r
94  * Call back for the alarm function.\r
95  */\r
96 void vPortSysTickHandler( void * context, alt_u32 id );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 static void prvReadGp( uint32_t *ulValue )\r
101 {\r
102         asm( "stw gp, (%0)" :: "r"(ulValue) );\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /* \r
107  * See header file for description. \r
108  */\r
109 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
110 {    \r
111 StackType_t *pxFramePointer = pxTopOfStack - 1;\r
112 StackType_t xGlobalPointer;\r
113 \r
114     prvReadGp( &xGlobalPointer ); \r
115 \r
116     /* End of stack marker. */\r
117     *pxTopOfStack = 0xdeadbeef;\r
118     pxTopOfStack--;\r
119     \r
120     *pxTopOfStack = ( StackType_t ) pxFramePointer; \r
121     pxTopOfStack--;\r
122     \r
123     *pxTopOfStack = xGlobalPointer; \r
124     \r
125     /* Space for R23 to R16. */\r
126     pxTopOfStack -= 9;\r
127 \r
128     *pxTopOfStack = ( StackType_t ) pxCode; \r
129     pxTopOfStack--;\r
130 \r
131     *pxTopOfStack = portINITIAL_ESTATUS; \r
132 \r
133     /* Space for R15 to R5. */    \r
134     pxTopOfStack -= 12;\r
135     \r
136     *pxTopOfStack = ( StackType_t ) pvParameters; \r
137 \r
138     /* Space for R3 to R1, muldiv and RA. */\r
139     pxTopOfStack -= 5;\r
140     \r
141     return pxTopOfStack;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* \r
146  * See header file for description. \r
147  */\r
148 BaseType_t xPortStartScheduler( void )\r
149 {\r
150         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
151         here already. */\r
152         prvSetupTimerInterrupt();\r
153         \r
154         /* Start the first task. */\r
155     asm volatile (  " movia r2, restore_sp_from_pxCurrentTCB        \n"\r
156                     " jmp r2                                          " );\r
157 \r
158         /* Should not get here! */\r
159         return 0;\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 void vPortEndScheduler( void )\r
164 {\r
165         /* It is unlikely that the NIOS2 port will require this function as there\r
166         is nothing to return to.  */\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 /*\r
171  * Setup the systick timer to generate the tick interrupts at the required\r
172  * frequency.\r
173  */\r
174 void prvSetupTimerInterrupt( void )\r
175 {\r
176         /* Try to register the interrupt handler. */\r
177         if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )\r
178         { \r
179                 /* Failed to install the Interrupt Handler. */\r
180                 asm( "break" );\r
181         }\r
182         else\r
183         {\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
189         } \r
190 \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
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void vPortSysTickHandler( void * context, alt_u32 id )\r
197 {\r
198         /* Increment the kernel tick. */\r
199         if( xTaskIncrementTick() != pdFALSE )\r
200         {\r
201         vTaskSwitchContext();\r
202         }\r
203                 \r
204         /* Clear the interrupt. */\r
205         IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \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
213  * correctly.\r
214  */\r
215 int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )\r
216 {\r
217         int rc = -EINVAL;  \r
218         alt_irq_context status;\r
219 \r
220         if (id < ALT_NIRQ)\r
221         {\r
222                 /* \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
225                  * state.\r
226                  */\r
227         \r
228                 status = alt_irq_disable_all ();\r
229         \r
230                 alt_irq[id].handler = handler;\r
231                 alt_irq[id].context = context;\r
232         \r
233                 rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);\r
234         \r
235                 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */\r
236         }\r
237     \r
238         return rc; \r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r