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