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