2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\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
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
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
79 * In addition to a subset of the standard demo application tasks, main.c also
\r
80 * defines the following tasks:
\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
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
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
109 /* Environment includes. */
\r
110 #include "DriverLib.h"
\r
112 /* Scheduler includes. */
\r
113 #include "FreeRTOS.h"
\r
116 #include "semphr.h"
\r
118 /* Demo app includes. */
\r
119 #include "integer.h"
\r
121 #include "semtest.h"
\r
122 #include "BlockQ.h"
\r
124 /* Delay between cycles of the 'check' task. */
\r
125 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
127 /* UART configuration - note this does not use the FIFO so is not very
\r
129 #define mainBAUD_RATE ( 19200 )
\r
130 #define mainFIFO_SET ( 0x10 )
\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
138 /* Demo board specifics. */
\r
139 #define mainPUSH_BUTTON GPIO_PIN_4
\r
142 #define mainQUEUE_SIZE ( 3 )
\r
143 #define mainDEBOUNCE_DELAY ( ( portTickType ) 150 / portTICK_RATE_MS )
\r
144 #define mainNO_DELAY ( ( portTickType ) 0 )
\r
146 * Configure the processor and peripherals for this demo.
\r
148 static void prvSetupHardware( void );
\r
151 * The 'check' task, as described at the top of this file.
\r
153 static void vCheckTask( void *pvParameters );
\r
156 * The task that is woken by the ISR that processes GPIO interrupts originating
\r
157 * from the push button.
\r
159 static void vButtonHandlerTask( void *pvParameters );
\r
162 * The task that controls access to the LCD.
\r
164 static void vPrintTask( void *pvParameter );
\r
166 /* String that is transmitted on the UART. */
\r
167 static portCHAR *cMessage = "Task woken by button interrupt! --- ";
\r
168 static volatile portCHAR *pcNextChar;
\r
170 /* The semaphore used to wake the button handler task from within the GPIO
\r
171 interrupt handler. */
\r
172 xSemaphoreHandle xButtonSemaphore;
\r
174 /* The queue used to send strings to the print task for display on the LCD. */
\r
175 xQueueHandle xPrintQueue;
\r
177 /*-----------------------------------------------------------*/
\r
181 /* Configure the clocks, UART and GPIO. */
\r
182 prvSetupHardware();
\r
184 /* Create the semaphore used to wake the button handler task from the GPIO
\r
186 vSemaphoreCreateBinary( xButtonSemaphore );
\r
187 xSemaphoreTake( xButtonSemaphore, 0 );
\r
189 /* Create the queue used to pass message to vPrintTask. */
\r
190 xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( portCHAR * ) );
\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
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
203 /* Start the scheduler. */
\r
204 vTaskStartScheduler();
\r
206 /* Will only get here if there was insufficient heap to start the
\r
211 /*-----------------------------------------------------------*/
\r
213 static void vCheckTask( void *pvParameters )
\r
215 portBASE_TYPE xErrorOccurred = pdFALSE;
\r
216 portTickType xLastExecutionTime;
\r
217 const portCHAR *pcPassMessage = "PASS";
\r
218 const portCHAR *pcFailMessage = "FAIL";
\r
220 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
221 works correctly. */
\r
222 xLastExecutionTime = xTaskGetTickCount();
\r
226 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
227 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
229 /* Has an error been found in any task? */
\r
231 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
233 xErrorOccurred = pdTRUE;
\r
236 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
238 xErrorOccurred = pdTRUE;
\r
241 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
243 xErrorOccurred = pdTRUE;
\r
246 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
248 xErrorOccurred = pdTRUE;
\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
256 xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
\r
260 xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvSetupHardware( void )
\r
268 /* Setup the PLL. */
\r
269 SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
\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
281 /* Enable the UART. */
\r
282 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
283 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
\r
285 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
287 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\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
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
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
302 /* Initialise the LCD> */
\r
303 OSRAMInit( false );
\r
304 OSRAMStringDraw("www.FreeRTOS.org", 0, 0);
\r
305 OSRAMStringDraw("LM3S811 demo", 16, 1);
\r
307 /*-----------------------------------------------------------*/
\r
309 static void vButtonHandlerTask( void *pvParameters )
\r
311 const portCHAR *pcInterruptMessage = "Int";
\r
315 /* Wait for a GPIO interrupt to wake this task. */
\r
316 while( xSemaphoreTake( xButtonSemaphore, portMAX_DELAY ) != pdPASS );
\r
318 /* Start the Tx of the message on the UART. */
\r
319 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
321 pcNextChar = cMessage;
\r
323 /* Send the first character. */
\r
324 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
326 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;
\r
331 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
333 /* Queue a message for the print task to display on the LCD. */
\r
334 xQueueSend( xPrintQueue, &pcInterruptMessage, portMAX_DELAY );
\r
336 /* Make sure we don't process bounces. */
\r
337 vTaskDelay( mainDEBOUNCE_DELAY );
\r
338 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
\r
342 /*-----------------------------------------------------------*/
\r
344 void vUART_ISR(void)
\r
346 unsigned portLONG ulStatus;
\r
348 /* What caused the interrupt. */
\r
349 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
351 /* Clear the interrupt. */
\r
352 UARTIntClear( UART0_BASE, ulStatus );
\r
354 /* Was a Tx interrupt pending? */
\r
355 if( ulStatus & UART_INT_TX )
\r
357 /* Send the next character in the string. We are not using the FIFO. */
\r
358 if( *pcNextChar != NULL )
\r
360 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
362 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;
\r
368 /*-----------------------------------------------------------*/
\r
370 void vGPIO_ISR( void )
\r
372 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
374 /* Clear the interrupt. */
\r
375 GPIOPinIntClear(GPIO_PORTC_BASE, mainPUSH_BUTTON);
\r
377 /* Wake the button handler task. */
\r
378 xSemaphoreGiveFromISR( xButtonSemaphore, &xHigherPriorityTaskWoken );
\r
379 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
381 /*-----------------------------------------------------------*/
\r
383 static void vPrintTask( void *pvParameters )
\r
385 portCHAR *pcMessage;
\r
386 unsigned portBASE_TYPE uxLine = 0, uxRow = 0;
\r
390 /* Wait for a message to arrive. */
\r
391 xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY );
\r
393 /* Write the message to the LCD. */
\r
397 OSRAMStringDraw( pcMessage, uxLine & 0x3f, uxRow & 0x01);
\r