]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32M1_Vega_GCC_Eclipse/projects/RTOSDemo_ri5cy/main.c
75a51b70f87ce61278de970280b9472167b4501d
[freertos] / FreeRTOS / Demo / RISC-V_RV32M1_Vega_GCC_Eclipse / projects / RTOSDemo_ri5cy / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\r
30  * Create implementation of vPortSetupTimerInterrupt() if the CLINT is not\r
31  * available, but make sure the configCLINT_BASE_ADDRESS constant is still\r
32  * defined.\r
33  *\r
34  * Define vPortHandleInterrupt to whatever the interrupt handler is called.  In\r
35  * this case done by defining vPortHandleInterrupt=SystemIrqHandler on the\r
36  * assembler command line as SystemIrqHandler is referenced from both FreeRTOS\r
37  * code and the libraries that come with the Vega board.\r
38  */\r
39 \r
40 /* FreeRTOS kernel includes. */\r
41 #include <FreeRTOS.h>\r
42 #include <task.h>\r
43 \r
44 /* Vega includes. */\r
45 #include "fsl_device_registers.h"\r
46 #include "fsl_debug_console.h"\r
47 #include "board.h"\r
48 #include "pin_mux.h"\r
49 #include "clock_config.h"\r
50 \r
51 \r
52 /******************************************************************************\r
53  * This project provides two demo applications.  A simple blinky style project,\r
54  * and a more comprehensive test and demo application.  The\r
55  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
56  * select between the two.  The simply blinky demo is implemented and described\r
57  * in main_blinky.c.  The more comprehensive test and demo application is\r
58  * implemented and described in main_full.c.\r
59  *\r
60  * This file implements the code that is not demo specific, including the\r
61  * hardware setup and standard FreeRTOS hook functions.\r
62  *\r
63  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
64  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
65  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
66  *\r
67  */\r
68 \r
69 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
70 or 0 to run the more comprehensive test and demo application. */\r
71 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
72 \r
73 /*\r
74  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
75  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
76  */\r
77 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
78         extern void main_blinky( void );\r
79 #else\r
80         extern void main_full( void );\r
81 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
82 \r
83 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
84 within this file.  See https://www.freertos.org/a00016.html */\r
85 void vApplicationMallocFailedHook( void );\r
86 void vApplicationIdleHook( void );\r
87 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
88 void vApplicationTickHook( void );\r
89 \r
90 /* Prepare haredware to run the demo. */\r
91 static void prvSetupHardware( void );\r
92 \r
93 /* Send a messaage to the UART initialised in prvSetupHardware. */\r
94 void vSendString( const char * const pcString );\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 void main( void )\r
99 {\r
100         prvSetupHardware();\r
101 \r
102         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
103         of this file. */\r
104         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
105         {\r
106                 main_blinky();\r
107         }\r
108         #else\r
109         {\r
110                 main_full();\r
111         }\r
112         #endif\r
113 }\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 static void prvSetupHardware( void )\r
117 {\r
118 gpio_pin_config_t mGpioPinConfigStruct;\r
119 \r
120         /* Init board hardware. */\r
121         BOARD_InitPins();\r
122         BOARD_BootClockRUN();\r
123         BOARD_InitDebugConsole();\r
124 \r
125         /* For LED. */\r
126         mGpioPinConfigStruct.outputLogic = 1U; /* High. */\r
127         mGpioPinConfigStruct.pinDirection = kGPIO_DigitalOutput;\r
128         GPIO_PinInit( BOARD_LED1_GPIO, BOARD_LED1_GPIO_PIN, &mGpioPinConfigStruct );\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vToggleLED( void )\r
133 {\r
134         GPIO_TogglePinsOutput( BOARD_LED1_GPIO, 1U << BOARD_LED1_GPIO_PIN );\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 void vSendString( const char * const pcString )\r
139 {\r
140         PRINTF( pcString );\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 void vApplicationMallocFailedHook( void )\r
145 {\r
146         /* vApplicationMallocFailedHook() will only be called if\r
147         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
148         function that will get called if a call to pvPortMalloc() fails.\r
149         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
150         timer or semaphore is created.  It is also called by various parts of the\r
151         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
152         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
153         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
154         to query the size of free heap space that remains (although it does not\r
155         provide information on how the remaining heap might be fragmented). */\r
156         taskDISABLE_INTERRUPTS();\r
157         __asm volatile( "ebreak" );\r
158         for( ;; );\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vApplicationIdleHook( void )\r
163 {\r
164         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
165         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
166         task.  It is essential that code added to this hook function never attempts\r
167         to block in any way (for example, call xQueueReceive() with a block time\r
168         specified, or call vTaskDelay()).  If the application makes use of the\r
169         vTaskDelete() API function (as this demo application does) then it is also\r
170         important that vApplicationIdleHook() is permitted to return to its calling\r
171         function, because it is the responsibility of the idle task to clean up\r
172         memory allocated by the kernel to any task that has since been deleted. */\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
177 {\r
178         ( void ) pcTaskName;\r
179         ( void ) pxTask;\r
180 \r
181         /* Run time stack overflow checking is performed if\r
182         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
183         function is called if a stack overflow is detected. */\r
184         taskDISABLE_INTERRUPTS();\r
185         __asm volatile( "ebreak" );\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vApplicationTickHook( void )\r
191 {\r
192         /* The tests in the full demo expect some interaction with interrupts. */\r
193         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
194         {\r
195                 extern void vFullDemoTickHook( void );\r
196                 vFullDemoTickHook();\r
197         }\r
198         #endif\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 void vPortSetupTimerInterrupt( void )\r
203 {\r
204 extern void SystemSetupSystick(uint32_t tickRateHz, uint32_t intPriority );\r
205 \r
206         /* No CLINT so use the LPIT (Low Power Interrupt Timer) to generate the tick\r
207         interrupt. */\r
208         CLOCK_SetIpSrc( kCLOCK_Lpit0, kCLOCK_IpSrcFircAsync );\r
209         SystemSetupSystick( configTICK_RATE_HZ, configKERNEL_INTERRUPT_PRIORITY - 1 );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void LPIT0_IRQHandler( void )\r
214 {\r
215 BaseType_t xTaskIncrementTick( void );\r
216 void vTaskSwitchContext( void );\r
217 \r
218 #warning requires critical section if interrpt nesting is used.\r
219 \r
220         /* vPortSetupTimerInterrupt() uses LPIT0 to generate the tick interrupt. */\r
221         if( xTaskIncrementTick() != 0 )\r
222         {\r
223                 vTaskSwitchContext();\r
224         }\r
225 \r
226         /* Clear LPIT0 interrupt. */\r
227         LPIT0->MSR = 1U;\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 /* At the time of writing, interrupt nesting is not supported, so do not use\r
232 the default SystemIrqHandler() implementation as that enables interrupts.  A\r
233 version that does not enable interrupts is provided below.  THIS INTERRUPT\r
234 HANDLER IS SPECIFIC TO THE VEGA BOARD WHICH DOES NOT INCLUDE A CLINT! */\r
235 void SystemIrqHandler( uint32_t mcause )\r
236 {\r
237 uint32_t ulInterruptNumber;\r
238 typedef void ( * irq_handler_t )( void );\r
239 extern const irq_handler_t isrTable[];\r
240 \r
241         ulInterruptNumber = mcause & 0x1FUL;\r
242 \r
243         /* Clear pending flag in EVENT unit .*/\r
244         EVENT_UNIT->INTPTPENDCLEAR = ( 1U << ulInterruptNumber );\r
245 \r
246         /* Read back to make sure write finished. */\r
247         (void)(EVENT_UNIT->INTPTPENDCLEAR);\r
248 \r
249         /* Now call the real irq handler for ulInterruptNumber */\r
250         isrTable[ ulInterruptNumber ]();\r
251 }\r
252 \r