]> begriffs open source - freertos/blob - Source/portable/SDCC/Cygnal/port.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Source / portable / SDCC / Cygnal / port.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*-----------------------------------------------------------\r
49  * Implementation of functions defined in portable.h for the Cygnal port.\r
50  *----------------------------------------------------------*/\r
51 \r
52 /* Standard includes. */\r
53 #include <string.h>\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "task.h"\r
58 \r
59 /* Constants required to setup timer 2 to produce the RTOS tick. */\r
60 #define portCLOCK_DIVISOR                               ( ( unsigned portLONG ) 12 )\r
61 #define portMAX_TIMER_VALUE                             ( ( unsigned portLONG ) 0xffff )\r
62 #define portENABLE_TIMER                                ( ( unsigned portCHAR ) 0x04 )\r
63 #define portTIMER_2_INTERRUPT_ENABLE    ( ( unsigned portCHAR ) 0x20 )\r
64 \r
65 /* The value used in the IE register when a task first starts. */\r
66 #define portGLOBAL_INTERRUPT_BIT        ( ( portSTACK_TYPE ) 0x80 )\r
67 \r
68 /* The value used in the PSW register when a task first starts. */\r
69 #define portINITIAL_PSW                         ( ( portSTACK_TYPE ) 0x00 )\r
70 \r
71 /* Macro to clear the timer 2 interrupt flag. */\r
72 #define portCLEAR_INTERRUPT_FLAG()      TMR2CN &= ~0x80;\r
73 \r
74 /* Used during a context switch to store the size of the stack being copied\r
75 to or from XRAM. */\r
76 data static unsigned portCHAR ucStackBytes;\r
77 \r
78 /* Used during a context switch to point to the next byte in XRAM from/to which\r
79 a RAM byte is to be copied. */\r
80 xdata static portSTACK_TYPE * data pxXRAMStack;\r
81 \r
82 /* Used during a context switch to point to the next byte in RAM from/to which\r
83 an XRAM byte is to be copied. */\r
84 data static portSTACK_TYPE * data pxRAMStack;\r
85 \r
86 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
87 any details of its type. */\r
88 typedef void tskTCB;\r
89 extern volatile tskTCB * volatile pxCurrentTCB;\r
90 \r
91 /*\r
92  * Setup the hardware to generate an interrupt off timer 2 at the required \r
93  * frequency.\r
94  */\r
95 static void prvSetupTimerInterrupt( void );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 /*\r
99  * Macro that copies the current stack from internal RAM to XRAM.  This is \r
100  * required as the 8051 only contains enough internal RAM for a single stack, \r
101  * but we have a stack for every task.\r
102  */\r
103 #define portCOPY_STACK_TO_XRAM()                                                                                                                                \\r
104 {                                                                                                                                                                                               \\r
105         /* pxCurrentTCB points to a TCB which itself points to the location into                                        \\r
106         which the first stack byte should be copied.  Set pxXRAMStack to point                                          \\r
107         to the location into which the first stack byte is to be copied. */                                                     \\r
108         pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB );         \\r
109                                                                                                                                                                                                 \\r
110         /* Set pxRAMStack to point to the first byte to be coped from the stack. */                                     \\r
111         pxRAMStack = ( data portSTACK_TYPE * data ) configSTACK_START;                                                          \\r
112                                                                                                                                                                                                 \\r
113         /* Calculate the size of the stack we are about to copy from the current                                        \\r
114         stack pointer value. */                                                                                                                                         \\r
115         ucStackBytes = SP - ( configSTACK_START - 1 );                                                                                          \\r
116                                                                                                                                                                                                 \\r
117         /* Before starting to copy the stack, store the calculated stack size so                                        \\r
118         the stack can be restored when the task is resumed. */                                                                          \\r
119         *pxXRAMStack = ucStackBytes;                                                                                                                            \\r
120                                                                                                                                                                                                 \\r
121         /* Copy each stack byte in turn.  pxXRAMStack is incremented first as we                                        \\r
122         have already stored the stack size into XRAM. */                                                                                        \\r
123         while( ucStackBytes )                                                                                                                                           \\r
124         {                                                                                                                                                                                       \\r
125                 pxXRAMStack++;                                                                                                                                                  \\r
126                 *pxXRAMStack = *pxRAMStack;                                                                                                                             \\r
127                 pxRAMStack++;                                                                                                                                                   \\r
128                 ucStackBytes--;                                                                                                                                                 \\r
129         }                                                                                                                                                                                       \\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * Macro that copies the stack of the task being resumed from XRAM into \r
135  * internal RAM.\r
136  */\r
137 #define portCOPY_XRAM_TO_STACK()                                                                                                                                \\r
138 {                                                                                                                                                                                               \\r
139         /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to                                         \\r
140         copy the data back out of XRAM and into the stack. */                                                                           \\r
141         pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB );         \\r
142         pxRAMStack = ( data portSTACK_TYPE * data ) ( configSTACK_START - 1 );                                          \\r
143                                                                                                                                                                                                 \\r
144         /* The first value stored in XRAM was the size of the stack - i.e. the                                          \\r
145         number of bytes we need to copy back. */                                                                                                        \\r
146         ucStackBytes = pxXRAMStack[ 0 ];                                                                                                                        \\r
147                                                                                                                                                                                                 \\r
148         /* Copy the required number of bytes back into the stack. */                                                            \\r
149         do                                                                                                                                                                                      \\r
150         {                                                                                                                                                                                       \\r
151                 pxXRAMStack++;                                                                                                                                                  \\r
152                 pxRAMStack++;                                                                                                                                                   \\r
153                 *pxRAMStack = *pxXRAMStack;                                                                                                                             \\r
154                 ucStackBytes--;                                                                                                                                                 \\r
155         } while( ucStackBytes );                                                                                                                                        \\r
156                                                                                                                                                                                                 \\r
157         /* Restore the stack pointer ready to use the restored stack. */                                                        \\r
158         SP = ( unsigned portCHAR ) pxRAMStack;                                                                                                          \\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * Macro to push the current execution context onto the stack, before the stack \r
164  * is moved to XRAM. \r
165  */\r
166 #define portSAVE_CONTEXT()                                                                                                                                              \\r
167 {                                                                                                                                                                                               \\r
168         _asm                                                                                                                                                                            \\r
169                 /* Push ACC first, as when restoring the context it must be restored                                    \\r
170                 last (it is used to set the IE register). */                                                                                    \\r
171                 push    ACC                                                                                                                                                             \\r
172                 /* Store the IE register then disable interrupts. */                                                                    \\r
173                 push    IE                                                                                                                                                              \\r
174                 clr             _EA                                                                                                                                                             \\r
175                 push    DPL                                                                                                                                                             \\r
176                 push    DPH                                                                                                                                                             \\r
177                 push    b                                                                                                                                                               \\r
178                 push    ar2                                                                                                                                                             \\r
179                 push    ar3                                                                                                                                                             \\r
180                 push    ar4                                                                                                                                                             \\r
181                 push    ar5                                                                                                                                                             \\r
182                 push    ar6                                                                                                                                                             \\r
183                 push    ar7                                                                                                                                                             \\r
184                 push    ar0                                                                                                                                                             \\r
185                 push    ar1                                                                                                                                                             \\r
186                 push    PSW                                                                                                                                                             \\r
187         _endasm;                                                                                                                                                                        \\r
188                 PSW = 0;                                                                                                                                                                \\r
189         _asm                                                                                                                                                                            \\r
190                 push    _bp                                                                                                                                                             \\r
191         _endasm;                                                                                                                                                                        \\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /*\r
196  * Macro that restores the execution context from the stack.  The execution \r
197  * context was saved into the stack before the stack was copied into XRAM.\r
198  */\r
199 #define portRESTORE_CONTEXT()                                                                                                                                   \\r
200 {                                                                                                                                                                                               \\r
201         _asm                                                                                                                                                                            \\r
202                 pop             _bp                                                                                                                                                             \\r
203                 pop             PSW                                                                                                                                                             \\r
204                 pop             ar1                                                                                                                                                             \\r
205                 pop             ar0                                                                                                                                                             \\r
206                 pop             ar7                                                                                                                                                             \\r
207                 pop             ar6                                                                                                                                                             \\r
208                 pop             ar5                                                                                                                                                             \\r
209                 pop             ar4                                                                                                                                                             \\r
210                 pop             ar3                                                                                                                                                             \\r
211                 pop             ar2                                                                                                                                                             \\r
212                 pop             b                                                                                                                                                               \\r
213                 pop             DPH                                                                                                                                                             \\r
214                 pop             DPL                                                                                                                                                             \\r
215                 /* The next byte of the stack is the IE register.  Only the global                                              \\r
216                 enable bit forms part of the task context.  Pop off the IE then set                                             \\r
217                 the global enable bit to match that of the stored IE register. */                                               \\r
218                 pop             ACC                                                                                                                                                             \\r
219                 JB              ACC.7,0098$                                                                                                                                             \\r
220                 CLR             IE.7                                                                                                                                                    \\r
221                 LJMP    0099$                                                                                                                                                   \\r
222         0098$:                                                                                                                                                                          \\r
223                 SETB    IE.7                                                                                                                                                    \\r
224         0099$:                                                                                                                                                                          \\r
225                 /* Finally pop off the ACC, which was the first register saved. */                                              \\r
226                 pop             ACC                                                                                                                                                             \\r
227                 reti                                                                                                                                                                    \\r
228         _endasm;                                                                                                                                                                        \\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /* \r
233  * See header file for description. \r
234  */\r
235 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
236 {\r
237 unsigned portLONG ulAddress;\r
238 portSTACK_TYPE *pxStartOfStack;\r
239 \r
240         /* Leave space to write the size of the stack as the first byte. */\r
241         pxStartOfStack = pxTopOfStack;\r
242         pxTopOfStack++;\r
243 \r
244         /* Place a few bytes of known values on the bottom of the stack. \r
245         This is just useful for debugging and can be uncommented if required.\r
246         *pxTopOfStack = 0x11;\r
247         pxTopOfStack++;\r
248         *pxTopOfStack = 0x22;\r
249         pxTopOfStack++;\r
250         *pxTopOfStack = 0x33;\r
251         pxTopOfStack++;\r
252         */\r
253 \r
254         /* Simulate how the stack would look after a call to the scheduler tick \r
255         ISR. \r
256 \r
257         The return address that would have been pushed by the MCU. */\r
258         ulAddress = ( unsigned portLONG ) pxCode;\r
259         *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;\r
260         ulAddress >>= 8;\r
261         pxTopOfStack++;\r
262         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress );\r
263         pxTopOfStack++;\r
264 \r
265         /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */\r
266         *pxTopOfStack = 0xaa;   /* acc */\r
267         pxTopOfStack++; \r
268 \r
269         /* We want tasks to start with interrupts enabled. */\r
270         *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;\r
271         pxTopOfStack++;\r
272 \r
273         /* The function parameters will be passed in the DPTR and B register as\r
274         a three byte generic pointer is used. */\r
275         ulAddress = ( unsigned portLONG ) pvParameters;\r
276         *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* DPL */\r
277         ulAddress >>= 8;\r
278         *pxTopOfStack++;\r
279         *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* DPH */\r
280         ulAddress >>= 8;\r
281         pxTopOfStack++;\r
282         *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;   /* b */\r
283         pxTopOfStack++;\r
284 \r
285         /* The remaining registers are straight forward. */\r
286         *pxTopOfStack = 0x02;   /* R2 */\r
287         pxTopOfStack++;\r
288         *pxTopOfStack = 0x03;   /* R3 */\r
289         pxTopOfStack++;\r
290         *pxTopOfStack = 0x04;   /* R4 */\r
291         pxTopOfStack++;\r
292         *pxTopOfStack = 0x05;   /* R5 */\r
293         pxTopOfStack++;\r
294         *pxTopOfStack = 0x06;   /* R6 */\r
295         pxTopOfStack++;\r
296         *pxTopOfStack = 0x07;   /* R7 */\r
297         pxTopOfStack++;\r
298         *pxTopOfStack = 0x00;   /* R0 */\r
299         pxTopOfStack++;\r
300         *pxTopOfStack = 0x01;   /* R1 */\r
301         pxTopOfStack++;\r
302         *pxTopOfStack = 0x00;   /* PSW */\r
303         pxTopOfStack++;\r
304         *pxTopOfStack = 0xbb;   /* BP */\r
305 \r
306         /* Dont increment the stack size here as we don't want to include\r
307         the stack size byte as part of the stack size count.\r
308 \r
309         Finally we place the stack size at the beginning. */\r
310         *pxStartOfStack = ( portSTACK_TYPE ) ( pxTopOfStack - pxStartOfStack );\r
311 \r
312         /* Unlike most ports, we return the start of the stack as this is where the\r
313         size of the stack is stored. */\r
314         return pxStartOfStack;\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 /* \r
319  * See header file for description. \r
320  */\r
321 portBASE_TYPE xPortStartScheduler( void )\r
322 {\r
323         /* Setup timer 2 to generate the RTOS tick. */\r
324         prvSetupTimerInterrupt();       \r
325 \r
326         /* Make sure we start with the expected SFR page.  This line should not\r
327         really be required. */\r
328         SFRPAGE = 0;\r
329 \r
330         /* Copy the stack for the first task to execute from XRAM into the stack,\r
331         restore the task context from the new stack, then start running the task. */\r
332         portCOPY_XRAM_TO_STACK();\r
333         portRESTORE_CONTEXT();\r
334 \r
335         /* Should never get here! */\r
336         return pdTRUE;\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 void vPortEndScheduler( void )\r
341 {\r
342         /* Not implemented for this port. */\r
343 }\r
344 /*-----------------------------------------------------------*/\r
345 \r
346 /*\r
347  * Manual context switch.  The first thing we do is save the registers so we\r
348  * can use a naked attribute.\r
349  */\r
350 void vPortYield( void ) _naked\r
351 {\r
352         /* Save the execution context onto the stack, then copy the entire stack\r
353         to XRAM.  This is necessary as the internal RAM is only large enough to\r
354         hold one stack, and we want one per task. \r
355         \r
356         PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH\r
357         IS REQUIRED. */\r
358         portSAVE_CONTEXT();\r
359         portCOPY_STACK_TO_XRAM();\r
360 \r
361         /* Call the standard scheduler context switch function. */\r
362         vTaskSwitchContext();\r
363 \r
364         /* Copy the stack of the task about to execute from XRAM into RAM and\r
365         restore it's context ready to run on exiting. */\r
366         portCOPY_XRAM_TO_STACK();\r
367         portRESTORE_CONTEXT();\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 #if configUSE_PREEMPTION == 1\r
372         void vTimer2ISR( void ) interrupt 5 _naked\r
373         {\r
374                 /* Preemptive context switch function triggered by the timer 2 ISR.\r
375                 This does the same as vPortYield() (see above) with the addition\r
376                 of incrementing the RTOS tick count. */\r
377 \r
378                 portSAVE_CONTEXT();\r
379                 portCOPY_STACK_TO_XRAM();\r
380 \r
381                 vTaskIncrementTick();\r
382                 vTaskSwitchContext();\r
383                 \r
384                 portCLEAR_INTERRUPT_FLAG();\r
385                 portCOPY_XRAM_TO_STACK();\r
386                 portRESTORE_CONTEXT();\r
387         }\r
388 #else\r
389         void vTimer2ISR( void ) interrupt 5\r
390         {\r
391                 /* When using the cooperative scheduler the timer 2 ISR is only \r
392                 required to increment the RTOS tick count. */\r
393 \r
394                 vTaskIncrementTick();\r
395                 portCLEAR_INTERRUPT_FLAG();\r
396         }\r
397 #endif\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 static void prvSetupTimerInterrupt( void )\r
401 {\r
402 unsigned portCHAR ucOriginalSFRPage;\r
403 \r
404 /* Constants calculated to give the required timer capture values. */\r
405 const unsigned portLONG ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;\r
406 const unsigned portLONG ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;\r
407 const unsigned portLONG ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;\r
408 const unsigned portCHAR ucLowCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue & ( unsigned portLONG ) 0xff );\r
409 const unsigned portCHAR ucHighCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue >> ( unsigned portLONG ) 8 );\r
410 \r
411         /* NOTE:  This uses a timer only present on 8052 architecture. */\r
412 \r
413         /* Remember the current SFR page so we can restore it at the end of the\r
414         function. */\r
415         ucOriginalSFRPage = SFRPAGE;\r
416         SFRPAGE = 0;\r
417 \r
418         /* TMR2CF can be left in its default state. */  \r
419         TMR2CF = ( unsigned portCHAR ) 0;\r
420 \r
421         /* Setup the overflow reload value. */\r
422         RCAP2L = ucLowCaptureByte;\r
423         RCAP2H = ucHighCaptureByte;\r
424 \r
425         /* The initial load is performed manually. */\r
426         TMR2L = ucLowCaptureByte;\r
427         TMR2H = ucHighCaptureByte;\r
428 \r
429         /* Enable the timer 2 interrupts. */\r
430         IE |= portTIMER_2_INTERRUPT_ENABLE;\r
431 \r
432         /* Interrupts are disabled when this is called so the timer can be started\r
433         here. */\r
434         TMR2CN = portENABLE_TIMER;\r
435 \r
436         /* Restore the original SFR page. */\r
437         SFRPAGE = ucOriginalSFRPage;\r
438 }\r
439 \r
440 \r
441 \r
442 \r