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