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