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