]> begriffs open source - freertos/blob - FreeRTOS/Demo/CORTEX_AT91SAM3U256_IAR/main.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / CORTEX_AT91SAM3U256_IAR / main.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  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
98  * documentation provides more details of the standard demo application tasks\r
99  * (which just exist to test the kernel port and provide an example of how to use\r
100  * each FreeRTOS API function).\r
101  *\r
102  * In addition to the standard demo tasks, the following tasks and tests are\r
103  * defined and/or created within this file:\r
104  *\r
105  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
106  * is permitted to access the display directly.  Other tasks wishing to write a\r
107  * message to the LCD send the message on a queue to the LCD task instead of\r
108  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
109  * for messages - waking and displaying the messages as they arrive.  The use\r
110  * of a gatekeeper in this manner permits both tasks and interrupts to write to\r
111  * the LCD without worrying about mutual exclusion.  This is demonstrated by the\r
112  * check hook (see below) which sends messages to the display even though it\r
113  * executes from an interrupt context.\r
114  *\r
115  * "Check" hook -  This only executes fully every five seconds from the tick\r
116  * hook.  Its main function is to check that all the standard demo tasks are\r
117  * still operational.  Should any unexpected behaviour be discovered within a\r
118  * demo task then the tick hook will write an error to the LCD (via the LCD task).\r
119  * If all the demo tasks are executing with their expected behaviour then the\r
120  * check task writes PASS to the LCD (again via the LCD task), as described above.\r
121  *\r
122  */\r
123 \r
124 /* Scheduler includes. */\r
125 #include "FreeRTOS.h"\r
126 #include "task.h"\r
127 #include "queue.h"\r
128 #include "semphr.h"\r
129 \r
130 /* Demo app includes. */\r
131 #include "BlockQ.h"\r
132 #include "integer.h"\r
133 #include "blocktim.h"\r
134 #include "flash.h"\r
135 #include "partest.h"\r
136 #include "semtest.h"\r
137 #include "PollQ.h"\r
138 #include "lcd_message.h"\r
139 #include "GenQTest.h"\r
140 #include "QPeek.h"\r
141 #include "recmutex.h"\r
142 #include "flash.h"\r
143 #include "comtest2.h"\r
144 \r
145 /* Atmel library includes. */\r
146 #include <board.h>\r
147 #include <lcd/color.h>\r
148 #include <lcd/lcdd.h>\r
149 #include <lcd/draw.h>\r
150 \r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* The time between cycles of the 'check' functionality (defined within the\r
155 tick hook). */\r
156 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
157 \r
158 /* The LCD task uses the sprintf function so requires a little more stack too. */\r
159 #define mainLCD_TASK_STACK_SIZE                         ( configMINIMAL_STACK_SIZE * 2 )\r
160 \r
161 /* Task priorities. */\r
162 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
163 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
164 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
165 #define mainLED_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
166 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
167 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
168 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
169 \r
170 /* The maximum number of message that can be waiting for display at any one\r
171 time. */\r
172 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
173 \r
174 /* Constants used by the comtest tasks.  There isn't a spare LED so an invalid\r
175 LED is specified. */\r
176 #define mainBAUD_RATE                                           ( 115200 )\r
177 #define mainCOM_TEST_LED                                        ( 10 )\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /*\r
182  * Configure the hardware for the demo.\r
183  */\r
184 static void prvSetupHardware( void );\r
185 \r
186 /*\r
187  * The LCD gatekeeper task.  Tasks wishing to write to the LCD do not access\r
188  * the LCD directly, but instead send the message to the LCD gatekeeper task.\r
189  */\r
190 static void prvLCDTask( void *pvParameters );\r
191 \r
192 /*\r
193  * Hook functions that can get called by the kernel.  The 'check' functionality\r
194  * is implemented within the tick hook.\r
195  */\r
196 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
197 \r
198 /*\r
199  * The tick hook function as described in the comments at the top of this file.\r
200  * The tick hook is used to monitor all the standard demo tasks to look for\r
201  * errors.  The tick hook is also used to demonstrate how the LCD gatekeeper\r
202  * task can be used to allow interrupts to write to the LCD.\r
203  */\r
204 void vApplicationTickHook( void );\r
205 \r
206 \r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /* The queue used to send messages to the LCD task. */\r
210 static QueueHandle_t xLCDQueue;\r
211 \r
212 /*-----------------------------------------------------------*/\r
213 \r
214 int main( void )\r
215 {\r
216         /* Prepare the hardware. */\r
217         prvSetupHardware();\r
218 \r
219         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
220         are received via this queue. */\r
221         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
222 \r
223         /* Start the standard demo tasks.  These do nothing other than test the\r
224         port and provide some APU usage examples. */\r
225     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
226     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
227         vStartRecursiveMutexTasks();\r
228         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
229         vCreateBlockTimeTasks();\r
230         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
231         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
232         vStartQueuePeekTasks();\r
233         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
234         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainBAUD_RATE, mainCOM_TEST_LED );\r
235 \r
236         /* Start the tasks defined within this file/specific to this demo. */\r
237         xTaskCreate( prvLCDTask, "LCD", mainLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
238 \r
239         /* Start the scheduler. */\r
240         vTaskStartScheduler();\r
241 \r
242     /* Will only get here if there was insufficient memory to create the idle\r
243     task. */\r
244         return 0;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void prvSetupHardware( void )\r
249 {\r
250         /* Initialise the port used for the LED outputs. */\r
251         vParTestInitialise();\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void vApplicationTickHook( void )\r
256 {\r
257 static xLCDMessage xMessage = { "PASS" };\r
258 static unsigned long ulTicksSinceLastDisplay = 0;\r
259 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
260 \r
261         /* Called from every tick interrupt.  Have enough ticks passed to make it\r
262         time to perform our health status check again? */\r
263         ulTicksSinceLastDisplay++;\r
264         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
265         {\r
266                 ulTicksSinceLastDisplay = 0;\r
267 \r
268                 /* Has an error been found in any task? */\r
269                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
270                 {\r
271                         xMessage.pcMessage = "ERROR IN GEN Q";\r
272                 }\r
273             if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
274             {\r
275                 xMessage.pcMessage = "ERROR IN MATH";\r
276             }\r
277                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
278                 {\r
279                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
280                 }\r
281                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
282                 {\r
283                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
284                 }\r
285                 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
286                 {\r
287                         xMessage.pcMessage = "ERROR IN SEMAPHORE";\r
288                 }\r
289                 else if( xArePollingQueuesStillRunning() != pdTRUE )\r
290                 {\r
291                         xMessage.pcMessage = "ERROR IN POLL Q";\r
292                 }\r
293                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
294                 {\r
295                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
296                 }\r
297                 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
298                 {\r
299                         xMessage.pcMessage = "ERROR IN REC MUTEX";\r
300                 }\r
301                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
302                 {\r
303                         xMessage.pcMessage = "ERROR IN COMTEST";\r
304                 }\r
305 \r
306                 /* Send the message to the LCD gatekeeper for display. */\r
307                 xHigherPriorityTaskWoken = pdFALSE;\r
308                 xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
309         }\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
314 {\r
315         ( void ) pxTask;\r
316         ( void ) pcTaskName;\r
317 \r
318         /* If the parameters have been corrupted then inspect pxCurrentTCB to\r
319         identify which task has overflowed its stack. */\r
320         for( ;; );\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 static void prvLCDTask( void *pvParameters )\r
325 {\r
326 xLCDMessage xMessage;\r
327 unsigned long ulY = 0;\r
328 const unsigned long ulX = 5;\r
329 const unsigned long ulMaxY = 250, ulYIncrement = 22, ulWidth = 250, ulHeight = 20;;\r
330 \r
331     /* Initialize LCD. */\r
332     LCDD_Initialize();\r
333     LCDD_Start();\r
334         LCDD_Fill( ( void * ) BOARD_LCD_BASE, COLOR_WHITE );\r
335         LCDD_DrawString( ( void * ) BOARD_LCD_BASE, 1, ulY + 3, "  www.FreeRTOS.org", COLOR_BLACK );\r
336 \r
337         for( ;; )\r
338         {\r
339                 /* Wait for a message from the check function (which is executed in\r
340                 the tick hook). */\r
341                 xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY );\r
342 \r
343                 /* Clear the space where the old message was. */\r
344         LCDD_DrawRectangle( ( void * ) BOARD_LCD_BASE, 0, ulY, ulWidth, ulHeight, COLOR_WHITE );\r
345 \r
346                 /* Increment to the next drawing position. */\r
347                 ulY += ulYIncrement;\r
348 \r
349                 /* Have the Y position moved past the end of the LCD? */\r
350                 if( ulY >= ulMaxY )\r
351                 {\r
352                         ulY = 0;\r
353                 }\r
354 \r
355                 /* Draw a new rectangle, in which the message will be written. */\r
356         LCDD_DrawRectangle( ( void * ) BOARD_LCD_BASE, 0, ulY, ulWidth, ulHeight, COLOR_GREEN );\r
357 \r
358                 /* Write the message. */\r
359         LCDD_DrawString( ( void * ) BOARD_LCD_BASE, ulX, ulY + 3, xMessage.pcMessage, COLOR_BLACK );\r
360         }\r
361 }\r