]> begriffs open source - freertos/blob - FreeRTOS/Demo/CORTEX_LM3S316_IAR/commstest.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Demo / CORTEX_LM3S316_IAR / commstest.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * The comms test Rx and Tx task and co-routine.  See the comments at the top\r
77  * of main.c for full information.\r
78  */\r
79 \r
80 \r
81 /* Scheduler include files. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 #include "queue.h"\r
85 #include "croutine.h"\r
86 \r
87 /* Demo application include files. */\r
88 #include "partest.h"\r
89 \r
90 /* Library include files. */\r
91 #include "DriverLib.h"\r
92 \r
93 /* The LED's toggled by the various tasks. */\r
94 #define commsFAIL_LED                   ( 7 )\r
95 #define commsRX_LED                     ( 6 )\r
96 #define commsTX_LED                     ( 5 )\r
97 \r
98 /* The length of the queue used to pass received characters to the Comms Rx\r
99 task. */\r
100 #define commsRX_QUEUE_LEN                       ( 5 )\r
101 \r
102 /* The baud rate used by the UART comms tasks/co-routine. */\r
103 #define commsBAUD_RATE                          ( 57600 )\r
104 \r
105 /* FIFO setting for the UART.  The FIFO is not used to create a better test. */\r
106 #define commsFIFO_SET                           ( 0x10 )\r
107 \r
108 /* The string that is transmitted on the UART contains sequentially the\r
109 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */\r
110 #define commsFIRST_TX_CHAR '0'\r
111 #define commsLAST_TX_CHAR 'z'\r
112 \r
113 /* Just used to walk through the program memory in order that some random data\r
114 can be generated. */\r
115 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )\r
116 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )\r
117 \r
118 /* The time between transmissions of the string on UART 0.   This is pseudo\r
119 random in order to generate a bit or randomness to when the interrupts occur.*/\r
120 #define commsMIN_TX_DELAY                       ( 40 / portTICK_RATE_MS )\r
121 #define commsMAX_TX_DELAY                       ( ( portTickType ) 0x7f )\r
122 #define commsOFFSET_TIME                        ( ( portTickType ) 3 )\r
123 \r
124 /* The time the Comms Rx task should wait to receive a character.  This should\r
125 be slightly longer than the time between transmissions.  If we do not receive\r
126 a character after this time then there must be an error in the transmission or\r
127 the timing of the transmission. */\r
128 #define commsRX_DELAY                   ( commsMAX_TX_DELAY + 20 )\r
129 \r
130 \r
131 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;\r
132 \r
133 /* The queue used to pass characters out of the ISR. */\r
134 static xQueueHandle xCommsQueue;\r
135 \r
136 /* The next character to transmit. */\r
137 static char cNextChar;\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vSerialInit( void )\r
142 {\r
143         /* Create the queue used to communicate between the UART ISR and the Comms\r
144         Rx task. */\r
145         xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );\r
146 \r
147         /* Enable the UART.  GPIOA has already been initialised. */\r
148         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);\r
149 \r
150         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the\r
151         UART signals. */\r
152         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );\r
153 \r
154         /* Configure the UART for 8-N-1 operation. */\r
155         UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );\r
156 \r
157         /* We dont want to use the fifo.  This is for test purposes to generate\r
158         as many interrupts as possible. */\r
159         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;\r
160 \r
161         /* Enable both Rx and Tx interrupts. */\r
162         HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );\r
163         IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );\r
164         IntEnable( INT_UART0 );\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
169 {\r
170 portTickType xDelayPeriod;\r
171 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
172 \r
173         /* Co-routine MUST start with a call to crSTART. */\r
174         crSTART( xHandle );\r
175 \r
176         for(;;)\r
177     {   \r
178                 /* Was the previously transmitted string received correctly? */\r
179                 if( uxCommsErrorStatus != pdPASS )\r
180                 {\r
181                         /* An error was encountered so set the error LED. */\r
182                         vParTestSetLED( commsFAIL_LED, pdTRUE );\r
183                 }\r
184 \r
185                 /* The next character to Tx is the first in the string. */\r
186                 cNextChar = commsFIRST_TX_CHAR;\r
187 \r
188                 UARTIntDisable( UART0_BASE, UART_INT_TX );\r
189                 {\r
190                         /* Send the first character. */\r
191                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
192                         {\r
193                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
194                         }\r
195 \r
196                         /* Move the variable to the char to Tx on so the ISR transmits\r
197                         the next character in the string once this one has completed. */\r
198                         cNextChar++;\r
199                 }\r
200                 UARTIntEnable(UART0_BASE, UART_INT_TX);\r
201 \r
202                 /* Toggle the LED to show a new string is being transmitted. */\r
203         vParTestToggleLED( commsTX_LED );\r
204 \r
205                 /* Delay before we start the string off again.  A pseudo-random delay\r
206                 is used as this will provide a better test. */\r
207                 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );\r
208 \r
209                 pulRandomBytes++;\r
210                 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )\r
211                 {\r
212                         pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
213                 }\r
214 \r
215                 /* Make sure we don't wait too long... */\r
216                 xDelayPeriod &= commsMAX_TX_DELAY;\r
217 \r
218                 /* ...but we do want to wait. */\r
219                 if( xDelayPeriod < commsMIN_TX_DELAY )\r
220                 {\r
221                         xDelayPeriod = commsMIN_TX_DELAY;\r
222                 }\r
223 \r
224                 /* Block for the random(ish) time. */\r
225                 crDELAY( xHandle, xDelayPeriod );\r
226     }\r
227 \r
228         /* Co-routine MUST end with a call to crEND. */\r
229         crEND();\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 void vUART_ISR( void )\r
234 {\r
235 unsigned long ulStatus;\r
236 char cRxedChar;\r
237 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
238 \r
239         /* What caused the interrupt. */\r
240         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );\r
241 \r
242         /* Clear the interrupt. */\r
243         UARTIntClear( UART0_BASE, ulStatus );\r
244 \r
245         /* Was an Rx interrpt pending? */\r
246         if( ulStatus & UART_INT_RX )\r
247         {\r
248                 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )\r
249                 {\r
250                         /* Get the char from the buffer and post it onto the queue of\r
251                         Rxed chars.  Posting the character should wake the task that is\r
252                         blocked on the queue waiting for characters. */\r
253                         cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );\r
254                         xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );\r
255                 }               \r
256         }\r
257 \r
258         /* Was a Tx interrupt pending? */\r
259         if( ulStatus & UART_INT_TX )\r
260         {\r
261                 /* Send the next character in the string.  We are not using the FIFO. */\r
262                 if( cNextChar <= commsLAST_TX_CHAR )\r
263                 {\r
264                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
265                         {\r
266                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
267                         }\r
268                         cNextChar++;\r
269                 }\r
270         }\r
271         \r
272         /* If a task was woken by the character being received then we force\r
273         a context switch to occur in case the task is of higher priority than\r
274         the currently executing task (i.e. the task that this interrupt\r
275         interrupted.) */\r
276         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vCommsRxTask( void * pvParameters )\r
281 {\r
282 static char cRxedChar, cExpectedChar;\r
283 \r
284         /* Set the char we expect to receive to the start of the string. */\r
285         cExpectedChar = commsFIRST_TX_CHAR;\r
286 \r
287         for( ;; )\r
288         {\r
289                 /* Wait for a character to be received. */\r
290                 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );\r
291 \r
292                 /* Was the character recived (if any) the expected character. */\r
293                 if( cRxedChar != cExpectedChar )\r
294                 {\r
295                         /* Got an unexpected character.  This can sometimes occur when\r
296                         reseting the system using the debugger leaving characters already\r
297                         in the UART regsters. */\r
298                         uxCommsErrorStatus = pdFAIL;\r
299 \r
300                         /* Resync by waiting for the end of the current string. */\r
301                         while( cRxedChar != commsLAST_TX_CHAR )\r
302                         {\r
303                                 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );\r
304                         }\r
305 \r
306                         /* The next expected character is the start of the string again. */\r
307                         cExpectedChar = commsFIRST_TX_CHAR;\r
308                 }\r
309                 else\r
310                 {\r
311                         if( cExpectedChar == commsLAST_TX_CHAR )\r
312                         {\r
313                                 /* We have reached the end of the string - we now expect to\r
314                                 receive the first character in the string again.   The LED is\r
315                                 toggled to indicate that the entire string was received without\r
316                                 error. */\r
317                                 vParTestToggleLED( commsRX_LED );\r
318                                 cExpectedChar = commsFIRST_TX_CHAR;\r
319                         }\r
320                         else\r
321                         {\r
322                                 /* We got the expected character, we now expect to receive the\r
323                                 next character in the string. */\r
324                                 cExpectedChar++;\r
325                         }\r
326                 }\r
327         }\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 unsigned portBASE_TYPE uxGetCommsStatus( void )\r
332 {\r
333         return uxCommsErrorStatus;\r
334 }\r