]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_AT91SAM3U256_IAR/main.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_AT91SAM3U256_IAR / main.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
77  * documentation provides more details of the standard demo application tasks\r
78  * (which just exist to test the kernel port and provide an example of how to use\r
79  * each FreeRTOS API function).\r
80  *\r
81  * In addition to the standard demo tasks, the following tasks and tests are\r
82  * defined and/or created within this file:\r
83  *\r
84  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
85  * is permitted to access the display directly.  Other tasks wishing to write a\r
86  * message to the LCD send the message on a queue to the LCD task instead of\r
87  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
88  * for messages - waking and displaying the messages as they arrive.  The use\r
89  * of a gatekeeper in this manner permits both tasks and interrupts to write to\r
90  * the LCD without worrying about mutual exclusion.  This is demonstrated by the\r
91  * check hook (see below) which sends messages to the display even though it\r
92  * executes from an interrupt context.\r
93  *\r
94  * "Check" hook -  This only executes fully every five seconds from the tick\r
95  * hook.  Its main function is to check that all the standard demo tasks are\r
96  * still operational.  Should any unexpected behaviour be discovered within a\r
97  * demo task then the tick hook will write an error to the LCD (via the LCD task).\r
98  * If all the demo tasks are executing with their expected behaviour then the\r
99  * check task writes PASS to the LCD (again via the LCD task), as described above.\r
100  *\r
101  */\r
102 \r
103 /* Scheduler includes. */\r
104 #include "FreeRTOS.h"\r
105 #include "task.h"\r
106 #include "queue.h"\r
107 #include "semphr.h"\r
108 \r
109 /* Demo app includes. */\r
110 #include "BlockQ.h"\r
111 #include "integer.h"\r
112 #include "blocktim.h"\r
113 #include "flash.h"\r
114 #include "partest.h"\r
115 #include "semtest.h"\r
116 #include "PollQ.h"\r
117 #include "lcd_message.h"\r
118 #include "GenQTest.h"\r
119 #include "QPeek.h"\r
120 #include "recmutex.h"\r
121 #include "flash.h"\r
122 #include "comtest2.h"\r
123 \r
124 /* Atmel library includes. */\r
125 #include <board.h>\r
126 #include <lcd/color.h>\r
127 #include <lcd/lcdd.h>\r
128 #include <lcd/draw.h>\r
129 \r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /* The time between cycles of the 'check' functionality (defined within the\r
134 tick hook). */\r
135 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
136 \r
137 /* The LCD task uses the sprintf function so requires a little more stack too. */\r
138 #define mainLCD_TASK_STACK_SIZE                         ( configMINIMAL_STACK_SIZE * 2 )\r
139 \r
140 /* Task priorities. */\r
141 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
142 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
143 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
144 #define mainLED_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
145 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
146 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
147 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
148 \r
149 /* The maximum number of message that can be waiting for display at any one\r
150 time. */\r
151 #define mainLCD_QUEUE_SIZE                                      ( 3 )\r
152 \r
153 /* Constants used by the comtest tasks.  There isn't a spare LED so an invalid\r
154 LED is specified. */\r
155 #define mainBAUD_RATE                                           ( 115200 )\r
156 #define mainCOM_TEST_LED                                        ( 10 )\r
157 \r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /*\r
161  * Configure the hardware for the demo.\r
162  */\r
163 static void prvSetupHardware( void );\r
164 \r
165 /*\r
166  * The LCD gatekeeper task.  Tasks wishing to write to the LCD do not access\r
167  * the LCD directly, but instead send the message to the LCD gatekeeper task.\r
168  */\r
169 static void prvLCDTask( void *pvParameters );\r
170 \r
171 /*\r
172  * Hook functions that can get called by the kernel.  The 'check' functionality\r
173  * is implemented within the tick hook.\r
174  */\r
175 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );\r
176 \r
177 /*\r
178  * The tick hook function as described in the comments at the top of this file.\r
179  * The tick hook is used to monitor all the standard demo tasks to look for\r
180  * errors.  The tick hook is also used to demonstrate how the LCD gatekeeper\r
181  * task can be used to allow interrupts to write to the LCD.\r
182  */\r
183 void vApplicationTickHook( void );\r
184 \r
185 \r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /* The queue used to send messages to the LCD task. */\r
189 static xQueueHandle xLCDQueue;\r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 int main( void )\r
194 {\r
195         /* Prepare the hardware. */\r
196         prvSetupHardware();\r
197 \r
198         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
199         are received via this queue. */\r
200         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );\r
201 \r
202         /* Start the standard demo tasks.  These do nothing other than test the\r
203         port and provide some APU usage examples. */\r
204     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
205     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
206         vStartRecursiveMutexTasks();    \r
207         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
208         vCreateBlockTimeTasks();\r
209         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
210         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
211         vStartQueuePeekTasks(); \r
212         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );   \r
213         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainBAUD_RATE, mainCOM_TEST_LED );\r
214 \r
215         /* Start the tasks defined within this file/specific to this demo. */\r
216         xTaskCreate( prvLCDTask, ( signed char * ) "LCD", mainLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
217 \r
218         /* Start the scheduler. */\r
219         vTaskStartScheduler();\r
220 \r
221     /* Will only get here if there was insufficient memory to create the idle\r
222     task. */\r
223         return 0;\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void prvSetupHardware( void )\r
228 {\r
229         /* Initialise the port used for the LED outputs. */\r
230         vParTestInitialise();\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vApplicationTickHook( void )\r
235 {\r
236 static xLCDMessage xMessage = { "PASS" };\r
237 static unsigned long ulTicksSinceLastDisplay = 0;\r
238 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
239 \r
240         /* Called from every tick interrupt.  Have enough ticks passed to make it\r
241         time to perform our health status check again? */\r
242         ulTicksSinceLastDisplay++;\r
243         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
244         {\r
245                 ulTicksSinceLastDisplay = 0;\r
246                 \r
247                 /* Has an error been found in any task? */\r
248                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
249                 {\r
250                         xMessage.pcMessage = "ERROR IN GEN Q";\r
251                 }\r
252             if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
253             {\r
254                 xMessage.pcMessage = "ERROR IN MATH";\r
255             }\r
256                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
257                 {\r
258                         xMessage.pcMessage = "ERROR IN BLOCK Q";\r
259                 }\r
260                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
261                 {\r
262                         xMessage.pcMessage = "ERROR IN BLOCK TIME";\r
263                 }\r
264                 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
265                 {\r
266                         xMessage.pcMessage = "ERROR IN SEMAPHORE";\r
267                 }\r
268                 else if( xArePollingQueuesStillRunning() != pdTRUE )\r
269                 {\r
270                         xMessage.pcMessage = "ERROR IN POLL Q";\r
271                 }\r
272                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
273                 {\r
274                         xMessage.pcMessage = "ERROR IN PEEK Q";\r
275                 }                       \r
276                 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
277                 {\r
278                         xMessage.pcMessage = "ERROR IN REC MUTEX";\r
279                 }               \r
280                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
281                 {\r
282                         xMessage.pcMessage = "ERROR IN COMTEST";\r
283                 }\r
284                 \r
285                 /* Send the message to the LCD gatekeeper for display. */\r
286                 xHigherPriorityTaskWoken = pdFALSE;\r
287                 xQueueSendFromISR( xLCDQueue, &xMessage, &xHigherPriorityTaskWoken );\r
288         }\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
293 {\r
294         ( void ) pxTask;\r
295         ( void ) pcTaskName;\r
296 \r
297         /* If the parameters have been corrupted then inspect pxCurrentTCB to\r
298         identify which task has overflowed its stack. */\r
299         for( ;; );\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 static void prvLCDTask( void *pvParameters )\r
304 {\r
305 xLCDMessage xMessage;\r
306 unsigned long ulY = 0;\r
307 const unsigned long ulX = 5;\r
308 const unsigned long ulMaxY = 250, ulYIncrement = 22, ulWidth = 250, ulHeight = 20;;\r
309 \r
310     /* Initialize LCD. */\r
311     LCDD_Initialize();\r
312     LCDD_Start();       \r
313         LCDD_Fill( ( void * ) BOARD_LCD_BASE, COLOR_WHITE );\r
314         LCDD_DrawString( ( void * ) BOARD_LCD_BASE, 1, ulY + 3, "  www.FreeRTOS.org", COLOR_BLACK );\r
315         \r
316         for( ;; )\r
317         {\r
318                 /* Wait for a message from the check function (which is executed in\r
319                 the tick hook). */\r
320                 xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY );\r
321 \r
322                 /* Clear the space where the old message was. */\r
323         LCDD_DrawRectangle( ( void * ) BOARD_LCD_BASE, 0, ulY, ulWidth, ulHeight, COLOR_WHITE );\r
324                 \r
325                 /* Increment to the next drawing position. */\r
326                 ulY += ulYIncrement;\r
327                 \r
328                 /* Have the Y position moved past the end of the LCD? */\r
329                 if( ulY >= ulMaxY )\r
330                 {\r
331                         ulY = 0;\r
332                 }\r
333 \r
334                 /* Draw a new rectangle, in which the message will be written. */\r
335         LCDD_DrawRectangle( ( void * ) BOARD_LCD_BASE, 0, ulY, ulWidth, ulHeight, COLOR_GREEN );\r
336                 \r
337                 /* Write the message. */\r
338         LCDD_DrawString( ( void * ) BOARD_LCD_BASE, ulX, ulY + 3, xMessage.pcMessage, COLOR_BLACK );\r
339         }\r
340 }\r