]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/SDCC/Cygnal/port.c
838c55457c5c4f491bd262975dc4f2f3ef17fcd0
[freertos] / FreeRTOS / Source / portable / SDCC / Cygnal / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the Cygnal port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* Standard includes. */\r
33 #include <string.h>\r
34 \r
35 /* Scheduler includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 \r
39 /* Constants required to setup timer 2 to produce the RTOS tick. */\r
40 #define portCLOCK_DIVISOR                               ( ( uint32_t ) 12 )\r
41 #define portMAX_TIMER_VALUE                             ( ( uint32_t ) 0xffff )\r
42 #define portENABLE_TIMER                                ( ( uint8_t ) 0x04 )\r
43 #define portTIMER_2_INTERRUPT_ENABLE    ( ( uint8_t ) 0x20 )\r
44 \r
45 /* The value used in the IE register when a task first starts. */\r
46 #define portGLOBAL_INTERRUPT_BIT        ( ( StackType_t ) 0x80 )\r
47 \r
48 /* The value used in the PSW register when a task first starts. */\r
49 #define portINITIAL_PSW                         ( ( StackType_t ) 0x00 )\r
50 \r
51 /* Macro to clear the timer 2 interrupt flag. */\r
52 #define portCLEAR_INTERRUPT_FLAG()      TMR2CN &= ~0x80;\r
53 \r
54 /* Used during a context switch to store the size of the stack being copied\r
55 to or from XRAM. */\r
56 data static uint8_t ucStackBytes;\r
57 \r
58 /* Used during a context switch to point to the next byte in XRAM from/to which\r
59 a RAM byte is to be copied. */\r
60 xdata static StackType_t * data pxXRAMStack;\r
61 \r
62 /* Used during a context switch to point to the next byte in RAM from/to which\r
63 an XRAM byte is to be copied. */\r
64 data static StackType_t * data pxRAMStack;\r
65 \r
66 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
67 any details of its type. */\r
68 typedef void TCB_t;\r
69 extern volatile TCB_t * volatile pxCurrentTCB;\r
70 \r
71 /*\r
72  * Setup the hardware to generate an interrupt off timer 2 at the required \r
73  * frequency.\r
74  */\r
75 static void prvSetupTimerInterrupt( void );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 /*\r
79  * Macro that copies the current stack from internal RAM to XRAM.  This is \r
80  * required as the 8051 only contains enough internal RAM for a single stack, \r
81  * but we have a stack for every task.\r
82  */\r
83 #define portCOPY_STACK_TO_XRAM()                                                                                                                                \\r
84 {                                                                                                                                                                                               \\r
85         /* pxCurrentTCB points to a TCB which itself points to the location into                                        \\r
86         which the first stack byte should be copied.  Set pxXRAMStack to point                                          \\r
87         to the location into which the first stack byte is to be copied. */                                                     \\r
88         pxXRAMStack = ( xdata StackType_t * ) *( ( xdata StackType_t ** ) pxCurrentTCB );               \\r
89                                                                                                                                                                                                 \\r
90         /* Set pxRAMStack to point to the first byte to be coped from the stack. */                                     \\r
91         pxRAMStack = ( data StackType_t * data ) configSTACK_START;                                                             \\r
92                                                                                                                                                                                                 \\r
93         /* Calculate the size of the stack we are about to copy from the current                                        \\r
94         stack pointer value. */                                                                                                                                         \\r
95         ucStackBytes = SP - ( configSTACK_START - 1 );                                                                                          \\r
96                                                                                                                                                                                                 \\r
97         /* Before starting to copy the stack, store the calculated stack size so                                        \\r
98         the stack can be restored when the task is resumed. */                                                                          \\r
99         *pxXRAMStack = ucStackBytes;                                                                                                                            \\r
100                                                                                                                                                                                                 \\r
101         /* Copy each stack byte in turn.  pxXRAMStack is incremented first as we                                        \\r
102         have already stored the stack size into XRAM. */                                                                                        \\r
103         while( ucStackBytes )                                                                                                                                           \\r
104         {                                                                                                                                                                                       \\r
105                 pxXRAMStack++;                                                                                                                                                  \\r
106                 *pxXRAMStack = *pxRAMStack;                                                                                                                             \\r
107                 pxRAMStack++;                                                                                                                                                   \\r
108                 ucStackBytes--;                                                                                                                                                 \\r
109         }                                                                                                                                                                                       \\r
110 }\r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /*\r
114  * Macro that copies the stack of the task being resumed from XRAM into \r
115  * internal RAM.\r
116  */\r
117 #define portCOPY_XRAM_TO_STACK()                                                                                                                                \\r
118 {                                                                                                                                                                                               \\r
119         /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to                                         \\r
120         copy the data back out of XRAM and into the stack. */                                                                           \\r
121         pxXRAMStack = ( xdata StackType_t * ) *( ( xdata StackType_t ** ) pxCurrentTCB );               \\r
122         pxRAMStack = ( data StackType_t * data ) ( configSTACK_START - 1 );                                             \\r
123                                                                                                                                                                                                 \\r
124         /* The first value stored in XRAM was the size of the stack - i.e. the                                          \\r
125         number of bytes we need to copy back. */                                                                                                        \\r
126         ucStackBytes = pxXRAMStack[ 0 ];                                                                                                                        \\r
127                                                                                                                                                                                                 \\r
128         /* Copy the required number of bytes back into the stack. */                                                            \\r
129         do                                                                                                                                                                                      \\r
130         {                                                                                                                                                                                       \\r
131                 pxXRAMStack++;                                                                                                                                                  \\r
132                 pxRAMStack++;                                                                                                                                                   \\r
133                 *pxRAMStack = *pxXRAMStack;                                                                                                                             \\r
134                 ucStackBytes--;                                                                                                                                                 \\r
135         } while( ucStackBytes );                                                                                                                                        \\r
136                                                                                                                                                                                                 \\r
137         /* Restore the stack pointer ready to use the restored stack. */                                                        \\r
138         SP = ( uint8_t ) pxRAMStack;                                                                                                            \\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\r
143  * Macro to push the current execution context onto the stack, before the stack \r
144  * is moved to XRAM. \r
145  */\r
146 #define portSAVE_CONTEXT()                                                                                                                                              \\r
147 {                                                                                                                                                                                               \\r
148         _asm                                                                                                                                                                            \\r
149                 /* Push ACC first, as when restoring the context it must be restored                                    \\r
150                 last (it is used to set the IE register). */                                                                                    \\r
151                 push    ACC                                                                                                                                                             \\r
152                 /* Store the IE register then disable interrupts. */                                                                    \\r
153                 push    IE                                                                                                                                                              \\r
154                 clr             _EA                                                                                                                                                             \\r
155                 push    DPL                                                                                                                                                             \\r
156                 push    DPH                                                                                                                                                             \\r
157                 push    b                                                                                                                                                               \\r
158                 push    ar2                                                                                                                                                             \\r
159                 push    ar3                                                                                                                                                             \\r
160                 push    ar4                                                                                                                                                             \\r
161                 push    ar5                                                                                                                                                             \\r
162                 push    ar6                                                                                                                                                             \\r
163                 push    ar7                                                                                                                                                             \\r
164                 push    ar0                                                                                                                                                             \\r
165                 push    ar1                                                                                                                                                             \\r
166                 push    PSW                                                                                                                                                             \\r
167         _endasm;                                                                                                                                                                        \\r
168                 PSW = 0;                                                                                                                                                                \\r
169         _asm                                                                                                                                                                            \\r
170                 push    _bp                                                                                                                                                             \\r
171         _endasm;                                                                                                                                                                        \\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * Macro that restores the execution context from the stack.  The execution \r
177  * context was saved into the stack before the stack was copied into XRAM.\r
178  */\r
179 #define portRESTORE_CONTEXT()                                                                                                                                   \\r
180 {                                                                                                                                                                                               \\r
181         _asm                                                                                                                                                                            \\r
182                 pop             _bp                                                                                                                                                             \\r
183                 pop             PSW                                                                                                                                                             \\r
184                 pop             ar1                                                                                                                                                             \\r
185                 pop             ar0                                                                                                                                                             \\r
186                 pop             ar7                                                                                                                                                             \\r
187                 pop             ar6                                                                                                                                                             \\r
188                 pop             ar5                                                                                                                                                             \\r
189                 pop             ar4                                                                                                                                                             \\r
190                 pop             ar3                                                                                                                                                             \\r
191                 pop             ar2                                                                                                                                                             \\r
192                 pop             b                                                                                                                                                               \\r
193                 pop             DPH                                                                                                                                                             \\r
194                 pop             DPL                                                                                                                                                             \\r
195                 /* The next byte of the stack is the IE register.  Only the global                                              \\r
196                 enable bit forms part of the task context.  Pop off the IE then set                                             \\r
197                 the global enable bit to match that of the stored IE register. */                                               \\r
198                 pop             ACC                                                                                                                                                             \\r
199                 JB              ACC.7,0098$                                                                                                                                             \\r
200                 CLR             IE.7                                                                                                                                                    \\r
201                 LJMP    0099$                                                                                                                                                   \\r
202         0098$:                                                                                                                                                                          \\r
203                 SETB    IE.7                                                                                                                                                    \\r
204         0099$:                                                                                                                                                                          \\r
205                 /* Finally pop off the ACC, which was the first register saved. */                                              \\r
206                 pop             ACC                                                                                                                                                             \\r
207                 reti                                                                                                                                                                    \\r
208         _endasm;                                                                                                                                                                        \\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 /* \r
213  * See header file for description. \r
214  */\r
215 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
216 {\r
217 uint32_t ulAddress;\r
218 StackType_t *pxStartOfStack;\r
219 \r
220         /* Leave space to write the size of the stack as the first byte. */\r
221         pxStartOfStack = pxTopOfStack;\r
222         pxTopOfStack++;\r
223 \r
224         /* Place a few bytes of known values on the bottom of the stack. \r
225         This is just useful for debugging and can be uncommented if required.\r
226         *pxTopOfStack = 0x11;\r
227         pxTopOfStack++;\r
228         *pxTopOfStack = 0x22;\r
229         pxTopOfStack++;\r
230         *pxTopOfStack = 0x33;\r
231         pxTopOfStack++;\r
232         */\r
233 \r
234         /* Simulate how the stack would look after a call to the scheduler tick \r
235         ISR. \r
236 \r
237         The return address that would have been pushed by the MCU. */\r
238         ulAddress = ( uint32_t ) pxCode;\r
239         *pxTopOfStack = ( StackType_t ) ulAddress;\r
240         ulAddress >>= 8;\r
241         pxTopOfStack++;\r
242         *pxTopOfStack = ( StackType_t ) ( ulAddress );\r
243         pxTopOfStack++;\r
244 \r
245         /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */\r
246         *pxTopOfStack = 0xaa;   /* acc */\r
247         pxTopOfStack++; \r
248 \r
249         /* We want tasks to start with interrupts enabled. */\r
250         *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;\r
251         pxTopOfStack++;\r
252 \r
253         /* The function parameters will be passed in the DPTR and B register as\r
254         a three byte generic pointer is used. */\r
255         ulAddress = ( uint32_t ) pvParameters;\r
256         *pxTopOfStack = ( StackType_t ) ulAddress;      /* DPL */\r
257         ulAddress >>= 8;\r
258         *pxTopOfStack++;\r
259         *pxTopOfStack = ( StackType_t ) ulAddress;      /* DPH */\r
260         ulAddress >>= 8;\r
261         pxTopOfStack++;\r
262         *pxTopOfStack = ( StackType_t ) ulAddress;      /* b */\r
263         pxTopOfStack++;\r
264 \r
265         /* The remaining registers are straight forward. */\r
266         *pxTopOfStack = 0x02;   /* R2 */\r
267         pxTopOfStack++;\r
268         *pxTopOfStack = 0x03;   /* R3 */\r
269         pxTopOfStack++;\r
270         *pxTopOfStack = 0x04;   /* R4 */\r
271         pxTopOfStack++;\r
272         *pxTopOfStack = 0x05;   /* R5 */\r
273         pxTopOfStack++;\r
274         *pxTopOfStack = 0x06;   /* R6 */\r
275         pxTopOfStack++;\r
276         *pxTopOfStack = 0x07;   /* R7 */\r
277         pxTopOfStack++;\r
278         *pxTopOfStack = 0x00;   /* R0 */\r
279         pxTopOfStack++;\r
280         *pxTopOfStack = 0x01;   /* R1 */\r
281         pxTopOfStack++;\r
282         *pxTopOfStack = 0x00;   /* PSW */\r
283         pxTopOfStack++;\r
284         *pxTopOfStack = 0xbb;   /* BP */\r
285 \r
286         /* Dont increment the stack size here as we don't want to include\r
287         the stack size byte as part of the stack size count.\r
288 \r
289         Finally we place the stack size at the beginning. */\r
290         *pxStartOfStack = ( StackType_t ) ( pxTopOfStack - pxStartOfStack );\r
291 \r
292         /* Unlike most ports, we return the start of the stack as this is where the\r
293         size of the stack is stored. */\r
294         return pxStartOfStack;\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 /* \r
299  * See header file for description. \r
300  */\r
301 BaseType_t xPortStartScheduler( void )\r
302 {\r
303         /* Setup timer 2 to generate the RTOS tick. */\r
304         prvSetupTimerInterrupt();       \r
305 \r
306         /* Make sure we start with the expected SFR page.  This line should not\r
307         really be required. */\r
308         SFRPAGE = 0;\r
309 \r
310         /* Copy the stack for the first task to execute from XRAM into the stack,\r
311         restore the task context from the new stack, then start running the task. */\r
312         portCOPY_XRAM_TO_STACK();\r
313         portRESTORE_CONTEXT();\r
314 \r
315         /* Should never get here! */\r
316         return pdTRUE;\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 void vPortEndScheduler( void )\r
321 {\r
322         /* Not implemented for this port. */\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 /*\r
327  * Manual context switch.  The first thing we do is save the registers so we\r
328  * can use a naked attribute.\r
329  */\r
330 void vPortYield( void ) _naked\r
331 {\r
332         /* Save the execution context onto the stack, then copy the entire stack\r
333         to XRAM.  This is necessary as the internal RAM is only large enough to\r
334         hold one stack, and we want one per task. \r
335         \r
336         PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH\r
337         IS REQUIRED. */\r
338         portSAVE_CONTEXT();\r
339         portCOPY_STACK_TO_XRAM();\r
340 \r
341         /* Call the standard scheduler context switch function. */\r
342         vTaskSwitchContext();\r
343 \r
344         /* Copy the stack of the task about to execute from XRAM into RAM and\r
345         restore it's context ready to run on exiting. */\r
346         portCOPY_XRAM_TO_STACK();\r
347         portRESTORE_CONTEXT();\r
348 }\r
349 /*-----------------------------------------------------------*/\r
350 \r
351 #if configUSE_PREEMPTION == 1\r
352         void vTimer2ISR( void ) interrupt 5 _naked\r
353         {\r
354                 /* Preemptive context switch function triggered by the timer 2 ISR.\r
355                 This does the same as vPortYield() (see above) with the addition\r
356                 of incrementing the RTOS tick count. */\r
357 \r
358                 portSAVE_CONTEXT();\r
359                 portCOPY_STACK_TO_XRAM();\r
360 \r
361                 if( xTaskIncrementTick() != pdFALSE )\r
362                 {\r
363                         vTaskSwitchContext();\r
364                 }\r
365                 \r
366                 portCLEAR_INTERRUPT_FLAG();\r
367                 portCOPY_XRAM_TO_STACK();\r
368                 portRESTORE_CONTEXT();\r
369         }\r
370 #else\r
371         void vTimer2ISR( void ) interrupt 5\r
372         {\r
373                 /* When using the cooperative scheduler the timer 2 ISR is only \r
374                 required to increment the RTOS tick count. */\r
375 \r
376                 xTaskIncrementTick();\r
377                 portCLEAR_INTERRUPT_FLAG();\r
378         }\r
379 #endif\r
380 /*-----------------------------------------------------------*/\r
381 \r
382 static void prvSetupTimerInterrupt( void )\r
383 {\r
384 uint8_t ucOriginalSFRPage;\r
385 \r
386 /* Constants calculated to give the required timer capture values. */\r
387 const uint32_t ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;\r
388 const uint32_t ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;\r
389 const uint32_t ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;\r
390 const uint8_t ucLowCaptureByte = ( uint8_t ) ( ulCaptureValue & ( uint32_t ) 0xff );\r
391 const uint8_t ucHighCaptureByte = ( uint8_t ) ( ulCaptureValue >> ( uint32_t ) 8 );\r
392 \r
393         /* NOTE:  This uses a timer only present on 8052 architecture. */\r
394 \r
395         /* Remember the current SFR page so we can restore it at the end of the\r
396         function. */\r
397         ucOriginalSFRPage = SFRPAGE;\r
398         SFRPAGE = 0;\r
399 \r
400         /* TMR2CF can be left in its default state. */  \r
401         TMR2CF = ( uint8_t ) 0;\r
402 \r
403         /* Setup the overflow reload value. */\r
404         RCAP2L = ucLowCaptureByte;\r
405         RCAP2H = ucHighCaptureByte;\r
406 \r
407         /* The initial load is performed manually. */\r
408         TMR2L = ucLowCaptureByte;\r
409         TMR2H = ucHighCaptureByte;\r
410 \r
411         /* Enable the timer 2 interrupts. */\r
412         IE |= portTIMER_2_INTERRUPT_ENABLE;\r
413 \r
414         /* Interrupts are disabled when this is called so the timer can be started\r
415         here. */\r
416         TMR2CN = portENABLE_TIMER;\r
417 \r
418         /* Restore the original SFR page. */\r
419         SFRPAGE = ucOriginalSFRPage;\r
420 }\r
421 \r
422 \r
423 \r
424 \r