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