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