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