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