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