]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LM3S102_GCC/main.c
there was an extra ')'... caused build to fail
[cmsis-freertos] / Demo / CORTEX_LM3S102_GCC / 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  * This demo application creates six co-routines and two tasks (three including 
30  * the idle task).  The co-routines execute as part of the idle task hook.
31  *
32  * Five of the created co-routines are the standard 'co-routine flash' 
33  * co-routines contained within the Demo/Common/Minimal/crflash.c file and 
34  * documented on the FreeRTOS.org WEB site.  
35  *
36  * The 'LCD Task' rotates a string on the LCD, delaying between each character
37  * as necessitated by the slow interface, and delaying between each string just
38  * long enough to enable the text to be read.
39  *
40  * The sixth co-routine and final task control the transmission and reception
41  * of a string to UART 0.  The co-routine periodically sends the first 
42  * character of the string to the UART, with the UART's TxEnd interrupt being
43  * used to transmit the remaining characters.  The UART's RxEnd interrupt 
44  * receives the characters and places them on a queue to be processed by the 
45  * 'COMs Rx' task.  An error is latched should an unexpected character be 
46  * received, or any character be received out of sequence.  
47  *
48  * A loopback connector is required to ensure that each character transmitted 
49  * on the UART is also received on the same UART.  For test purposes the UART
50  * FIFO's are not utalised in order to maximise the interrupt overhead.  Also
51  * a pseudo random interval is used between the start of each transmission in 
52  * order that the resultant interrupts are more randomly distributed and 
53  * therefore more likely to highlight any problems.
54  *
55  * The flash co-routines control LED's zero to four.  LED five is toggled each
56  * time the string is transmitted on the UART.  LED six is toggled each time
57  * the string is CORRECTLY received on the UART.  LED seven is latched on should
58  * an error be detected in any task or co-routine.
59  *
60  * In addition the idle task makes repetative calls to 
61  * prvSetAndCheckRegisters().  This simply loads the general purpose registers 
62  * with a known value, then checks each register to ensure the held value is 
63  * still correct.  As a low priority task this checking routine is likely to 
64  * get repeatedly swapped in and out.  A register being found to contain an 
65  * incorrect value is therefore indicative of an error in the task switching 
66  * mechansim.
67  *
68  */
69
70 /* Scheduler include files. */
71 #include "FreeRTOS.h"
72 #include "task.h"
73 #include "queue.h"
74 #include "croutine.h"
75
76 /* Demo application include files. */
77 #include "partest.h"
78 #include "crflash.h"
79
80 /* Library include files. */
81 #include "DriverLib.h"
82
83 /* The time to delay between writing each character to the LCD. */
84 #define mainCHAR_WRITE_DELAY            ( 2 / portTICK_PERIOD_MS )
85
86 /* The time to delay between writing each string to the LCD. */
87 #define mainSTRING_WRITE_DELAY          ( 400 / portTICK_PERIOD_MS )
88
89 /* The number of flash co-routines to create. */
90 #define mainNUM_FLASH_CO_ROUTINES       ( 5 )
91
92 /* The length of the queue used to pass received characters to the Comms Rx
93 task. */
94 #define mainRX_QUEUE_LEN                        ( 5 )
95
96 /* The priority of the co-routine used to initiate the transmission of the 
97 string on UART 0. */
98 #define mainTX_CO_ROUTINE_PRIORITY      ( 1 )
99
100 /* Only one co-routine is created so its index is not important. */
101 #define mainTX_CO_ROUTINE_INDEX         ( 0 )
102
103 /* The time between transmissions of the string on UART 0.   This is pseudo
104 random in order to generate a bit or randomness to when the interrupts occur.*/
105 #define mainMIN_TX_DELAY                        ( 40 / portTICK_PERIOD_MS )
106 #define mainMAX_TX_DELAY                        ( ( TickType_t ) 0x7f )
107 #define mainOFFSET_TIME                         ( ( TickType_t ) 3 )
108
109 /* The time the Comms Rx task should wait to receive a character.  This should
110 be slightly longer than the time between transmissions.  If we do not receive
111 a character after this time then there must be an error in the transmission or
112 the timing of the transmission. */
113 #define mainCOMMS_RX_DELAY                      ( mainMAX_TX_DELAY + 20 )
114
115 /* The task priorites. */
116 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY )
117 #define mainCOMMS_RX_TASK_PRIORITY      ( tskIDLE_PRIORITY + 1 )
118
119 /* The LED's toggled by the various tasks. */
120 #define mainCOMMS_FAIL_LED                      ( 7 )
121 #define mainCOMMS_RX_LED                        ( 6 )
122 #define mainCOMMS_TX_LED                        ( 5 )
123
124 /* The baud rate used by the UART comms tasks/co-routine. */
125 #define mainBAUD_RATE                           ( 57600 )
126
127 /* FIFO setting for the UART.  The FIFO is not used to create a better test. */
128 #define mainFIFO_SET                            ( 0x10 )
129
130 /* The string that is transmitted on the UART contains sequentially the 
131 characters from mainFIRST_TX_CHAR to mainLAST_TX_CHAR. */
132 #define mainFIRST_TX_CHAR '0'
133 #define mainLAST_TX_CHAR 'z'
134
135 /* Just used to walk through the program memory in order that some random data
136 can be generated. */
137 #define mainTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
138 #define mainFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
139
140 /* The error routine that is called if the driver library encounters an error. */
141 #ifdef DEBUG
142 void
143 __error__(char *pcFilename, unsigned long ulLine)
144 {
145 }
146 #endif
147
148 /*-----------------------------------------------------------*/
149
150 /*
151  * The task that rotates text on the LCD.
152  */
153 static void vLCDTask( void * pvParameters );
154
155 /*
156  * The task that receives the characters from UART 0.
157  */
158 static void vCommsRxTask( void * pvParameters );
159
160 /*
161  * The co-routine that periodically initiates the transmission of the string on
162  * the UART.
163  */
164 static void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
165
166 /* 
167  * Writes a string the the LCD.
168  */
169 static void prvWriteString( const char *pcString );
170
171 /*
172  * Initialisation routine for the UART.
173  */
174 static void vSerialInit( void );
175
176 /*
177  * Thread safe write to the PDC.
178  */
179 static void prvPDCWrite( char cAddress, char cData );
180
181 /*
182  * Function to simply set a known value into the general purpose registers
183  * then read them back to ensure they remain set correctly.  An incorrect value
184  * being indicative of an error in the task switching mechanism.
185  */
186 void prvSetAndCheckRegisters( void );
187
188 /*
189  * Latch the LED that indicates that an error has occurred. 
190  */
191 void vSetErrorLED( void );
192
193 /*
194  * Sets up the PLL and ports used by the demo.
195  */
196 static void prvSetupHardware( void );
197
198 /*-----------------------------------------------------------*/
199
200 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
201 defined within this file. */
202 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
203
204 /* The next character to transmit. */
205 static char cNextChar;
206
207 /* The queue used to transmit characters from the interrupt to the Comms Rx
208 task. */
209 static QueueHandle_t xCommsQueue;
210
211 /*-----------------------------------------------------------*/
212
213 void Main( void )
214 {
215         /* Create the queue used to communicate between the UART ISR and the Comms
216         Rx task. */
217         xCommsQueue = xQueueCreate( mainRX_QUEUE_LEN, sizeof( char ) );
218
219         /* Setup the ports used by the demo and the clock. */
220         prvSetupHardware();
221
222         /* Create the co-routines that flash the LED's. */
223         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
224
225         /* Create the co-routine that initiates the transmission of characters
226         on the UART. */
227         xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
228
229         /* Create the LCD and Comms Rx tasks. */
230         xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, mainLCD_TASK_PRIORITY, NULL );
231         xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
232
233         /* Start the scheduler running the tasks and co-routines just created. */
234         vTaskStartScheduler();
235
236         /* Should not get here unless we did not have enough memory to start the
237         scheduler. */
238         for( ;; );
239 }
240 /*-----------------------------------------------------------*/
241
242 static void prvSetupHardware( void )
243 {
244         /* Setup the PLL. */
245         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
246
247         /* Initialise the hardware used to talk to the LCD, LED's and UART. */
248         PDCInit();
249         vParTestInitialise();
250         vSerialInit();
251 }
252 /*-----------------------------------------------------------*/
253
254 void vApplicationIdleHook( void )
255 {
256         /* The co-routines are executed in the idle task using the idle task 
257         hook. */
258         for( ;; )
259         {
260                 /* Schedule the co-routines. */
261                 vCoRoutineSchedule();
262
263                 /* Run the register check function between each co-routine. */
264                 prvSetAndCheckRegisters();
265         }
266 }
267 /*-----------------------------------------------------------*/
268
269 static void prvWriteString( const char *pcString )
270 {
271         /* Write pcString to the LED, pausing between each character. */
272         prvPDCWrite(PDC_LCD_CSR, LCD_CLEAR);        
273         while( *pcString )
274         {
275                 vTaskDelay( mainCHAR_WRITE_DELAY );
276                 prvPDCWrite( PDC_LCD_RAM, *pcString );
277                 pcString++;
278         }
279 }
280 /*-----------------------------------------------------------*/
281
282 void vLCDTask( void * pvParameters )
283 {
284 unsigned portBASE_TYPE uxIndex;
285 const unsigned char ucCFGData[] = {     
286                                                                                         0x30,   /* Set data bus to 8-bits. */
287                                                                                         0x30,
288                                                                                         0x30,
289                                                                                         0x3C,   /* Number of lines/font. */
290                                                                                         0x08,   /* Display off. */
291                                                                                         0x01,   /* Display clear. */
292                                                                                         0x06,   /* Entry mode [cursor dir][shift]. */
293                                                                                         0x0C    /* Display on [display on][curson on][blinking on]. */
294                                                                           };  
295
296 /* The strings that are written to the LCD. */
297 const char *pcStringsToDisplay[] = {                                                                            
298                                                                                         "Stellaris",
299                                                                                         "Demo",
300                                                                                         "One",
301                                                                                         "www.FreeRTOS.org",
302                                                                                         ""
303                                                                            };
304
305         /* Configure the LCD. */
306         uxIndex = 0;
307         while( uxIndex < sizeof( ucCFGData ) )
308         {
309                 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
310                 uxIndex++;
311                 vTaskDelay( mainCHAR_WRITE_DELAY );
312         }
313
314         /* Turn the LCD Backlight on. */
315         prvPDCWrite( PDC_CSR, 0x01 );
316
317         /* Clear display. */
318         vTaskDelay( mainCHAR_WRITE_DELAY );
319         prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR ); 
320
321         uxIndex = 0;
322         for( ;; )    
323         {
324                 /* Display the string on the LCD. */
325                 prvWriteString( pcStringsToDisplay[ uxIndex ] );
326                 
327                 /* Move on to the next string - wrapping if necessary. */
328                 uxIndex++;
329                 if( *( pcStringsToDisplay[ uxIndex ] ) == 0x00 )
330                 {
331                         uxIndex = 0;
332                         /* Longer pause on the last string to be sent. */
333                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
334                 }
335
336                 /* Wait until it is time to move onto the next string. */
337                 vTaskDelay( mainSTRING_WRITE_DELAY );
338         }
339 }
340 /*-----------------------------------------------------------*/
341
342 static void vCommsRxTask( void * pvParameters )
343 {
344 static char cRxedChar, cExpectedChar;
345
346         /* Set the char we expect to receive to the start of the string. */
347         cExpectedChar = mainFIRST_TX_CHAR;
348
349         for( ;; )
350         {
351                 /* Wait for a character to be received. */
352                 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, mainCOMMS_RX_DELAY );
353
354                 /* Was the character recived (if any) the expected character. */
355                 if( cRxedChar != cExpectedChar )
356                 {
357                         /* Got an unexpected character.  This can sometimes occur when
358                         reseting the system using the debugger leaving characters already
359                         in the UART regsters. */
360                         uxErrorStatus = pdFAIL;
361
362                         /* Resync by waiting for the end of the current string. */
363                         while( cRxedChar != mainLAST_TX_CHAR )
364                         {
365                                 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
366                         }
367
368                         /* The next expected character is the start of the string again. */
369                         cExpectedChar = mainFIRST_TX_CHAR;
370                 }
371                 else
372                 {
373                         if( cExpectedChar == mainLAST_TX_CHAR )
374                         {
375                                 /* We have reached the end of the string - we now expect to 
376                                 receive the first character in the string again.   The LED is 
377                                 toggled to indicate that the entire string was received without
378                                 error. */
379                                 vParTestToggleLED( mainCOMMS_RX_LED );
380                                 cExpectedChar = mainFIRST_TX_CHAR;
381                         }
382                         else
383                         {
384                                 /* We got the expected character, we now expect to receive the
385                                 next character in the string. */
386                                 cExpectedChar++;
387                         }
388                 }
389         }
390 }
391 /*-----------------------------------------------------------*/
392
393 static void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
394 {
395 TickType_t xDelayPeriod;
396 static unsigned long *pulRandomBytes = mainFIRST_PROGRAM_BYTES;
397
398         /* Co-routine MUST start with a call to crSTART. */
399         crSTART( xHandle );
400
401         for(;;)
402     {   
403                 /* Was the previously transmitted string received correctly? */
404                 if( uxErrorStatus != pdPASS )
405                 {
406                         /* An error was encountered so set the error LED. */
407                         vSetErrorLED();
408                 }
409
410                 /* The next character to Tx is the first in the string. */
411                 cNextChar = mainFIRST_TX_CHAR;
412
413                 UARTIntDisable( UART0_BASE, UART_INT_TX );
414                 {
415                         /* Send the first character. */
416                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
417                         {
418                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
419                         }
420
421                         /* Move the variable to the char to Tx on so the ISR transmits
422                         the next character in the string once this one has completed. */
423                         cNextChar++;
424                 }
425                 UARTIntEnable(UART0_BASE, UART_INT_TX);
426
427                 /* Toggle the LED to show a new string is being transmitted. */
428         vParTestToggleLED( mainCOMMS_TX_LED );
429
430                 /* Delay before we start the string off again.  A pseudo-random delay
431                 is used as this will provide a better test. */
432                 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
433
434                 pulRandomBytes++;
435                 if( pulRandomBytes > mainTOTAL_PROGRAM_MEMORY )
436                 {
437                         pulRandomBytes = mainFIRST_PROGRAM_BYTES;
438                 }
439
440                 /* Make sure we don't wait too long... */
441                 xDelayPeriod &= mainMAX_TX_DELAY;
442
443                 /* ...but we do want to wait. */
444                 if( xDelayPeriod < mainMIN_TX_DELAY )
445                 {
446                         xDelayPeriod = mainMIN_TX_DELAY;
447                 }
448
449                 /* Block for the random(ish) time. */
450                 crDELAY( xHandle, xDelayPeriod );
451     }
452
453         /* Co-routine MUST end with a call to crEND. */
454         crEND();
455 }
456 /*-----------------------------------------------------------*/
457
458 static void vSerialInit( void )
459 {
460         /* Enable the UART.  GPIOA has already been initialised. */
461         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
462
463         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the
464         UART signals. */
465         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
466
467         /* Configure the UART for 8-N-1 operation. */
468         UARTConfigSet( UART0_BASE, mainBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
469
470         /* We dont want to use the fifo.  This is for test purposes to generate
471         as many interrupts as possible. */
472         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~mainFIFO_SET;
473
474         /* Enable both Rx and Tx interrupts. */
475         HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
476         IntEnable( INT_UART0 );
477 }
478 /*-----------------------------------------------------------*/
479
480 void vUART_ISR(void)
481 {
482 unsigned long ulStatus;
483 char cRxedChar;
484 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
485
486         /* What caused the interrupt. */
487         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
488
489         /* Clear the interrupt. */
490         UARTIntClear( UART0_BASE, ulStatus );
491
492         /* Was an Rx interrpt pending? */
493         if( ulStatus & UART_INT_RX )
494         {
495                 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
496                 {
497                         /* Get the char from the buffer and post it onto the queue of
498                         Rxed chars.  Posting the character should wake the task that is 
499                         blocked on the queue waiting for characters. */
500                         cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
501                         xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
502                 }               
503         }
504
505         /* Was a Tx interrupt pending? */
506         if( ulStatus & UART_INT_TX )
507         {
508                 /* Send the next character in the string.  We are not using the FIFO. */
509                 if( cNextChar <= mainLAST_TX_CHAR )
510                 {
511                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
512                         {
513                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
514                         }
515                         cNextChar++;
516                 }
517         }
518         
519         /* If a task was woken by the character being received then we force
520         a context switch to occur in case the task is of higher priority than
521         the currently executing task (i.e. the task that this interrupt 
522         interrupted.) */
523         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
524 }
525 /*-----------------------------------------------------------*/
526
527 static void prvPDCWrite( char cAddress, char cData )
528 {
529         vTaskSuspendAll();
530         {
531                 PDCWrite( cAddress, cData );
532         }
533         xTaskResumeAll();
534 }
535 /*-----------------------------------------------------------*/
536
537 void vSetErrorLED( void )
538 {
539         vParTestSetLED( mainCOMMS_FAIL_LED, pdTRUE );
540 }
541 /*-----------------------------------------------------------*/
542
543 void prvSetAndCheckRegisters( void )
544 {
545         /* Fill the general purpose registers with known values. */
546         __asm volatile( "    mov r11, #10\n"
547                   "    add r0, r11, #1\n"
548                   "    add r1, r11, #2\n"
549                         "    add r2, r11, #3\n"
550                         "    add r3, r11, #4\n"
551                         "    add r4, r11, #5\n"
552                         "    add r5, r11, #6\n"
553                         "    add r6, r11, #7\n"
554                         "    add r7, r11, #8\n"
555                         "    add r8, r11, #9\n"
556                         "    add r9, r11, #10\n"
557                         "    add r10, r11, #11\n"
558                         "    add r12, r11, #12" );
559
560         /* Check the values are as expected. */
561         __asm volatile( "    cmp r11, #10\n"
562                         "    bne set_error_led\n"
563                         "    cmp r0, #11\n"
564                         "    bne set_error_led\n"
565                         "    cmp r1, #12\n"
566                         "    bne set_error_led\n"
567                         "    cmp r2, #13\n"
568                         "    bne set_error_led\n"
569                         "    cmp r3, #14\n"
570                         "    bne set_error_led\n"
571                         "    cmp r4, #15\n"
572                         "    bne set_error_led\n"
573                         "    cmp r5, #16\n"
574                         "    bne set_error_led\n"
575                         "    cmp r6, #17\n"
576                         "    bne set_error_led\n"
577                         "    cmp r7, #18\n"
578                         "    bne set_error_led\n"
579                         "    cmp r8, #19\n"
580                         "    bne set_error_led\n"
581                         "    cmp r9, #20\n"
582                         "    bne set_error_led\n"
583                         "    cmp r10, #21\n"
584                         "    bne set_error_led\n"
585                         "    cmp r12, #22\n"
586                         "    bne set_error_led\n"
587                         "    bx lr" );
588
589   __asm volatile( "set_error_led:\n"
590                         "    push {r14}\n"
591                         "    ldr r1, =vSetErrorLED\n"
592                         "    blx r1\n"
593                         "    pop {r14}\n"
594                         "    bx lr" );
595 }
596 /*-----------------------------------------------------------*/