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