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