2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
43 #include "FreeRTOS.h"
\r
45 #include "mb96348hs.h"
\r
47 /*-----------------------------------------------------------
\r
48 * Implementation of functions defined in portable.h for the 16FX port.
\r
49 *----------------------------------------------------------*/
\r
52 * Get current value of DPR and ADB registers
\r
54 portSTACK_TYPE xGet_DPR_ADB_bank( void );
\r
57 * Get current value of DTB and PCB registers
\r
59 portSTACK_TYPE xGet_DTB_PCB_bank( void );
\r
62 * Get current register pointer
\r
64 portCHAR xGet_RP( void );
\r
67 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
68 * can be done using any given RLT.
\r
70 static void prvSetupRLT0Interrupt( void );
\r
72 /*-----------------------------------------------------------*/
\r
75 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
76 * any details of its type.
\r
78 typedef void tskTCB;
\r
79 extern volatile tskTCB * volatile pxCurrentTCB;
\r
81 /* Constants required to handle critical sections. */
\r
82 #define portNO_CRITICAL_NESTING ( ( unsigned portBASE_TYPE ) 0 )
\r
83 volatile unsigned portBASE_TYPE uxCriticalNesting = 9999UL;
\r
85 /*-----------------------------------------------------------*/
\r
88 * Macro to save a task context to the task stack. This macro copies the
\r
89 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
90 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
91 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
92 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
93 * stack. Finally the resultant stack pointer value is saved into the
\r
94 * task control block so it can be retrieved the next time the task
\r
98 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
100 #define portSAVE_CONTEXT() \
\r
101 { __asm(" POPW A "); \
\r
102 __asm(" AND CCR,#H'DF "); \
\r
103 __asm(" PUSHW A "); \
\r
104 __asm(" OR CCR,#H'20 "); \
\r
105 __asm(" POPW A "); \
\r
106 __asm(" AND CCR,#H'DF "); \
\r
107 __asm(" PUSHW A "); \
\r
108 __asm(" OR CCR,#H'20 "); \
\r
109 __asm(" POPW A "); \
\r
110 __asm(" AND CCR,#H'DF "); \
\r
111 __asm(" PUSHW A "); \
\r
112 __asm(" OR CCR,#H'20 "); \
\r
113 __asm(" POPW A "); \
\r
114 __asm(" AND CCR,#H'DF "); \
\r
115 __asm(" PUSHW A "); \
\r
116 __asm(" OR CCR,#H'20 "); \
\r
117 __asm(" POPW A "); \
\r
118 __asm(" AND CCR,#H'DF "); \
\r
119 __asm(" PUSHW A "); \
\r
120 __asm(" OR CCR,#H'20 "); \
\r
121 __asm(" POPW A "); \
\r
122 __asm(" AND CCR,#H'DF "); \
\r
123 __asm(" PUSHW A "); \
\r
124 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
126 /* Save the critical nesting count to the stack. */ \
\r
127 __asm(" MOVW RW0, _uxCriticalNesting "); \
\r
128 __asm(" PUSHW (RW0) "); \
\r
130 __asm(" MOVW A, _pxCurrentTCB "); \
\r
131 __asm(" MOVW A, SP "); \
\r
132 __asm(" SWAPW "); \
\r
133 __asm(" MOVW @AL, AH "); \
\r
134 __asm(" OR CCR,#H'20 "); \
\r
138 * Macro to restore a task context from the task stack. This is effecti-
\r
139 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
140 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
141 * LARGE memory model ) is loaded from the task control block. Next the
\r
142 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
143 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
144 * the task to be executed upon RETI from user stack to system stack.
\r
147 #define portRESTORE_CONTEXT() \
\r
148 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
149 __asm(" MOVW A, @A "); \
\r
150 __asm(" AND CCR,#H'DF "); \
\r
151 __asm(" MOVW SP, A "); \
\r
153 /* Load the saved uxCriticalNesting value into RW0. */ \
\r
154 __asm(" POPW (RW0) "); \
\r
156 /* Save the loaded value into the uxCriticalNesting variable. */ \
\r
157 __asm(" MOVW _uxCriticalNesting, RW0 "); \
\r
159 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
160 __asm(" POPW A "); \
\r
161 __asm(" OR CCR,#H'20 "); \
\r
162 __asm(" PUSHW A "); \
\r
163 __asm(" AND CCR,#H'DF "); \
\r
164 __asm(" POPW A "); \
\r
165 __asm(" OR CCR,#H'20 "); \
\r
166 __asm(" PUSHW A "); \
\r
167 __asm(" AND CCR,#H'DF "); \
\r
168 __asm(" POPW A "); \
\r
169 __asm(" OR CCR,#H'20 "); \
\r
170 __asm(" PUSHW A "); \
\r
171 __asm(" AND CCR,#H'DF "); \
\r
172 __asm(" POPW A "); \
\r
173 __asm(" OR CCR,#H'20 "); \
\r
174 __asm(" PUSHW A "); \
\r
175 __asm(" AND CCR,#H'DF "); \
\r
176 __asm(" POPW A "); \
\r
177 __asm(" OR CCR,#H'20 "); \
\r
178 __asm(" PUSHW A "); \
\r
179 __asm(" AND CCR,#H'DF "); \
\r
180 __asm(" POPW A "); \
\r
181 __asm(" OR CCR,#H'20 "); \
\r
182 __asm(" PUSHW A "); \
\r
185 #elif (configMEMMODEL == portCOMPACT || configMEMMODEL == portLARGE)
\r
187 #define portSAVE_CONTEXT() \
\r
188 { __asm(" POPW A "); \
\r
189 __asm(" AND CCR,#H'DF "); \
\r
190 __asm(" PUSHW A "); \
\r
191 __asm(" OR CCR,#H'20 "); \
\r
192 __asm(" POPW A "); \
\r
193 __asm(" AND CCR,#H'DF "); \
\r
194 __asm(" PUSHW A "); \
\r
195 __asm(" OR CCR,#H'20 "); \
\r
196 __asm(" POPW A "); \
\r
197 __asm(" AND CCR,#H'DF "); \
\r
198 __asm(" PUSHW A "); \
\r
199 __asm(" OR CCR,#H'20 "); \
\r
200 __asm(" POPW A "); \
\r
201 __asm(" AND CCR,#H'DF "); \
\r
202 __asm(" PUSHW A "); \
\r
203 __asm(" OR CCR,#H'20 "); \
\r
204 __asm(" POPW A "); \
\r
205 __asm(" AND CCR,#H'DF "); \
\r
206 __asm(" PUSHW A "); \
\r
207 __asm(" OR CCR,#H'20 "); \
\r
208 __asm(" POPW A "); \
\r
209 __asm(" AND CCR,#H'DF "); \
\r
210 __asm(" PUSHW A "); \
\r
211 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
212 __asm(" MOVL A, _pxCurrentTCB "); \
\r
213 __asm(" MOVL RL2, A "); \
\r
214 __asm(" MOVW A, SP "); \
\r
215 __asm(" MOVW @RL2+0, A "); \
\r
216 __asm(" MOV A, USB "); \
\r
217 __asm(" MOV @RL2+2, A "); \
\r
220 #define portRESTORE_CONTEXT() \
\r
221 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
222 __asm(" MOVL RL2, A "); \
\r
223 __asm(" MOVW A, @RL2+0 "); \
\r
224 __asm(" AND CCR,#H'DF "); \
\r
225 __asm(" MOVW SP, A "); \
\r
226 __asm(" MOV A, @RL2+2 "); \
\r
227 __asm(" MOV USB, A "); \
\r
228 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
229 __asm(" POPW A "); \
\r
230 __asm(" OR CCR,#H'20 "); \
\r
231 __asm(" PUSHW A "); \
\r
232 __asm(" AND CCR,#H'DF "); \
\r
233 __asm(" POPW A "); \
\r
234 __asm(" OR CCR,#H'20 "); \
\r
235 __asm(" PUSHW A "); \
\r
236 __asm(" AND CCR,#H'DF "); \
\r
237 __asm(" POPW A "); \
\r
238 __asm(" OR CCR,#H'20 "); \
\r
239 __asm(" PUSHW A "); \
\r
240 __asm(" AND CCR,#H'DF "); \
\r
241 __asm(" POPW A "); \
\r
242 __asm(" OR CCR,#H'20 "); \
\r
243 __asm(" PUSHW A "); \
\r
244 __asm(" AND CCR,#H'DF "); \
\r
245 __asm(" POPW A "); \
\r
246 __asm(" OR CCR,#H'20 "); \
\r
247 __asm(" PUSHW A "); \
\r
248 __asm(" AND CCR,#H'DF "); \
\r
249 __asm(" POPW A "); \
\r
250 __asm(" OR CCR,#H'20 "); \
\r
251 __asm(" PUSHW A "); \
\r
255 /*-----------------------------------------------------------*/
\r
258 * The below are the functions for getting the current value of DPR:ADB,
\r
259 * DTB:PCB bank registers
\r
264 .GLOBAL _xGet_DPR_ADB_bank
\r
265 .GLOBAL _xGet_DTB_PCB_bank
\r
267 .SECTION CODE, CODE, ALIGN=1
\r
269 _xGet_DPR_ADB_bank:
\r
275 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
277 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
282 _xGet_DTB_PCB_bank:
\r
288 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
290 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
301 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
303 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
309 /*-----------------------------------------------------------*/
\r
312 * Initialise the stack of a task to look exactly as if a call to
\r
313 * portSAVE_CONTEXT had been called.
\r
315 * See the header file portable.h.
\r
317 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
319 /* Place a few bytes of known values on the bottom of the stack.
\r
320 This is just useful for debugging. */
\r
321 *pxTopOfStack = 0x1111;
\r
323 *pxTopOfStack = 0x2222;
\r
325 *pxTopOfStack = 0x3333;
\r
328 /* Once the task is called the task would push the pointer to the
\r
329 parameter on to the stack. Hence here the pointer would be copied first
\r
330 to the stack. In case of COMPACT or LARGE memory model such pointer
\r
331 would be 24 bit and in case of SMALL or MEDIUM memory model such pointer
\r
332 would be 16 bit */
\r
333 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
335 *pxTopOfStack = ( portSTACK_TYPE ) ( ( unsigned portLONG ) ( pvParameters ) >> 16 );
\r
340 *pxTopOfStack = ( portSTACK_TYPE ) ( pvParameters );
\r
343 /* This is redundant push to the stack. This is required in order to introduce
\r
344 an offset so that the task accesses a parameter correctly that is passed on to
\r
346 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
348 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( portLONG ) ( pxCode ) >> 16 ) & 0xff );
\r
353 /* This is redundant push to the stack. This is required in order to introduce
\r
354 an offset so that the task accesses a parameter correctly that is passed on to
\r
356 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
359 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
360 *pxTopOfStack = 0xE0C0;
\r
364 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
368 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
370 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
375 /* DTB | PCB, in case of portMEDIUM or portLARGE memory model PCB would be used
\r
376 along with PC to indicate the start address of the functiom */
\r
377 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
379 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( portLONG ) ( pxCode ) >> 16 ) & 0xff );
\r
385 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
389 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
393 *pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA;
\r
396 /* Next the general purpose registers. */
\r
397 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777; /* RW7 */
\r
399 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666; /* RW6 */
\r
401 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555; /* RW5 */
\r
403 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444; /* RW4 */
\r
405 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333; /* RW3 */
\r
407 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222; /* RW2 */
\r
409 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111; /* RW1 */
\r
411 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888; /* RW0 */
\r
414 /* The task starts with its uxCriticalNesting variable set to 0, interrupts
\r
416 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
418 return pxTopOfStack;
\r
420 /*-----------------------------------------------------------*/
\r
422 static void prvSetupRLT0Interrupt( void )
\r
424 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
427 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
430 /*-----------------------------------------------------------*/
\r
432 portBASE_TYPE xPortStartScheduler( void )
\r
434 /* Setup the hardware to generate the tick. */
\r
435 prvSetupRLT0Interrupt();
\r
437 /* Restore the context of the first task that is going to run. */
\r
438 portRESTORE_CONTEXT();
\r
440 /* Simulate a function call end as generated by the compiler. We will now
\r
441 jump to the start of the task the context of which we have just restored. */
\r
446 /* Should not get here. */
\r
449 /*-----------------------------------------------------------*/
\r
451 void vPortEndScheduler( void )
\r
453 /* Not implemented - unlikely to ever be required as there is nothing to
\r
457 /*-----------------------------------------------------------*/
\r
460 * The interrupt service routine used depends on whether the pre-emptive
\r
461 * scheduler is being used or not.
\r
464 #if configUSE_PREEMPTION == 1
\r
467 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
468 * as the context would be saved by PortSAVE_CONTEXT(). The tick count
\r
469 * is incremented after the context is saved.
\r
471 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
473 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
476 /* Save the context of the interrupted task. */
\r
477 portSAVE_CONTEXT();
\r
479 /* Enable interrupts */
\r
482 /* Clear RLT0 interrupt flag */
\r
485 /* Increment the tick count then switch to the highest priority task
\r
486 that is ready to run. */
\r
487 vTaskIncrementTick();
\r
488 vTaskSwitchContext();
\r
490 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
493 /* Restore the context of the new task. */
\r
494 portRESTORE_CONTEXT();
\r
496 /* Enable interrupts */
\r
503 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
504 * tick count. We don't need to switch context, this can only be done by
\r
505 * manual calls to taskYIELD();
\r
507 __interrupt void prvRLT0_TICKISR( void )
\r
509 /* Clear RLT0 interrupt flag */
\r
512 vTaskIncrementTick();
\r
517 /*-----------------------------------------------------------*/
\r
520 * Manual context switch. We can use a __nosavereg attribute as the context
\r
521 * would be saved by PortSAVE_CONTEXT(). The context is switched and then
\r
522 * the context of the new task is restored saved.
\r
524 __nosavereg __interrupt void vPortYield( void )
\r
526 /* Save the context of the interrupted task. */
\r
527 portSAVE_CONTEXT();
\r
529 /* Switch to the highest priority task that is ready to run. */
\r
530 vTaskSwitchContext();
\r
532 /* Restore the context of the new task. */
\r
533 portRESTORE_CONTEXT();
\r
535 /*-----------------------------------------------------------*/
\r
537 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
539 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
542 /* Save the context of the interrupted task. */
\r
543 portSAVE_CONTEXT();
\r
545 /* Enable interrupts */
\r
548 /* Clear delayed interrupt flag */
\r
549 __asm (" CLRB 03A4H:0 ");
\r
551 /* Switch to the highest priority task that is ready to run. */
\r
552 vTaskSwitchContext();
\r
554 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
557 /* Restore the context of the new task. */
\r
558 portRESTORE_CONTEXT();
\r
560 /* Enable interrupts */
\r
563 /*-----------------------------------------------------------*/
\r
565 void vPortEnterCritical( void )
\r
567 /* Disable interrupts */
\r
568 portDISABLE_INTERRUPTS();
\r
570 /* Now interrupts are disabled uxCriticalNesting can be accessed
\r
571 directly. Increment uxCriticalNesting to keep a count of how many times
\r
572 portENTER_CRITICAL() has been called. */
\r
573 uxCriticalNesting++;
\r
575 /*-----------------------------------------------------------*/
\r
577 void vPortExitCritical( void )
\r
579 if( uxCriticalNesting > portNO_CRITICAL_NESTING )
\r
581 uxCriticalNesting--;
\r
582 if( uxCriticalNesting == portNO_CRITICAL_NESTING )
\r
584 /* Enable all interrupt/exception. */
\r
585 portENABLE_INTERRUPTS();
\r
589 /*-----------------------------------------------------------*/
\r