]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V-Qemu-sifive_e-Eclipse-GCC/full_demo/main_full.c
Rework RISC-V QEMU example to use vanilla Eclipse in place of Freedom Studio. NOTE...
[freertos] / FreeRTOS / Demo / RISC-V-Qemu-sifive_e-Eclipse-GCC / full_demo / main_full.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  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
33  * in main.c.  This file implements the comprehensive test and demo version.\r
34  *\r
35  * NOTE 2:  This file only contains the source code that is specific to the\r
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
37  * required to configure the hardware, are defined in main.c.\r
38  *\r
39  ******************************************************************************\r
40  *\r
41  * main_full() creates all the demo application tasks and software timers, then\r
42  * starts the scheduler.  The web documentation provides more details of the\r
43  * standard demo application tasks, which provide no particular functionality,\r
44  * but do provide a good example of how to use the FreeRTOS API.\r
45  *\r
46  * In addition to the standard demo tasks, the following tasks and tests are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Reg test" tasks - These fill both the core registers with known values, then\r
50  * check that each register maintains its expected value for the lifetime of the\r
51  * task.  Each task uses a different set of values.  The reg test tasks execute\r
52  * with a very low priority, so get preempted very frequently.  A register\r
53  * containing an unexpected value is indicative of an error in the context\r
54  * switching mechanism.\r
55  *\r
56  * "Check" task - The check executes every five seconds.  It checks that all\r
57  * the standard demo tasks, and the register check tasks, are not only still\r
58  * executing, but are executing without reporting any errors.  If the check task\r
59  * discovers that a task has either stalled, or reported an error, then it\r
60  * prints an error message to the UART, otherwise it prints "Pass" followed\r
61  * by an additional period (".") after each successful loop of its implementing\r
62  * function.\r
63  */\r
64 \r
65 /* Standard includes. */\r
66 #include <stdio.h>\r
67 #include <string.h>\r
68 \r
69 /* Kernel includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "timers.h"\r
73 #include "semphr.h"\r
74 \r
75 /* Standard demo application includes. */\r
76 #include "dynamic.h"\r
77 #include "blocktim.h"\r
78 #include "recmutex.h"\r
79 #include "TimerDemo.h"\r
80 #include "EventGroupsDemo.h"\r
81 #include "TaskNotify.h"\r
82 \r
83 /* SiFive includes. */\r
84 #include "platform.h"\r
85 #include "encoding.h"\r
86 #include "unistd.h"\r
87 \r
88 /* Priorities for the demo application tasks. */\r
89 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
90 \r
91 /* The period of the check task, in ms, converted to ticks using the\r
92 pdMS_TO_TICKS() macro. */\r
93 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 5000UL )\r
94 \r
95 /* Parameters that are passed into the register check tasks solely for the\r
96 purpose of ensuring parameters are passed into tasks correctl5. */\r
97 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
98 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
99 \r
100 /* The base period used by the timer test tasks. */\r
101 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
102 \r
103 /* The size of the stack allocated to the check task (as described in the\r
104 comments at the top of this file. */\r
105 #define mainCHECK_TASK_STACK_SIZE_WORDS 85\r
106 \r
107 /* Size of the stacks to allocated for the register check tasks. */\r
108 #define mainREG_TEST_STACK_SIZE_WORDS 50\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
114  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
115  */\r
116 void main_full( void );\r
117 \r
118 /*\r
119  * The check task, as described at the top of this file.\r
120  */\r
121 static void prvCheckTask( void *pvParameters );\r
122 \r
123 /*\r
124  * Register check tasks as described at the top of this file.  The nature of\r
125  * these files necessitates that they are written in an assembly file, but the\r
126  * entry points are kept in the C file for the convenience of checking the task\r
127  * parameter.\r
128  */\r
129 static void prvRegTestTaskEntry1( void *pvParameters );\r
130 extern void vRegTest1Implementation( void );\r
131 static void prvRegTestTaskEntry2( void *pvParameters );\r
132 extern void vRegTest2Implementation( void );\r
133 \r
134 /*\r
135  * Tick hook used by the full demo, which includes code that interacts with\r
136  * some of the tests.\r
137  */\r
138 void vFullDemoTickHook( void );\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /* The following two variables are used to communicate the status of the\r
143 register check tasks to the check task.  If the variables keep incrementing,\r
144 then the register check tasks have not discovered any errors.  If a variable\r
145 stops incrementing, then an error has been found. */\r
146 volatile uint32_t ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 void main_full( void )\r
151 {\r
152         /* Create the register check tasks, as described at the top of this     file.\r
153         Use xTaskCreateStatic() to create a task using only statically allocated\r
154         memory. */\r
155         xTaskCreate( prvRegTestTaskEntry1,                      /* The function that implements the task. */\r
156                                  "Reg1",                                                /* The name of the task. */\r
157                                  mainREG_TEST_STACK_SIZE_WORDS, /* Size of stack to allocate for the task - in words not bytes!. */\r
158                                  mainREG_TEST_TASK_1_PARAMETER, /* Parameter passed into the task. */\r
159                                  tskIDLE_PRIORITY,                              /* Priority of the task. */\r
160                                  NULL );                                                /* Can be used to pass out a handle to the created task. */\r
161         xTaskCreate( prvRegTestTaskEntry2, "Reg2", mainREG_TEST_STACK_SIZE_WORDS, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
162 \r
163         /* Start all the other standard demo/test tasks.  They have no particular\r
164         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
165         kernel port. */\r
166         vStartDynamicPriorityTasks();\r
167         vCreateBlockTimeTasks();\r
168         vStartRecursiveMutexTasks();\r
169         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
170         vStartEventGroupTasks();\r
171         vStartTaskNotifyTask();\r
172 \r
173         /* Create the task that performs the 'check' functionality,     as described at\r
174         the top of this file. */\r
175         xTaskCreate( prvCheckTask, "Check", mainCHECK_TASK_STACK_SIZE_WORDS, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
176 \r
177         /* Start the scheduler. */\r
178         vTaskStartScheduler();\r
179 \r
180         /* If all is well, the scheduler will now be running, and the following\r
181         line will never be reached.  If the following line does execute, then\r
182         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
183         timer tasks to be created.  See the memory management section on the\r
184         FreeRTOS web site for more details on the FreeRTOS heap\r
185         http://www.freertos.org/a00111.html. */\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void prvCheckTask( void *pvParameters )\r
191 {\r
192 const TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
193 TickType_t xLastExecutionTime;\r
194 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
195 const char * pcStatusMessage = ".";\r
196 extern void vSendString( const char * pcString );\r
197 \r
198         /* Just to stop compiler warnings. */\r
199         ( void ) pvParameters;\r
200 \r
201         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
202         works correctly. */\r
203         xLastExecutionTime = xTaskGetTickCount();\r
204 \r
205         /* Cycle for ever, delaying then checking all the other tasks are still\r
206         operating without error.  The onboard LED is toggled on each iteration.\r
207         If an error is detected then the delay period is decreased from\r
208         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
209         effect of increasing the rate at which the onboard LED toggles, and in so\r
210         doing gives visual feedback of the system status. */\r
211         for( ;; )\r
212         {\r
213                 /* Delay until it is time to execute again. */\r
214                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
215 \r
216                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
217                 that they are all still running, and that none have detected an error. */\r
218                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
219                 {\r
220                         pcStatusMessage = "ERROR: Dynamic priority demo/tests.\r\n";\r
221                 }\r
222 \r
223                 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
224                 {\r
225                         pcStatusMessage = "ERROR: Block time demo/tests.\r\n";\r
226                 }\r
227 \r
228                 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
229                 {\r
230                         pcStatusMessage = "ERROR: Recursive mutex demo/tests.\r\n";\r
231                 }\r
232 \r
233                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) xDelayPeriod ) != pdPASS )\r
234                 {\r
235                         pcStatusMessage = "ERROR: Timer demo/tests.\r\n";\r
236                 }\r
237 \r
238                 if( xAreEventGroupTasksStillRunning() != pdPASS )\r
239                 {\r
240                         pcStatusMessage = "ERROR: Event group demo/tests.\r\n";\r
241                 }\r
242 \r
243                 if( xAreTaskNotificationTasksStillRunning() != pdPASS )\r
244                 {\r
245                         pcStatusMessage = "ERROR: Task notification demo/tests.\r\n";\r
246                 }\r
247 \r
248                 /* Check that the register test 1 task is still running. */\r
249                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
250                 {\r
251                         pcStatusMessage = "ERROR: Register test 1.\r\n";\r
252                 }\r
253                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
254 \r
255                 /* Check that the register test 2 task is still running. */\r
256                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
257                 {\r
258                         pcStatusMessage = "ERROR: Register test 2.\r\n";\r
259                 }\r
260                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
261 \r
262                 /* Write the status message to the UART. */\r
263                 vSendString( pcStatusMessage );\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static void prvRegTestTaskEntry1( void *pvParameters )\r
269 {\r
270         /* Although the regtest task is written in assembler, its entry point is\r
271         written in C for convenience of checking the task parameter is being passed\r
272         in correctly. */\r
273         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
274         {\r
275                 /* Start the part of the test that is written in assembler. */\r
276                 vRegTest1Implementation();\r
277         }\r
278 \r
279         /* The following line will only execute if the task parameter is found to\r
280         be incorrect.  The check task will detect that the regtest loop counter is\r
281         not being incremented and flag an error. */\r
282         vTaskDelete( NULL );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvRegTestTaskEntry2( void *pvParameters )\r
287 {\r
288         /* Although the regtest task is written in assembler, its entry point is\r
289         written in C for convenience of checking the task parameter is being passed\r
290         in correctly. */\r
291         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
292         {\r
293                 /* Start the part of the test that is written in assembler. */\r
294                 vRegTest2Implementation();\r
295         }\r
296 \r
297         /* The following line will only execute if the task parameter is found to\r
298         be incorrect.  The check task will detect that the regtest loop counter is\r
299         not being incremented and flag an error. */\r
300         vTaskDelete( NULL );\r
301 }\r
302 /*-----------------------------------------------------------*/\r
303 \r
304 void vFullDemoTickHook( void )\r
305 {\r
306         /* Called from vApplicationTickHook() when the project is configured to\r
307         build the full demo. */\r
308         vTimerPeriodicISRTests();\r
309         vPeriodicEventGroupsProcessing();\r
310         xNotifyTaskFromISR();\r
311 }\r