]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/RL78/port.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / IAR / RL78 / port.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 /* The critical nesting value is initialised to a non zero value to ensure\r
72 interrupts don't accidentally become enabled before the scheduler is started. */\r
73 #define portINITIAL_CRITICAL_NESTING  ( ( unsigned short ) 10 )\r
74 \r
75 /* Initial PSW value allocated to a newly created task.\r
76  *   1100011000000000\r
77  *   ||||||||-------------- Fill byte\r
78  *   |||||||--------------- Carry Flag cleared\r
79  *   |||||----------------- In-service priority Flags set to low level\r
80  *   ||||------------------ Register bank Select 0 Flag cleared\r
81  *   |||------------------- Auxiliary Carry Flag cleared\r
82  *   ||-------------------- Register bank Select 1 Flag cleared\r
83  *   |--------------------- Zero Flag set\r
84  *   ---------------------- Global Interrupt Flag set (enabled)\r
85  */\r
86 #define portPSW           ( 0xc6UL )\r
87 \r
88 /* The address of the pxCurrentTCB variable, but don't know or need to know its\r
89 type. */\r
90 typedef void tskTCB;\r
91 extern volatile tskTCB * volatile pxCurrentTCB;\r
92 \r
93 /* Each task maintains a count of the critical section nesting depth.  Each time\r
94 a critical section is entered the count is incremented.  Each time a critical\r
95 section is exited the count is decremented - with interrupts only being\r
96 re-enabled if the count is zero.\r
97 \r
98 usCriticalNesting will get set to zero when the scheduler starts, but must\r
99 not be initialised to zero as that could cause problems during the startup\r
100 sequence. */\r
101 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /*\r
106  * Sets up the periodic ISR used for the RTOS tick.\r
107  */\r
108 static void prvSetupTimerInterrupt( void );\r
109 \r
110 /*\r
111  * Defined in portasm.s87, this function starts the scheduler by loading the\r
112  * context of the first task to run.\r
113  */\r
114 extern void vPortStartFirstTask( void );\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /*\r
119  * Initialise the stack of a task to look exactly as if a call to\r
120  * portSAVE_CONTEXT had been called.\r
121  *\r
122  * See the header file portable.h.\r
123  */\r
124 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
125 {\r
126 unsigned long *pulLocal;\r
127 \r
128         #if __DATA_MODEL__ == __DATA_MODEL_FAR__\r
129         {\r
130                 /* Parameters are passed in on the stack, and written using a 32bit value\r
131                 hence a space is left for the second two bytes. */\r
132                 pxTopOfStack--;\r
133 \r
134                 /* Write in the parameter value. */\r
135                 pulLocal =  ( unsigned long * ) pxTopOfStack;\r
136                 *pulLocal = ( unsigned long ) pvParameters;\r
137                 pxTopOfStack--;\r
138 \r
139                 /* These values are just spacers.  The return address of the function\r
140                 would normally be written here. */\r
141                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
142                 pxTopOfStack--;\r
143                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
144                 pxTopOfStack--;\r
145 \r
146                 /* The start address / PSW value is also written in as a 32bit value,\r
147                 so leave a space for the second two bytes. */\r
148                 pxTopOfStack--;\r
149         \r
150                 /* Task function start address combined with the PSW. */\r
151                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
152                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
153                 pxTopOfStack--;\r
154 \r
155                 /* An initial value for the AX register. */\r
156                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
157                 pxTopOfStack--;\r
158         }\r
159         #else\r
160         {\r
161                 /* Task function address is written to the stack first.  As it is\r
162                 written as a 32bit value a space is left on the stack for the second\r
163                 two bytes. */\r
164                 pxTopOfStack--;\r
165 \r
166                 /* Task function start address combined with the PSW. */\r
167                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
168                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
169                 pxTopOfStack--;\r
170 \r
171                 /* The parameter is passed in AX. */\r
172                 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
173                 pxTopOfStack--;\r
174         }\r
175         #endif\r
176 \r
177         /* An initial value for the HL register. */\r
178         *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
179         pxTopOfStack--;\r
180 \r
181         /* CS and ES registers. */\r
182         *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;\r
183         pxTopOfStack--;\r
184 \r
185         /* Finally the remaining general purpose registers DE and BC */\r
186         *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;\r
189         pxTopOfStack--;\r
190 \r
191         /* Finally the critical section nesting count is set to zero when the task\r
192         first starts. */\r
193         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
194 \r
195         /* Return a pointer to the top of the stack that has beene generated so it\r
196         can     be stored in the task control block for the task. */\r
197         return pxTopOfStack;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 portBASE_TYPE xPortStartScheduler( void )\r
202 {\r
203         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
204         this function is called. */\r
205         prvSetupTimerInterrupt();\r
206 \r
207         /* Restore the context of the first task that is going to run. */\r
208         vPortStartFirstTask();\r
209 \r
210         /* Execution should not reach here as the tasks are now running! */\r
211         return pdTRUE;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void vPortEndScheduler( void )\r
216 {\r
217         /* It is unlikely that the RL78/G13 port will get stopped.  If required simply\r
218         disable the tick interrupt here. */\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 static void prvSetupTimerInterrupt( void )\r
223 {\r
224 const unsigned short usClockHz = 15000UL; /* Internal clock. */\r
225 const unsigned short usCompareMatch = ( usClockHz / configTICK_RATE_HZ ) + 1UL;\r
226 \r
227         /* Use the internal 15K clock. */\r
228         OSMC = 0x16U;\r
229 \r
230         /* Supply the RTC clock. */\r
231         RTCEN = 1U;\r
232         \r
233         /* Disable ITMC operation. */\r
234         ITMC = 0x0000;\r
235         \r
236         /* Disable INTIT interrupt. */\r
237         ITMK = 1U;\r
238         \r
239         /* Set INTIT high priority */\r
240         ITPR1 = 1U;\r
241         ITPR0 = 1U;\r
242         \r
243         /* Set interval. */\r
244         ITMC = usCompareMatch;\r
245 \r
246         /* Clear INIT interrupt. */\r
247         ITIF = 0U;\r
248         \r
249         /* Enable INTIT interrupt. */\r
250         ITMK = 0U;\r
251         \r
252         /* Enable IT operation. */\r
253         ITMC |= 0x8000;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r