]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V-Qemu-sifive_e-Eclipse-GCC/main.c
e3a2c3a166bdcd17d9779edd3a752ec7084028b0
[freertos] / FreeRTOS / Demo / RISC-V-Qemu-sifive_e-Eclipse-GCC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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  * NOTE 1:\r
49  *\r
50  * This project has only been tested in the QEMU emulation of the HiFive board\r
51  * from SiFive.\r
52  *\r
53  * NOTE - Requires QEMU 1908xx or higher.  Start QEMU using the following command \r
54  * line:\r
55  *\r
56  * [your_path_1]\qemu-system-riscv32 -kernel [your_path_2]\FreeRTOS\Demo\RISC-V-Qemu-sifive_e-FreedomStudio\Debug\RTOSDemo.elf -S -s -machine sifive_e\r
57  *\r
58  * Where [your_path_1] must be replaced with the correct path to your QEMU\r
59  * installation and the elf file generated by this project respectively.\r
60  *\r
61  *\r
62  * NOTE 2:\r
63  *\r
64  * Start GDB using the following command line (this can be entered in the\r
65  * Eclipse Debug Launch Configuration dialogue):\r
66  *\r
67  * riscv64-unknown-elf-gdb.exe -iex "set mem inaccessible-by-default off" -iex "set arch riscv:rv32" -iex "set riscv use_compressed_breakpoint off"\r
68  *\r
69  *\r
70  * Note 3:\r
71  *\r
72  * Status information is sent to the QEMU serial console.\r
73  */\r
74 \r
75 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
76 or 0 to run the more comprehensive test and demo application. */\r
77 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
78 \r
79 /*\r
80  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
81  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
82  */\r
83 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
84         extern void main_blinky( void );\r
85 #else\r
86         #warning At the time of writing the QEMU MTIME behaviour is erratic, resulting in test failures.\r
87         extern void main_full( void );\r
88 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
89 \r
90 /*\r
91  * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
92  * within this file.  See https://www.freertos.org/a00016.html\r
93  */\r
94 void vApplicationMallocFailedHook( void );\r
95 void vApplicationIdleHook( void );\r
96 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
97 void vApplicationTickHook( void );\r
98 \r
99 /*\r
100  * Very simply polling write to the UART.  The full demo only writes single\r
101  * characters at a time so as not to disrupt the timing of the test and demo\r
102  * tasks.\r
103  */\r
104 void vSendString( const char * pcString );\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 int main( void )\r
109 {\r
110         vSendString( "Starting" );\r
111 \r
112         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
113         of this file. */\r
114         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
115         {\r
116                 main_blinky();\r
117         }\r
118         #else\r
119         {\r
120                 main_full();\r
121         }\r
122         #endif\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vApplicationMallocFailedHook( void )\r
127 {\r
128         /* vApplicationMallocFailedHook() will only be called if\r
129         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
130         function that will get called if a call to pvPortMalloc() fails.\r
131         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
132         timer or semaphore is created.  It is also called by various parts of the\r
133         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
134         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
135         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
136         to query the size of free heap space that remains (although it does not\r
137         provide information on how the remaining heap might be fragmented). */\r
138         taskDISABLE_INTERRUPTS();\r
139         for( ;; );\r
140 }\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vApplicationIdleHook( void )\r
144 {\r
145         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
146         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
147         task.  It is essential that code added to this hook function never attempts\r
148         to block in any way (for example, call xQueueReceive() with a block time\r
149         specified, or call vTaskDelay()).  If the application makes use of the\r
150         vTaskDelete() API function (as this demo application does) then it is also\r
151         important that vApplicationIdleHook() is permitted to return to its calling\r
152         function, because it is the responsibility of the idle task to clean up\r
153         memory allocated by the kernel to any task that has since been deleted. */\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
158 {\r
159         ( void ) pcTaskName;\r
160         ( void ) pxTask;\r
161 \r
162         /* Run time stack overflow checking is performed if\r
163         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
164         function is called if a stack overflow is detected. */\r
165         taskDISABLE_INTERRUPTS();\r
166         for( ;; );\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vApplicationTickHook( void )\r
171 {\r
172         /* The tests in the full demo expect some interaction with interrupts. */\r
173         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
174         {\r
175                 extern void vFullDemoTickHook( void );\r
176                 vFullDemoTickHook();\r
177         }\r
178         #endif\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vAssertCalled( void )\r
183 {\r
184 volatile uint32_t ulSetTo1ToExitFunction = 0;\r
185 \r
186         taskDISABLE_INTERRUPTS();\r
187         while( ulSetTo1ToExitFunction != 1 )\r
188         {\r
189                 __asm volatile( "NOP" );\r
190         }\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vSendString( const char * pcString )\r
195 {\r
196 const uint32_t ulTxFifoFullBit = 0x80000000UL;\r
197 \r
198         while( *pcString != 0x00 )\r
199         {\r
200                 while( ( UART0_REG( UART_REG_TXFIFO ) & ulTxFifoFullBit ) != 0UL );\r
201                 UART0_REG( UART_REG_TXFIFO ) = *pcString;\r
202                 pcString++;\r
203         }\r
204 }\r
205 \r