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