]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RL78/port.c
2440bf0352aa39f8a697c1ddbe85996ef5ae0432
[freertos] / FreeRTOS / Source / portable / IAR / RL78 / port.c
1 /*\r
2     FreeRTOS V7.5.3 - Copyright (C) 2013 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Scheduler includes. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 \r
70 /* The critical nesting value is initialised to a non zero value to ensure\r
71 interrupts don't accidentally become enabled before the scheduler is started. */\r
72 #define portINITIAL_CRITICAL_NESTING  ( ( unsigned short ) 10 )\r
73 \r
74 /* Initial PSW value allocated to a newly created task.\r
75  *   1100011000000000\r
76  *   ||||||||-------------- Fill byte\r
77  *   |||||||--------------- Carry Flag cleared\r
78  *   |||||----------------- In-service priority Flags set to low level\r
79  *   ||||------------------ Register bank Select 0 Flag cleared\r
80  *   |||------------------- Auxiliary Carry Flag cleared\r
81  *   ||-------------------- Register bank Select 1 Flag cleared\r
82  *   |--------------------- Zero Flag set\r
83  *   ---------------------- Global Interrupt Flag set (enabled)\r
84  */\r
85 #define portPSW           ( 0xc6UL )\r
86 \r
87 /* The address of the pxCurrentTCB variable, but don't know or need to know its\r
88 type. */\r
89 typedef void tskTCB;\r
90 extern volatile tskTCB * volatile pxCurrentTCB;\r
91 \r
92 /* Each task maintains a count of the critical section nesting depth.  Each time\r
93 a critical section is entered the count is incremented.  Each time a critical\r
94 section is exited the count is decremented - with interrupts only being\r
95 re-enabled if the count is zero.\r
96 \r
97 usCriticalNesting will get set to zero when the scheduler starts, but must\r
98 not be initialised to zero as that could cause problems during the startup\r
99 sequence. */\r
100 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Sets up the periodic ISR used for the RTOS tick using the interval timer.\r
106  * The application writer can define configSETUP_TICK_INTERRUPT() (in\r
107  * FreeRTOSConfig.h) such that their own tick interrupt configuration is used\r
108  * in place of prvSetupTimerInterrupt().\r
109  */\r
110 static void prvSetupTimerInterrupt( void );\r
111 #ifndef configSETUP_TICK_INTERRUPT\r
112         /* The user has not provided their own tick interrupt configuration so use\r
113     the definition in this file (which uses the interval timer). */\r
114         #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()\r
115 #endif /* configSETUP_TICK_INTERRUPT */\r
116 \r
117 /*\r
118  * Defined in portasm.s87, this function starts the scheduler by loading the\r
119  * context of the first task to run.\r
120  */\r
121 extern void vPortStartFirstTask( void );\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * Initialise the stack of a task to look exactly as if a call to\r
127  * portSAVE_CONTEXT had been called.\r
128  *\r
129  * See the header file portable.h.\r
130  */\r
131 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
132 {\r
133 unsigned long *pulLocal;\r
134 \r
135         #if __DATA_MODEL__ == __DATA_MODEL_FAR__\r
136         {\r
137                 /* Parameters are passed in on the stack, and written using a 32bit value\r
138                 hence a space is left for the second two bytes. */\r
139                 pxTopOfStack--;\r
140 \r
141                 /* Write in the parameter value. */\r
142                 pulLocal =  ( unsigned long * ) pxTopOfStack;\r
143                 *pulLocal = ( unsigned long ) pvParameters;\r
144                 pxTopOfStack--;\r
145 \r
146                 /* These values are just spacers.  The return address of the function\r
147                 would normally be written here. */\r
148                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
149                 pxTopOfStack--;\r
150                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
151                 pxTopOfStack--;\r
152 \r
153                 /* The start address / PSW value is also written in as a 32bit value,\r
154                 so leave a space for the second two bytes. */\r
155                 pxTopOfStack--;\r
156 \r
157                 /* Task function start address combined with the PSW. */\r
158                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
159                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
160                 pxTopOfStack--;\r
161 \r
162                 /* An initial value for the AX register. */\r
163                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
164                 pxTopOfStack--;\r
165         }\r
166         #else\r
167         {\r
168                 /* Task function address is written to the stack first.  As it is\r
169                 written as a 32bit value a space is left on the stack for the second\r
170                 two bytes. */\r
171                 pxTopOfStack--;\r
172 \r
173                 /* Task function start address combined with the PSW. */\r
174                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
175                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
176                 pxTopOfStack--;\r
177 \r
178                 /* The parameter is passed in AX. */\r
179                 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
180                 pxTopOfStack--;\r
181         }\r
182         #endif\r
183 \r
184         /* An initial value for the HL register. */\r
185         *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
186         pxTopOfStack--;\r
187 \r
188         /* CS and ES registers. */\r
189         *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;\r
190         pxTopOfStack--;\r
191 \r
192         /* The remaining general purpose registers DE and BC */\r
193         *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;\r
194         pxTopOfStack--;\r
195         *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;\r
196         pxTopOfStack--;\r
197 \r
198         /* Finally the critical section nesting count is set to zero when the task\r
199         first starts. */\r
200         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;\r
201 \r
202         /* Return a pointer to the top of the stack that has been generated so it\r
203         can     be stored in the task control block for the task. */\r
204         return pxTopOfStack;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 portBASE_TYPE xPortStartScheduler( void )\r
209 {\r
210         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
211         this function is called. */\r
212         configSETUP_TICK_INTERRUPT();\r
213 \r
214         /* Restore the context of the first task that is going to run. */\r
215         vPortStartFirstTask();\r
216 \r
217         /* Execution should not reach here as the tasks are now running!\r
218         prvSetupTimerInterrupt() is called here to prevent the compiler outputting\r
219         a warning about a statically declared function not being referenced in the\r
220         case that the application writer has provided their own tick interrupt\r
221         configuration routine (and defined configSETUP_TICK_INTERRUPT() such that\r
222         their own routine will be called in place of prvSetupTimerInterrupt()). */\r
223         prvSetupTimerInterrupt();\r
224         return pdTRUE;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vPortEndScheduler( void )\r
229 {\r
230         /* It is unlikely that the RL78 port will get stopped. */\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void prvSetupTimerInterrupt( void )\r
235 {\r
236 const unsigned short usClockHz = 15000UL; /* Internal clock. */\r
237 const unsigned short usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
238 \r
239         /* Use the internal 15K clock. */\r
240         OSMC = ( unsigned char ) 0x16;\r
241 \r
242         #ifdef RTCEN\r
243         {\r
244                 /* Supply the interval timer clock. */\r
245                 RTCEN = ( unsigned char ) 1U;\r
246 \r
247                 /* Disable INTIT interrupt. */\r
248                 ITMK = ( unsigned char ) 1;\r
249 \r
250                 /* Disable ITMC operation. */\r
251                 ITMC = ( unsigned char ) 0x0000;\r
252 \r
253                 /* Clear INIT interrupt. */\r
254                 ITIF = ( unsigned char ) 0;\r
255 \r
256                 /* Set interval and enable interrupt operation. */\r
257                 ITMC = usCompareMatch | 0x8000U;\r
258 \r
259                 /* Enable INTIT interrupt. */\r
260                 ITMK = ( unsigned char ) 0;\r
261         }\r
262         #endif\r
263 \r
264         #ifdef TMKAEN\r
265         {\r
266                 /* Supply the interval timer clock. */\r
267                 TMKAEN = ( unsigned char ) 1U;\r
268 \r
269                 /* Disable INTIT interrupt. */\r
270                 TMKAMK = ( unsigned char ) 1;\r
271 \r
272                 /* Disable ITMC operation. */\r
273                 ITMC = ( unsigned char ) 0x0000;\r
274 \r
275                 /* Clear INIT interrupt. */\r
276                 TMKAIF = ( unsigned char ) 0;\r
277 \r
278                 /* Set interval and enable interrupt operation. */\r
279                 ITMC = usCompareMatch | 0x8000U;\r
280 \r
281                 /* Enable INTIT interrupt. */\r
282                 TMKAMK = ( unsigned char ) 0;\r
283         }\r
284         #endif\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r