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