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