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