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