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