]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1-RevB_IAR/main.c
Renamed RISC-V_RV32_SiFive_HiFive1_IAR directory to RISC-V_RV32_SiFive_HiFive1-RevB_I...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1-RevB_IAR / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\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  * This project provides two demo applications.  A simple blinky style project,\r
30  * and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
32  * select between the two.  The simply blinky demo is implemented and described\r
33  * in main_blinky.c.  The more comprehensive test and demo application is\r
34  * implemented and described in main_full.c.\r
35  *\r
36  * This file implements the code that is not demo specific, including the\r
37  * hardware setup and standard FreeRTOS hook functions.\r
38  *\r
39  * When running on the HiFive Rev B hardware:\r
40  * When executing correctly the red LED will toggle every three seconds.  If\r
41  * the red LED toggles every 500ms then one of the self-monitoring test tasks\r
42  * discovered a potential issue.  If the red led stops toggling then a hardware\r
43  * exception occurred or an assert was hit.\r
44  *\r
45  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
46  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
47  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
48  *\r
49  */\r
50 \r
51 /* FreeRTOS kernel includes. */\r
52 #include <FreeRTOS.h>\r
53 #include <task.h>\r
54 \r
55 /* Standard includes. */\r
56 #include <stdio.h>\r
57 \r
58 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
59 or 0 to run the more comprehensive test and demo application. */\r
60 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
61 \r
62 /* UART hardware constants. */\r
63 #define mainUART_BASE_ADDRESS                           ( *( volatile uint32_t * ) 0x20000000UL )\r
64 #define mainUART_TX_DATA                                        0x00\r
65 #define mainUART_TX_CTRL                                        0x08\r
66 #define mainUART_RX_CTRL                                        0x0c\r
67 #define mainUART_CLOCK_DIV                                      0x18\r
68 #define mainUART_TX_ENABLE_BIT                          (1UL <<  0UL)\r
69 #define mainUART_RX_ENABLE_BIT                          (1UL <<  0UL)\r
70 #define mainUART_TX_FULL_BIT                            (1UL << 31UL)\r
71 #define mainUART_REGISTER( offset )                     ( ( mainUART_BASE_ADDRESS + offset ) )\r
72 #define mainUART_REGISTER_WORD( offset )        ( *( ( uint32_t * ) mainUART_REGISTER( offset ) ) )\r
73 \r
74 /* Hardware LED specifics. */\r
75 #define mainRED_LED_PIN                                         ( 1UL << 0x16UL )\r
76 #define mainLED_IO_BASE_ADDRESS                         ( 0x10012000UL )\r
77 #define mainRED_LED_INPUT_ENABLE_REG            ( * ( uint32_t * ) ( mainLED_IO_BASE_ADDRESS + 4UL ) )\r
78 #define mainRED_LED_OUTPUT_ENABLE_REG           ( * ( uint32_t * ) ( mainLED_IO_BASE_ADDRESS + 8UL ) )\r
79 \r
80 /* Hardware LED specifics. */\r
81 #define mainUART_PINMUX_BASE_ADDRESS    ( 0x10012000 )\r
82 #define mainUART0_BASE_ADDRESS                  0x10013000UL\r
83 #define mainUART_CLOCK_RATE                     16000000UL\r
84 #define mainUART_BAUD_RATE                              115200UL\r
85 #define mainUART0_TX_DATA_REG                   ( * ( uint32_t * ) ( mainUART0_BASE_ADDRESS + 0UL ) )\r
86 #define mainUART0_TX_DATA_BYTE_REG              ( * ( uint8_t * ) ( mainUART0_BASE_ADDRESS + 0UL ) )\r
87 #define mainUART0_DIV_REG                               ( * ( uint32_t * ) ( mainUART0_BASE_ADDRESS + 24UL ) )\r
88 #define mainUART0_TXCTRL_REG                    ( * ( uint32_t * ) ( mainUART0_BASE_ADDRESS + 8UL ) )\r
89 #define mainUART0_RXCTRL_REG                    ( * ( uint32_t * ) ( mainUART0_BASE_ADDRESS + 12UL ) )\r
90 #define mainUART0_GPIO_SEL_REG                  ( * ( uint32_t * ) ( mainUART_PINMUX_BASE_ADDRESS + 60UL ) )\r
91 #define mainUART0_GPIO_SEL_EN                   ( * ( uint32_t * ) ( mainUART_PINMUX_BASE_ADDRESS + 56UL ) )\r
92 #define mainUART_TXEN_BIT                               ( 1UL )\r
93 #define mainUART0_PIN                                   ( 0x30000UL )\r
94 \r
95 /* Registers used to initialise the PLIC. */\r
96 #define mainPLIC_PENDING_0 ( * ( ( volatile uint32_t * ) 0x0C001000UL ) )\r
97 #define mainPLIC_PENDING_1 ( * ( ( volatile uint32_t * ) 0x0C001004UL ) )\r
98 #define mainPLIC_ENABLE_0  ( * ( ( volatile uint32_t * ) 0x0C002000UL ) )\r
99 #define mainPLIC_ENABLE_1  ( * ( ( volatile uint32_t * ) 0x0C002004UL ) )\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
105  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
106  */\r
107 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
108         extern void main_blinky( void );\r
109 #else\r
110         extern void main_full( void );\r
111 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
112 \r
113 /*\r
114  * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
115  * within this file.  See https://www.freertos.org/a00016.html\r
116  */\r
117 void vApplicationMallocFailedHook( void );\r
118 void vApplicationIdleHook( void );\r
119 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
120 void vApplicationTickHook( void );\r
121 \r
122 /*\r
123  * Setup the hardware to run this demo.\r
124  */\r
125 static void prvSetupHardware( void );\r
126 \r
127 /* Simple polling UART send function. */\r
128 void vSendString( const char * const pcString );\r
129 \r
130 /* Toggle the red LED. */\r
131 void vTogglelED( void );\r
132 \r
133 /*-----------------------------------------------------------*/\r
134 \r
135 int main( void )\r
136 {\r
137         prvSetupHardware();\r
138 \r
139         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
140         of this file. */\r
141         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
142         {\r
143                 main_blinky();\r
144         }\r
145         #else\r
146         {\r
147                 main_full();\r
148         }\r
149         #endif\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 static void prvSetupHardware( void )\r
154 {\r
155         /* Set all interrupt enable bits to 0. */\r
156         mainPLIC_ENABLE_0 = 0UL;\r
157         mainPLIC_ENABLE_1 = 0UL;\r
158 \r
159         /* Clear all pending interrupts. */\r
160         mainPLIC_PENDING_0 = 0UL;\r
161         mainPLIC_PENDING_1 = 0UL;\r
162 \r
163         /* Disable Red LED input. */\r
164         mainRED_LED_INPUT_ENABLE_REG &= ~mainRED_LED_PIN;\r
165 \r
166         /* Enable Red LED output. */\r
167         mainRED_LED_OUTPUT_ENABLE_REG |= mainRED_LED_PIN;\r
168 \r
169         /* Set UART baud rate. */\r
170         mainUART0_DIV_REG = ( mainUART_CLOCK_RATE / mainUART_BAUD_RATE ) - 1;\r
171 \r
172         /* Enable UART Tx. */\r
173         mainUART0_TXCTRL_REG |= mainUART_TXEN_BIT;\r
174         mainUART0_GPIO_SEL_REG &= mainUART0_PIN;\r
175         mainUART0_GPIO_SEL_EN |= mainUART0_PIN;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vToggleLED( void )\r
180 {\r
181 static uint32_t ulLEDState = 0;\r
182 \r
183         if( ulLEDState == 0 )\r
184         {\r
185                 mainRED_LED_OUTPUT_ENABLE_REG |= mainRED_LED_PIN;\r
186         }\r
187         else\r
188         {\r
189                 mainRED_LED_OUTPUT_ENABLE_REG &= ~mainRED_LED_PIN;\r
190         }\r
191         ulLEDState = !ulLEDState;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vSendString( const char * const pcString )\r
196 {\r
197 uint32_t ulIndex = 0;\r
198 \r
199         /* Crude polling UART Tx. */\r
200         while( pcString[ ulIndex ] != 0x00 )\r
201         {\r
202                 while( ( mainUART0_TX_DATA_REG & mainUART_TX_FULL_BIT ) != 0UL );\r
203                 mainUART0_TX_DATA_BYTE_REG = pcString[ ulIndex ];\r
204                 ulIndex++;\r
205         }\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vApplicationMallocFailedHook( void )\r
210 {\r
211         /* vApplicationMallocFailedHook() will only be called if\r
212         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
213         function that will get called if a call to pvPortMalloc() fails.\r
214         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
215         timer or semaphore is created.  It is also called by various parts of the\r
216         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
217         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
218         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
219         to query the size of free heap space that remains (although it does not\r
220         provide information on how the remaining heap might be fragmented). */\r
221         taskDISABLE_INTERRUPTS();\r
222         __asm volatile( "ebreak" );\r
223         for( ;; );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vApplicationIdleHook( void )\r
228 {\r
229         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
230         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
231         task.  It is essential that code added to this hook function never attempts\r
232         to block in any way (for example, call xQueueReceive() with a block time\r
233         specified, or call vTaskDelay()).  If the application makes use of the\r
234         vTaskDelete() API function (as this demo application does) then it is also\r
235         important that vApplicationIdleHook() is permitted to return to its calling\r
236         function, because it is the responsibility of the idle task to clean up\r
237         memory allocated by the kernel to any task that has since been deleted. */\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
242 {\r
243         ( void ) pcTaskName;\r
244         ( void ) pxTask;\r
245 \r
246         /* Run time stack overflow checking is performed if\r
247         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
248         function is called if a stack overflow is detected. */\r
249         taskDISABLE_INTERRUPTS();\r
250         __asm volatile( "ebreak" );\r
251         for( ;; );\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void vApplicationTickHook( void )\r
256 {\r
257         /* The tests in the full demo expect some interaction with interrupts. */\r
258         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
259         {\r
260                 extern void vFullDemoTickHook( void );\r
261                 vFullDemoTickHook();\r
262         }\r
263         #endif\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 /* Called from the kernel's port layer to handle device specific external\r
268 interrupts. */\r
269 void vApplicationHandleTrap( uint32_t mcause )\r
270 {\r
271 char pcCause[ 20 ];\r
272 \r
273         /* Not implemented yet. */\r
274         sprintf( pcCause, "%u", mcause );\r
275         vSendString( pcCause );\r
276         configASSERT( mcause == 0 );\r
277 #warning vApplicationHandleTrap not implemented.\r
278 #if 0\r
279 uint32_t ulInterruptNumber;\r
280 typedef void ( * irq_handler_t )( void );\r
281 extern const irq_handler_t isrTable[];\r
282 \r
283         ulInterruptNumber = PLIC->TARGET[ 0 ].CLAIM_COMPLETE;\r
284 \r
285         /* Read handler from table. */\r
286         /* Call handler. */\r
287 \r
288         PLIC->TARGET[ 0 ].CLAIM_COMPLETE = ulInterruptNumber;\r
289 #endif\r
290 }\r
291 \r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void *malloc( size_t xSize )\r
295 {\r
296         /* The linker script does not define a heap so artificially force an assert()\r
297         if something unexpectedly uses the C library heap.  See\r
298         https://www.freertos.org/a00111.html for more information. */\r
299         configASSERT( xTaskGetTickCount() == 0x00 );\r
300         return NULL;\r
301 }\r
302 /*-----------------------------------------------------------*/\r