]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RL78/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / IAR / RL78 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Scheduler includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 \r
33 /* The critical nesting value is initialised to a non zero value to ensure\r
34 interrupts don't accidentally become enabled before the scheduler is started. */\r
35 #define portINITIAL_CRITICAL_NESTING  ( ( uint16_t ) 10 )\r
36 \r
37 /* Initial PSW value allocated to a newly created task.\r
38  *   1100011000000000\r
39  *   ||||||||-------------- Fill byte\r
40  *   |||||||--------------- Carry Flag cleared\r
41  *   |||||----------------- In-service priority Flags set to low level\r
42  *   ||||------------------ Register bank Select 0 Flag cleared\r
43  *   |||------------------- Auxiliary Carry Flag cleared\r
44  *   ||-------------------- Register bank Select 1 Flag cleared\r
45  *   |--------------------- Zero Flag set\r
46  *   ---------------------- Global Interrupt Flag set (enabled)\r
47  */\r
48 #define portPSW           ( 0xc6UL )\r
49 \r
50 /* The address of the pxCurrentTCB variable, but don't know or need to know its\r
51 type. */\r
52 typedef void TCB_t;\r
53 extern volatile TCB_t * volatile pxCurrentTCB;\r
54 \r
55 /* Each task maintains a count of the critical section nesting depth.  Each time\r
56 a critical section is entered the count is incremented.  Each time a critical\r
57 section is exited the count is decremented - with interrupts only being\r
58 re-enabled if the count is zero.\r
59 \r
60 usCriticalNesting will get set to zero when the scheduler starts, but must\r
61 not be initialised to zero as that could cause problems during the startup\r
62 sequence. */\r
63 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /*\r
68  * Sets up the periodic ISR used for the RTOS tick using the interval timer.\r
69  * The application writer can define configSETUP_TICK_INTERRUPT() (in\r
70  * FreeRTOSConfig.h) such that their own tick interrupt configuration is used\r
71  * in place of prvSetupTimerInterrupt().\r
72  */\r
73 static void prvSetupTimerInterrupt( void );\r
74 #ifndef configSETUP_TICK_INTERRUPT\r
75         /* The user has not provided their own tick interrupt configuration so use\r
76     the definition in this file (which uses the interval timer). */\r
77         #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()\r
78 #endif /* configSETUP_TICK_INTERRUPT */\r
79 \r
80 /*\r
81  * Defined in portasm.s87, this function starts the scheduler by loading the\r
82  * context of the first task to run.\r
83  */\r
84 extern void vPortStartFirstTask( void );\r
85 \r
86 /*\r
87  * Used to catch tasks that attempt to return from their implementing function.\r
88  */\r
89 static void prvTaskExitError( void );\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /*\r
94  * Initialise the stack of a task to look exactly as if a call to\r
95  * portSAVE_CONTEXT had been called.\r
96  *\r
97  * See the header file portable.h.\r
98  */\r
99 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
100 {\r
101 uint32_t *pulLocal;\r
102 \r
103         /* With large code and large data sizeof( StackType_t ) == 2, and\r
104         sizeof( StackType_t * ) == 4.  With small code and small data\r
105         sizeof( StackType_t ) == 2 and sizeof( StackType_t * ) == 2. */\r
106 \r
107         #if __DATA_MODEL__ == __DATA_MODEL_FAR__\r
108         {\r
109                 /* Parameters are passed in on the stack, and written using a 32-bit value\r
110                 hence a space is left for the second two bytes. */\r
111                 pxTopOfStack--;\r
112 \r
113                 /* Write in the parameter value. */\r
114                 pulLocal =  ( uint32_t * ) pxTopOfStack;\r
115                 *pulLocal = ( uint32_t ) pvParameters;\r
116                 pxTopOfStack--;\r
117 \r
118                 /* The return address, leaving space for the first two bytes of the\r
119                 32-bit value.  See the comments above the prvTaskExitError() prototype\r
120                 at the top of this file. */\r
121                 pxTopOfStack--;\r
122                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
123                 *pulLocal = ( uint32_t ) prvTaskExitError;\r
124                 pxTopOfStack--;\r
125 \r
126                 /* The start address / PSW value is also written in as a 32-bit value,\r
127                 so leave a space for the second two bytes. */\r
128                 pxTopOfStack--;\r
129 \r
130                 /* Task function start address combined with the PSW. */\r
131                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
132                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
133                 pxTopOfStack--;\r
134 \r
135                 /* An initial value for the AX register. */\r
136                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
137                 pxTopOfStack--;\r
138         }\r
139         #else\r
140         {\r
141                 /* The return address, leaving space for the first two bytes of the\r
142                 32-bit value.  See the comments above the prvTaskExitError() prototype\r
143                 at the top of this file. */\r
144                 pxTopOfStack--;\r
145                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
146                 *pulLocal = ( uint32_t ) prvTaskExitError;\r
147                 pxTopOfStack--;\r
148 \r
149                 /* Task function.  Again as it is written as a 32-bit value a space is\r
150                 left on the stack for the second two bytes. */\r
151                 pxTopOfStack--;\r
152 \r
153                 /* Task function start address combined with the PSW. */\r
154                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
155                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
156                 pxTopOfStack--;\r
157 \r
158                 /* The parameter is passed in AX. */\r
159                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
160                 pxTopOfStack--;\r
161         }\r
162         #endif\r
163 \r
164         /* An initial value for the HL register. */\r
165         *pxTopOfStack = ( StackType_t ) 0x2222;\r
166         pxTopOfStack--;\r
167 \r
168         /* CS and ES registers. */\r
169         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
170         pxTopOfStack--;\r
171 \r
172         /* The remaining general purpose registers DE and BC */\r
173         *pxTopOfStack = ( StackType_t ) 0xDEDE;\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = ( StackType_t ) 0xBCBC;\r
176         pxTopOfStack--;\r
177 \r
178         /* Finally the critical section nesting count is set to zero when the task\r
179         first starts. */\r
180         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;\r
181 \r
182         /* Return a pointer to the top of the stack that has been generated so it\r
183         can     be stored in the task control block for the task. */\r
184         return pxTopOfStack;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void prvTaskExitError( void )\r
189 {\r
190         /* A function that implements a task must not exit or attempt to return to\r
191         its caller as there is nothing to return to.  If a task wants to exit it\r
192         should instead call vTaskDelete( NULL ).\r
193 \r
194         Artificially force an assert() to be triggered if configASSERT() is\r
195         defined, then stop here so application writers can catch the error. */\r
196         configASSERT( usCriticalNesting == ~0U );\r
197         portDISABLE_INTERRUPTS();\r
198         for( ;; );\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 BaseType_t xPortStartScheduler( void )\r
203 {\r
204         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
205         this function is called. */\r
206         configSETUP_TICK_INTERRUPT();\r
207 \r
208         /* Restore the context of the first task that is going to run. */\r
209         vPortStartFirstTask();\r
210 \r
211         /* Execution should not reach here as the tasks are now running!\r
212         prvSetupTimerInterrupt() is called here to prevent the compiler outputting\r
213         a warning about a statically declared function not being referenced in the\r
214         case that the application writer has provided their own tick interrupt\r
215         configuration routine (and defined configSETUP_TICK_INTERRUPT() such that\r
216         their own routine will be called in place of prvSetupTimerInterrupt()). */\r
217         prvSetupTimerInterrupt();\r
218         return pdTRUE;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vPortEndScheduler( void )\r
223 {\r
224         /* It is unlikely that the RL78 port will get stopped. */\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvSetupTimerInterrupt( void )\r
229 {\r
230 const uint16_t usClockHz = 15000UL; /* Internal clock. */\r
231 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
232 \r
233         /* Use the internal 15K clock. */\r
234         OSMC = ( uint8_t ) 0x16;\r
235 \r
236         #ifdef RTCEN\r
237         {\r
238                 /* Supply the interval timer clock. */\r
239                 RTCEN = ( uint8_t ) 1U;\r
240 \r
241                 /* Disable INTIT interrupt. */\r
242                 ITMK = ( uint8_t ) 1;\r
243 \r
244                 /* Disable ITMC operation. */\r
245                 ITMC = ( uint8_t ) 0x0000;\r
246 \r
247                 /* Clear INIT interrupt. */\r
248                 ITIF = ( uint8_t ) 0;\r
249 \r
250                 /* Set interval and enable interrupt operation. */\r
251                 ITMC = usCompareMatch | 0x8000U;\r
252 \r
253                 /* Enable INTIT interrupt. */\r
254                 ITMK = ( uint8_t ) 0;\r
255         }\r
256         #endif\r
257 \r
258         #ifdef TMKAEN\r
259         {\r
260                 /* Supply the interval timer clock. */\r
261                 TMKAEN = ( uint8_t ) 1U;\r
262 \r
263                 /* Disable INTIT interrupt. */\r
264                 TMKAMK = ( uint8_t ) 1;\r
265 \r
266                 /* Disable ITMC operation. */\r
267                 ITMC = ( uint8_t ) 0x0000;\r
268 \r
269                 /* Clear INIT interrupt. */\r
270                 TMKAIF = ( uint8_t ) 0;\r
271 \r
272                 /* Set interval and enable interrupt operation. */\r
273                 ITMC = usCompareMatch | 0x8000U;\r
274 \r
275                 /* Enable INTIT interrupt. */\r
276                 TMKAMK = ( uint8_t ) 0;\r
277         }\r
278         #endif\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r