]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/78K0R/port.c
05a9fa1571a676f5e9ee5b4d57cd56dffbd6e7b2
[freertos] / FreeRTOS / Source / portable / IAR / 78K0R / port.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* Standard includes. */\r
97 #include <stdlib.h>\r
98 \r
99 /* Scheduler includes. */\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 \r
103 /* The critical nesting value is initialised to a non zero value to ensure\r
104 interrupts don't accidentally become enabled before the scheduler is started. */\r
105 #define portINITIAL_CRITICAL_NESTING  (( uint16_t ) 10)\r
106 \r
107 /* Initial PSW value allocated to a newly created task.\r
108  *   1100011000000000\r
109  *   ||||||||-------------- Fill byte\r
110  *   |||||||--------------- Carry Flag cleared\r
111  *   |||||----------------- In-service priority Flags set to low level\r
112  *   ||||------------------ Register bank Select 0 Flag cleared\r
113  *   |||------------------- Auxiliary Carry Flag cleared\r
114  *   ||-------------------- Register bank Select 1 Flag cleared\r
115  *   |--------------------- Zero Flag set\r
116  *   ---------------------- Global Interrupt Flag set (enabled)\r
117  */\r
118 #define portPSW           (0xc6UL)\r
119 \r
120 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
121 any details of its type. */\r
122 typedef void TCB_t;\r
123 extern volatile TCB_t * volatile pxCurrentTCB;\r
124 \r
125 /* Most ports implement critical sections by placing the interrupt flags on\r
126 the stack before disabling interrupts.  Exiting the critical section is then\r
127 simply a case of popping the flags from the stack.  As 78K0 IAR does not use\r
128 a frame pointer this cannot be done as modifying the stack will clobber all\r
129 the stack variables.  Instead each task maintains a count of the critical\r
130 section nesting depth.  Each time a critical section is entered the count is\r
131 incremented.  Each time a critical section is left the count is decremented -\r
132 with interrupts only being re-enabled if the count is zero.\r
133 \r
134 usCriticalNesting will get set to zero when the scheduler starts, but must\r
135 not be initialised to zero as this will cause problems during the startup\r
136 sequence. */\r
137 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /*\r
141  * Sets up the periodic ISR used for the RTOS tick.\r
142  */\r
143 static void prvSetupTimerInterrupt( void );\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 /*\r
147  * Initialise the stack of a task to look exactly as if a call to\r
148  * portSAVE_CONTEXT had been called.\r
149  *\r
150  * See the header file portable.h.\r
151  */\r
152 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
153 {\r
154 uint32_t *pulLocal;\r
155 \r
156         #if configMEMORY_MODE == 1\r
157         {\r
158                 /* Parameters are passed in on the stack, and written using a 32bit value\r
159                 hence a space is left for the second two bytes. */\r
160                 pxTopOfStack--;\r
161 \r
162                 /* Write in the parameter value. */\r
163                 pulLocal =  ( uint32_t * ) pxTopOfStack;\r
164                 *pulLocal = ( uint32_t ) pvParameters;\r
165                 pxTopOfStack--;\r
166 \r
167                 /* These values are just spacers.  The return address of the function\r
168                 would normally be written here. */\r
169                 *pxTopOfStack = ( StackType_t ) 0xcdcd;\r
170                 pxTopOfStack--;\r
171                 *pxTopOfStack = ( StackType_t ) 0xcdcd;\r
172                 pxTopOfStack--;\r
173 \r
174                 /* The start address / PSW value is also written in as a 32bit value,\r
175                 so leave a space for the second two bytes. */\r
176                 pxTopOfStack--;\r
177         \r
178                 /* Task function start address combined with the PSW. */\r
179                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
180                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
181                 pxTopOfStack--;\r
182 \r
183                 /* An initial value for the AX register. */\r
184                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
185                 pxTopOfStack--;\r
186         }\r
187         #else\r
188         {\r
189                 /* Task function address is written to the stack first.  As it is\r
190                 written as a 32bit value a space is left on the stack for the second\r
191                 two bytes. */\r
192                 pxTopOfStack--;\r
193 \r
194                 /* Task function start address combined with the PSW. */\r
195                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
196                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
197                 pxTopOfStack--;\r
198 \r
199                 /* The parameter is passed in AX. */\r
200                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
201                 pxTopOfStack--;\r
202         }\r
203         #endif\r
204 \r
205         /* An initial value for the HL register. */\r
206         *pxTopOfStack = ( StackType_t ) 0x2222;\r
207         pxTopOfStack--;\r
208 \r
209         /* CS and ES registers. */\r
210         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
211         pxTopOfStack--;\r
212 \r
213         /* Finally the remaining general purpose registers DE and BC */\r
214         *pxTopOfStack = ( StackType_t ) 0xDEDE;\r
215         pxTopOfStack--;\r
216         *pxTopOfStack = ( StackType_t ) 0xBCBC;\r
217         pxTopOfStack--;\r
218 \r
219         /* Finally the critical section nesting count is set to zero when the task\r
220         first starts. */\r
221         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;        \r
222 \r
223         /* Return a pointer to the top of the stack we have generated so this can\r
224         be stored in the task control block for the task. */\r
225         return pxTopOfStack;\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 BaseType_t xPortStartScheduler( void )\r
230 {\r
231         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
232         this function is called. */\r
233         prvSetupTimerInterrupt();\r
234 \r
235         /* Restore the context of the first task that is going to run. */\r
236         vPortStart();\r
237 \r
238         /* Should not get here as the tasks are now running! */\r
239         return pdTRUE;\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vPortEndScheduler( void )\r
244 {\r
245         /* It is unlikely that the 78K0R port will get stopped.  If required simply\r
246         disable the tick interrupt here. */\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 static void prvSetupTimerInterrupt( void )\r
251 {\r
252         /* Setup channel 5 of the TAU to generate the tick interrupt. */\r
253 \r
254         /* First the Timer Array Unit has to be enabled. */\r
255         TAU0EN = 1;\r
256 \r
257         /* To configure the Timer Array Unit all Channels have to first be stopped. */\r
258         TT0 = 0xff;\r
259 \r
260         /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt\r
261         priority. */\r
262         TMMK05 = 1;\r
263 \r
264         /* Clear Timer Array Unit Channel 5 interrupt flag. */  \r
265         TMIF05 = 0;\r
266 \r
267         /* Set Timer Array Unit Channel 5 interrupt priority */\r
268         TMPR005 = 0;\r
269         TMPR105 = 0;\r
270 \r
271         /* Set Timer Array Unit Channel 5 Mode as interval timer. */\r
272         TMR05 = 0x0000;\r
273 \r
274         /* Set the compare match value according to the tick rate we want. */\r
275         TDR05 = ( TickType_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
276 \r
277         /* Set Timer Array Unit Channel 5 output mode */\r
278         TOM0 &= ~0x0020;\r
279 \r
280         /* Set Timer Array Unit Channel 5 output level */       \r
281         TOL0 &= ~0x0020;\r
282 \r
283         /* Set Timer Array Unit Channel 5 output enable */      \r
284         TOE0 &= ~0x0020;\r
285 \r
286         /* Interrupt of Timer Array Unit Channel 5 enabled */\r
287         TMMK05 = 0;\r
288 \r
289         /* Start Timer Array Unit Channel 5.*/\r
290         TS0 |= 0x0020;\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r