]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/Softune/MB91460/port.c
Update version number.
[freertos] / FreeRTOS / Source / portable / Softune / MB91460 / port.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 #include "FreeRTOS.h"\r
66 #include "task.h"\r
67 #include "mb91467d.h"\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
72 any details of its type. */\r
73 typedef void tskTCB;\r
74 extern volatile tskTCB * volatile pxCurrentTCB;\r
75 \r
76 /*-----------------------------------------------------------*/\r
77  \r
78 #pragma asm\r
79 #macro  SaveContext\r
80          ORCCR #0x20                                                            ;Switch to user stack\r
81          ST RP,@-R15                                                            ;Store RP\r
82          STM0 (R7,R6,R5,R4,R3,R2,R1,R0)                         ;Store R7-R0\r
83          STM1 (R14,R13,R12,R11,R10,R9,R8)                       ;Store R14-R8\r
84          ST MDH, @-R15                                                          ;Store MDH\r
85          ST MDL, @-R15                                                          ;Store MDL\r
86          \r
87          ANDCCR #0xDF                                                           ;Switch back to system stack\r
88          LD @R15+,R0                                                            ;Store PC to R0 \r
89          ORCCR #0x20                                                            ;Switch to user stack\r
90          ST R0,@-R15                                                            ;Store PC to User stack\r
91          \r
92          ANDCCR #0xDF                                                           ;Switch back to system stack\r
93          LD @R15+,R0                                                            ;Store PS to R0\r
94          ORCCR #0x20                                                            ;Switch to user stack\r
95          ST R0,@-R15                                                            ;Store PS to User stack\r
96          \r
97          LDI #_pxCurrentTCB, R0                                         ;Get pxCurrentTCB address\r
98          LD @R0, R0                                                                     ;Get the pxCurrentTCB->pxTopOfStack address\r
99          ST R15,@R0                                                                     ;Store USP to pxCurrentTCB->pxTopOfStack\r
100          \r
101          ANDCCR #0xDF                                                           ;Switch back to system stack for the rest of tick ISR\r
102 #endm\r
103 \r
104 #macro RestoreContext\r
105          LDI #_pxCurrentTCB, R0                                         ;Get pxCurrentTCB address\r
106          LD @R0, R0                                                                     ;Get the pxCurrentTCB->pxTopOfStack address\r
107          ORCCR #0x20                                                            ;Switch to user stack\r
108          LD @R0, R15                                                            ;Restore USP from pxCurrentTCB->pxTopOfStack\r
109 \r
110          LD @R15+,R0                                                            ;Store PS to R0\r
111          ANDCCR #0xDF                                                           ;Switch to system stack\r
112          ST R0,@-R15                                                            ;Store PS to system stack\r
113 \r
114          ORCCR #0x20                                                            ;Switch to user stack\r
115          LD @R15+,R0                                                            ;Store PC to R0\r
116          ANDCCR #0xDF                                                           ;Switch to system stack\r
117          ST R0,@-R15                                                            ;Store PC to system stack\r
118 \r
119          ORCCR #0x20                                                            ;Switch back to retrieve the remaining context\r
120 \r
121          LD @R15+, MDL                                                          ;Restore MDL\r
122          LD @R15+, MDH                                                          ;Restore MDH\r
123          LDM1 (R14,R13,R12,R11,R10,R9,R8)                       ;Restore R14-R8\r
124          LDM0 (R7,R6,R5,R4,R3,R2,R1,R0)                         ;Restore R7-R0\r
125          LD @R15+, RP                                                           ;Restore RP\r
126          \r
127          ANDCCR #0xDF                                                           ;Switch back to system stack for the rest of tick ISR\r
128 #endm\r
129 #pragma endasm\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * Perform hardware setup to enable ticks from timer 1,\r
135  */\r
136 static void prvSetupTimerInterrupt( void );\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* \r
140  * Initialise the stack of a task to look exactly as if a call to \r
141  * portSAVE_CONTEXT had been called.\r
142  * \r
143  * See the header file portable.h.\r
144  */\r
145 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
146 {\r
147         /* Place a few bytes of known values on the bottom of the stack. \r
148         This is just useful for debugging. */\r
149 \r
150         *pxTopOfStack = 0x11111111;\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = 0x22222222;\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x33333333;\r
155         pxTopOfStack--;\r
156 \r
157         /* This is a redundant push to the stack, it may be required if \r
158         in some implementations of the compiler the parameter to the task \r
159         is passed on to the stack rather than in R4 register. */\r
160         *pxTopOfStack = (portSTACK_TYPE)(pvParameters);\r
161         pxTopOfStack--;                  \r
162     \r
163         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;  /* RP */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = ( portSTACK_TYPE ) 0x00007777;  /* R7 */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = ( portSTACK_TYPE ) 0x00006666;  /* R6 */\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = ( portSTACK_TYPE ) 0x00005555;  /* R5 */\r
170         pxTopOfStack--;\r
171         \r
172         /* In the current implementation of the compiler the first \r
173         parameter to the task (or function) is passed via R4 parameter \r
174         to the task, hence the pvParameters pointer is copied into the R4 \r
175         register. See compiler manual section 4.6.2 for more information. */\r
176         *pxTopOfStack = ( portSTACK_TYPE ) (pvParameters);      /* R4 */\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = ( portSTACK_TYPE ) 0x00003333;  /* R3 */\r
179         pxTopOfStack--;\r
180         *pxTopOfStack = ( portSTACK_TYPE ) 0x00002222;  /* R2 */\r
181         pxTopOfStack--;\r
182         *pxTopOfStack = ( portSTACK_TYPE ) 0x00001111;  /* R1 */\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000001;  /* R0 */\r
185         pxTopOfStack--; \r
186         *pxTopOfStack = ( portSTACK_TYPE ) 0x0000EEEE;  /* R14 */\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = ( portSTACK_TYPE ) 0x0000DDDD;  /* R13 */\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = ( portSTACK_TYPE ) 0x0000CCCC;  /* R12 */\r
191         pxTopOfStack--;\r
192         *pxTopOfStack = ( portSTACK_TYPE ) 0x0000BBBB;  /* R11 */\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = ( portSTACK_TYPE ) 0x0000AAAA;  /* R10 */\r
195         pxTopOfStack--;\r
196         *pxTopOfStack = ( portSTACK_TYPE ) 0x00009999;  /* R9 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = ( portSTACK_TYPE ) 0x00008888;  /* R8 */\r
199         pxTopOfStack--; \r
200         *pxTopOfStack = ( portSTACK_TYPE ) 0x11110000;  /* MDH */\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = ( portSTACK_TYPE ) 0x22220000;  /* MDL */\r
203         pxTopOfStack--;\r
204 \r
205         /* The start of the task code. */\r
206         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
207         pxTopOfStack--;\r
208          \r
209     /* PS - User Mode, USP, ILM=31, Interrupts enabled */\r
210         *pxTopOfStack = ( portSTACK_TYPE ) 0x001F0030;  /* PS */\r
211 \r
212         return pxTopOfStack;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 portBASE_TYPE xPortStartScheduler( void )\r
217 {\r
218         /* Setup the hardware to generate the tick. */\r
219         prvSetupTimerInterrupt();\r
220 \r
221         /* Restore the context of the first task that is going to run. */\r
222         #pragma asm\r
223                 RestoreContext\r
224         #pragma endasm\r
225 \r
226         /* Simulate a function call end as generated by the compiler.  We will now\r
227         jump to the start of the task the context of which we have just restored. */    \r
228         __asm(" reti ");\r
229 \r
230         /* Should not get here. */\r
231         return pdFAIL;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vPortEndScheduler( void )\r
236 {\r
237         /* Not implemented - unlikely to ever be required as there is nothing to\r
238         return to. */\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 static void prvSetupTimerInterrupt( void )\r
243 {\r
244 /* The peripheral clock divided by 32 is used by the timer. */\r
245 const unsigned short usReloadValue = ( unsigned short ) ( ( ( configPER_CLOCK_HZ / configTICK_RATE_HZ ) / 32UL ) - 1UL );\r
246 \r
247         /* Setup RLT0 to generate a tick interrupt. */\r
248 \r
249         TMCSR0_CNTE = 0;                /* Count Disable */\r
250     TMCSR0_CSL = 0x2;           /* CLKP/32 */\r
251     TMCSR0_MOD = 0;                     /* Software trigger */\r
252     TMCSR0_RELD = 1;            /* Reload */\r
253     \r
254     TMCSR0_UF = 0;                      /* Clear underflow flag */\r
255         TMRLR0 = usReloadValue;\r
256         TMCSR0_INTE = 1;                /* Interrupt Enable */\r
257         TMCSR0_CNTE = 1;                /* Count Enable */\r
258         TMCSR0_TRG = 1;                 /* Trigger */\r
259         \r
260     PORTEN = 0x3;                       /* Port Enable */\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 #if configUSE_PREEMPTION == 1\r
265 \r
266         /* \r
267          * Tick ISR for preemptive scheduler. The tick count is incremented \r
268          * after the context is saved. Then the context is switched if required,\r
269          * and last the context of the task which is to be resumed is restored.\r
270          */\r
271 \r
272         #pragma asm\r
273 \r
274         .global _ReloadTimer0_IRQHandler\r
275         _ReloadTimer0_IRQHandler:\r
276 \r
277         ANDCCR #0xEF                                                    ;Disable Interrupts\r
278         SaveContext                                                             ;Save context\r
279         ORCCR #0x10                                                             ;Re-enable Interrupts\r
280 \r
281         LDI #0xFFFB,R1\r
282         LDI #_tmcsr0, R0\r
283         AND R1,@R0                                                              ;Clear RLT0 interrupt flag\r
284 \r
285         CALL32   _xTaskIncrementTick,R12                ;Increment Tick\r
286         CALL32   _vTaskSwitchContext,R12                ;Switch context if required\r
287 \r
288         ANDCCR #0xEF                                                    ;Disable Interrupts\r
289         RestoreContext                                                  ;Restore context\r
290         ORCCR #0x10                                                             ;Re-enable Interrupts\r
291 \r
292         RETI\r
293 \r
294         #pragma endasm\r
295         \r
296 #else\r
297         \r
298         /* \r
299          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
300          * tick count.  We don't need to switch context, this can only be done by\r
301          * manual calls to taskYIELD();\r
302          */\r
303         __interrupt void ReloadTimer0_IRQHandler( void )\r
304         {\r
305                 /* Clear RLT0 interrupt flag */\r
306                 TMCSR0_UF = 0; \r
307                 xTaskIncrementTick();\r
308         }\r
309 \r
310 #endif\r
311 \r
312 /*\r
313  * Manual context switch. We can use a __nosavereg attribute  as the context \r
314  * would be saved by PortSAVE_CONTEXT().  The context is switched and then \r
315  * the context of the new task is restored saved. \r
316  */\r
317 #pragma asm\r
318 \r
319         .global _vPortYieldDelayed\r
320         _vPortYieldDelayed:\r
321 \r
322         ANDCCR #0xEF                                                    ;Disable Interrupts\r
323         SaveContext                                                             ;Save context\r
324         ORCCR #0x10                                                             ;Re-enable Interrupts\r
325 \r
326         LDI #_dicr, R0\r
327         BANDL #0x0E, @R0                                                ;Clear Delayed interrupt flag\r
328 \r
329         CALL32   _vTaskSwitchContext,R12                ;Switch context if required\r
330 \r
331         ANDCCR #0xEF                                                    ;Disable Interrupts\r
332         RestoreContext                                                  ;Restore context\r
333         ORCCR #0x10                                                             ;Re-enable Interrupts\r
334 \r
335         RETI\r
336 \r
337 #pragma endasm\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 /*\r
341  * Manual context switch. We can use a __nosavereg attribute  as the context \r
342  * would be saved by PortSAVE_CONTEXT().  The context is switched and then \r
343  * the context of the new task is restored saved. \r
344  */      \r
345 #pragma asm\r
346 \r
347         .global _vPortYield\r
348         _vPortYield:\r
349 \r
350         SaveContext                                                             ;Save context\r
351         CALL32   _vTaskSwitchContext,R12                ;Switch context if required\r
352         RestoreContext                                                  ;Restore context\r
353         \r
354         RETI\r
355 \r
356 #pragma endasm\r
357 /*-----------------------------------------------------------*/\r
358 \r