]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/ThirdParty/XCC/Xtensa/port.c
Add Xtensa port
[freertos] / FreeRTOS / Source / portable / ThirdParty / XCC / Xtensa / port.c
1 /*\r
2     FreeRTOS V9.0.0 - 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 // Copyright (c) 2003-2015 Cadence Design Systems, Inc.\r
72 //\r
73 // Permission is hereby granted, free of charge, to any person obtaining\r
74 // a copy of this software and associated documentation files (the\r
75 // "Software"), to deal in the Software without restriction, including\r
76 // without limitation the rights to use, copy, modify, merge, publish,\r
77 // distribute, sublicense, and/or sell copies of the Software, and to\r
78 // permit persons to whom the Software is furnished to do so, subject to\r
79 // the following conditions:\r
80 //\r
81 // The above copyright notice and this permission notice shall be included\r
82 // in all copies or substantial portions of the Software.\r
83 //\r
84 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
85 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
86 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\r
87 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\r
88 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\r
89 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\r
90 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
91 --------------------------------------------------------------------------------\r
92 */\r
93 \r
94 #include <stdlib.h>\r
95 #include <xtensa/config/core.h>\r
96 \r
97 #include "xtensa_rtos.h"\r
98 \r
99 #include "FreeRTOS.h"\r
100 #include "task.h"\r
101 \r
102 \r
103 /* Defined in portasm.h */\r
104 extern void _frxt_tick_timer_init(void);\r
105 \r
106 /* Defined in xtensa_context.S */\r
107 extern void _xt_coproc_init(void);\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
111 any details of its type. */\r
112 typedef void TCB_t;\r
113 extern volatile TCB_t * volatile pxCurrentTCB;\r
114 \r
115 unsigned port_xSchedulerRunning = 0; // Duplicate of inaccessible xSchedulerRunning; needed at startup to avoid counting nesting\r
116 unsigned port_interruptNesting = 0;  // Interrupt nesting level\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 // User exception dispatcher when exiting\r
121 void _xt_user_exit(void);\r
122 \r
123 /*\r
124  * Stack initialization\r
125  */\r
126 #if portUSING_MPU_WRAPPERS\r
127 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged )\r
128 #else\r
129 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
130 #endif\r
131 {\r
132         StackType_t *sp, *tp;\r
133         XtExcFrame  *frame;\r
134         #if XCHAL_CP_NUM > 0\r
135         uint32_t *p;\r
136         #endif\r
137 \r
138         /* Create interrupt stack frame aligned to 16 byte boundary */\r
139         sp = (StackType_t *) (((UBaseType_t)(pxTopOfStack + 1) - XT_CP_SIZE - XT_STK_FRMSZ) & ~0xf);\r
140 \r
141         /* Clear the entire frame (do not use memset() because we don't depend on C library) */\r
142         for (tp = sp; tp <= pxTopOfStack; ++tp)\r
143                 *tp = 0;\r
144 \r
145         frame = (XtExcFrame *) sp;\r
146 \r
147         /* Explicitly initialize certain saved registers */\r
148         frame->pc   = (UBaseType_t) pxCode;             /* task entrypoint                */\r
149         frame->a0   = 0;                                /* to terminate GDB backtrace     */\r
150         frame->a1   = (UBaseType_t) sp + XT_STK_FRMSZ;  /* physical top of stack frame    */\r
151         frame->exit = (UBaseType_t) _xt_user_exit;      /* user exception exit dispatcher */\r
152 \r
153         /* Set initial PS to int level 0, EXCM disabled ('rfe' will enable), user mode. */\r
154         /* Also set entry point argument parameter. */\r
155         #ifdef __XTENSA_CALL0_ABI__\r
156         frame->a2 = (UBaseType_t) pvParameters;\r
157         frame->ps = PS_UM | PS_EXCM;\r
158         #else\r
159         /* + for windowed ABI also set WOE and CALLINC (pretend task was 'call4'd). */\r
160         frame->a6 = (UBaseType_t) pvParameters;\r
161         frame->ps = PS_UM | PS_EXCM | PS_WOE | PS_CALLINC(1);\r
162         #endif\r
163 \r
164         #ifdef XT_USE_SWPRI\r
165         /* Set the initial virtual priority mask value to all 1's. */\r
166         frame->vpri = 0xFFFFFFFF;\r
167         #endif\r
168 \r
169         #if XCHAL_CP_NUM > 0\r
170         /* Init the coprocessor save area (see xtensa_context.h) */\r
171         /* No access to TCB here, so derive indirectly. Stack growth is top to bottom.\r
172          * //p = (uint32_t *) xMPUSettings->coproc_area;\r
173          */\r
174         p = (uint32_t *)(((uint32_t) pxTopOfStack - XT_CP_SIZE) & ~0xf);\r
175         p[0] = 0;\r
176         p[1] = 0;\r
177         p[2] = (((uint32_t) p) + 12 + XCHAL_TOTAL_SA_ALIGN - 1) & -XCHAL_TOTAL_SA_ALIGN;\r
178         #endif\r
179 \r
180         return sp;\r
181 }\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vPortEndScheduler( void )\r
186 {\r
187         /* It is unlikely that the Xtensa port will get stopped.  If required simply\r
188         disable the tick interrupt here. */\r
189 }\r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 BaseType_t xPortStartScheduler( void )\r
194 {\r
195         // Interrupts are disabled at this point and stack contains PS with enabled interrupts when task context is restored\r
196 \r
197         #if XCHAL_CP_NUM > 0\r
198         /* Initialize co-processor management for tasks. Leave CPENABLE alone. */\r
199         _xt_coproc_init();\r
200         #endif\r
201 \r
202         /* Init the tick divisor value */\r
203         _xt_tick_divisor_init();\r
204 \r
205         /* Setup the hardware to generate the tick. */\r
206         _frxt_tick_timer_init();\r
207 \r
208         #if XT_USE_THREAD_SAFE_CLIB\r
209         // Init C library\r
210         vPortClibInit();\r
211         #endif\r
212 \r
213         port_xSchedulerRunning = 1;\r
214 \r
215         // Cannot be directly called from C; never returns\r
216         __asm__ volatile ("call0    _frxt_dispatch\n");\r
217 \r
218         /* Should not get here. */\r
219         return pdTRUE;\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 BaseType_t xPortSysTickHandler( void )\r
224 {\r
225         BaseType_t ret;\r
226         unsigned interruptMask;\r
227 \r
228         portbenchmarkIntLatency();\r
229 \r
230         /* Interrupts upto configMAX_SYSCALL_INTERRUPT_PRIORITY must be\r
231          * disabled before calling xTaskIncrementTick as it access the\r
232          * kernel lists. */\r
233         interruptMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
234         {\r
235                 ret = xTaskIncrementTick();\r
236         }\r
237         portCLEAR_INTERRUPT_MASK_FROM_ISR( interruptMask );\r
238 \r
239         portYIELD_FROM_ISR( ret );\r
240 \r
241         return ret;\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 /*\r
246  * Used to set coprocessor area in stack. Current hack is to reuse MPU pointer for coprocessor area.\r
247  */\r
248 #if portUSING_MPU_WRAPPERS\r
249 void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth )\r
250 {\r
251         #if XCHAL_CP_NUM > 0\r
252         xMPUSettings->coproc_area = (StackType_t*)((((uint32_t)(pxBottomOfStack + ulStackDepth - 1)) - XT_CP_SIZE ) & ~0xf);\r
253 \r
254 \r
255         /* NOTE: we cannot initialize the coprocessor save area here because FreeRTOS is going to\r
256          * clear the stack area after we return. This is done in pxPortInitialiseStack().\r
257          */\r
258         #endif\r
259 }\r
260 #endif\r
261 /*-----------------------------------------------------------*/\r