]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/NiosII/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / GCC / NiosII / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the NIOS2 port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* Standard Includes. */\r
33 #include <string.h>\r
34 #include <errno.h>\r
35 \r
36 /* Altera includes. */\r
37 #include "sys/alt_irq.h"\r
38 #include "altera_avalon_timer_regs.h"\r
39 #include "priv/alt_irq_table.h"\r
40 \r
41 /* Scheduler includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 \r
45 /* Interrupts are enabled. */\r
46 #define portINITIAL_ESTATUS     ( StackType_t ) 0x01 \r
47 \r
48 /*-----------------------------------------------------------*/\r
49 \r
50 /* \r
51  * Setup the timer to generate the tick interrupts.\r
52  */\r
53 static void prvSetupTimerInterrupt( void );\r
54 \r
55 /*\r
56  * Call back for the alarm function.\r
57  */\r
58 void vPortSysTickHandler( void * context, alt_u32 id );\r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 static void prvReadGp( uint32_t *ulValue )\r
63 {\r
64         asm( "stw gp, (%0)" :: "r"(ulValue) );\r
65 }\r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /* \r
69  * See header file for description. \r
70  */\r
71 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
72 {    \r
73 StackType_t *pxFramePointer = pxTopOfStack - 1;\r
74 StackType_t xGlobalPointer;\r
75 \r
76     prvReadGp( &xGlobalPointer ); \r
77 \r
78     /* End of stack marker. */\r
79     *pxTopOfStack = 0xdeadbeef;\r
80     pxTopOfStack--;\r
81     \r
82     *pxTopOfStack = ( StackType_t ) pxFramePointer; \r
83     pxTopOfStack--;\r
84     \r
85     *pxTopOfStack = xGlobalPointer; \r
86     \r
87     /* Space for R23 to R16. */\r
88     pxTopOfStack -= 9;\r
89 \r
90     *pxTopOfStack = ( StackType_t ) pxCode; \r
91     pxTopOfStack--;\r
92 \r
93     *pxTopOfStack = portINITIAL_ESTATUS; \r
94 \r
95     /* Space for R15 to R5. */    \r
96     pxTopOfStack -= 12;\r
97     \r
98     *pxTopOfStack = ( StackType_t ) pvParameters; \r
99 \r
100     /* Space for R3 to R1, muldiv and RA. */\r
101     pxTopOfStack -= 5;\r
102     \r
103     return pxTopOfStack;\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* \r
108  * See header file for description. \r
109  */\r
110 BaseType_t xPortStartScheduler( void )\r
111 {\r
112         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
113         here already. */\r
114         prvSetupTimerInterrupt();\r
115         \r
116         /* Start the first task. */\r
117     asm volatile (  " movia r2, restore_sp_from_pxCurrentTCB        \n"\r
118                     " jmp r2                                          " );\r
119 \r
120         /* Should not get here! */\r
121         return 0;\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 void vPortEndScheduler( void )\r
126 {\r
127         /* It is unlikely that the NIOS2 port will require this function as there\r
128         is nothing to return to.  */\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /*\r
133  * Setup the systick timer to generate the tick interrupts at the required\r
134  * frequency.\r
135  */\r
136 void prvSetupTimerInterrupt( void )\r
137 {\r
138         /* Try to register the interrupt handler. */\r
139         if ( -EINVAL == alt_irq_register( SYS_CLK_IRQ, 0x0, vPortSysTickHandler ) )\r
140         { \r
141                 /* Failed to install the Interrupt Handler. */\r
142                 asm( "break" );\r
143         }\r
144         else\r
145         {\r
146                 /* Configure SysTick to interrupt at the requested rate. */\r
147                 IOWR_ALTERA_AVALON_TIMER_CONTROL( SYS_CLK_BASE, ALTERA_AVALON_TIMER_CONTROL_STOP_MSK );\r
148                 IOWR_ALTERA_AVALON_TIMER_PERIODL( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) & 0xFFFF );\r
149                 IOWR_ALTERA_AVALON_TIMER_PERIODH( SYS_CLK_BASE, ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) >> 16 );\r
150                 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
151         } \r
152 \r
153         /* Clear any already pending interrupts generated by the Timer. */\r
154         IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void vPortSysTickHandler( void * context, alt_u32 id )\r
159 {\r
160         /* Increment the kernel tick. */\r
161         if( xTaskIncrementTick() != pdFALSE )\r
162         {\r
163         vTaskSwitchContext();\r
164         }\r
165                 \r
166         /* Clear the interrupt. */\r
167         IOWR_ALTERA_AVALON_TIMER_STATUS( SYS_CLK_BASE, ~ALTERA_AVALON_TIMER_STATUS_TO_MSK );\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /** This function is a re-implementation of the Altera provided function.\r
172  * The function is re-implemented to prevent it from enabling an interrupt\r
173  * when it is registered. Interrupts should only be enabled after the FreeRTOS.org\r
174  * kernel has its scheduler started so that contexts are saved and switched \r
175  * correctly.\r
176  */\r
177 int alt_irq_register( alt_u32 id, void* context, void (*handler)(void*, alt_u32) )\r
178 {\r
179         int rc = -EINVAL;  \r
180         alt_irq_context status;\r
181 \r
182         if (id < ALT_NIRQ)\r
183         {\r
184                 /* \r
185                  * interrupts are disabled while the handler tables are updated to ensure\r
186                  * that an interrupt doesn't occur while the tables are in an inconsistent\r
187                  * state.\r
188                  */\r
189         \r
190                 status = alt_irq_disable_all ();\r
191         \r
192                 alt_irq[id].handler = handler;\r
193                 alt_irq[id].context = context;\r
194         \r
195                 rc = (handler) ? alt_irq_enable (id): alt_irq_disable (id);\r
196         \r
197                 /* alt_irq_enable_all(status); This line is removed to prevent the interrupt from being immediately enabled. */\r
198         }\r
199     \r
200         return rc; \r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r