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