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