]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RL78/port.c
8cfb7501383d4963fc4be4b21342e346d0c3d9a4
[freertos] / FreeRTOS / Source / portable / IAR / RL78 / port.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     ***************************************************************************\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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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  ( ( uint16_t ) 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 TCB_t;\r
90 extern volatile TCB_t * 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 uint16_t 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  * Used to catch tasks that attempt to return from their implementing function.\r
125  */\r
126 static void prvTaskExitError( void );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /*\r
131  * Initialise the stack of a task to look exactly as if a call to\r
132  * portSAVE_CONTEXT had been called.\r
133  *\r
134  * See the header file portable.h.\r
135  */\r
136 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
137 {\r
138 uint32_t *pulLocal;\r
139 \r
140         /* With large code and large data sizeof( StackType_t ) == 2, and\r
141         sizeof( StackType_t * ) == 4.  With small code and small data\r
142         sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */\r
143 \r
144         #if __DATA_MODEL__ == __DATA_MODEL_FAR__\r
145         {\r
146                 /* Parameters are passed in on the stack, and written using a 32-bit value\r
147                 hence a space is left for the second two bytes. */\r
148                 pxTopOfStack--;\r
149 \r
150                 /* Write in the parameter value. */\r
151                 pulLocal =  ( uint32_t * ) pxTopOfStack;\r
152                 *pulLocal = ( uint32_t ) pvParameters;\r
153                 pxTopOfStack--;\r
154 \r
155                 /* The return address, leaving space for the first two bytes of the\r
156                 32-bit value.  See the comments above the prvTaskExitError() prototype\r
157                 at the top of this file. */\r
158                 pxTopOfStack--;\r
159                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
160                 *pulLocal = ( uint32_t ) prvTaskExitError;\r
161                 pxTopOfStack--;\r
162 \r
163                 /* The start address / PSW value is also written in as a 32-bit value,\r
164                 so leave a space for the second two bytes. */\r
165                 pxTopOfStack--;\r
166 \r
167                 /* Task function start address combined with the PSW. */\r
168                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
169                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
170                 pxTopOfStack--;\r
171 \r
172                 /* An initial value for the AX register. */\r
173                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
174                 pxTopOfStack--;\r
175         }\r
176         #else\r
177         {\r
178                 /* The return address, leaving space for the first two bytes of the\r
179                 32-bit value.  See the comments above the prvTaskExitError() prototype\r
180                 at the top of this file. */\r
181                 pxTopOfStack--;\r
182                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
183                 *pulLocal = ( uint32_t ) prvTaskExitError;\r
184                 pxTopOfStack--;\r
185 \r
186                 /* Task function.  Again as it is written as a 32-bit value a space is\r
187                 left on the stack for the second two bytes. */\r
188                 pxTopOfStack--;\r
189 \r
190                 /* Task function start address combined with the PSW. */\r
191                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
192                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
193                 pxTopOfStack--;\r
194 \r
195                 /* The parameter is passed in AX. */\r
196                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
197                 pxTopOfStack--;\r
198         }\r
199         #endif\r
200 \r
201         /* An initial value for the HL register. */\r
202         *pxTopOfStack = ( StackType_t ) 0x2222;\r
203         pxTopOfStack--;\r
204 \r
205         /* CS and ES registers. */\r
206         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
207         pxTopOfStack--;\r
208 \r
209         /* The remaining general purpose registers DE and BC */\r
210         *pxTopOfStack = ( StackType_t ) 0xDEDE;\r
211         pxTopOfStack--;\r
212         *pxTopOfStack = ( StackType_t ) 0xBCBC;\r
213         pxTopOfStack--;\r
214 \r
215         /* Finally the critical section nesting count is set to zero when the task\r
216         first starts. */\r
217         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;\r
218 \r
219         /* Return a pointer to the top of the stack that has been generated so it\r
220         can     be stored in the task control block for the task. */\r
221         return pxTopOfStack;\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static void prvTaskExitError( void )\r
226 {\r
227         /* A function that implements a task must not exit or attempt to return to\r
228         its caller as there is nothing to return to.  If a task wants to exit it\r
229         should instead call vTaskDelete( NULL ).\r
230 \r
231         Artificially force an assert() to be triggered if configASSERT() is\r
232         defined, then stop here so application writers can catch the error. */\r
233         configASSERT( usCriticalNesting == ~0U );\r
234         portDISABLE_INTERRUPTS();\r
235         for( ;; );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 BaseType_t xPortStartScheduler( void )\r
240 {\r
241         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
242         this function is called. */\r
243         configSETUP_TICK_INTERRUPT();\r
244 \r
245         /* Restore the context of the first task that is going to run. */\r
246         vPortStartFirstTask();\r
247 \r
248         /* Execution should not reach here as the tasks are now running!\r
249         prvSetupTimerInterrupt() is called here to prevent the compiler outputting\r
250         a warning about a statically declared function not being referenced in the\r
251         case that the application writer has provided their own tick interrupt\r
252         configuration routine (and defined configSETUP_TICK_INTERRUPT() such that\r
253         their own routine will be called in place of prvSetupTimerInterrupt()). */\r
254         prvSetupTimerInterrupt();\r
255         return pdTRUE;\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 void vPortEndScheduler( void )\r
260 {\r
261         /* It is unlikely that the RL78 port will get stopped. */\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static void prvSetupTimerInterrupt( void )\r
266 {\r
267 const uint16_t usClockHz = 15000UL; /* Internal clock. */\r
268 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
269 \r
270         /* Use the internal 15K clock. */\r
271         OSMC = ( uint8_t ) 0x16;\r
272 \r
273         #ifdef RTCEN\r
274         {\r
275                 /* Supply the interval timer clock. */\r
276                 RTCEN = ( uint8_t ) 1U;\r
277 \r
278                 /* Disable INTIT interrupt. */\r
279                 ITMK = ( uint8_t ) 1;\r
280 \r
281                 /* Disable ITMC operation. */\r
282                 ITMC = ( uint8_t ) 0x0000;\r
283 \r
284                 /* Clear INIT interrupt. */\r
285                 ITIF = ( uint8_t ) 0;\r
286 \r
287                 /* Set interval and enable interrupt operation. */\r
288                 ITMC = usCompareMatch | 0x8000U;\r
289 \r
290                 /* Enable INTIT interrupt. */\r
291                 ITMK = ( uint8_t ) 0;\r
292         }\r
293         #endif\r
294 \r
295         #ifdef TMKAEN\r
296         {\r
297                 /* Supply the interval timer clock. */\r
298                 TMKAEN = ( uint8_t ) 1U;\r
299 \r
300                 /* Disable INTIT interrupt. */\r
301                 TMKAMK = ( uint8_t ) 1;\r
302 \r
303                 /* Disable ITMC operation. */\r
304                 ITMC = ( uint8_t ) 0x0000;\r
305 \r
306                 /* Clear INIT interrupt. */\r
307                 TMKAIF = ( uint8_t ) 0;\r
308 \r
309                 /* Set interval and enable interrupt operation. */\r
310                 ITMC = usCompareMatch | 0x8000U;\r
311 \r
312                 /* Enable INTIT interrupt. */\r
313                 TMKAMK = ( uint8_t ) 0;\r
314         }\r
315         #endif\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r