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