]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_IAR/main.c
Minor updates to comment block for xTaskCheckForTimeOut().
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_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 /* FreeRTOS kernel includes. */\r
29 #include <FreeRTOS.h>\r
30 #include <task.h>\r
31 \r
32 /******************************************************************************\r
33  * This project provides two demo applications.  A simple blinky style project,\r
34  * and a more comprehensive test and demo application.  The\r
35  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
36  * select between the two.  The simply blinky demo is implemented and described\r
37  * in main_blinky.c.  The more comprehensive test and demo application is\r
38  * implemented and described in main_full.c.\r
39  *\r
40  * This file implements the code that is not demo specific, including the\r
41  * hardware setup and standard FreeRTOS hook functions.\r
42  *\r
43  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
44  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
45  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
46  *\r
47  */\r
48 \r
49 /* UART hardware constants. */\r
50 #define mainUART_BASE_ADDRESS                           ( *( volatile uint32_t * ) 0x20000000UL )\r
51 #define mainUART_TX_DATA                                        0x00\r
52 #define mainUART_TX_CTRL                                        0x08\r
53 #define mainUART_RX_CTRL                                        0x0c\r
54 #define mainUART_CLOCK_DIV                                      0x18\r
55 #define mainUART_TX_ENABLE_BIT                          (1UL <<  0UL)\r
56 #define mainUART_RX_ENABLE_BIT                          (1UL <<  0UL)\r
57 #define mainUART_TX_FULL_BIT                            (1UL << 31UL)\r
58 #define mainUART_REGISTER( offset )                     ( ( mainUART_BASE_ADDRESS + offset ) )\r
59 #define mainUART_REGISTER_WORD( offset )        ( *( ( uint32_t * ) mainUART_REGISTER( offset ) ) )\r
60 \r
61 \r
62 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
63 or 0 to run the more comprehensive test and demo application. */\r
64 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
65 \r
66 /*\r
67  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
68  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
69  */\r
70 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
71         extern void main_blinky( void );\r
72 #else\r
73         extern void main_full( void );\r
74 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
75 \r
76 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
77 within this file.  See https://www.freertos.org/a00016.html */\r
78 void vApplicationMallocFailedHook( void );\r
79 void vApplicationIdleHook( void );\r
80 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
81 void vApplicationTickHook( void );\r
82 \r
83 /* Prepare hardware to run the demo. */\r
84 static void prvSetupHardware( void );\r
85 \r
86 /* Send a message to the UART initialised in prvSetupHardware. */\r
87 void vSendString( const char * const pcString );\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 int main( void )\r
92 {\r
93         prvSetupHardware();\r
94 \r
95         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
96         of this file. */\r
97         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
98         {\r
99                 main_blinky();\r
100         }\r
101         #else\r
102         {\r
103                 main_full();\r
104         }\r
105         #endif\r
106 }\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 static void prvSetupHardware( void )\r
110 {\r
111 const unsigned long clock_rate = 66000000, baud_rate = 115200;\r
112 \r
113         /* Initialise the UART. */\r
114         mainUART_REGISTER_WORD( mainUART_CLOCK_DIV ) = clock_rate / baud_rate - 1;\r
115         mainUART_REGISTER_WORD( mainUART_TX_CTRL ) |= mainUART_TX_ENABLE_BIT;\r
116         mainUART_REGISTER_WORD( mainUART_RX_CTRL ) |= mainUART_RX_ENABLE_BIT;\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 void vToggleLED( void )\r
121 {\r
122 static uint32_t ulLEDState = 0;\r
123 \r
124         ulLEDState = !ulLEDState;\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vSendString( const char * const pcString )\r
129 {\r
130 uint32_t ulIndex = 0;\r
131 \r
132         while( pcString[ ulIndex ] != 0x00 )\r
133         {\r
134                 while( ( mainUART_REGISTER_WORD( mainUART_TX_DATA ) & mainUART_TX_FULL_BIT ) != 0UL );\r
135                 mainUART_REGISTER_WORD(mainUART_TX_DATA) = pcString[ ulIndex ];\r
136                 ulIndex++;\r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vApplicationMallocFailedHook( void )\r
142 {\r
143         /* vApplicationMallocFailedHook() will only be called if\r
144         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
145         function that will get called if a call to pvPortMalloc() fails.\r
146         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
147         timer or semaphore is created.  It is also called by various parts of the\r
148         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
149         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
150         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
151         to query the size of free heap space that remains (although it does not\r
152         provide information on how the remaining heap might be fragmented). */\r
153         taskDISABLE_INTERRUPTS();\r
154         __asm volatile( "ebreak" );\r
155         for( ;; );\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vApplicationIdleHook( void )\r
160 {\r
161         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
162         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
163         task.  It is essential that code added to this hook function never attempts\r
164         to block in any way (for example, call xQueueReceive() with a block time\r
165         specified, or call vTaskDelay()).  If the application makes use of the\r
166         vTaskDelete() API function (as this demo application does) then it is also\r
167         important that vApplicationIdleHook() is permitted to return to its calling\r
168         function, because it is the responsibility of the idle task to clean up\r
169         memory allocated by the kernel to any task that has since been deleted. */\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
174 {\r
175         ( void ) pcTaskName;\r
176         ( void ) pxTask;\r
177 \r
178         /* Run time stack overflow checking is performed if\r
179         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
180         function is called if a stack overflow is detected. */\r
181         taskDISABLE_INTERRUPTS();\r
182         __asm volatile( "ebreak" );\r
183         for( ;; );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vApplicationTickHook( void )\r
188 {\r
189         /* The tests in the full demo expect some interaction with interrupts. */\r
190         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
191         {\r
192                 extern void vFullDemoTickHook( void );\r
193                 vFullDemoTickHook();\r
194         }\r
195         #endif\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 /* Called from the kernel's port layer to handle device specific external\r
200 interrupts. */\r
201 void vApplicationHandleTrap( uint32_t mcause )\r
202 {\r
203         /* Not implemented yet. */\r
204         configASSERT( mcause == 0 );\r
205 #warning vApplicationHandleTrap not implemented.\r
206 #if 0\r
207 uint32_t ulInterruptNumber;\r
208 typedef void ( * irq_handler_t )( void );\r
209 extern const irq_handler_t isrTable[];\r
210 \r
211         ulInterruptNumber = PLIC->TARGET[ 0 ].CLAIM_COMPLETE;\r
212 \r
213         /* Read handler from table. */\r
214         /* Call handler. */\r
215 \r
216         PLIC->TARGET[ 0 ].CLAIM_COMPLETE = ulInterruptNumber;\r
217 #endif\r
218 }\r
219 \r
220 \r
221 \r
222 \r