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