2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 #include "FreeRTOS.h"
\r
69 /*-----------------------------------------------------------
\r
70 * Implementation of functions defined in portable.h for the 16FX port.
\r
71 *----------------------------------------------------------*/
\r
74 * Get current value of DPR and ADB registers
\r
76 StackType_t xGet_DPR_ADB_bank( void );
\r
79 * Get current value of DTB and PCB registers
\r
81 StackType_t xGet_DTB_PCB_bank( void );
\r
84 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
85 * can be done using any given RLT.
\r
87 static void prvSetupRLT0Interrupt( void );
\r
89 /*-----------------------------------------------------------*/
\r
92 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
93 * any details of its type.
\r
96 extern volatile TCB_t * volatile pxCurrentTCB;
\r
98 /*-----------------------------------------------------------*/
\r
101 * Macro to save a task context to the task stack. This macro copies the
\r
102 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
103 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
104 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
105 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
106 * stack. Finally the resultant stack pointer value is saved into the
\r
107 * task control block so it can be retrieved the next time the task
\r
110 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
112 #define portSAVE_CONTEXT() \
\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(" OR CCR,#H'20 "); \
\r
125 __asm(" POPW A "); \
\r
126 __asm(" AND CCR,#H'DF "); \
\r
127 __asm(" PUSHW A "); \
\r
128 __asm(" OR CCR,#H'20 "); \
\r
129 __asm(" POPW A "); \
\r
130 __asm(" AND CCR,#H'DF "); \
\r
131 __asm(" PUSHW A "); \
\r
132 __asm(" OR CCR,#H'20 "); \
\r
133 __asm(" POPW A "); \
\r
134 __asm(" AND CCR,#H'DF "); \
\r
135 __asm(" PUSHW A "); \
\r
136 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
137 __asm(" MOVW A, _pxCurrentTCB "); \
\r
138 __asm(" MOVW A, SP "); \
\r
139 __asm(" SWAPW "); \
\r
140 __asm(" MOVW @AL, AH "); \
\r
141 __asm(" OR CCR,#H'20 "); \
\r
145 * Macro to restore a task context from the task stack. This is effecti-
\r
146 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
147 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
148 * LARGE memory model ) is loaded from the task control block. Next the
\r
149 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
150 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
151 * the task to be executed upon RETI from user stack to system stack.
\r
154 #define portRESTORE_CONTEXT() \
\r
155 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
156 __asm(" MOVW A, @A "); \
\r
157 __asm(" AND CCR,#H'DF "); \
\r
158 __asm(" MOVW SP, A "); \
\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 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
263 .GLOBAL _xGet_DPR_ADB_bank
\r
264 .GLOBAL _xGet_DTB_PCB_bank
\r
265 .SECTION CODE, CODE, ALIGN=1
\r
267 _xGet_DPR_ADB_bank:
\r
273 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
275 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
280 _xGet_DTB_PCB_bank:
\r
286 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
288 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
293 /*-----------------------------------------------------------*/
\r
296 * Initialise the stack of a task to look exactly as if a call to
\r
297 * portSAVE_CONTEXT had been called.
\r
299 * See the header file portable.h.
\r
301 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
303 /* Place a few bytes of known values on the bottom of the stack.
\r
304 This is just useful for debugging. */
\r
305 *pxTopOfStack = 0x1111;
\r
307 *pxTopOfStack = 0x2222;
\r
309 *pxTopOfStack = 0x3333;
\r
312 /* Once the task is called the task would push the pointer to the
\r
313 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
314 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
315 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
317 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
319 *pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
\r
324 *pxTopOfStack = ( StackType_t ) ( pvParameters );
\r
327 /* This is redundant push to the stack. This is required in order to introduce
\r
328 an offset so that the task accesses a parameter correctly that is passed on to
\r
330 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
332 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
337 /* This is redundant push to the stack. This is required in order to introduce
\r
338 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
339 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
342 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
343 *pxTopOfStack = 0xE0C0;
\r
347 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
351 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
353 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
358 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
359 along with PC to indicate the start address of the function. */
\r
360 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
362 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
368 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
372 *pxTopOfStack = ( StackType_t ) 0x9999;
\r
376 *pxTopOfStack = ( StackType_t ) 0xAAAA;
\r
379 /* Next the general purpose registers. */
\r
380 *pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
\r
382 *pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
\r
384 *pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
\r
386 *pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
\r
388 *pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
\r
390 *pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
\r
392 *pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
\r
394 *pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
\r
396 return pxTopOfStack;
\r
398 /*-----------------------------------------------------------*/
\r
400 static void prvSetupRLT0Interrupt( void )
\r
402 /* The peripheral clock divided by 16 is used by the timer. */
\r
403 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
405 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
406 TMRLR0 = usReloadValue;
\r
408 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
411 /*-----------------------------------------------------------*/
\r
413 BaseType_t xPortStartScheduler( void )
\r
415 /* Setup the hardware to generate the tick. */
\r
416 prvSetupRLT0Interrupt();
\r
418 /* Restore the context of the first task that is going to run. */
\r
419 portRESTORE_CONTEXT();
\r
421 /* Simulate a function call end as generated by the compiler. We will now
\r
422 jump to the start of the task the context of which we have just restored. */
\r
426 /* Should not get here. */
\r
429 /*-----------------------------------------------------------*/
\r
431 void vPortEndScheduler( void )
\r
433 /* Not implemented - unlikely to ever be required as there is nothing to
\r
437 /*-----------------------------------------------------------*/
\r
440 * The interrupt service routine used depends on whether the pre-emptive
\r
441 * scheduler is being used or not.
\r
444 #if configUSE_PREEMPTION == 1
\r
447 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
448 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
449 * compiler generated code. The tick count is incremented after the context
\r
452 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
454 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
457 /* Save the context of the interrupted task. */
\r
458 portSAVE_CONTEXT();
\r
460 /* Enable interrupts */
\r
463 /* Clear RLT0 interrupt flag */
\r
466 /* Increment the tick count then switch to the highest priority task
\r
467 that is ready to run. */
\r
468 if( xTaskIncrementTick() != pdFALSE )
\r
470 vTaskSwitchContext();
\r
473 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
476 /* Restore the context of the new task. */
\r
477 portRESTORE_CONTEXT();
\r
479 /* Enable interrupts */
\r
486 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
487 * tick count. We don't need to switch context, this can only be done by
\r
488 * manual calls to taskYIELD();
\r
490 __interrupt void prvRLT0_TICKISR( void )
\r
492 /* Clear RLT0 interrupt flag */
\r
495 xTaskIncrementTick();
\r
500 /*-----------------------------------------------------------*/
\r
503 * Manual context switch. We can use a __nosavereg attribute as the context
\r
504 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
507 __nosavereg __interrupt void vPortYield( void )
\r
509 /* Save the context of the interrupted task. */
\r
510 portSAVE_CONTEXT();
\r
512 /* Switch to the highest priority task that is ready to run. */
\r
513 vTaskSwitchContext();
\r
515 /* Restore the context of the new task. */
\r
516 portRESTORE_CONTEXT();
\r
518 /*-----------------------------------------------------------*/
\r
520 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
522 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
525 /* Save the context of the interrupted task. */
\r
526 portSAVE_CONTEXT();
\r
528 /* Enable interrupts */
\r
531 /* Clear delayed interrupt flag */
\r
532 __asm (" CLRB 03A4H:0 ");
\r
534 /* Switch to the highest priority task that is ready to run. */
\r
535 vTaskSwitchContext();
\r
537 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
540 /* Restore the context of the new task. */
\r
541 portRESTORE_CONTEXT();
\r
543 /* Enable interrupts */
\r
546 /*-----------------------------------------------------------*/
\r