]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/78K0R/port.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Source / portable / IAR / 78K0R / port.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Standard includes. */\r
68 #include <stdlib.h>\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* The critical nesting value is initialised to a non zero value to ensure\r
75 interrupts don't accidentally become enabled before the scheduler is started. */\r
76 #define portINITIAL_CRITICAL_NESTING  (( unsigned short ) 10)\r
77 \r
78 /* Initial PSW value allocated to a newly created task.\r
79  *   1100011000000000\r
80  *   ||||||||-------------- Fill byte\r
81  *   |||||||--------------- Carry Flag cleared\r
82  *   |||||----------------- In-service priority Flags set to low level\r
83  *   ||||------------------ Register bank Select 0 Flag cleared\r
84  *   |||------------------- Auxiliary Carry Flag cleared\r
85  *   ||-------------------- Register bank Select 1 Flag cleared\r
86  *   |--------------------- Zero Flag set\r
87  *   ---------------------- Global Interrupt Flag set (enabled)\r
88  */\r
89 #define portPSW           (0xc6UL)\r
90 \r
91 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
92 any details of its type. */\r
93 typedef void tskTCB;\r
94 extern volatile tskTCB * volatile pxCurrentTCB;\r
95 \r
96 /* Most ports implement critical sections by placing the interrupt flags on\r
97 the stack before disabling interrupts.  Exiting the critical section is then\r
98 simply a case of popping the flags from the stack.  As 78K0 IAR does not use\r
99 a frame pointer this cannot be done as modifying the stack will clobber all\r
100 the stack variables.  Instead each task maintains a count of the critical\r
101 section nesting depth.  Each time a critical section is entered the count is\r
102 incremented.  Each time a critical section is left the count is decremented -\r
103 with interrupts only being re-enabled if the count is zero.\r
104 \r
105 usCriticalNesting will get set to zero when the scheduler starts, but must\r
106 not be initialised to zero as this will cause problems during the startup\r
107 sequence. */\r
108 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /*\r
112  * Sets up the periodic ISR used for the RTOS tick.\r
113  */\r
114 static void prvSetupTimerInterrupt( void );\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * Initialise the stack of a task to look exactly as if a call to\r
119  * portSAVE_CONTEXT had been called.\r
120  *\r
121  * See the header file portable.h.\r
122  */\r
123 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
124 {\r
125 unsigned long *pulLocal;\r
126 \r
127         #if configMEMORY_MODE == 1\r
128         {\r
129                 /* Parameters are passed in on the stack, and written using a 32bit value\r
130                 hence a space is left for the second two bytes. */\r
131                 pxTopOfStack--;\r
132 \r
133                 /* Write in the parameter value. */\r
134                 pulLocal =  ( unsigned long * ) pxTopOfStack;\r
135                 *pulLocal = ( unsigned long ) pvParameters;\r
136                 pxTopOfStack--;\r
137 \r
138                 /* These values are just spacers.  The return address of the function\r
139                 would normally be written here. */\r
140                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
141                 pxTopOfStack--;\r
142                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
143                 pxTopOfStack--;\r
144 \r
145                 /* The start address / PSW value is also written in as a 32bit value,\r
146                 so leave a space for the second two bytes. */\r
147                 pxTopOfStack--;\r
148         \r
149                 /* Task function start address combined with the PSW. */\r
150                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
151                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
152                 pxTopOfStack--;\r
153 \r
154                 /* An initial value for the AX register. */\r
155                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
156                 pxTopOfStack--;\r
157         }\r
158         #else\r
159         {\r
160                 /* Task function address is written to the stack first.  As it is\r
161                 written as a 32bit value a space is left on the stack for the second\r
162                 two bytes. */\r
163                 pxTopOfStack--;\r
164 \r
165                 /* Task function start address combined with the PSW. */\r
166                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
167                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
168                 pxTopOfStack--;\r
169 \r
170                 /* The parameter is passed in AX. */\r
171                 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
172                 pxTopOfStack--;\r
173         }\r
174         #endif\r
175 \r
176         /* An initial value for the HL register. */\r
177         *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
178         pxTopOfStack--;\r
179 \r
180         /* CS and ES registers. */\r
181         *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;\r
182         pxTopOfStack--;\r
183 \r
184         /* Finally the remaining general purpose registers DE and BC */\r
185         *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;\r
188         pxTopOfStack--;\r
189 \r
190         /* Finally the critical section nesting count is set to zero when the task\r
191         first starts. */\r
192         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
193 \r
194         /* Return a pointer to the top of the stack we have generated so this can\r
195         be stored in the task control block for the task. */\r
196         return pxTopOfStack;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 portBASE_TYPE xPortStartScheduler( void )\r
201 {\r
202         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
203         this function is called. */\r
204         prvSetupTimerInterrupt();\r
205 \r
206         /* Restore the context of the first task that is going to run. */\r
207         vPortStart();\r
208 \r
209         /* Should not get here as the tasks are now running! */\r
210         return pdTRUE;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 void vPortEndScheduler( void )\r
215 {\r
216         /* It is unlikely that the 78K0R port will get stopped.  If required simply\r
217         disable the tick interrupt here. */\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvSetupTimerInterrupt( void )\r
222 {\r
223         /* Setup channel 5 of the TAU to generate the tick interrupt. */\r
224 \r
225         /* First the Timer Array Unit has to be enabled. */\r
226         TAU0EN = 1;\r
227 \r
228         /* To configure the Timer Array Unit all Channels have to first be stopped. */\r
229         TT0 = 0xff;\r
230 \r
231         /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt\r
232         priority. */\r
233         TMMK05 = 1;\r
234 \r
235         /* Clear Timer Array Unit Channel 5 interrupt flag. */  \r
236         TMIF05 = 0;\r
237 \r
238         /* Set Timer Array Unit Channel 5 interrupt priority */\r
239         TMPR005 = 0;\r
240         TMPR105 = 0;\r
241 \r
242         /* Set Timer Array Unit Channel 5 Mode as interval timer. */\r
243         TMR05 = 0x0000;\r
244 \r
245         /* Set the compare match value according to the tick rate we want. */\r
246         TDR05 = ( portTickType ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
247 \r
248         /* Set Timer Array Unit Channel 5 output mode */\r
249         TOM0 &= ~0x0020;\r
250 \r
251         /* Set Timer Array Unit Channel 5 output level */       \r
252         TOL0 &= ~0x0020;\r
253 \r
254         /* Set Timer Array Unit Channel 5 output enable */      \r
255         TOE0 &= ~0x0020;\r
256 \r
257         /* Interrupt of Timer Array Unit Channel 5 enabled */\r
258         TMMK05 = 0;\r
259 \r
260         /* Start Timer Array Unit Channel 5.*/\r
261         TS0 |= 0x0020;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r