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