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