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