]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LM3S811_KEIL/main.c
Set error state if no delay or already expired
[cmsis-freertos] / Demo / CORTEX_LM3S811_KEIL / main.c
1 /*
2  * FreeRTOS Kernel V10.1.1
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /*
30  * This project contains an application demonstrating the use of the 
31  * FreeRTOS.org mini real time scheduler on the Luminary Micro LM3S811 Eval
32  * board.  See http://www.FreeRTOS.org for more information.
33  *
34  * main() simply sets up the hardware, creates all the demo application tasks, 
35  * then starts the scheduler.  http://www.freertos.org/a00102.html provides
36  * more information on the standard demo tasks. 
37  *
38  * In addition to a subset of the standard demo application tasks, main.c also
39  * defines the following tasks: 
40  *
41  * + A 'Print' task.  The print task is the only task permitted to access the
42  * LCD - thus ensuring mutual exclusion and consistent access to the resource.
43  * Other tasks do not access the LCD directly, but instead send the text they
44  * wish to display to the print task.  The print task spends most of its time
45  * blocked - only waking when a message is queued for display.
46  *
47  * + A 'Button handler' task.  The eval board contains a user push button that
48  * is configured to generate interrupts.  The interrupt handler uses a 
49  * semaphore to wake the button handler task - demonstrating how the priority 
50  * mechanism can be used to defer interrupt processing to the task level.  The
51  * button handler task sends a message both to the LCD (via the print task) and
52  * the UART where it can be viewed using a dumb terminal (via the UART to USB
53  * converter on the eval board).  NOTES:  The dumb terminal must be closed in 
54  * order to reflash the microcontroller.  A very basic interrupt driven UART
55  * driver is used that does not use the FIFO.  19200 baud is used.
56  *
57  * + A 'check' task.  The check task only executes every five seconds but has a
58  * high priority so is guaranteed to get processor time.  Its function is to
59  * check that all the other tasks are still operational and that no errors have
60  * been detected at any time.  If no errors have every been detected 'PASS' is
61  * written to the display (via the print task) - if an error has ever been
62  * detected the message is changed to 'FAIL'.  The position of the message is
63  * changed for each write.
64  */
65
66
67
68 /* Environment includes. */
69 #include "DriverLib.h"
70
71 /* Scheduler includes. */
72 #include "FreeRTOS.h"
73 #include "task.h"
74 #include "queue.h"
75 #include "semphr.h"
76
77 /* Demo app includes. */
78 #include "integer.h"
79 #include "PollQ.h"
80 #include "semtest.h"
81 #include "BlockQ.h"
82
83 /* Delay between cycles of the 'check' task. */
84 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
85
86 /* UART configuration - note this does not use the FIFO so is not very 
87 efficient. */
88 #define mainBAUD_RATE                           ( 19200 )
89 #define mainFIFO_SET                            ( 0x10 )
90
91 /* Demo task priorities. */
92 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )
93 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )
94 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )
95 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )
96
97 /* Demo board specifics. */
98 #define mainPUSH_BUTTON             GPIO_PIN_4
99
100 /* Misc. */
101 #define mainQUEUE_SIZE                          ( 3 )
102 #define mainDEBOUNCE_DELAY                      ( ( TickType_t ) 150 / portTICK_PERIOD_MS )
103 #define mainNO_DELAY                            ( ( TickType_t ) 0 )
104 /*
105  * Configure the processor and peripherals for this demo. 
106  */
107 static void prvSetupHardware( void );
108
109 /*
110  * The 'check' task, as described at the top of this file.
111  */
112 static void vCheckTask( void *pvParameters );
113
114 /*
115  * The task that is woken by the ISR that processes GPIO interrupts originating
116  * from the push button.
117  */
118 static void vButtonHandlerTask( void *pvParameters );
119
120 /*
121  * The task that controls access to the LCD.
122  */
123 static void vPrintTask( void *pvParameter );
124
125 /* String that is transmitted on the UART. */
126 static char *cMessage = "Task woken by button interrupt! --- ";
127 static volatile char *pcNextChar;
128
129 /* The semaphore used to wake the button handler task from within the GPIO
130 interrupt handler. */
131 SemaphoreHandle_t xButtonSemaphore;
132
133 /* The queue used to send strings to the print task for display on the LCD. */
134 QueueHandle_t xPrintQueue;
135
136 /* Newer library version. */
137 extern void UARTConfigSetExpClk(unsigned long ulBase, unsigned long ulUARTClk, unsigned long ulBaud, unsigned long ulConfig);
138 /*-----------------------------------------------------------*/
139
140 int main( void )
141 {
142         /* Configure the clocks, UART and GPIO. */
143         prvSetupHardware();
144
145         /* Create the semaphore used to wake the button handler task from the GPIO
146         ISR. */
147         vSemaphoreCreateBinary( xButtonSemaphore );
148         xSemaphoreTake( xButtonSemaphore, 0 );
149
150         /* Create the queue used to pass message to vPrintTask. */
151         xPrintQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
152
153         /* Start the standard demo tasks. */
154         vStartIntegerMathTasks( tskIDLE_PRIORITY );
155         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
156         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
157         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
158
159         /* Start the tasks defined within the file. */
160         xTaskCreate( vCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
161         xTaskCreate( vButtonHandlerTask, "Status", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY + 1, NULL );
162         xTaskCreate( vPrintTask, "Print", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY - 1, NULL );
163
164         /* Start the scheduler. */
165         vTaskStartScheduler();
166
167         /* Will only get here if there was insufficient heap to start the 
168         scheduler. */
169
170         return 0;
171 }
172 /*-----------------------------------------------------------*/
173
174 static void vCheckTask( void *pvParameters )
175 {
176 portBASE_TYPE xErrorOccurred = pdFALSE;
177 TickType_t xLastExecutionTime;
178 const char *pcPassMessage = "PASS";
179 const char *pcFailMessage = "FAIL";
180
181         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
182         works correctly. */
183         xLastExecutionTime = xTaskGetTickCount();
184
185         for( ;; )
186         {
187                 /* Perform this check every mainCHECK_DELAY milliseconds. */
188                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
189
190                 /* Has an error been found in any task? */
191
192                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
193                 {
194                         xErrorOccurred = pdTRUE;
195                 }
196         
197                 if( xArePollingQueuesStillRunning() != pdTRUE )
198                 {
199                         xErrorOccurred = pdTRUE;
200                 }
201         
202                 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
203                 {
204                         xErrorOccurred = pdTRUE;
205                 }
206
207                 if( xAreBlockingQueuesStillRunning() != pdTRUE )
208                 {
209                         xErrorOccurred = pdTRUE;
210                 }
211
212                 /* Send either a pass or fail message.  If an error is found it is
213                 never cleared again.  We do not write directly to the LCD, but instead
214                 queue a message for display by the print task. */
215                 if( xErrorOccurred == pdTRUE )
216                 {
217                         xQueueSend( xPrintQueue, &pcFailMessage, portMAX_DELAY );
218                 }
219                 else
220                 {
221                         xQueueSend( xPrintQueue, &pcPassMessage, portMAX_DELAY );
222                 }
223         }
224 }
225 /*-----------------------------------------------------------*/
226
227 static void prvSetupHardware( void )
228 {
229         /* Setup the PLL. */
230         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
231
232         /* Setup the push button. */
233         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
234     GPIODirModeSet(GPIO_PORTC_BASE, mainPUSH_BUTTON, GPIO_DIR_MODE_IN);
235         GPIOIntTypeSet( GPIO_PORTC_BASE, mainPUSH_BUTTON,GPIO_FALLING_EDGE );
236         IntPrioritySet( INT_GPIOC, configKERNEL_INTERRUPT_PRIORITY );
237         GPIOPinIntEnable( GPIO_PORTC_BASE, mainPUSH_BUTTON );
238         IntEnable( INT_GPIOC );
239
240
241
242         /* Enable the UART.  */
243         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
244         SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
245
246         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the
247         UART signals. */
248         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
249
250         /* Configure the UART for 8-N-1 operation. */
251         UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
252
253         /* We don't want to use the fifo.  This is for test purposes to generate
254         as many interrupts as possible. */
255         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;
256
257         /* Enable Tx interrupts. */
258         HWREG( UART0_BASE + UART_O_IM ) |= UART_INT_TX;
259         IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
260         IntEnable( INT_UART0 );
261
262
263         /* Initialise the LCD> */
264     OSRAMInit( false );
265     OSRAMStringDraw("www.FreeRTOS.org", 0, 0);
266         OSRAMStringDraw("LM3S811 demo", 16, 1);
267 }
268 /*-----------------------------------------------------------*/
269
270 static void vButtonHandlerTask( void *pvParameters )
271 {
272 const char *pcInterruptMessage = "Int";
273
274         for( ;; )
275         {
276                 /* Wait for a GPIO interrupt to wake this task. */
277                 while( xSemaphoreTake( xButtonSemaphore, portMAX_DELAY ) != pdPASS );
278
279                 /* Start the Tx of the message on the UART. */
280                 UARTIntDisable( UART0_BASE, UART_INT_TX );
281                 {
282                         pcNextChar = cMessage;
283
284                         /* Send the first character. */
285                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
286                         {
287                                 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;
288                         }
289
290                         pcNextChar++;
291                 }
292                 UARTIntEnable(UART0_BASE, UART_INT_TX);
293
294                 /* Queue a message for the print task to display on the LCD. */
295                 xQueueSend( xPrintQueue, &pcInterruptMessage, portMAX_DELAY );
296
297                 /* Make sure we don't process bounces. */
298                 vTaskDelay( mainDEBOUNCE_DELAY );
299                 xSemaphoreTake( xButtonSemaphore, mainNO_DELAY );
300         }
301 }
302
303 /*-----------------------------------------------------------*/
304
305 void vUART_ISR(void)
306 {
307 unsigned long ulStatus;
308
309         /* What caused the interrupt. */
310         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
311
312         /* Clear the interrupt. */
313         UARTIntClear( UART0_BASE, ulStatus );
314
315         /* Was a Tx interrupt pending? */
316         if( ulStatus & UART_INT_TX )
317         {
318                 /* Send the next character in the string.  We are not using the FIFO. */
319                 if( *pcNextChar != NULL )
320                 {
321                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
322                         {
323                                 HWREG( UART0_BASE + UART_O_DR ) = *pcNextChar;
324                         }
325                         pcNextChar++;
326                 }
327         }
328 }
329 /*-----------------------------------------------------------*/
330
331 void vGPIO_ISR( void )
332 {
333 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
334
335         /* Clear the interrupt. */
336         GPIOPinIntClear( GPIO_PORTC_BASE, mainPUSH_BUTTON );
337
338         /* Wake the button handler task. */
339         xSemaphoreGiveFromISR( xButtonSemaphore, &xHigherPriorityTaskWoken );
340         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
341 }
342 /*-----------------------------------------------------------*/
343
344 static void vPrintTask( void *pvParameters )
345 {
346 char *pcMessage;
347 unsigned portBASE_TYPE uxLine = 0, uxRow = 0;
348
349         for( ;; )
350         {
351                 /* Wait for a message to arrive. */
352                 xQueueReceive( xPrintQueue, &pcMessage, portMAX_DELAY );
353
354                 /* Write the message to the LCD. */
355                 uxRow++;
356                 uxLine++;
357                 OSRAMClear();
358                 OSRAMStringDraw( pcMessage, uxLine & 0x3f, uxRow & 0x01);
359         }
360 }
361