]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/RL78/port.c
Prepare for V9.0.0 release:
[freertos] / FreeRTOS / Source / portable / GCC / RL78 / port.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* The critical nesting value is initialised to a non zero value to ensure\r
75 interrupts don't accidentally become enabled before the scheduler is started. */\r
76 #define portINITIAL_CRITICAL_NESTING  ( ( uint16_t ) 10 )\r
77 \r
78 /* Initial PSW value allocated to a newly created task.\r
79  *   11000110\r
80  *   ||||||||-------------- Fill byte\r
81  *   |||||||--------------- Carry Flag cleared\r
82  *   |||||----------------- In-service priority Flags set to low level\r
83  *   ||||------------------ Register bank Select 0 Flag cleared\r
84  *   |||------------------- Auxiliary Carry Flag cleared\r
85  *   ||-------------------- Register bank Select 1 Flag cleared\r
86  *   |--------------------- Zero Flag set\r
87  *   ---------------------- Global Interrupt Flag set (enabled)\r
88  */\r
89 #define portPSW           ( 0xc6UL )\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 uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * Sets up the periodic ISR used for the RTOS tick.\r
105  */\r
106 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void );\r
107 \r
108 /*\r
109  * Starts the scheduler by loading the context of the first task to run.\r
110  * (defined in portasm.S).\r
111  */\r
112 extern void vPortStartFirstTask( void );\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * Initialise the stack of a task to look exactly as if a call to\r
118  * portSAVE_CONTEXT had been called.\r
119  *\r
120  * See the header file portable.h.\r
121  */\r
122 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
123 {\r
124 uint32_t *pulLocal;\r
125 \r
126         /* Stack type and pointers to the stack type are both 2 bytes. */\r
127 \r
128         /* Parameters are passed in on the stack, and written using a 32bit value\r
129         hence a space is left for the second two bytes. */\r
130         pxTopOfStack--;\r
131 \r
132         /* Write in the parameter value. */\r
133         pulLocal =  ( uint32_t * ) pxTopOfStack;\r
134         *pulLocal = ( StackType_t ) pvParameters;\r
135         pxTopOfStack--;\r
136 \r
137         /* The return address, leaving space for the first two bytes of the\r
138         32-bit value. */\r
139         pxTopOfStack--;\r
140         pulLocal = ( uint32_t * ) pxTopOfStack;\r
141         *pulLocal = ( uint32_t ) 0;\r
142         pxTopOfStack--;\r
143 \r
144         /* The start address / PSW value is also written in as a 32bit value,\r
145         so leave a space for the second two bytes. */\r
146         pxTopOfStack--;\r
147 \r
148         /* Task function start address combined with the PSW. */\r
149         pulLocal = ( uint32_t * ) pxTopOfStack;\r
150         *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
151         pxTopOfStack--;\r
152 \r
153         /* An initial value for the AX register. */\r
154         *pxTopOfStack = ( StackType_t ) 0x1111;\r
155         pxTopOfStack--;\r
156 \r
157         /* An initial value for the HL register. */\r
158         *pxTopOfStack = ( StackType_t ) 0x2222;\r
159         pxTopOfStack--;\r
160 \r
161         /* CS and ES registers. */\r
162         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
163         pxTopOfStack--;\r
164 \r
165         /* The remaining general purpose registers bank 0 (DE and BC) and the other\r
166         two register banks...register bank 3 is dedicated for use by interrupts so\r
167         is not saved as part of the task context. */\r
168         pxTopOfStack -= 10;\r
169 \r
170         /* Finally the critical section nesting count is set to zero when the task\r
171         first starts. */\r
172         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;\r
173 \r
174         /* Return a pointer to the top of the stack that has beene generated so it\r
175         can     be stored in the task control block for the task. */\r
176         return pxTopOfStack;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 portBASE_TYPE xPortStartScheduler( void )\r
181 {\r
182         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
183         this function is called. */\r
184         vApplicationSetupTimerInterrupt();\r
185 \r
186         /* Restore the context of the first task that is going to run. */\r
187         vPortStartFirstTask();\r
188 \r
189         /* Execution should not reach here as the tasks are now running! */\r
190         return pdTRUE;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vPortEndScheduler( void )\r
195 {\r
196         /* It is unlikely that the RL78 port will get stopped. */\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 __attribute__((weak)) void vApplicationSetupTimerInterrupt( void )\r
201 {\r
202 const uint16_t usClockHz = 15000UL; /* Internal clock. */\r
203 const uint16_t usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
204 \r
205         /* Use the internal 15K clock. */\r
206         OSMC = ( unsigned char ) 0x16;\r
207 \r
208         #ifdef RTCEN\r
209         {\r
210                 /* Supply the interval timer clock. */\r
211                 RTCEN = ( unsigned char ) 1U;\r
212 \r
213                 /* Disable INTIT interrupt. */\r
214                 ITMK = ( unsigned char ) 1;\r
215 \r
216                 /* Disable ITMC operation. */\r
217                 ITMC = ( unsigned char ) 0x0000;\r
218 \r
219                 /* Clear INIT interrupt. */\r
220                 ITIF = ( unsigned char ) 0;\r
221 \r
222                 /* Set interval and enable interrupt operation. */\r
223                 ITMC = usCompareMatch | 0x8000U;\r
224 \r
225                 /* Enable INTIT interrupt. */\r
226                 ITMK = ( unsigned char ) 0;\r
227         }\r
228         #endif\r
229 \r
230         #ifdef TMKAEN\r
231         {\r
232                 /* Supply the interval timer clock. */\r
233                 TMKAEN = ( unsigned char ) 1U;\r
234 \r
235                 /* Disable INTIT interrupt. */\r
236                 TMKAMK = ( unsigned char ) 1;\r
237 \r
238                 /* Disable ITMC operation. */\r
239                 ITMC = ( unsigned char ) 0x0000;\r
240 \r
241                 /* Clear INIT interrupt. */\r
242                 TMKAIF = ( unsigned char ) 0;\r
243 \r
244                 /* Set interval and enable interrupt operation. */\r
245                 ITMC = usCompareMatch | 0x8000U;\r
246 \r
247                 /* Enable INTIT interrupt. */\r
248                 TMKAMK = ( unsigned char ) 0;\r
249         }\r
250         #endif\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r