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