]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_LM3S316_IAR/main.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / CORTEX_LM3S316_IAR / main.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /* 
71  * This demo application creates eight co-routines and four tasks (five 
72  * including the idle task).  The co-routines execute as part of the idle task 
73  * hook.  The application is limited in size to allow its compilation using
74  * the KickStart version of the IAR compiler.
75  *
76  * Six of the created co-routines are the standard 'co-routine flash' 
77  * co-routines contained within the Demo/Common/Minimal/crflash.c file and 
78  * documented on the FreeRTOS.org WEB site.  
79  *
80  * The 'LCD Task' waits on a message queue for messages informing it what and
81  * where to display text.  This is the only task that accesses the LCD
82  * so mutual exclusion is guaranteed.
83  *
84  * The 'LCD Message Task' periodically sends strings to the LCD Task using
85  * the message queue.  The strings are rotated to form a short message and
86  * are written to the top row of the LCD.
87  *
88  * The 'ADC Co-routine' periodically reads the ADC input that is connected to
89  * the light sensor, forms a short message from the value, and then sends this
90  * message to the LCD Task using the same message queue.  The ADC readings are
91  * displayed on the bottom row of the LCD.  
92  *
93  * The eighth co-routine and final task control the transmission and reception
94  * of a string to UART 0.  The co-routine periodically sends the first 
95  * character of the string to the UART, with the UART's TxEnd interrupt being
96  * used to transmit the remaining characters.  The UART's RxEnd interrupt 
97  * receives the characters and places them on a queue to be processed by the 
98  * 'COMs Rx' task.  An error is latched should an unexpected character be 
99  * received, or any character be received out of sequence.  
100  *
101  * A loopback connector is required to ensure that each character transmitted 
102  * on the UART is also received on the same UART.  For test purposes the UART
103  * FIFO's are not utalised in order to maximise the interrupt overhead.  Also
104  * a pseudo random interval is used between the start of each transmission in 
105  * order that the resultant interrupts are more randomly distributed and 
106  * therefore more likely to highlight any problems.
107  *
108  * The flash co-routines control LED's zero to four.  LED five is toggled each
109  * time the string is transmitted on the UART.  LED six is toggled each time
110  * the string is CORRECTLY received on the UART.  LED seven is latched on 
111  * should an error be detected in any task or co-routine.
112  *
113  * In addition the idle task makes repetitive calls to 
114  * vSetAndCheckRegisters().  This simply loads the general purpose registers 
115  * with a known value, then checks each register to ensure the held value is 
116  * still correct.  As a low priority task this checking routine is likely to 
117  * get repeatedly swapped in and out.  A register being found to contain an 
118  * incorrect value is therefore indicative of an error in the task switching 
119  * mechanism.
120  *
121  */
122
123 /* standard include files. */
124 #include <stdio.h>
125
126 /* Scheduler include files. */
127 #include "FreeRTOS.h"
128 #include "task.h"
129 #include "queue.h"
130 #include "croutine.h"
131
132 /* Demo application include files. */
133 #include "partest.h"
134 #include "crflash.h"
135 #include "commstest.h"
136
137 /* Library include files. */
138 #include "DriverLib.h"
139
140 /* The time to delay between writing each character to the LCD. */
141 #define mainCHAR_WRITE_DELAY            ( 2 / portTICK_PERIOD_MS )
142
143 /* The time to delay between writing each string to the LCD. */
144 #define mainSTRING_WRITE_DELAY          ( 400 / portTICK_PERIOD_MS )
145
146 #define mainADC_DELAY                           ( 200 / portTICK_PERIOD_MS )
147
148 /* The number of flash co-routines to create. */
149 #define mainNUM_FLASH_CO_ROUTINES       ( 5 )
150
151 /* The length of the queue used to send messages to the LCD task. */
152 #define mainLCD_QUEUE_LEN                       ( 3 )
153
154 /* The priority of the co-routine used to initiate the transmission of the 
155 string on UART 0. */
156 #define mainTX_CO_ROUTINE_PRIORITY      ( 1 )
157 #define mainADC_CO_ROUTINE_PRIORITY     ( 2 )
158
159 /* Only one of each co-routine is created so its index is not important. */
160 #define mainTX_CO_ROUTINE_INDEX         ( 0 )
161 #define mainADC_CO_ROUTINE_INDEX        ( 0 )
162
163 /* The task priorities. */
164 #define mainLCD_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )
165 #define mainMSG_TASK_PRIORITY           ( mainLCD_TASK_PRIORITY - 1 )
166 #define mainCOMMS_RX_TASK_PRIORITY      ( tskIDLE_PRIORITY + 1 )
167
168 /* The LCD had two rows. */
169 #define mainTOP_ROW             0
170 #define mainBOTTOM_ROW  1
171
172 /* Dimension for the buffer into which the ADC value string is written. */
173 #define mainMAX_ADC_STRING_LEN  20
174
175 /* The LED that is lit should an error be detected in any of the tasks or
176 co-routines. */
177 #define mainFAIL_LED                    ( 7 )
178
179 /*-----------------------------------------------------------*/
180
181 /*
182  * The task that displays text on the LCD.
183  */
184 static void prvLCDTask( void * pvParameters );
185
186 /*
187  * The task that sends messages to be displayed on the top row of the LCD.
188  */
189 static void prvLCDMessageTask( void * pvParameters );
190
191 /*
192  * The co-routine that reads the ADC and sends messages for display on the
193  * bottom row of the LCD.
194  */
195 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );
196
197 /*
198  * Function to simply set a known value into the general purpose registers
199  * then read them back to ensure they remain set correctly.  An incorrect value
200  * being indicative of an error in the task switching mechanism.
201  */
202 extern void vSetAndCheckRegisters( void );
203
204 /*
205  * Latch the LED that indicates that an error has occurred. 
206  */
207 void vSetErrorLED( void );
208
209 /*
210  * Thread safe write to the PDC.
211  */
212 static void prvPDCWrite( char cAddress, char cData );
213
214 /*
215  * Sets up the hardware used by the demo.
216  */
217 static void prvSetupHardware( void );
218
219
220 /*-----------------------------------------------------------*/
221
222 /* The structure that is passed on the LCD message queue. */
223 typedef struct
224 {
225         char **ppcMessageToDisplay; /*<< Points to a char* pointing to the message to display. */
226         portBASE_TYPE xRow;                             /*<< The row on which the message should be displayed. */
227 } xLCDMessage;
228
229 /* Error flag set to pdFAIL if an error is encountered in the tasks/co-routines
230 defined within this file. */
231 unsigned portBASE_TYPE uxErrorStatus = pdPASS;
232
233 /* The queue used to transmit messages to the LCD task. */
234 static QueueHandle_t xLCDQueue;
235
236 /*-----------------------------------------------------------*/
237
238 /*
239  * Setup the hardware, create the tasks/co-routines, then start the scheduler.
240  */
241 void main( void )
242 {
243         /* Create the queue used by tasks wanting to write to the LCD. */
244         xLCDQueue = xQueueCreate( mainLCD_QUEUE_LEN, sizeof( xLCDMessage ) );
245
246         /* Setup the ports used by the demo and the clock. */
247         prvSetupHardware();
248
249         /* Create the co-routines that flash the LED's. */
250         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
251
252         /* Create the co-routine that initiates the transmission of characters
253         on the UART and the task that receives them, as described at the top of
254         this file. */
255         xCoRoutineCreate( vSerialTxCoRoutine, mainTX_CO_ROUTINE_PRIORITY, mainTX_CO_ROUTINE_INDEX );
256         xTaskCreate( vCommsRxTask, "CMS", configMINIMAL_STACK_SIZE, NULL, mainCOMMS_RX_TASK_PRIORITY, NULL );
257
258         /* Create the task that waits for messages to display on the LCD, plus the
259         task and co-routine that send messages for display (as described at the top
260         of this file. */
261         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainLCD_TASK_PRIORITY, NULL );
262         xTaskCreate( prvLCDMessageTask, "MSG", configMINIMAL_STACK_SIZE, ( void * ) &xLCDQueue, mainMSG_TASK_PRIORITY, NULL );
263         xCoRoutineCreate( prvADCCoRoutine, mainADC_CO_ROUTINE_PRIORITY, mainADC_CO_ROUTINE_INDEX );
264
265         /* Start the scheduler running the tasks and co-routines just created. */
266         vTaskStartScheduler();
267
268         /* Should not get here unless we did not have enough memory to start the
269         scheduler. */
270         for( ;; );
271 }
272 /*-----------------------------------------------------------*/
273
274 static void prvLCDMessageTask( void * pvParameters )
275 {
276 /* The strings that are written to the LCD. */
277 char *pcStringsToDisplay[] = {                                                                          
278                                                                         "IAR             ",
279                                                                         "Stellaris       ",
280                                                                         "Demo            ",
281                                                                         "www.FreeRTOS.org",
282                                                                         ""
283                                                                 };
284
285 QueueHandle_t *pxLCDQueue;      
286 xLCDMessage xMessageToSend;
287 portBASE_TYPE xIndex = 0;
288
289         /* To test the parameter passing mechanism, the queue on which messages are
290         posted is passed in as a parameter even though it is available as a file
291         scope variable anyway. */
292         pxLCDQueue = ( QueueHandle_t * ) pvParameters;
293
294         for( ;; )
295         {
296                 /* Wait until it is time to move onto the next string. */
297                 vTaskDelay( mainSTRING_WRITE_DELAY );           
298                 
299                 /* Create the message object to send to the LCD task. */
300                 xMessageToSend.ppcMessageToDisplay = &pcStringsToDisplay[ xIndex ];
301                 xMessageToSend.xRow = mainTOP_ROW;
302                 
303                 /* Post the message to be displayed. */
304                 if( !xQueueSend( *pxLCDQueue, ( void * ) &xMessageToSend, 0 ) )
305                 {
306                         uxErrorStatus = pdFAIL;
307                 }
308                 
309                 /* Move onto the next message, wrapping when necessary. */
310                 xIndex++;               
311                 if( *( pcStringsToDisplay[ xIndex ] ) == 0x00 )
312                 {
313                         xIndex = 0;
314
315                         /* Delay longer before going back to the start of the messages. */
316                         vTaskDelay( mainSTRING_WRITE_DELAY * 2 );
317                 }
318         }
319 }
320 /*-----------------------------------------------------------*/
321
322 void prvLCDTask( void * pvParameters )
323 {
324 unsigned portBASE_TYPE uxIndex;
325 QueueHandle_t *pxLCDQueue;
326 xLCDMessage xReceivedMessage;
327 char *pcString;
328 const unsigned char ucCFGData[] = {     
329                                                                                         0x30,   /* Set data bus to 8-bits. */
330                                                                                         0x30,
331                                                                                         0x30,
332                                                                                         0x3C,   /* Number of lines/font. */
333                                                                                         0x08,   /* Display off. */
334                                                                                         0x01,   /* Display clear. */
335                                                                                         0x06,   /* Entry mode [cursor dir][shift]. */
336                                                                                         0x0C    /* Display on [display on][curson on][blinking on]. */
337                                                                           };  
338
339         /* To test the parameter passing mechanism, the queue on which messages are
340         received is passed in as a parameter even though it is available as a file
341         scope variable anyway. */
342         pxLCDQueue = ( QueueHandle_t * ) pvParameters;
343
344         /* Configure the LCD. */
345         uxIndex = 0;
346         while( uxIndex < sizeof( ucCFGData ) )
347         {
348                 prvPDCWrite( PDC_LCD_CSR, ucCFGData[ uxIndex ] );
349                 uxIndex++;
350                 vTaskDelay( mainCHAR_WRITE_DELAY );
351         }
352
353         /* Turn the LCD Backlight on. */
354         prvPDCWrite( PDC_CSR, 0x01 );
355
356         /* Clear display. */
357         vTaskDelay( mainCHAR_WRITE_DELAY );
358         prvPDCWrite( PDC_LCD_CSR, LCD_CLEAR ); 
359
360         uxIndex = 0;
361         for( ;; )    
362         {
363                 /* Wait for a message to arrive. */
364                 if( xQueueReceive( *pxLCDQueue, &xReceivedMessage, portMAX_DELAY ) )
365                 {
366                         /* Which row does the received message say to write to? */
367                         PDCLCDSetPos( 0, xReceivedMessage.xRow );
368
369                         /* Where is the string we are going to display? */
370                         pcString = *xReceivedMessage.ppcMessageToDisplay;
371                         
372                         while( *pcString )
373                         {
374                                 /* Don't write out the string too quickly as LCD's are usually 
375                                 pretty slow devices. */
376                                 vTaskDelay( mainCHAR_WRITE_DELAY );
377                                 prvPDCWrite( PDC_LCD_RAM, *pcString );
378                                 pcString++;
379                         }               
380                 }
381         }
382 }
383 /*-----------------------------------------------------------*/
384
385 static void prvADCCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
386 {
387 static unsigned long ulADCValue;
388 static char cMessageBuffer[ mainMAX_ADC_STRING_LEN ];
389 static char *pcMessage;
390 static xLCDMessage xMessageToSend;
391
392         /* Co-routines MUST start with a call to crSTART(). */
393         crSTART( xHandle );
394         
395         for( ;; )
396         {
397                 /* Start an ADC conversion. */
398                 ADCProcessorTrigger( ADC_BASE, 0 );
399                 
400                 /* Simply delay - when we unblock the result should be available */     
401                 crDELAY( xHandle, mainADC_DELAY );
402                 
403                 /* Get the ADC result. */
404                 ADCSequenceDataGet( ADC_BASE, 0, &ulADCValue );
405
406                 /* Create a string with the result. */          
407                 sprintf( cMessageBuffer, "ADC = %d   ", ulADCValue );
408                 pcMessage = cMessageBuffer;
409
410                 /* Configure the message we are going to send for display. */
411                 xMessageToSend.ppcMessageToDisplay = ( char** ) &pcMessage;
412                 xMessageToSend.xRow = mainBOTTOM_ROW;
413                 
414                 /* Send the string to the LCD task for display.  We are sending
415                 on a task queue so do not have the option to block. */
416                 if( !xQueueSend( xLCDQueue, ( void * ) &xMessageToSend, 0 ) )
417                 {
418                         uxErrorStatus = pdFAIL;
419                 }               
420         }
421         
422         /* Co-routines MUST end with a call to crEND(). */
423         crEND();
424 }
425 /*-----------------------------------------------------------*/
426
427 static void prvSetupHardware( void )
428 {
429         /* Setup the PLL. */
430         SysCtlClockSet( SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_6MHZ );
431         
432         /* Initialise the hardware used to talk to the LCD, LED's and UART. */
433         PDCInit();
434         vParTestInitialise();
435         vSerialInit();
436
437         /* The ADC is used to read the light sensor. */
438         SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
439     ADCSequenceConfigure( ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
440     ADCSequenceStepConfigure( ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_END );
441     ADCSequenceEnable( ADC_BASE, 0 );
442
443 }
444 /*-----------------------------------------------------------*/
445
446 static void prvPDCWrite( char cAddress, char cData )
447 {
448         vTaskSuspendAll();
449         {
450                 PDCWrite( cAddress, cData );
451         }
452         xTaskResumeAll();
453 }
454 /*-----------------------------------------------------------*/
455
456 void vSetErrorLED( void )
457 {
458         vParTestSetLED( mainFAIL_LED, pdTRUE );
459 }
460 /*-----------------------------------------------------------*/
461
462 void vApplicationIdleHook( void )
463 {
464         /* The co-routines are executed in the idle task using the idle task 
465         hook. */
466         for( ;; )
467         {
468                 /* Schedule the co-routines. */
469                 vCoRoutineSchedule();
470
471                 /* Run the register check function between each co-routine. */
472                 vSetAndCheckRegisters();
473                 
474                 /* See if the comms task and co-routine has found any errors. */
475                 if( uxGetCommsStatus() != pdPASS )
476                 {
477                         vParTestSetLED( mainFAIL_LED, pdTRUE );
478                 }
479         }
480 }
481 /*-----------------------------------------------------------*/