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