]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_A2F200_SoftConsole/main-full.c
osEventFlagsWait: Fix flag comparison
[cmsis-freertos] / Demo / CORTEX_A2F200_SoftConsole / main-full.c
1 /*
2  * FreeRTOS Kernel V10.2.1
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * main-blinky.c is included when the "Blinky" build configuration is used.
30  * main-full.c is included when the "Full" build configuration is used.
31  *
32  * main-full.c (this file) defines a comprehensive demo that creates many
33  * tasks, queues, semaphores and timers.  It also demonstrates how Cortex-M3
34  * interrupts can interact with FreeRTOS tasks/timers, and implements a simple
35  * and small interactive web server.
36  *
37  * This project runs on the SmartFusion A2F-EVAL-KIT evaluation board, which
38  * is populated with an A2F200M3F SmartFusion mixed signal FPGA.  The A2F200M3F
39  * incorporates a Cortex-M3 microcontroller.
40  *
41  * The main() Function:
42  * main() creates two demo specific software timers, one demo specific queue,
43  * and three demo specific tasks.  It then creates a whole host of 'standard
44  * demo' tasks/queues/semaphores, before starting the scheduler.  The demo
45  * specific tasks and timers are described in the comments here.  The standard
46  * demo tasks are described on the FreeRTOS.org web site.
47  *
48  * The standard demo tasks provide no specific functionality.  They are
49  * included to both test the FreeRTOS port, and provide examples of how the
50  * various FreeRTOS API functions can be used.
51  *
52  * The Demo Specific Queue Send Task:
53  * The queue send task is implemented by the prvQueueSendTask() function in
54  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
55  * block for 200 milliseconds, before sending the value 100 to the queue that
56  * was created within main().  Once the value is sent, the task loops back
57  * around to block for another 200 milliseconds.
58  *
59  * The Demo Specific Queue Receive Task:
60  * The queue receive task is implemented by the prvQueueReceiveTask() function
61  * in this file.  prvQueueReceiveTask() sits in a loop that causes it to
62  * repeatedly attempt to read data from the queue that was created within
63  * main().  When data is received, the task checks the value of the data, and
64  * if the value equals the expected 100, toggles the green LED.  The 'block
65  * time' parameter passed to the queue receive function specifies that the task
66  * should be held in the Blocked state indefinitely to wait for data to be
67  * available on the queue.  The queue receive task will only leave the Blocked
68  * state when the queue send task writes to the queue.  As the queue send task
69  * writes to the queue every 200 milliseconds, the queue receive task leaves
70  * the Blocked state every 200 milliseconds, and therefore toggles the LED
71  * every 200 milliseconds.
72  *
73  * The Demo Specific OLED Task:
74  * The OLED task is a very simple task that just scrolls a message across the
75  * OLED.  Ideally this would be done in a timer, but the OLED driver accesses
76  * the I2C which is time consuming.
77  *
78  * The Demo Specific LED Software Timer and the Button Interrupt:
79  * The user button SW1 is configured to generate an interrupt each time it is
80  * pressed.  The interrupt service routine switches an LED on, and resets the
81  * LED software timer.  The LED timer has a 5000 millisecond (5 second) period,
82  * and uses a callback function that is defined to just turn the LED off again.
83  * Therefore, pressing the user button will turn the LED on, and the LED will
84  * remain on until a full five seconds pass without the button being pressed.
85  *
86  * The Demo Specific "Check" Callback Function:
87  * This is called each time the 'check' timer expires.  The check timer
88  * callback function inspects all the standard demo tasks to see if they are
89  * all executing as expected.  The check timer is initially configured to
90  * expire every three seconds, but will shorted this to every 500ms if an error
91  * is ever discovered.  The check timer callback toggles the LED defined by
92  * the mainCHECK_LED definition each time it executes.  Therefore, if LED
93  * mainCHECK_LED is toggling every three seconds, then no error have been found.
94  * If LED mainCHECK_LED is toggling every 500ms, then at least one errors has
95  * been found.  The task in which the error was discovered is displayed at the
96  * bottom of the "task stats" page that is served by the embedded web server.
97  *
98  * The Demo Specific Idle Hook Function:
99  * The idle hook function demonstrates how to query the amount of FreeRTOS heap
100  * space that is remaining (see vApplicationIdleHook() defined in this file).
101  *
102  * The Web Server Task:
103  * The IP address used by the SmartFusion target is configured by the
104  * definitions configIP_ADDR0 to configIP_ADDR3, which are located in the
105  * FreeRTOSConfig.h header file.  See the documentation page for this example
106  * on the http://www.FreeRTOS.org web site for further connection information.
107  */
108
109 /* Kernel includes. */
110 #include "FreeRTOS.h"
111 #include "task.h"
112 #include "queue.h"
113 #include "timers.h"
114
115 /* Microsemi drivers/libraries includes. */
116 #include "mss_gpio.h"
117 #include "mss_watchdog.h"
118 #include "mss_timer.h"
119 #include "mss_ace.h"
120 #include "oled.h"
121
122 /* Common demo includes. */
123 #include "partest.h"
124 #include "flash.h"
125 #include "BlockQ.h"
126 #include "death.h"
127 #include "blocktim.h"
128 #include "semtest.h"
129 #include "GenQTest.h"
130 #include "QPeek.h"
131 #include "recmutex.h"
132 #include "TimerDemo.h"
133
134 /* Priorities at which the tasks are created. */
135 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
136 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
137
138 /* The rate at which data is sent to the queue, specified in milliseconds, and
139 converted to ticks using the portTICK_PERIOD_MS constant. */
140 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )
141
142 /* The number of items the queue can hold.  This is 1 as the receive task
143 will remove items as they are added, meaning the send task should always find
144 the queue empty. */
145 #define mainQUEUE_LENGTH                        ( 1 )
146
147 /* The LED toggled by the check timer callback function. */
148 #define mainCHECK_LED                           0x07UL
149
150 /* The LED turned on by the button interrupt, and turned off by the LED timer. */
151 #define mainTIMER_CONTROLLED_LED        0x06UL
152
153 /* The LED toggle by the queue receive task. */
154 #define mainTASK_CONTROLLED_LED         0x05UL
155
156 /* Constant used by the standard timer test functions. */
157 #define mainTIMER_TEST_PERIOD           ( 50 )
158
159 /* Priorities used by the various different tasks. */
160 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )
161 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )
162 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
163 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
164 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )
165 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )
166 #define mainuIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )
167 #define mainOLED_TASK_PRIORITY          ( tskIDLE_PRIORITY + 1 )
168 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )
169 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )
170
171 /* The WEB server uses string handling functions, which in turn use a bit more
172 stack than most of the other tasks. */
173 #define mainuIP_STACK_SIZE                      ( configMINIMAL_STACK_SIZE * 3 )
174
175 /* The period at which the check timer will expire, in ms, provided no errors
176 have been reported by any of the standard demo tasks. */
177 #define mainCHECK_TIMER_PERIOD_MS       ( 3000UL / portTICK_PERIOD_MS )
178
179 /* The period at which the OLED timer will expire.  Each time it expires, it's
180 callback function updates the OLED text. */
181 #define mainOLED_PERIOD_MS                      ( 75UL / portTICK_PERIOD_MS )
182
183 /* The period at which the check timer will expire, in ms, if an error has been
184 reported in one of the standard demo tasks. */
185 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 500UL / portTICK_PERIOD_MS )
186
187 /* The LED will remain on until the button has not been pushed for a full
188 5000ms. */
189 #define mainLED_TIMER_PERIOD_MS         ( 5000UL / portTICK_PERIOD_MS )
190
191 /* A zero block time. */
192 #define mainDONT_BLOCK                          ( 0UL )
193 /*-----------------------------------------------------------*/
194
195 /*
196  * Setup the NVIC, LED outputs, and button inputs.
197  */
198 static void prvSetupHardware( void );
199
200 /*
201  * The tasks as described in the comments at the top of this file.
202  */
203 static void prvQueueReceiveTask( void *pvParameters );
204 static void prvQueueSendTask( void *pvParameters );
205
206 /*
207  * The LED timer callback function.  This does nothing but switch the red LED
208  * off.
209  */
210 static void prvLEDTimerCallback( TimerHandle_t xTimer );
211
212 /*
213  * The check timer callback function, as described at the top of this file.
214  */
215 static void prvCheckTimerCallback( TimerHandle_t xTimer );
216
217 /*
218  * This is not a 'standard' partest function, so the prototype is not in
219  * partest.h, and is instead included here.
220  */
221 void vParTestSetLEDFromISR( unsigned portBASE_TYPE uxLED, signed portBASE_TYPE xValue );
222
223 /*
224  * Contains the implementation of the WEB server.
225  */
226 extern void vuIP_Task( void *pvParameters );
227
228 /*
229  * A very simply task that does nothing but scroll the OLED display.  Ideally
230  * this would be done within a timer, but it accesses the I2C port which is
231  * time consuming.
232  */
233 static void prvOLEDTask( void * pvParameters);
234
235 /*-----------------------------------------------------------*/
236
237 /* The queue used by both application specific demo tasks defined in this file. */
238 static QueueHandle_t xQueue = NULL;
239
240 /* The LED software timer.  This uses prvLEDTimerCallback() as it's callback
241 function. */
242 static TimerHandle_t xLEDTimer = NULL;
243
244 /* The check timer.  This uses prvCheckTimerCallback() as it's callback
245 function. */
246 static TimerHandle_t xCheckTimer = NULL;
247
248 /* The status message that is displayed at the bottom of the "task stats" web
249 page, which is served by the uIP task.  This will report any errors picked up
250 by the check timer callback. */
251 static const char *pcStatusMessage = NULL;
252
253 /*-----------------------------------------------------------*/
254
255 int main(void)
256 {
257         /* Configure the NVIC, LED outputs and button inputs. */
258         prvSetupHardware();
259
260         /* Create the queue. */
261         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
262
263         if( xQueue != NULL )
264         {
265                 /* Start the three application specific demo tasks, as described in the
266                 comments at the top of this     file. */
267                 xTaskCreate( prvQueueReceiveTask, "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );
268                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
269                 xTaskCreate( prvOLEDTask, "OLED", configMINIMAL_STACK_SIZE, NULL, mainOLED_TASK_PRIORITY, NULL );
270
271                 /* Create the software timer that is responsible for turning off the LED
272                 if the button is not pushed within 5000ms, as described at the top of
273                 this file. */
274                 xLEDTimer = xTimerCreate(       "LEDTimer",                                     /* A text name, purely to help debugging. */
275                                                                         ( mainLED_TIMER_PERIOD_MS ),    /* The timer period, in this case 5000ms (5s). */
276                                                                         pdFALSE,                                                /* This is a one shot timer, so xAutoReload is set to pdFALSE. */
277                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
278                                                                         prvLEDTimerCallback                             /* The callback function that switches the LED off. */
279                                                                 );
280
281                 /* Create the software timer that performs the 'check' functionality,
282                 as described at the top of this file. */
283                 xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */
284                                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */
285                                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
286                                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
287                                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */
288                                                                   );
289
290                 /* Create a lot of 'standard demo' tasks. */
291                 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
292                 vCreateBlockTimeTasks();
293                 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
294                 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
295                 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
296                 vStartQueuePeekTasks();
297                 vStartRecursiveMutexTasks();
298                 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
299
300                 /* Create the web server task. */
301                 xTaskCreate( vuIP_Task, "uIP", mainuIP_STACK_SIZE, NULL, mainuIP_TASK_PRIORITY, NULL );
302
303                 /* The suicide tasks must be created last, as they need to know how many
304                 tasks were running prior to their creation in order to ascertain whether
305                 or not the correct/expected number of tasks are running at any given
306                 time. */
307                 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
308
309                 /* Start the tasks and timer running. */
310                 vTaskStartScheduler();
311         }
312
313         /* If all is well, the scheduler will now be running, and the following line
314         will never be reached.  If the following line does execute, then there was
315         insufficient FreeRTOS heap memory available for the idle and/or timer tasks
316         to be created.  See the memory management section on the FreeRTOS web site
317         for more details. */
318         for( ;; );
319 }
320 /*-----------------------------------------------------------*/
321
322 static void prvCheckTimerCallback( TimerHandle_t xTimer )
323 {
324         /* Check the standard demo tasks are running without error.   Latch the
325         latest reported error in the pcStatusMessage character pointer. */
326         if( xAreGenericQueueTasksStillRunning() != pdTRUE )
327         {
328                 pcStatusMessage = "Error: GenQueue";
329         }
330
331         if( xAreQueuePeekTasksStillRunning() != pdTRUE )
332         {
333                 pcStatusMessage = "Error: QueuePeek\r\n";
334         }
335
336         if( xAreBlockingQueuesStillRunning() != pdTRUE )
337         {
338                 pcStatusMessage = "Error: BlockQueue\r\n";
339         }
340
341         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
342         {
343                 pcStatusMessage = "Error: BlockTime\r\n";
344         }
345
346         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
347         {
348                 pcStatusMessage = "Error: SemTest\r\n";
349         }
350
351         if( xIsCreateTaskStillRunning() != pdTRUE )
352         {
353                 pcStatusMessage = "Error: Death\r\n";
354         }
355
356         if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
357         {
358                 pcStatusMessage = "Error: RecMutex\r\n";
359         }
360
361         if( xAreTimerDemoTasksStillRunning( ( mainCHECK_TIMER_PERIOD_MS ) ) != pdTRUE )
362         {
363                 pcStatusMessage = "Error: TimerDemo";
364         }
365
366         /* Toggle the check LED to give an indication of the system status.  If
367         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
368         everything is ok.  A faster toggle indicates an error. */
369         vParTestToggleLED( mainCHECK_LED );
370
371         /* Have any errors been latch in pcStatusMessage?  If so, shorten the
372         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
373         This will result in an increase in the rate at which mainCHECK_LED
374         toggles. */
375         if( pcStatusMessage != NULL )
376         {
377                 /* This call to xTimerChangePeriod() uses a zero block time.  Functions
378                 called from inside of a timer callback function must *never* attempt
379                 to block. */
380                 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
381         }
382 }
383 /*-----------------------------------------------------------*/
384
385 static void prvLEDTimerCallback( TimerHandle_t xTimer )
386 {
387         /* The timer has expired - so no button pushes have occurred in the last
388         five seconds - turn the LED off. */
389         vParTestSetLED( mainTIMER_CONTROLLED_LED, pdFALSE );
390 }
391 /*-----------------------------------------------------------*/
392
393 /* The ISR executed when the user button is pushed. */
394 void GPIO8_IRQHandler( void )
395 {
396 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
397
398         /* The button was pushed, so ensure the LED is on before resetting the
399         LED timer.  The LED timer will turn the LED off if the button is not
400         pushed within 5000ms. */
401         vParTestSetLEDFromISR( mainTIMER_CONTROLLED_LED, pdTRUE );
402
403         /* This interrupt safe FreeRTOS function can be called from this interrupt
404         because the interrupt priority is below the
405         configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */
406         xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );
407
408         /* Clear the interrupt before leaving. */
409     MSS_GPIO_clear_irq( MSS_GPIO_8 );
410
411         /* If calling xTimerResetFromISR() caused a task (in this case the timer
412         service/daemon task) to unblock, and the unblocked task has a priority
413         higher than or equal to the task that was interrupted, then
414         xHigherPriorityTaskWoken will now be set to pdTRUE, and calling
415         portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */
416         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
417 }
418 /*-----------------------------------------------------------*/
419
420 static void prvQueueSendTask( void *pvParameters )
421 {
422 TickType_t xNextWakeTime;
423 const unsigned long ulValueToSend = 100UL;
424
425         /* The timer command queue will have been filled when the timer test tasks
426         were created in main() (this is part of the test they perform).  Therefore,
427         while the check timer can be created in main(), it cannot be started from
428         main().  Once the scheduler has started, the timer service task will drain
429         the command queue, and now the check timer can be started successfully. */
430         xTimerStart( xCheckTimer, portMAX_DELAY );
431
432         /* Initialise xNextWakeTime - this only needs to be done once. */
433         xNextWakeTime = xTaskGetTickCount();
434
435         for( ;; )
436         {
437                 /* Place this task in the blocked state until it is time to run again.
438                 The block time is specified in ticks, the constant used converts ticks
439                 to ms.  While in the Blocked state this task will not consume any CPU
440                 time. */
441                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
442
443                 /* Send to the queue - causing the queue receive task to unblock and
444                 toggle an LED.  0 is used as the block time so the sending operation
445                 will not block - it shouldn't need to block as the queue should always
446                 be empty at this point in the code. */
447                 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
448         }
449 }
450 /*-----------------------------------------------------------*/
451
452 static void prvQueueReceiveTask( void *pvParameters )
453 {
454 unsigned long ulReceivedValue;
455
456         for( ;; )
457         {
458                 /* Wait until something arrives in the queue - this task will block
459                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
460                 FreeRTOSConfig.h. */
461                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
462
463                 /*  To get here something must have been received from the queue, but
464                 is it the expected value?  If it is, toggle the LED. */
465                 if( ulReceivedValue == 100UL )
466                 {
467                         vParTestToggleLED( mainTASK_CONTROLLED_LED );
468                 }
469         }
470 }
471 /*-----------------------------------------------------------*/
472
473 static void prvOLEDTask( void * pvParameters)
474 {
475 static struct oled_data xOLEDData;
476 static unsigned char ucOffset1 = 0, ucOffset2 = 5;
477 static TickType_t xLastScrollTime = 0UL;
478
479         /* Initialise the display. */
480         OLED_init();
481
482         /* Initialise the parts of the oled_data structure that do not change. */
483         xOLEDData.line1          = FIRST_LINE;
484         xOLEDData.string1        = " www.FreeRTOS.org";
485         xOLEDData.line2          = SECOND_LINE;
486         xOLEDData.string2        = " www.FreeRTOS.org";
487         xOLEDData.contrast_val                 = OLED_CONTRAST_VAL;
488         xOLEDData.on_off                       = OLED_HORIZ_SCROLL_OFF;
489         xOLEDData.column_scrool_per_step       = OLED_HORIZ_SCROLL_STEP;
490         xOLEDData.start_page                   = OLED_START_PAGE;
491         xOLEDData.time_intrval_btw_scroll_step = OLED_HORIZ_SCROLL_TINVL;
492         xOLEDData.end_page                     = OLED_END_PAGE;
493
494
495         /* Initialise the last scroll time.  This only needs to be done once,
496         because from this point on it will get automatically updated in the
497         xTaskDelayUntil() API function. */
498         xLastScrollTime = xTaskGetTickCount();
499
500         for( ;; )
501         {
502                 /* Wait until it is time to update the OLED again. */
503                 vTaskDelayUntil( &xLastScrollTime, mainOLED_PERIOD_MS );
504
505                 xOLEDData.char_offset1   = ucOffset1++;
506                 xOLEDData.char_offset2   = ucOffset2++;
507
508                 OLED_write_data( &xOLEDData, BOTH_LINES );
509         }
510 }
511 /*-----------------------------------------------------------*/
512
513 static void prvSetupHardware( void )
514 {
515         SystemCoreClockUpdate();
516
517         /* Disable the Watch Dog Timer */
518         MSS_WD_disable( );
519
520         /* Configure the GPIO for the LEDs. */
521         vParTestInitialise();
522
523         /* ACE Initialization */
524         ACE_init();
525
526         /* Setup the GPIO and the NVIC for the switch used in this simple demo. */
527         NVIC_SetPriority( GPIO8_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
528     NVIC_EnableIRQ( GPIO8_IRQn );
529     MSS_GPIO_config( MSS_GPIO_8, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );
530     MSS_GPIO_enable_irq( MSS_GPIO_8 );
531 }
532 /*-----------------------------------------------------------*/
533
534 void vApplicationMallocFailedHook( void )
535 {
536         /* Called if a call to pvPortMalloc() fails because there is insufficient
537         free memory available in the FreeRTOS heap.  pvPortMalloc() is called
538         internally by FreeRTOS API functions that create tasks, queues, software
539         timers, and semaphores.  The size of the FreeRTOS heap is set by the
540         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
541         for( ;; );
542 }
543 /*-----------------------------------------------------------*/
544
545 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
546 {
547         ( void ) pcTaskName;
548         ( void ) pxTask;
549
550         /* Run time stack overflow checking is performed if
551         configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
552         function is called if a stack overflow is detected. */
553         taskDISABLE_INTERRUPTS();
554         for( ;; );
555 }
556 /*-----------------------------------------------------------*/
557
558 void vApplicationIdleHook( void )
559 {
560 volatile size_t xFreeStackSpace;
561
562         /* This function is called on each cycle of the idle task.  In this case it
563         does nothing useful, other than report the amount of FreeRTOS heap that
564         remains unallocated. */
565         xFreeStackSpace = xPortGetFreeHeapSize();
566
567         if( xFreeStackSpace > 100 )
568         {
569                 /* By now, the kernel has allocated everything it is going to, so
570                 if there is a lot of heap remaining unallocated then
571                 the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be
572                 reduced accordingly. */
573         }
574 }
575 /*-----------------------------------------------------------*/
576
577 char *pcGetTaskStatusMessage( void )
578 {
579         /* Not bothered about a critical section here although technically because
580         of the task priorities the pointer could change it will be atomic if not
581         near atomic and its not critical. */
582         if( pcStatusMessage == NULL )
583         {
584                 return "All tasks running without error";
585         }
586         else
587         {
588                 return ( char * ) pcStatusMessage;
589         }
590 }
591 /*-----------------------------------------------------------*/
592
593 void vMainConfigureTimerForRunTimeStats( void )
594 {
595 const unsigned long ulMax32BitValue = 0xffffffffUL;
596
597         MSS_TIM64_init( MSS_TIMER_PERIODIC_MODE );
598         MSS_TIM64_load_immediate( ulMax32BitValue, ulMax32BitValue );
599         MSS_TIM64_start();
600 }
601 /*-----------------------------------------------------------*/
602
603 unsigned long ulGetRunTimeCounterValue( void )
604 {
605 unsigned long long ullCurrentValue;
606 const unsigned long long ulMax64BitValue = 0xffffffffffffffffULL;
607 unsigned long *pulHighWord, *pulLowWord;
608
609         pulHighWord = ( unsigned long * ) &ullCurrentValue;
610         pulLowWord = pulHighWord++;
611
612         MSS_TIM64_get_current_value( ( uint32_t * ) pulHighWord, ( uint32_t * ) pulLowWord );
613
614         /* Convert the down count into an upcount. */
615         ullCurrentValue = ulMax64BitValue - ullCurrentValue;
616
617         /* Scale to a 32bit number of suitable frequency. */
618         ullCurrentValue >>= 13;
619
620         /* Just return 32 bits. */
621         return ( unsigned long ) ullCurrentValue;
622 }
623