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