]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_Renode_Emulator_SoftConsole/full_demo/main_full.c
53f79d9fd3bc74f19e7460d180ce6cec23f7a1ba
[freertos] / FreeRTOS / Demo / RISC-V_Renode_Emulator_SoftConsole / 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 three 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.".\r
61  */\r
62 \r
63 /* Standard includes. */\r
64 #include <stdio.h>\r
65 #include <string.h>\r
66 \r
67 /* Kernel includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 #include "timers.h"\r
71 #include "semphr.h"\r
72 \r
73 /* Microsemi incldues. */\r
74 #include "core_timer.h"\r
75 #include "riscv_hal.h"\r
76 \r
77 /* Standard demo application includes. */\r
78 #include "dynamic.h"\r
79 #include "blocktim.h"\r
80 #include "GenQTest.h"\r
81 #include "recmutex.h"\r
82 #include "TimerDemo.h"\r
83 #include "EventGroupsDemo.h"\r
84 #include "TaskNotify.h"\r
85 #include "AbortDelay.h"\r
86 #include "countsem.h"\r
87 #include "death.h"\r
88 #include "MessageBufferDemo.h"\r
89 #include "StreamBufferDemo.h"\r
90 #include "StreamBufferInterrupt.h"\r
91 \r
92 /* Priorities for the demo application tasks. */\r
93 #define mainCHECK_TASK_PRIORITY                         ( configMAX_PRIORITIES - 1 )\r
94 #define mainCREATOR_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 3UL )\r
95 \r
96 /* The period of the check task, in ms, converted to ticks using the\r
97 pdMS_TO_TICKS() macro.  mainNO_ERROR_CHECK_TASK_PERIOD is used if no errors have\r
98 been found, mainERROR_CHECK_TASK_PERIOD is used if an error has been found. */\r
99 #define mainNO_ERROR_CHECK_TASK_PERIOD          pdMS_TO_TICKS( 3000UL )\r
100 #define mainERROR_CHECK_TASK_PERIOD             pdMS_TO_TICKS( 500UL )\r
101 \r
102 /* Parameters that are passed into the register check tasks solely for the\r
103 purpose of ensuring parameters are passed into tasks correctly. */\r
104 #define mainREG_TEST_TASK_1_PARAMETER           ( ( void * ) 0x12345678 )\r
105 #define mainREG_TEST_TASK_2_PARAMETER           ( ( void * ) 0x87654321 )\r
106 \r
107 /* The base period used by the timer test tasks. */\r
108 #define mainTIMER_TEST_PERIOD                           ( 50 )\r
109 \r
110 /* The size of the stack allocated to the check task (as described in the\r
111 comments at the top of this file. */\r
112 #define mainCHECK_TASK_STACK_SIZE_WORDS 100\r
113 \r
114 /* Size of the stacks to allocated for the register check tasks. */\r
115 #define mainREG_TEST_STACK_SIZE_WORDS 70\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * Called by main() to run the full demo (as opposed to the blinky demo) when\r
121  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
122  */\r
123 void main_full( void );\r
124 \r
125 /*\r
126  * The check task, as described at the top of this file.\r
127  */\r
128 static void prvCheckTask( void *pvParameters );\r
129 \r
130 /*\r
131  * Initialise and start the peripheral timers that are used to exercise external\r
132  * interrupt processing.\r
133  */\r
134 static void prvSetupPeripheralTimers( void );\r
135 \r
136 /*\r
137  * Register check tasks as described at the top of this file.  The nature of\r
138  * these files necessitates that they are written in an assembly file, but the\r
139  * entry points are kept in the C file for the convenience of checking the task\r
140  * parameter.\r
141  */\r
142 static void prvRegTestTaskEntry1( void *pvParameters );\r
143 extern void vRegTest1Implementation( void );\r
144 static void prvRegTestTaskEntry2( void *pvParameters );\r
145 extern void vRegTest2Implementation( void );\r
146 \r
147 /*\r
148  * Tick hook used by the full demo, which includes code that interacts with\r
149  * some of the tests.\r
150  */\r
151 void vFullDemoTickHook( void );\r
152 \r
153 /*-----------------------------------------------------------*/\r
154 \r
155 /* Timers used to exercise external interrupt processing. */\r
156 static timer_instance_t g_timer0, g_timer1;\r
157 \r
158 /* Variables incremented by the peripheral timers used to exercise external\r
159 interrupts. */\r
160 volatile uint32_t ulTimer0Interrupts = 0, ulTimer1Interrupts = 0;\r
161 \r
162 /* The following two variables are used to communicate the status of the\r
163 register check tasks to the check task.  If the variables keep incrementing,\r
164 then the register check tasks have not discovered any errors.  If a variable\r
165 stops incrementing, then an error has been found. */\r
166 volatile uint32_t ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
167 \r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void main_full( void )\r
171 {\r
172         /* Start all the other standard demo/test tasks.  They have no particular\r
173         functionality, but do demonstrate how to use the FreeRTOS API and test the\r
174         kernel port. */\r
175         vStartDynamicPriorityTasks();\r
176         vCreateBlockTimeTasks();\r
177         vStartGenericQueueTasks( tskIDLE_PRIORITY );\r
178         vStartRecursiveMutexTasks();\r
179         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
180         vStartEventGroupTasks();\r
181         vStartTaskNotifyTask();\r
182         vCreateAbortDelayTasks();\r
183         vStartCountingSemaphoreTasks();\r
184         vStartMessageBufferTasks( configMINIMAL_STACK_SIZE  );\r
185         vStartStreamBufferTasks();\r
186         vStartStreamBufferInterruptDemo();\r
187 \r
188         /* Create the register check tasks, as described at the top of this     file.\r
189         Use xTaskCreateStatic() to create a task using only statically allocated\r
190         memory. */\r
191         xTaskCreate( prvRegTestTaskEntry1,                      /* The function that implements the task. */\r
192                                  "Reg1",                                                /* The name of the task. */\r
193                                  mainREG_TEST_STACK_SIZE_WORDS, /* Size of stack to allocate for the task - in words not bytes!. */\r
194                                  mainREG_TEST_TASK_1_PARAMETER, /* Parameter passed into the task. */\r
195                                  tskIDLE_PRIORITY,                              /* Priority of the task. */\r
196                                  NULL );                                                /* Can be used to pass out a handle to the created task. */\r
197         xTaskCreate( prvRegTestTaskEntry2, "Reg2", mainREG_TEST_STACK_SIZE_WORDS, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
198 \r
199         /* Create the task that performs the 'check' functionality,     as described at\r
200         the top of this file. */\r
201         xTaskCreate( prvCheckTask, "Check", mainCHECK_TASK_STACK_SIZE_WORDS, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
202 \r
203         /* The set of tasks created by the following function call have to be\r
204         created last as they keep account of the number of tasks they expect to see\r
205         running. */\r
206         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
207 \r
208         /* Start the timers that are used to exercise external interrupt handling. */\r
209         prvSetupPeripheralTimers();\r
210 \r
211         /* Start the scheduler. */\r
212         vTaskStartScheduler();\r
213 \r
214         /* If all is well, the scheduler will now be running, and the following\r
215         line will never be reached.  If the following line does execute, then\r
216         there was insufficient FreeRTOS heap memory available for the Idle and/or\r
217         timer tasks to be created.  See the memory management section on the\r
218         FreeRTOS web site for more details on the FreeRTOS heap\r
219         http://www.freertos.org/a00111.html. */\r
220         for( ;; );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvCheckTask( void *pvParameters )\r
225 {\r
226 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_TASK_PERIOD;\r
227 TickType_t xLastExecutionTime;\r
228 uint32_t ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
229 uint32_t ulLastTimer0Interrupts = 0, ulLastTimer1Interrupts = 0;\r
230 char * const pcPassMessage = ".";\r
231 char * pcStatusMessage = pcPassMessage;\r
232 extern void vSendString( const char * const pcString );\r
233 extern void vToggleLED( void );\r
234 \r
235         /* Just to stop compiler warnings. */\r
236         ( void ) pvParameters;\r
237 \r
238         /* Start with a pass message, after which a '.' character will be printed\r
239         out on each successful loop. */\r
240         vSendString( "Pass" );\r
241 \r
242         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
243         works correctly. */\r
244         xLastExecutionTime = xTaskGetTickCount();\r
245 \r
246         /* Cycle for ever, delaying then checking all the other tasks are still\r
247         operating without error.  The onboard LED is toggled on each iteration.\r
248         If an error is detected then the delay period is decreased from\r
249         mainNO_ERROR_CHECK_TASK_PERIOD to mainERROR_CHECK_TASK_PERIOD.  This has the\r
250         effect of increasing the rate at which the onboard LED toggles, and in so\r
251         doing gives visual feedback of the system status. */\r
252         for( ;; )\r
253         {\r
254                 /* Delay until it is time to execute again. */\r
255                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
256 \r
257                 /* Check all the demo tasks (other than the flash tasks) to ensure\r
258                 that they are all still running, and that none have detected an error. */\r
259                 if( xAreDynamicPriorityTasksStillRunning() == pdFALSE )\r
260                 {\r
261                         pcStatusMessage = "ERROR: Dynamic priority demo/tests.\r\n";\r
262                 }\r
263 \r
264                 if( xAreBlockTimeTestTasksStillRunning() == pdFALSE )\r
265                 {\r
266                         pcStatusMessage = "ERROR: Block time demo/tests.\r\n";\r
267                 }\r
268 \r
269                 if( xAreGenericQueueTasksStillRunning() == pdFALSE )\r
270                 {\r
271                         pcStatusMessage = "ERROR: Generic queue demo/tests.\r\n";\r
272                 }\r
273 \r
274                 if( xAreRecursiveMutexTasksStillRunning() == pdFALSE )\r
275                 {\r
276                         pcStatusMessage = "ERROR: Recursive mutex demo/tests.\r\n";\r
277                 }\r
278 \r
279                 if( xAreTimerDemoTasksStillRunning( ( TickType_t ) xDelayPeriod ) == pdFALSE )\r
280                 {\r
281                         pcStatusMessage = "ERROR: Timer demo/tests.\r\n";\r
282                 }\r
283 \r
284                 if( xAreEventGroupTasksStillRunning() == pdFALSE )\r
285                 {\r
286                         pcStatusMessage = "ERROR: Event group demo/tests.\r\n";\r
287                 }\r
288 \r
289                 if( xAreTaskNotificationTasksStillRunning() == pdFALSE )\r
290                 {\r
291                         pcStatusMessage = "ERROR: Task notification demo/tests.\r\n";\r
292                 }\r
293 \r
294                 if( xAreAbortDelayTestTasksStillRunning() == pdFALSE )\r
295                 {\r
296                         pcStatusMessage = "ERROR: Abort delay.\r\n";\r
297                 }\r
298 \r
299                 if( xAreCountingSemaphoreTasksStillRunning() == pdFALSE )\r
300                 {\r
301                         pcStatusMessage = "ERROR: Counting semaphores.\r\n";\r
302                 }\r
303 \r
304                 if( xIsCreateTaskStillRunning() == pdFALSE )\r
305                 {\r
306                         pcStatusMessage = "ERROR: Suicide tasks.\r\n";\r
307                 }\r
308 \r
309                 if( xAreMessageBufferTasksStillRunning() == pdFALSE )\r
310                 {\r
311                         pcStatusMessage = "ERROR: Message buffer.\r\n";\r
312                 }\r
313 \r
314                 if( xAreStreamBufferTasksStillRunning() == pdFALSE )\r
315                 {\r
316                         pcStatusMessage = "ERROR: Stream buffer.\r\n";\r
317                 }\r
318 \r
319                 if( xIsInterruptStreamBufferDemoStillRunning() == pdFALSE )\r
320                 {\r
321                         pcStatusMessage = "ERROR: Stream buffer interrupt.\r\n";\r
322                 }\r
323 \r
324                 /* Check that the register test 1 task is still running. */\r
325                 if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
326                 {\r
327                         pcStatusMessage = "ERROR: Register test 1.\r\n";\r
328                 }\r
329                 ulLastRegTest1Value = ulRegTest1LoopCounter;\r
330 \r
331                 /* Check that the register test 2 task is still running. */\r
332                 if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
333                 {\r
334                         pcStatusMessage = "ERROR: Register test 2.\r\n";\r
335                 }\r
336                 ulLastRegTest2Value = ulRegTest2LoopCounter;\r
337 \r
338                 /* Check interrupts from the peripheral timers are being handled. */\r
339                 if( ulLastTimer0Interrupts == ulTimer0Interrupts )\r
340                 {\r
341                         pcStatusMessage = "ERROR: Peripheral timer 0.\r\n";\r
342                 }\r
343                 ulLastTimer0Interrupts = ulTimer0Interrupts;\r
344 \r
345                 if( ulLastTimer1Interrupts == ulTimer1Interrupts )\r
346                 {\r
347                         pcStatusMessage = "ERROR: Peripheral timer 1.\r\n";\r
348                 }\r
349                 ulLastTimer1Interrupts = ulTimer1Interrupts;\r
350 \r
351                 /* Write the status message to the UART. */\r
352                 vSendString( pcStatusMessage );\r
353 \r
354                 /* If an error has been found then increase the LED toggle rate by\r
355                 increasing the cycle frequency. */\r
356                 if( pcStatusMessage != pcPassMessage )\r
357                 {\r
358                         xDelayPeriod = mainERROR_CHECK_TASK_PERIOD;\r
359                 }\r
360         }\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 static void prvRegTestTaskEntry1( void *pvParameters )\r
365 {\r
366         /* Although the regtest task is written in assembler, its entry point is\r
367         written in C for convenience of checking the task parameter is being passed\r
368         in correctly. */\r
369         if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )\r
370         {\r
371                 /* Start the part of the test that is written in assembler. */\r
372                 vRegTest1Implementation();\r
373         }\r
374 \r
375         /* The following line will only execute if the task parameter is found to\r
376         be incorrect.  The check task will detect that the regtest loop counter is\r
377         not being incremented and flag an error. */\r
378         vTaskDelete( NULL );\r
379 }\r
380 /*-----------------------------------------------------------*/\r
381 \r
382 static void prvRegTestTaskEntry2( void *pvParameters )\r
383 {\r
384         /* Although the regtest task is written in assembler, its entry point is\r
385         written in C for convenience of checking the task parameter is being passed\r
386         in correctly. */\r
387         if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )\r
388         {\r
389                 /* Start the part of the test that is written in assembler. */\r
390                 vRegTest2Implementation();\r
391         }\r
392 \r
393         /* The following line will only execute if the task parameter is found to\r
394         be incorrect.  The check task will detect that the regtest loop counter is\r
395         not being incremented and flag an error. */\r
396         vTaskDelete( NULL );\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 void vFullDemoTickHook( void )\r
401 {\r
402         /* The full demo includes a software timer demo/test that requires\r
403         prodding periodically from the tick interrupt. */\r
404         vTimerPeriodicISRTests();\r
405 \r
406         /* Call the periodic event group from ISR demo. */\r
407         vPeriodicEventGroupsProcessing();\r
408 \r
409         /* Use task notifications from an interrupt. */\r
410         xNotifyTaskFromISR();\r
411 \r
412         /* Writes to stream buffer byte by byte to test the stream buffer trigger\r
413         level functionality. */\r
414         vPeriodicStreamBufferProcessing();\r
415 \r
416         /* Writes a string to a string buffer four bytes at a time to demonstrate\r
417         a stream being sent from an interrupt to a task. */\r
418         vBasicStreamBufferSendFromISR();\r
419 \r
420         /* Called from vApplicationTickHook() when the project is configured to\r
421         build the full test/demo applications. */\r
422 }\r
423 /*-----------------------------------------------------------*/\r
424 \r
425 static void prvSetupPeripheralTimers( void )\r
426 {\r
427         TMR_init(       &g_timer0,\r
428                                 CORETIMER0_BASE_ADDR,\r
429                                 TMR_CONTINUOUS_MODE,\r
430                                 PRESCALER_DIV_1024,\r
431                                 83000 );\r
432 \r
433     TMR_init(   &g_timer1,\r
434                                 CORETIMER1_BASE_ADDR,\r
435                                 TMR_CONTINUOUS_MODE,\r
436                                 PRESCALER_DIV_512,\r
437                                 42000 );\r
438 \r
439         /* In this version of the PLIC, the priorities are fixed at 1.\r
440         Lower numbered devices have higher priorities. But this code is given as\r
441         an example.\r
442         */\r
443         PLIC_SetPriority( External_30_IRQn, 1 );\r
444         PLIC_SetPriority( External_31_IRQn, 1 );\r
445 \r
446         /*Enable Timer 1 & 0 Interrupt*/\r
447         PLIC_EnableIRQ( External_30_IRQn );\r
448         PLIC_EnableIRQ( External_31_IRQn );\r
449 \r
450         /* Enable the timers */\r
451         TMR_enable_int( &g_timer0 );\r
452         TMR_enable_int( &g_timer1 );\r
453 \r
454         /* Make sure timers don't interrupt until the scheduler is running. */\r
455         portDISABLE_INTERRUPTS();\r
456 \r
457         /*Start the timer*/\r
458         TMR_start( &g_timer0 );\r
459         TMR_start( &g_timer1 );\r
460 }\r
461 /*-----------------------------------------------------------*/\r
462 \r
463 /*Core Timer 0 Interrupt Handler*/\r
464 uint8_t External_30_IRQHandler( void )\r
465 {\r
466         ulTimer0Interrupts++;\r
467     TMR_clear_int(&g_timer0);\r
468     return( EXT_IRQ_KEEP_ENABLED );\r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 /*Core Timer 1 Interrupt Handler*/\r
473 uint8_t External_31_IRQHandler( void )\r
474 {\r
475         ulTimer1Interrupts++;\r
476     TMR_clear_int(&g_timer1);\r
477 \r
478     return( EXT_IRQ_KEEP_ENABLED );\r
479 }\r