]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S811_IAR/main.c
Ensure LPC1768 demos are correct prior to V5.4.0 release.
[freertos] / Demo / CORTEX_LM3S811_IAR / main.c
1 /*\r
2         FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\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         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 \r
49 /*\r
50  * This project contains an application demonstrating the use of the\r
51  * FreeRTOS.org mini real time scheduler on the Luminary Micro LM3S811 Eval\r
52  * board.  See http://www.FreeRTOS.org for more information.\r
53  *\r
54  * main() simply sets up the hardware, creates all the demo application tasks,\r
55  * then starts the scheduler.  http://www.freertos.org/a00102.html provides\r
56  * more information on the standard demo tasks.\r
57  *\r
58  * In addition to a subset of the standard demo application tasks, main.c also\r
59  * defines the following tasks:\r
60  *\r
61  * + A 'Print' task.  The print task is the only task permitted to access the\r
62  * LCD - thus ensuring mutual exclusion and consistent access to the resource.\r
63  * Other tasks do not access the LCD directly, but instead send the text they\r
64  * wish to display to the print task.  The print task spends most of its time\r
65  * blocked - only waking when a message is queued for display.\r
66  *\r
67  * + A 'Button handler' task.  The eval board contains a user push button that\r
68  * is configured to generate interrupts.  The interrupt handler uses a\r
69  * semaphore to wake the button handler task - demonstrating how the priority\r
70  * mechanism can be used to defer interrupt processing to the task level.  The\r
71  * button handler task sends a message both to the LCD (via the print task) and\r
72  * the UART where it can be viewed using a dumb terminal (via the UART to USB\r
73  * converter on the eval board).  NOTES:  The dumb terminal must be closed in\r
74  * order to reflash the microcontroller.  A very basic interrupt driven UART\r
75  * driver is used that does not use the FIFO.  19200 baud is used.\r
76  *\r
77  * + A 'check' task.  The check task only executes every five seconds but has a\r
78  * high priority so is guaranteed to get processor time.  Its function is to\r
79  * check that all the other tasks are still operational and that no errors have\r
80  * been detected at any time.  If no errors have every been detected 'PASS' is\r
81  * written to the display (via the print task) - if an error has ever been\r
82  * detected the message is changed to 'FAIL'.  The position of the message is\r
83  * changed for each write.\r
84  */\r
85 \r
86 \r
87 \r
88 /* Environment includes. */\r
89 #include "DriverLib.h"\r
90 \r
91 /* Scheduler includes. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "queue.h"\r
95 #include "semphr.h"\r
96 \r
97 /* Demo app includes. */\r
98 #include "integer.h"\r
99 #include "PollQ.h"\r
100 #include "semtest.h"\r
101 #include "BlockQ.h"\r
102 \r
103 /* Delay between cycles of the 'check' task. */\r
104 #define mainCHECK_DELAY                                         ( ( portTickType ) 5000 / portTICK_RATE_MS )\r
105 \r
106 /* UART configuration - note this does not use the FIFO so is not very\r
107 efficient. */\r
108 #define mainBAUD_RATE                           ( 19200 )\r
109 #define mainFIFO_SET                            ( 0x10 )\r
110 \r
111 /* Demo task priorities. */\r
112 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
113 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
114 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
115 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
116 \r
117 /* Demo board specifics. */\r
118 #define mainPUSH_BUTTON             GPIO_PIN_4\r
119 \r
120 /* Misc. */\r
121 #define mainQUEUE_SIZE                          ( 3 )\r
122 #define mainDEBOUNCE_DELAY                      ( ( portTickType ) 150 / portTICK_RATE_MS )\r
123 #define mainNO_DELAY                            ( ( portTickType ) 0 )\r
124 /*\r
125  * Configure the processor and peripherals for this demo.\r
126  */\r
127 static void prvSetupHardware( void );\r
128 \r
129 /*\r
130  * The 'check' task, as described at the top of this file.\r
131  */\r
132 static void vCheckTask( void *pvParameters );\r
133 \r
134 /*\r
135  * The task that is woken by the ISR that processes GPIO interrupts originating\r
136  * from the push button.\r
137  */\r
138 static void vButtonHandlerTask( void *pvParameters );\r
139 \r
140 /*\r
141  * The task that controls access to the LCD.\r
142  */\r
143 static void vPrintTask( void *pvParameter );\r
144 \r
145 /* String that is transmitted on the UART. */\r
146 static portCHAR *cMessage = "Task woken by button interrupt! --- ";\r
147 static volatile portCHAR *pcNextChar;\r
148 \r
149 /* The semaphore used to wake the button handler task from within the GPIO\r
150 interrupt handler. */\r
151 xSemaphoreHandle xButtonSemaphore;\r
152 \r
153 /* The queue used to send strings to the print task for display on the LCD. */\r
154 xQueueHandle xPrintQueue;\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 int main( void )\r
159 {\r
160         /* Configure the clocks, UART and GPIO. */\r
161         prvSetupHardware();\r
162 \r
163         /* Create the semaphore used to wake the button handler task from the GPIO\r
164         ISR. */\r
165         vSemaphoreCreateBinary( xButtonSemaphore );\r
166         xSemaphoreTake( xButtonSemaphore, 0 );\r
167 \r
168         /* Create the queue used to pass message to vPrintTask. */\r
169         xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( portCHAR * ) );\r
170 \r
171         /* Start the standard demo tasks. */\r
172         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
173         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
174         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
175         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
176 \r
177         /* Start the tasks defined within the file. */\r
178         xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
179         xTaskCreate( vButtonHandlerTask, "Status", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY + 1, NULL );\r
180         xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );\r
181 \r
182         /* Start the scheduler. */\r
183         vTaskStartScheduler();\r
184 \r
185         /* Will only get here if there was insufficient heap to start the\r
186         scheduler. */\r
187 \r
188         return 0;\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void vCheckTask( void *pvParameters )\r
193 {\r
194 portBASE_TYPE xErrorOccurred = pdFALSE;\r
195 portTickType xLastExecutionTime;\r
196 const portCHAR *pcPassMessage = "PASS";\r
197 const portCHAR *pcFailMessage = "FAIL";\r
198 \r
199         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
200         works correctly. */\r
201         xLastExecutionTime = xTaskGetTickCount();\r
202 \r
203         for( ;; )\r
204         {\r
205                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
206                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );\r
207 \r
208                 /* Has an error been found in any task? */\r
209 \r
210                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
211                 {\r
212                         xErrorOccurred = pdTRUE;\r
213                 }\r
214         \r
215                 if( xArePollingQueuesStillRunning() != pdTRUE )\r
216                 {\r
217                         xErrorOccurred = pdTRUE;\r
218                 }\r
219         \r
220                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
221                 {\r
222                         xErrorOccurred = pdTRUE;\r
223                 }\r
224 \r
225                 if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
226                 {\r
227                         xErrorOccurred = pdTRUE;\r
228                 }\r
229 \r
230                 /* Send either a pass or fail message.  If an error is found it is\r
231                 never cleared again.  We do not write directly to the LCD, but instead\r
232                 queue a message for display by the print task. */\r
233                 if( xErrorOccurred == pdTRUE )\r
234                 {\r
235                         xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );\r
236                 }\r
237                 else\r
238                 {\r
239                         xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );\r
240                 }\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvSetupHardware( void )\r
246 {\r
247         /* Setup the PLL. */\r
248         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );\r
249 \r
250         /* Setup the push button. */\r
251         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);\r
252     GPIODirModeSet(GPIO_PORTC_BASE, mainPUSH_BUTTON, GPIO_DIR_MODE_IN);\r
253         GPIOIntTypeSet( GPIO_PORTC_BASE, mainPUSH_BUTTON,GPIO_FALLING_EDGE );\r
254         IntPrioritySet( INT_GPIOC, configKERNEL_INTERRUPT_PRIORITY );\r
255         GPIOPinIntEnable( GPIO_PORTC_BASE, mainPUSH_BUTTON );\r
256         IntEnable( INT_GPIOC );\r
257 \r
258 \r
259 \r
260         /* Enable the UART.  */\r
261         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);\r
262         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);\r
263 \r
264         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the\r
265         UART signals. */\r
266         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );\r
267 \r
268         /* Configure the UART for 8-N-1 operation. */\r
269         UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );\r
270 \r
271         /* We don't want to use the fifo.  This is for test purposes to generate\r
272         as many interrupts as possible. */\r
273         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;\r
274 \r
275         /* Enable Tx interrupts. */\r
276         HWREG( UART0_BASE + UART_O_IM ) |= UART_INT_TX;\r
277         IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );\r
278         IntEnable( INT_UART0 );\r
279 \r
280 \r
281         /* Initialise the LCD> */\r
282     OSRAMInit( false );\r
283     OSRAMStringDraw("www.FreeRTOS.org", 0, 0);\r
284         OSRAMStringDraw("LM3S811 demo", 16, 1);\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 static void vButtonHandlerTask( void *pvParameters )\r
289 {\r
290 const portCHAR *pcInterruptMessage = "Int";\r
291 \r
292         for( ;; )\r
293         {\r
294                 /* Wait for a GPIO interrupt to wake this task. */\r
295                 while( xSemaphoreTake( xButtonSemaphore, portMAX_DELAY ) != pdPASS );\r
296 \r
297                 /* Start the Tx of the message on the UART. */\r
298                 UARTIntDisable( UART0_BASE, UART_INT_TX );\r
299                 {\r
300                         pcNextChar = cMessage;\r
301 \r
302                         /* Send the first character. */\r
303                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
304                         {\r
305                                 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;\r
306                         }\r
307 \r
308                         pcNextChar++;\r
309                 }\r
310                 UARTIntEnable(UART0_BASE, UART_INT_TX);\r
311 \r
312                 /* Queue a message for the print task to display on the LCD. */\r
313                 xQueueSend( xPrintQueue, &pcInterruptMessage, portMAX_DELAY );\r
314 \r
315                 /* Make sure we don't process bounces. */\r
316                 vTaskDelay( mainDEBOUNCE_DELAY );\r
317                 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );\r
318         }\r
319 }\r
320 \r
321 /*-----------------------------------------------------------*/\r
322 \r
323 void vUART_ISR(void)\r
324 {\r
325 unsigned portLONG ulStatus;\r
326 \r
327         /* What caused the interrupt. */\r
328         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );\r
329 \r
330         /* Clear the interrupt. */\r
331         UARTIntClear( UART0_BASE, ulStatus );\r
332 \r
333         /* Was a Tx interrupt pending? */\r
334         if( ulStatus & UART_INT_TX )\r
335         {\r
336                 /* Send the next character in the string.  We are not using the FIFO. */\r
337                 if( *pcNextChar != NULL )\r
338                 {\r
339                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
340                         {\r
341                                 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;\r
342                         }\r
343                         pcNextChar++;\r
344                 }\r
345         }\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 void vGPIO_ISR( void )\r
350 {\r
351 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
352 \r
353         /* Clear the interrupt. */\r
354         GPIOPinIntClear(GPIO_PORTC_BASE, mainPUSH_BUTTON);\r
355 \r
356         /* Wake the button handler task. */\r
357         xSemaphoreGiveFromISR( xButtonSemaphore, &xHigherPriorityTaskWoken );\r
358         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 static void vPrintTask( void *pvParameters )\r
363 {\r
364 portCHAR *pcMessage;\r
365 unsigned portBASE_TYPE uxLine = 0, uxRow = 0;\r
366 \r
367         for( ;; )\r
368         {\r
369                 /* Wait for a message to arrive. */\r
370                 xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY );\r
371 \r
372                 /* Write the message to the LCD. */\r
373                 uxRow++;\r
374                 uxLine++;\r
375                 OSRAMClear();\r
376                 OSRAMStringDraw( pcMessage, uxLine & 0x3f, uxRow & 0x01);\r
377         }\r
378 }\r
379 \r