]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_R4F_RZ_T_GCC_IAR_ARM/src/main.c
ee79402b32a87d85d6ba2bbea16c1ab67ed03434
[freertos] / FreeRTOS / Demo / CORTEX_R4F_RZ_T_GCC_IAR_ARM / src / main.c
1 #if 1\r
2 /*\r
3     FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.\r
4     All rights reserved\r
5 \r
6     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
7 \r
8     This file is part of the FreeRTOS distribution.\r
9 \r
10     FreeRTOS is free software; you can redistribute it and/or modify it under\r
11     the terms of the GNU General Public License (version 2) as published by the\r
12     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
13 \r
14     ***************************************************************************\r
15     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
16     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
17     >>!   obliged to provide the source code for proprietary components     !<<\r
18     >>!   outside of the FreeRTOS kernel.                                   !<<\r
19     ***************************************************************************\r
20 \r
21     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
22     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
23     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
24     link: http://www.freertos.org/a00114.html\r
25 \r
26     ***************************************************************************\r
27      *                                                                       *\r
28      *    FreeRTOS provides completely free yet professionally developed,    *\r
29      *    robust, strictly quality controlled, supported, and cross          *\r
30      *    platform software that is more than just the market leader, it     *\r
31      *    is the industry's de facto standard.                               *\r
32      *                                                                       *\r
33      *    Help yourself get started quickly while simultaneously helping     *\r
34      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
35      *    tutorial book, reference manual, or both:                          *\r
36      *    http://www.FreeRTOS.org/Documentation                              *\r
37      *                                                                       *\r
38     ***************************************************************************\r
39 \r
40     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
41     the FAQ page "My application does not run, what could be wrong?".  Have you\r
42     defined configASSERT()?\r
43 \r
44     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
45     embedded software for free we request you assist our global community by\r
46     participating in the support forum.\r
47 \r
48     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
49     be as productive as possible as early as possible.  Now you can receive\r
50     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
51     Ltd, and the world's leading authority on the world's leading RTOS.\r
52 \r
53     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
54     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
55     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
56 \r
57     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
58     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
59 \r
60     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
61     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
62     licenses offer ticketed support, indemnification and commercial middleware.\r
63 \r
64     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
65     engineered and independently SIL3 certified version for use in safety and\r
66     mission critical applications that require provable dependability.\r
67 \r
68     1 tab == 4 spaces!\r
69 */\r
70 \r
71 /******************************************************************************\r
72  * This project provides two demo applications.  A simple blinky style project,\r
73  * and a more comprehensive test and demo application.  The\r
74  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
75  * select between the two.  The simply blinky demo is implemented and described\r
76  * in main_blinky.c.  The more comprehensive test and demo application is\r
77  * implemented and described in main_full.c.\r
78  *\r
79  * This file implements the code that is not demo specific, including the\r
80  * hardware setup, standard FreeRTOS hook functions, and the ISR hander called\r
81  * by the RTOS after interrupt entry (including nesting) has been taken care of.\r
82  *\r
83  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
84  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
85  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
86  *\r
87  */\r
88 \r
89 /* Standard includes. */\r
90 #include "string.h"\r
91 \r
92 /* Scheduler include files. */\r
93 #include "FreeRTOS.h"\r
94 #include "task.h"\r
95 #include "semphr.h"\r
96 \r
97 /* Renesas includes. */\r
98 #include "r_cg_macrodriver.h"\r
99 #include "r_cg_icu.h"\r
100 #include "r_cg_scifa.h"\r
101 #include "r_cg_rspi.h"\r
102 #include "r_system.h"\r
103 #include "r_reset.h"\r
104 #include "siochar.h"\r
105 #include "r_cg_userdefine.h"\r
106 \r
107 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
108 or 0 to run the more comprehensive test and demo application. */\r
109 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 static void prvClearBSS( void );\r
114 \r
115 /*\r
116  * Configure the hardware as necessary to run this demo.\r
117  */\r
118 static void prvSetupHardware( void );\r
119 \r
120 /*\r
121  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
122  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
123  */\r
124 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
125         extern void main_blinky( void );\r
126 #else\r
127         extern void main_full( void );\r
128 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
129 \r
130 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
131 within this file. */\r
132 void vApplicationMallocFailedHook( void );\r
133 void vApplicationIdleHook( void );\r
134 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
135 void vApplicationTickHook( void );\r
136 \r
137 /* Prototype for the IRQ handler called by the generic Cortex-A5 RTOS port\r
138 layer. */\r
139 void vApplicationIRQHandler( void );\r
140 \r
141 /* Library initialisation. */\r
142 extern void R_Systeminit( void );\r
143 \r
144 /*-----------------------------------------------------------*/\r
145 \r
146 volatile uint32_t ultest = 0, ultest2 = 9999;\r
147 \r
148 int main( void )\r
149 {\r
150         prvClearBSS();\r
151 \r
152         configASSERT( ultest == 0 );\r
153         configASSERT( ultest2 == 9999 );\r
154 \r
155         /* Configure the hardware ready to run the demo. */\r
156         prvSetupHardware();\r
157 \r
158         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
159         of this file. */\r
160         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
161         {\r
162                 main_blinky();\r
163         }\r
164         #else\r
165         {\r
166                 main_full();\r
167         }\r
168         #endif\r
169 \r
170         return 0;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static void prvSetupHardware( void )\r
175 {\r
176         R_Systeminit();\r
177 \r
178         /* Enable RSPI1 (serial peripheral interface). */\r
179         R_RSPI1_Start();\r
180 \r
181         /* Configure the UART channel for communication with a host PC via on-board\r
182         RL78/G1C device. */\r
183         io_init_scifa2();\r
184 \r
185         /* Enable SCIFA2 (serial communications interface with FIFO). */\r
186         R_SCIFA2_Start();\r
187 \r
188         /* SW3 interrupts. */\r
189         R_ICU_IRQ12_Start();\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 void vApplicationMallocFailedHook( void )\r
194 {\r
195         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
196         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
197         internally by FreeRTOS API functions that create tasks, queues, software\r
198         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
199         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
200 \r
201         /* Force an assert. */\r
202         configASSERT( ( volatile void * ) NULL );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
207 {\r
208         ( void ) pcTaskName;\r
209         ( void ) pxTask;\r
210 \r
211         /* Run time stack overflow checking is performed if\r
212         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
213         function is called if a stack overflow is detected. */\r
214 \r
215         /* Force an assert. */\r
216         configASSERT( ( volatile void * ) NULL );\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 void vApplicationIdleHook( void )\r
221 {\r
222 volatile size_t xFreeHeapSpace;\r
223 \r
224         /* This is just a trivial example of an idle hook.  It is called on each\r
225         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
226         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
227         memory management section on the http://www.FreeRTOS.org web site for memory\r
228         management options.  If there is a lot of heap memory free then the\r
229         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
230         RAM. */\r
231         xFreeHeapSpace = xPortGetFreeHeapSize();\r
232 \r
233         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
234         ( void ) xFreeHeapSpace;\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 void vApplicationTickHook( void )\r
239 {\r
240         #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0\r
241         {\r
242                 extern void vFullDemoTickHook( void );\r
243 \r
244                 vFullDemoTickHook();\r
245         }\r
246         #endif\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /* The function called by the RTOS port layer after it has managed interrupt\r
251 entry. */\r
252 void vApplicationIRQHandler( void )\r
253 {\r
254 #if 1\r
255 extern void FreeRTOS_Tick_Handler( void );\r
256 \r
257         /* Clear the interrupt source CMI5. */\r
258         VIC.PIC9.LONG = 0x00001000UL;\r
259 \r
260         FreeRTOS_Tick_Handler();\r
261 \r
262         /* Dummy write */\r
263         portDISABLE_INTERRUPTS();\r
264         // Done in the epilogue code VIC.HVA0.LONG = 0x00000000UL;\r
265 \r
266 #else\r
267 typedef void (*ISRFunction_t)( void );\r
268 ISRFunction_t pxISRFunction;\r
269 volatile uint32_t * pulAIC_IVR = ( uint32_t * ) configINTERRUPT_VECTOR_ADDRESS;\r
270 \r
271         /* Obtain the address of the interrupt handler from the AIR. */\r
272         pxISRFunction = ( ISRFunction_t ) *pulAIC_IVR;\r
273 \r
274         /* Write back to the SAMA5's interrupt controller's IVR register in case the\r
275         CPU is in protect mode.  If the interrupt controller is not in protect mode\r
276         then this write is not necessary. */\r
277         *pulAIC_IVR = ( uint32_t ) pxISRFunction;\r
278 \r
279         /* Ensure the write takes before re-enabling interrupts. */\r
280         __DSB();\r
281         __ISB();\r
282     __enable_irq();\r
283 \r
284         /* Call the installed ISR. */\r
285         pxISRFunction();\r
286 #endif\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 static void prvClearBSS( void )\r
291 {\r
292 extern uint32_t __bss_start__[];\r
293 extern uint32_t __bss_end__[];\r
294 size_t xSize;\r
295 \r
296         /* Zero out bss. */\r
297         xSize = ( ( size_t ) __bss_end__ ) - ( ( size_t ) __bss_start__ );\r
298         memset( ( void * ) __bss_start__, 0x00, xSize );\r
299 }\r
300 \r
301 \r
302 \r
303 \r
304 \r
305 \r
306 \r
307 \r
308 \r
309 \r
310 \r
311 \r
312 \r
313 \r
314 \r
315 \r
316 \r
317 \r
318 \r
319 \r
320 \r
321 \r
322 \r
323 \r
324 \r
325 \r
326 \r
327 \r
328 \r
329 \r
330 #else\r
331 \r
332 \r
333 \r
334 \r
335 \r
336 \r
337 \r
338 \r
339 \r
340 \r
341 \r
342 \r
343 \r
344 #include "FreeRTOS.h"\r
345 #include "task.h"\r
346 \r
347 /***********************************************************************************************************************\r
348 Includes\r
349 ***********************************************************************************************************************/\r
350 #include "r_cg_macrodriver.h"\r
351 #include "r_cg_cgc.h"\r
352 #include "r_cg_icu.h"\r
353 #include "r_cg_port.h"\r
354 #include "r_cg_tpu.h"\r
355 #include "r_cg_cmt.h"\r
356 #include "r_cg_scifa.h"\r
357 #include "r_cg_rspi.h"\r
358 #include "r_cg_s12ad.h"\r
359 /* Start user code for include. Do not edit comment generated here */\r
360 #include "r_cg_mpc.h"\r
361 #include "r_system.h"\r
362 #include "r_reset.h"\r
363 #include "lcd_pmod.h"\r
364 #include "logo_data.h"\r
365 #include "stdio.h"\r
366 #include "siochar.h"\r
367 /* End user code. Do not edit comment generated here */\r
368 #include "r_cg_userdefine.h"\r
369 \r
370 /* Start user code for global. Do not edit comment generated here */\r
371 \r
372 #define LZ_ENABLE   (1)\r
373 #define LZ_DISABLE  (0)\r
374 \r
375 /* Welcome banner - displayed on serial port at startup*/\r
376 static uint8_t welcome_banner[] = "\n\n\rRSK+RZT1 \n\n\r- Tutorial - Press 'c' or SW3 for ADC Conversion\r\n\0";\r
377 \r
378 /* Used as a Data Transmit counter    */\r
379 static uint8_t uart_buffer[] = " ADC count: x.     Value: xxxxx\r\n";\r
380 \r
381 /* Used as a Data Transmit counter    */\r
382 static uint8_t lcd_buffer[] = " ADC = xxxx ";\r
383 \r
384 /* Function prototype for displaying the 2 bit binary counter using LEDs */\r
385 static void led_display_count (const uint8_t count);\r
386 \r
387 extern void R_Systeminit(void);\r
388 void R_MAIN_UserInit(void);\r
389 \r
390 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
391 within this file. */\r
392 void vApplicationMallocFailedHook( void );\r
393 void vApplicationIdleHook( void );\r
394 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
395 void vApplicationTickHook( void );\r
396 \r
397 \r
398 /* Prototype for the IRQ handler called by the generic Cortex-A5 RTOS port\r
399 layer. */\r
400 void vApplicationIRQHandler( void );\r
401 \r
402 void main(void)\r
403 {\r
404 uint32_t adc_count = 0;\r
405 \r
406     R_Systeminit();\r
407 \r
408     R_MAIN_UserInit();\r
409 \r
410     /* SW3 interrupts */\r
411     R_ICU_IRQ12_Start();\r
412 \r
413     /* Clear flags */\r
414     g_switch_press_flg = 0;\r
415     g_terminal_request = 0;\r
416 \r
417     /* Display the welcome banner on the serial terminal */\r
418     R_SCIFA2_Serial_Send((uint8_t *)&welcome_banner, sizeof(welcome_banner));\r
419         \r
420     /* Data transmission and reception done in the infinite loop */\r
421     while (1U)\r
422     {         \r
423         /* Check for a valid request from the switch or serial terminal */\r
424         if ((g_terminal_request) || (g_switch_press_flg & SW3_PRESS_FLG))\r
425         {          \r
426             /* Update the binary count using LED2 and LED3 */\r
427             led_display_count(adc_count);\r
428 \r
429             while(0u == SCIFA2.FSR.BIT.TDFE)\r
430             {\r
431                 /* Wait for previous transmission to complete */\r
432             }\r
433 \r
434             /* Write send data */\r
435             R_SCIFA2_Serial_Send((uint8_t *)&uart_buffer, sizeof(uart_buffer));\r
436 \r
437             /* Clear TDFE */\r
438             SCIFA2.FSR.BIT.TDFE = 0U;\r
439             \r
440             if (g_terminal_request)\r
441             {\r
442                 /* Clear the request */\r
443                 g_terminal_request = 0U;\r
444             }\r
445                         \r
446             if (g_switch_press_flg & SW3_PRESS_FLG)\r
447             {\r
448                 /* Clear the request */\r
449                 g_switch_press_flg &= ((uint8_t)~SW3_PRESS_FLG);\r
450             }\r
451 \r
452             adc_count++;\r
453         }\r
454     }\r
455 }\r
456 /***********************************************************************************************************************\r
457 * Function Name: R_MAIN_UserInit\r
458 * Description  : This function adds user code before implementing main function.\r
459 * Arguments    : None\r
460 * Return Value : None\r
461 ***********************************************************************************************************************/\r
462 void R_MAIN_UserInit(void)\r
463 {\r
464     /* Enable RSPI1 operations */\r
465     R_RSPI1_Start();\r
466         \r
467     /* Configure UART channel for communication with host PC via RL78/G1C device */\r
468     io_init_scifa2();\r
469     \r
470     /* Enable SCIFA2 operations */\r
471     R_SCIFA2_Start();\r
472 }\r
473 \r
474 static void led_display_count (const uint8_t count)\r
475 {\r
476     /* Set LEDs according to lower nibble of count parameter */\r
477     LED2 = (uint8_t) ((count & 0x01) ? LED_ON : LED_OFF);\r
478     LED3 = (uint8_t) ((count & 0x02) ? LED_ON : LED_OFF);\r
479 }\r
480 \r
481 /*-----------------------------------------------------------*/\r
482 \r
483 void vApplicationMallocFailedHook( void )\r
484 {\r
485         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
486         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
487         internally by FreeRTOS API functions that create tasks, queues, software\r
488         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
489         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
490 \r
491         /* Force an assert. */\r
492         configASSERT( ( volatile void * ) NULL );\r
493 }\r
494 /*-----------------------------------------------------------*/\r
495 \r
496 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
497 {\r
498         ( void ) pcTaskName;\r
499         ( void ) pxTask;\r
500 \r
501         /* Run time stack overflow checking is performed if\r
502         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
503         function is called if a stack overflow is detected. */\r
504 \r
505         /* Force an assert. */\r
506         configASSERT( ( volatile void * ) NULL );\r
507 }\r
508 /*-----------------------------------------------------------*/\r
509 \r
510 void vApplicationIdleHook( void )\r
511 {\r
512 volatile size_t xFreeHeapSpace;\r
513 \r
514         /* This is just a trivial example of an idle hook.  It is called on each\r
515         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
516         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
517         memory management section on the http://www.FreeRTOS.org web site for memory\r
518         management options.  If there is a lot of heap memory free then the\r
519         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
520         RAM. */\r
521         xFreeHeapSpace = xPortGetFreeHeapSize();\r
522 \r
523         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
524         ( void ) xFreeHeapSpace;\r
525 }\r
526 /*-----------------------------------------------------------*/\r
527 \r
528 void vApplicationTickHook( void )\r
529 {\r
530 }\r
531 /*-----------------------------------------------------------*/\r
532 \r
533 void vApplicationIRQHandler( void )\r
534 {\r
535 }\r
536 \r
537 \r
538 #endif\r
539 \r