2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 #include "FreeRTOS.h"
\r
73 /*-----------------------------------------------------------
\r
74 * Implementation of functions defined in portable.h for the 16FX port.
\r
75 *----------------------------------------------------------*/
\r
78 * Get current value of DPR and ADB registers
\r
80 StackType_t xGet_DPR_ADB_bank( void );
\r
83 * Get current value of DTB and PCB registers
\r
85 StackType_t xGet_DTB_PCB_bank( void );
\r
88 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
89 * can be done using any given RLT.
\r
91 static void prvSetupRLT0Interrupt( void );
\r
93 /*-----------------------------------------------------------*/
\r
96 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
97 * any details of its type.
\r
100 extern volatile TCB_t * volatile pxCurrentTCB;
\r
102 /*-----------------------------------------------------------*/
\r
105 * Macro to save a task context to the task stack. This macro copies the
\r
106 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
107 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
108 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
109 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
110 * stack. Finally the resultant stack pointer value is saved into the
\r
111 * task control block so it can be retrieved the next time the task
\r
114 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
116 #define portSAVE_CONTEXT() \
\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(" OR CCR,#H'20 "); \
\r
137 __asm(" POPW A "); \
\r
138 __asm(" AND CCR,#H'DF "); \
\r
139 __asm(" PUSHW A "); \
\r
140 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
141 __asm(" MOVW A, _pxCurrentTCB "); \
\r
142 __asm(" MOVW A, SP "); \
\r
143 __asm(" SWAPW "); \
\r
144 __asm(" MOVW @AL, AH "); \
\r
145 __asm(" OR CCR,#H'20 "); \
\r
149 * Macro to restore a task context from the task stack. This is effecti-
\r
150 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
151 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
152 * LARGE memory model ) is loaded from the task control block. Next the
\r
153 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
154 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
155 * the task to be executed upon RETI from user stack to system stack.
\r
158 #define portRESTORE_CONTEXT() \
\r
159 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
160 __asm(" MOVW A, @A "); \
\r
161 __asm(" AND CCR,#H'DF "); \
\r
162 __asm(" MOVW SP, A "); \
\r
163 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\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
183 __asm(" AND CCR,#H'DF "); \
\r
184 __asm(" POPW A "); \
\r
185 __asm(" OR CCR,#H'20 "); \
\r
186 __asm(" PUSHW A "); \
\r
189 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
191 #define portSAVE_CONTEXT() \
\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(" OR CCR,#H'20 "); \
\r
212 __asm(" POPW A "); \
\r
213 __asm(" AND CCR,#H'DF "); \
\r
214 __asm(" PUSHW A "); \
\r
215 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
216 __asm(" MOVL A, _pxCurrentTCB "); \
\r
217 __asm(" MOVL RL2, A "); \
\r
218 __asm(" MOVW A, SP "); \
\r
219 __asm(" MOVW @RL2+0, A "); \
\r
220 __asm(" MOV A, USB "); \
\r
221 __asm(" MOV @RL2+2, A "); \
\r
224 #define portRESTORE_CONTEXT() \
\r
225 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
226 __asm(" MOVL RL2, A "); \
\r
227 __asm(" MOVW A, @RL2+0 "); \
\r
228 __asm(" AND CCR,#H'DF "); \
\r
229 __asm(" MOVW SP, A "); \
\r
230 __asm(" MOV A, @RL2+2 "); \
\r
231 __asm(" MOV USB, A "); \
\r
232 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\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
252 __asm(" AND CCR,#H'DF "); \
\r
253 __asm(" POPW A "); \
\r
254 __asm(" OR CCR,#H'20 "); \
\r
255 __asm(" PUSHW A "); \
\r
259 /*-----------------------------------------------------------*/
\r
262 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
267 .GLOBAL _xGet_DPR_ADB_bank
\r
268 .GLOBAL _xGet_DTB_PCB_bank
\r
269 .SECTION CODE, CODE, ALIGN=1
\r
271 _xGet_DPR_ADB_bank:
\r
277 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
279 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
284 _xGet_DTB_PCB_bank:
\r
290 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
292 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
297 /*-----------------------------------------------------------*/
\r
300 * Initialise the stack of a task to look exactly as if a call to
\r
301 * portSAVE_CONTEXT had been called.
\r
303 * See the header file portable.h.
\r
305 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
307 /* Place a few bytes of known values on the bottom of the stack.
\r
308 This is just useful for debugging. */
\r
309 *pxTopOfStack = 0x1111;
\r
311 *pxTopOfStack = 0x2222;
\r
313 *pxTopOfStack = 0x3333;
\r
316 /* Once the task is called the task would push the pointer to the
\r
317 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
318 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
319 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
321 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
323 *pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
\r
328 *pxTopOfStack = ( StackType_t ) ( pvParameters );
\r
331 /* This is redundant push to the stack. This is required in order to introduce
\r
332 an offset so that the task accesses a parameter correctly that is passed on to
\r
334 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
336 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
341 /* This is redundant push to the stack. This is required in order to introduce
\r
342 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
343 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
346 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
347 *pxTopOfStack = 0xE0C0;
\r
351 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
355 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
357 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
362 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
363 along with PC to indicate the start address of the function. */
\r
364 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
366 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
372 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
376 *pxTopOfStack = ( StackType_t ) 0x9999;
\r
380 *pxTopOfStack = ( StackType_t ) 0xAAAA;
\r
383 /* Next the general purpose registers. */
\r
384 *pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
\r
386 *pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
\r
388 *pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
\r
390 *pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
\r
392 *pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
\r
394 *pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
\r
396 *pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
\r
398 *pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
\r
400 return pxTopOfStack;
\r
402 /*-----------------------------------------------------------*/
\r
404 static void prvSetupRLT0Interrupt( void )
\r
406 /* The peripheral clock divided by 16 is used by the timer. */
\r
407 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
409 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
410 TMRLR0 = usReloadValue;
\r
412 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
415 /*-----------------------------------------------------------*/
\r
417 BaseType_t xPortStartScheduler( void )
\r
419 /* Setup the hardware to generate the tick. */
\r
420 prvSetupRLT0Interrupt();
\r
422 /* Restore the context of the first task that is going to run. */
\r
423 portRESTORE_CONTEXT();
\r
425 /* Simulate a function call end as generated by the compiler. We will now
\r
426 jump to the start of the task the context of which we have just restored. */
\r
430 /* Should not get here. */
\r
433 /*-----------------------------------------------------------*/
\r
435 void vPortEndScheduler( void )
\r
437 /* Not implemented - unlikely to ever be required as there is nothing to
\r
441 /*-----------------------------------------------------------*/
\r
444 * The interrupt service routine used depends on whether the pre-emptive
\r
445 * scheduler is being used or not.
\r
448 #if configUSE_PREEMPTION == 1
\r
451 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
452 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
453 * compiler generated code. The tick count is incremented after the context
\r
456 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
458 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
461 /* Save the context of the interrupted task. */
\r
462 portSAVE_CONTEXT();
\r
464 /* Enable interrupts */
\r
467 /* Clear RLT0 interrupt flag */
\r
470 /* Increment the tick count then switch to the highest priority task
\r
471 that is ready to run. */
\r
472 if( xTaskIncrementTick() != pdFALSE )
\r
474 vTaskSwitchContext();
\r
477 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
480 /* Restore the context of the new task. */
\r
481 portRESTORE_CONTEXT();
\r
483 /* Enable interrupts */
\r
490 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
491 * tick count. We don't need to switch context, this can only be done by
\r
492 * manual calls to taskYIELD();
\r
494 __interrupt void prvRLT0_TICKISR( void )
\r
496 /* Clear RLT0 interrupt flag */
\r
499 xTaskIncrementTick();
\r
504 /*-----------------------------------------------------------*/
\r
507 * Manual context switch. We can use a __nosavereg attribute as the context
\r
508 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
511 __nosavereg __interrupt void vPortYield( void )
\r
513 /* Save the context of the interrupted task. */
\r
514 portSAVE_CONTEXT();
\r
516 /* Switch to the highest priority task that is ready to run. */
\r
517 vTaskSwitchContext();
\r
519 /* Restore the context of the new task. */
\r
520 portRESTORE_CONTEXT();
\r
522 /*-----------------------------------------------------------*/
\r
524 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
526 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
529 /* Save the context of the interrupted task. */
\r
530 portSAVE_CONTEXT();
\r
532 /* Enable interrupts */
\r
535 /* Clear delayed interrupt flag */
\r
536 __asm (" CLRB 03A4H:0 ");
\r
538 /* Switch to the highest priority task that is ready to run. */
\r
539 vTaskSwitchContext();
\r
541 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
544 /* Restore the context of the new task. */
\r
545 portRESTORE_CONTEXT();
\r
547 /* Enable interrupts */
\r
550 /*-----------------------------------------------------------*/
\r