]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/RL78/port.c
bd8c8c0e5dd105e0a59944fcdc1f92366f7594a2
[freertos] / FreeRTOS / Source / portable / GCC / RL78 / port.c
1 /*\r
2     FreeRTOS V8.0.1 - 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  *   11000110\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 /* Each task maintains a count of the critical section nesting depth.  Each time\r
88 a critical section is entered the count is incremented.  Each time a critical\r
89 section is exited the count is decremented - with interrupts only being\r
90 re-enabled if the count is zero.\r
91 \r
92 usCriticalNesting will get set to zero when the scheduler starts, but must\r
93 not be initialised to zero as that could cause problems during the startup\r
94 sequence. */\r
95 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /*\r
100  * Sets up the periodic ISR used for the RTOS tick.\r
101  */\r
102 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void );\r
103 \r
104 /*\r
105  * Starts the scheduler by loading the context of the first task to run.\r
106  * (defined in portasm.S).\r
107  */\r
108 extern void vPortStartFirstTask( void );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Initialise the stack of a task to look exactly as if a call to\r
114  * portSAVE_CONTEXT had been called.\r
115  *\r
116  * See the header file portable.h.\r
117  */\r
118 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
119 {\r
120 uint32_t *pulLocal;\r
121 \r
122         /* Stack type and pointers to the stack type are both 2 bytes. */\r
123 \r
124         /* Parameters are passed in on the stack, and written using a 32bit value\r
125         hence a space is left for the second two bytes. */\r
126         pxTopOfStack--;\r
127 \r
128         /* Write in the parameter value. */\r
129         pulLocal =  ( uint32_t * ) pxTopOfStack;\r
130         *pulLocal = ( StackType_t ) pvParameters;\r
131         pxTopOfStack--;\r
132 \r
133         /* The return address, leaving space for the first two bytes of the\r
134         32-bit value. */\r
135         pxTopOfStack--;\r
136         pulLocal = ( uint32_t * ) pxTopOfStack;\r
137         *pulLocal = ( uint32_t ) 0;\r
138         pxTopOfStack--;\r
139 \r
140         /* The start address / PSW value is also written in as a 32bit value,\r
141         so leave a space for the second two bytes. */\r
142         pxTopOfStack--;\r
143 \r
144         /* Task function start address combined with the PSW. */\r
145         pulLocal = ( uint32_t * ) pxTopOfStack;\r
146         *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
147         pxTopOfStack--;\r
148 \r
149         /* An initial value for the AX register. */\r
150         *pxTopOfStack = ( StackType_t ) 0x1111;\r
151         pxTopOfStack--;\r
152 \r
153         /* An initial value for the HL register. */\r
154         *pxTopOfStack = ( StackType_t ) 0x2222;\r
155         pxTopOfStack--;\r
156 \r
157         /* CS and ES registers. */\r
158         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
159         pxTopOfStack--;\r
160 \r
161         /* The remaining general purpose registers bank 0 (DE and BC) and the other\r
162         two register banks...register bank 3 is dedicated for use by interrupts so\r
163         is not saved as part of the task context. */\r
164         pxTopOfStack -= 10;\r
165 \r
166         /* Finally the critical section nesting count is set to zero when the task\r
167         first starts. */\r
168         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;\r
169 \r
170         /* Return a pointer to the top of the stack that has beene generated so it\r
171         can     be stored in the task control block for the task. */\r
172         return pxTopOfStack;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 portBASE_TYPE xPortStartScheduler( void )\r
177 {\r
178         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
179         this function is called. */\r
180         vApplicationSetupTimerInterrupt();\r
181 \r
182         /* Restore the context of the first task that is going to run. */\r
183         vPortStartFirstTask();\r
184 \r
185         /* Execution should not reach here as the tasks are now running! */\r
186         return pdTRUE;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vPortEndScheduler( void )\r
191 {\r
192         /* It is unlikely that the RL78 port will get stopped. */\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void )\r
197 {\r
198 const uint16_t usClockHz = 15000UL; /* Internal clock. */\r
199 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
200 \r
201         /* Use the internal 15K clock. */\r
202         OSMC = ( unsigned char ) 0x16;\r
203 \r
204         #ifdef RTCEN\r
205         {\r
206                 /* Supply the interval timer clock. */\r
207                 RTCEN = ( unsigned char ) 1U;\r
208 \r
209                 /* Disable INTIT interrupt. */\r
210                 ITMK = ( unsigned char ) 1;\r
211 \r
212                 /* Disable ITMC operation. */\r
213                 ITMC = ( unsigned char ) 0x0000;\r
214 \r
215                 /* Clear INIT interrupt. */\r
216                 ITIF = ( unsigned char ) 0;\r
217 \r
218                 /* Set interval and enable interrupt operation. */\r
219                 ITMC = usCompareMatch | 0x8000U;\r
220 \r
221                 /* Enable INTIT interrupt. */\r
222                 ITMK = ( unsigned char ) 0;\r
223         }\r
224         #endif\r
225 \r
226         #ifdef TMKAEN\r
227         {\r
228                 /* Supply the interval timer clock. */\r
229                 TMKAEN = ( unsigned char ) 1U;\r
230 \r
231                 /* Disable INTIT interrupt. */\r
232                 TMKAMK = ( unsigned char ) 1;\r
233 \r
234                 /* Disable ITMC operation. */\r
235                 ITMC = ( unsigned char ) 0x0000;\r
236 \r
237                 /* Clear INIT interrupt. */\r
238                 TMKAIF = ( unsigned char ) 0;\r
239 \r
240                 /* Set interval and enable interrupt operation. */\r
241                 ITMC = usCompareMatch | 0x8000U;\r
242 \r
243                 /* Enable INTIT interrupt. */\r
244                 TMKAMK = ( unsigned char ) 0;\r
245         }\r
246         #endif\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r