1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
/*
FreeRTOS V8.1.0 - Copyright (C) 2014 Real Time Engineers Ltd.
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that has become a de facto standard. *
* *
* Help yourself get started quickly and support the FreeRTOS *
* project by purchasing a FreeRTOS tutorial book, reference *
* manual, or both from: http://www.FreeRTOS.org/Documentation *
* *
* Thank you! *
* *
***************************************************************************
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available from the following
link: http://www.freertos.org/a00114.html
1 tab == 4 spaces!
***************************************************************************
* *
* Having a problem? Start by reading the FAQ "My application does *
* not run, what could be wrong?" *
* *
* http://www.FreeRTOS.org/FAQHelp.html *
* *
***************************************************************************
http://www.FreeRTOS.org - Documentation, books, training, latest versions,
license and Real Time Engineers Ltd. contact details.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
#include "FreeRTOS.h"
#include "task.h"
/*-----------------------------------------------------------
* Implementation of functions defined in portable.h for the 16FX port.
*----------------------------------------------------------*/
/*
* Get current value of DPR and ADB registers
*/
StackType_t xGet_DPR_ADB_bank( void );
/*
* Get current value of DTB and PCB registers
*/
StackType_t xGet_DTB_PCB_bank( void );
/*
* Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
* can be done using any given RLT.
*/
static void prvSetupRLT0Interrupt( void );
/*-----------------------------------------------------------*/
/*
* We require the address of the pxCurrentTCB variable, but don't want to know
* any details of its type.
*/
typedef void TCB_t;
extern volatile TCB_t * volatile pxCurrentTCB;
/*-----------------------------------------------------------*/
/*
* Macro to save a task context to the task stack. This macro copies the
* saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
* stack to task stack pointed by user stack pointer ( USP for SMALL and
* MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
* then it pushes the general purpose registers RW0-RW7 on to the task
* stack. Finally the resultant stack pointer value is saved into the
* task control block so it can be retrieved the next time the task
* executes.
*/
#if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
#define portSAVE_CONTEXT() \
{ __asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
__asm(" MOVW A, _pxCurrentTCB "); \
__asm(" MOVW A, SP "); \
__asm(" SWAPW "); \
__asm(" MOVW @AL, AH "); \
__asm(" OR CCR,#H'20 "); \
}
/*
* Macro to restore a task context from the task stack. This is effecti-
* vely the reverse of SAVE_CONTEXT(). First the stack pointer value
* (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
* LARGE memory model ) is loaded from the task control block. Next the
* value of all the general purpose registers RW0-RW7 is retrieved. Fina-
* lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
* the task to be executed upon RETI from user stack to system stack.
*/
#define portRESTORE_CONTEXT() \
{ __asm(" MOVW A, _pxCurrentTCB "); \
__asm(" MOVW A, @A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" MOVW SP, A "); \
__asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
}
#elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
#define portSAVE_CONTEXT() \
{ __asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" POPW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" PUSHW A "); \
__asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
__asm(" MOVL A, _pxCurrentTCB "); \
__asm(" MOVL RL2, A "); \
__asm(" MOVW A, SP "); \
__asm(" MOVW @RL2+0, A "); \
__asm(" MOV A, USB "); \
__asm(" MOV @RL2+2, A "); \
}
#define portRESTORE_CONTEXT() \
{ __asm(" MOVL A, _pxCurrentTCB "); \
__asm(" MOVL RL2, A "); \
__asm(" MOVW A, @RL2+0 "); \
__asm(" AND CCR,#H'DF "); \
__asm(" MOVW SP, A "); \
__asm(" MOV A, @RL2+2 "); \
__asm(" MOV USB, A "); \
__asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
__asm(" AND CCR,#H'DF "); \
__asm(" POPW A "); \
__asm(" OR CCR,#H'20 "); \
__asm(" PUSHW A "); \
}
#endif
/*-----------------------------------------------------------*/
/*
* Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
*/
#pragma asm
.GLOBAL _xGet_DPR_ADB_bank
.GLOBAL _xGet_DTB_PCB_bank
.SECTION CODE, CODE, ALIGN=1
_xGet_DPR_ADB_bank:
MOV A, DPR
SWAP
MOV A, ADB
ORW A
#if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
RETP
#elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
RET
#endif
_xGet_DTB_PCB_bank:
MOV A, DTB
SWAP
MOV A, PCB
ORW A
#if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
RETP
#elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
RET
#endif
#pragma endasm
/*-----------------------------------------------------------*/
/*
* Initialise the stack of a task to look exactly as if a call to
* portSAVE_CONTEXT had been called.
*
* See the header file portable.h.
*/
StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
{
/* Place a few bytes of known values on the bottom of the stack.
This is just useful for debugging. */
*pxTopOfStack = 0x1111;
pxTopOfStack--;
*pxTopOfStack = 0x2222;
pxTopOfStack--;
*pxTopOfStack = 0x3333;
pxTopOfStack--;
/* Once the task is called the task would push the pointer to the
parameter onto the stack. Hence here the pointer would be copied to the stack
first. When using the COMPACT or LARGE memory model the pointer would be 24
bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
bits. */
#if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
{
*pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
pxTopOfStack--;
}
#endif
*pxTopOfStack = ( StackType_t ) ( pvParameters );
pxTopOfStack--;
/* This is redundant push to the stack. This is required in order to introduce
an offset so that the task accesses a parameter correctly that is passed on to
the task stack. */
#if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
{
*pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
pxTopOfStack--;
}
#endif
/* This is redundant push to the stack. This is required in order to introduce
an offset so the task correctly accesses the parameter passed on the task stack. */
*pxTopOfStack = ( StackType_t ) ( pxCode );
pxTopOfStack--;
/* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
*pxTopOfStack = 0xE0C0;
pxTopOfStack--;
/* PC */
*pxTopOfStack = ( StackType_t ) ( pxCode );
pxTopOfStack--;
/* DTB | PCB */
#if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
{
*pxTopOfStack = xGet_DTB_PCB_bank();
pxTopOfStack--;
}
#endif
/* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
along with PC to indicate the start address of the function. */
#if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
{
*pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
pxTopOfStack--;
}
#endif
/* DPR | ADB */
*pxTopOfStack = xGet_DPR_ADB_bank();
pxTopOfStack--;
/* AL */
*pxTopOfStack = ( StackType_t ) 0x9999;
pxTopOfStack--;
/* AH */
*pxTopOfStack = ( StackType_t ) 0xAAAA;
pxTopOfStack--;
/* Next the general purpose registers. */
*pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
pxTopOfStack--;
*pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
return pxTopOfStack;
}
/*-----------------------------------------------------------*/
static void prvSetupRLT0Interrupt( void )
{
/* The peripheral clock divided by 16 is used by the timer. */
const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
/* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
TMRLR0 = usReloadValue;
/* prescaler 1:16, reload, interrupt enable, count enable, trigger */
TMCSR0 = 0x041B;
}
/*-----------------------------------------------------------*/
BaseType_t xPortStartScheduler( void )
{
/* Setup the hardware to generate the tick. */
prvSetupRLT0Interrupt();
/* Restore the context of the first task that is going to run. */
portRESTORE_CONTEXT();
/* Simulate a function call end as generated by the compiler. We will now
jump to the start of the task the context of which we have just restored. */
__asm(" reti ");
/* Should not get here. */
return pdTRUE;
}
/*-----------------------------------------------------------*/
void vPortEndScheduler( void )
{
/* Not implemented - unlikely to ever be required as there is nothing to
return to. */
}
/*-----------------------------------------------------------*/
/*
* The interrupt service routine used depends on whether the pre-emptive
* scheduler is being used or not.
*/
#if configUSE_PREEMPTION == 1
/*
* Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
* as the context is to be saved by the portSAVE_CONTEXT() macro, not the
* compiler generated code. The tick count is incremented after the context
* is saved.
*/
__nosavereg __interrupt void prvRLT0_TICKISR( void )
{
/* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
__DI();
/* Save the context of the interrupted task. */
portSAVE_CONTEXT();
/* Enable interrupts */
__EI();
/* Clear RLT0 interrupt flag */
TMCSR0_UF = 0;
/* Increment the tick count then switch to the highest priority task
that is ready to run. */
if( xTaskIncrementTick() != pdFALSE )
{
vTaskSwitchContext();
}
/* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
__DI();
/* Restore the context of the new task. */
portRESTORE_CONTEXT();
/* Enable interrupts */
__EI();
}
#else
/*
* Tick ISR for the cooperative scheduler. All this does is increment the
* tick count. We don't need to switch context, this can only be done by
* manual calls to taskYIELD();
*/
__interrupt void prvRLT0_TICKISR( void )
{
/* Clear RLT0 interrupt flag */
TMCSR0_UF = 0;
xTaskIncrementTick();
}
#endif
/*-----------------------------------------------------------*/
/*
* Manual context switch. We can use a __nosavereg attribute as the context
* is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
* code.
*/
__nosavereg __interrupt void vPortYield( void )
{
/* Save the context of the interrupted task. */
portSAVE_CONTEXT();
/* Switch to the highest priority task that is ready to run. */
vTaskSwitchContext();
/* Restore the context of the new task. */
portRESTORE_CONTEXT();
}
/*-----------------------------------------------------------*/
__nosavereg __interrupt void vPortYieldDelayed( void )
{
/* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
__DI();
/* Save the context of the interrupted task. */
portSAVE_CONTEXT();
/* Enable interrupts */
__EI();
/* Clear delayed interrupt flag */
__asm (" CLRB 03A4H:0 ");
/* Switch to the highest priority task that is ready to run. */
vTaskSwitchContext();
/* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
__DI();
/* Restore the context of the new task. */
portRESTORE_CONTEXT();
/* Enable interrupts */
__EI();
}
/*-----------------------------------------------------------*/
|