]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_multiple_IAR/main.c
0894b3ebe132f3c71df37f4729f1cf2b652ae24c
[freertos] / FreeRTOS / Demo / RL78_multiple_IAR / main.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /******************************************************************************\r
97  * This project provides two demo applications.  A simple blinky style project,\r
98  * and a more comprehensive test and demo application.  The\r
99  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
100  * select between the two.  The simply blinky demo is implemented and described\r
101  * in main_blinky.c.  The more comprehensive test and demo application is\r
102  * implemented and described in main_full.c.\r
103  *\r
104  * This file implements the code that is not demo specific, including the\r
105  * hardware setup and FreeRTOS hook functions.\r
106  *\r
107  * This project does not provide an example of how to write an RTOS compatible\r
108  * interrupt service routine (other than the tick interrupt itself), so this\r
109  * file contains the function vAnExampleISR_C_Handler() as a dummy example (that\r
110  * is not actually installed) that can be used as a reference.  Also see the\r
111  * file ExampleISR.s87, and the documentation page for this demo on the\r
112  * FreeRTOS.org website for full instructions.\r
113  *\r
114  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
115  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
116  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
117  *\r
118  */\r
119 \r
120 /* Scheduler include files. */\r
121 #include "FreeRTOS.h"\r
122 #include "task.h"\r
123 #include "semphr.h"\r
124 \r
125 /* Hardware includes. */\r
126 #include "demo_specific_io.h"\r
127 \r
128 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
129 or 0 to run the more comprehensive test and demo application. */\r
130 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
136  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
137  */\r
138 extern void main_blinky( void );\r
139 extern void main_full( void );\r
140 \r
141 /*\r
142  * This function is called from the C startup routine to setup the processor -\r
143  * in particular the clock source.\r
144  */\r
145 int __low_level_init(void);\r
146 \r
147 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
148 within this file. */\r
149 void vApplicationMallocFailedHook( void );\r
150 void vApplicationIdleHook( void );\r
151 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
152 void vApplicationTickHook( void );\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /* This variable is not actually used, but provided to allow an example of how\r
157 to write an ISR to be included in this file. */\r
158 static SemaphoreHandle_t xSemaphore = NULL;\r
159 \r
160 /* RL78 Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface\r
161 enabled. */\r
162 __root __far const unsigned char OptionByte[] @ 0x00C0 =\r
163 {\r
164         0x6eU, 0xffU, 0xe8U, 0x85U\r
165 };\r
166 \r
167 /* Security byte definition */\r
168 __root __far const unsigned char ucSecurityCode[]  @ 0x00C4 =\r
169 {\r
170         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
171 };\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void main( void )\r
176 {\r
177         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
178         of this file. */\r
179         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
180         {\r
181                 main_blinky();\r
182         }\r
183         #else\r
184         {\r
185                 main_full();\r
186         }\r
187         #endif\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vAnExampleISR_C_Handler( void )\r
192 {\r
193         /*\r
194          * This demo does not include a functional interrupt service routine - so\r
195          * this dummy handler (which is not actually installed) is provided as an\r
196          * example of how an ISR that needs to cause a context switch needs to be\r
197          * implemented.  ISRs that do not cause a context switch have no special\r
198          * requirements and can be written as per the compiler documentation.\r
199          *\r
200          * This C function is called from a wrapper function that is implemented\r
201          * in assembly code.  See vANExampleISR_ASM_Wrapper() in ExampleISR.s87.\r
202          * Also see the documentation page for this demo on the FreeRTOS.org website\r
203          * for full instructions.\r
204          */\r
205 short sHigherPriorityTaskWoken = pdFALSE;\r
206 \r
207         /* Handler code goes here...*/\r
208 \r
209         /* For purposes of demonstration, assume at some point the hander calls\r
210         xSemaphoreGiveFromISR().*/\r
211         xSemaphoreGiveFromISR( xSemaphore, &sHigherPriorityTaskWoken );\r
212 \r
213         /* If giving the semaphore unblocked a task, and the unblocked task has a\r
214         priority higher than or equal to the currently running task, then\r
215         sHigherPriorityTaskWoken will have been set to pdTRUE internally within the\r
216         xSemaphoreGiveFromISR() function.  Passing a pdTRUE     value to\r
217         portYIELD_FROM_ISR() will cause this interrupt to return directly to the\r
218         higher priority unblocked task. */\r
219         portYIELD_FROM_ISR( sHigherPriorityTaskWoken );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 int __low_level_init(void)\r
224 {\r
225         portDISABLE_INTERRUPTS();\r
226 \r
227         /* Set fMX */\r
228         CMC = 0x00;\r
229         MSTOP = 1U;\r
230 \r
231         /* Set fMAIN */\r
232         MCM0 = 0U;\r
233 \r
234         /* Set fSUB */\r
235         XTSTOP = 1U;\r
236         OSMC = 0x10;\r
237 \r
238         /* Set fCLK */\r
239         CSS = 0U;\r
240 \r
241         /* Set fIH */\r
242         HIOSTOP = 0U;\r
243 \r
244         /* LED port initialization. */\r
245         LED_INIT();\r
246 \r
247         return pdTRUE;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 void vApplicationMallocFailedHook( void )\r
252 {\r
253         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
254         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
255         internally by FreeRTOS API functions that create tasks, queues, software\r
256         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
257         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
258         taskDISABLE_INTERRUPTS();\r
259         for( ;; );\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
264 {\r
265         ( void ) pcTaskName;\r
266         ( void ) pxTask;\r
267 \r
268         /* Run time stack overflow checking is performed if\r
269         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
270         function is called if a stack overflow is detected. */\r
271         taskDISABLE_INTERRUPTS();\r
272         for( ;; );\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 void vApplicationIdleHook( void )\r
277 {\r
278 volatile size_t xFreeHeapSpace;\r
279 \r
280         /* This is just a trivial example of an idle hook.  It is called on each\r
281         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
282         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
283         memory management section on the http://www.FreeRTOS.org web site for memory\r
284         management options.  If there is a lot of heap memory free then the\r
285         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
286         RAM. */\r
287         xFreeHeapSpace = xPortGetFreeHeapSize();\r
288 \r
289         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
290         ( void ) xFreeHeapSpace;\r
291 }\r
292 \r
293 \r