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