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