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