]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_LM3S316_IAR/commstest.c
Prepare for V5.3.0 release.
[freertos] / Demo / CORTEX_LM3S316_IAR / commstest.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /*\r
53  * The comms test Rx and Tx task and co-routine.  See the comments at the top\r
54  * of main.c for full information.\r
55  */\r
56 \r
57 \r
58 /* Scheduler include files. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 #include "croutine.h"\r
63 \r
64 /* Demo application include files. */\r
65 #include "partest.h"\r
66 \r
67 /* Library include files. */\r
68 #include "DriverLib.h"\r
69 \r
70 /* The LED's toggled by the various tasks. */\r
71 #define commsFAIL_LED                   ( 7 )\r
72 #define commsRX_LED                     ( 6 )\r
73 #define commsTX_LED                     ( 5 )\r
74 \r
75 /* The length of the queue used to pass received characters to the Comms Rx\r
76 task. */\r
77 #define commsRX_QUEUE_LEN                       ( 5 )\r
78 \r
79 /* The baud rate used by the UART comms tasks/co-routine. */\r
80 #define commsBAUD_RATE                          ( 57600 )\r
81 \r
82 /* FIFO setting for the UART.  The FIFO is not used to create a better test. */\r
83 #define commsFIFO_SET                           ( 0x10 )\r
84 \r
85 /* The string that is transmitted on the UART contains sequentially the\r
86 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */\r
87 #define commsFIRST_TX_CHAR '0'\r
88 #define commsLAST_TX_CHAR 'z'\r
89 \r
90 /* Just used to walk through the program memory in order that some random data\r
91 can be generated. */\r
92 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned portLONG * ) ( 8 * 1024 ) )\r
93 #define commsFIRST_PROGRAM_BYTES ( ( unsigned portLONG * ) 4 )\r
94 \r
95 /* The time between transmissions of the string on UART 0.   This is pseudo\r
96 random in order to generate a bit or randomness to when the interrupts occur.*/\r
97 #define commsMIN_TX_DELAY                       ( 40 / portTICK_RATE_MS )\r
98 #define commsMAX_TX_DELAY                       ( ( portTickType ) 0x7f )\r
99 #define commsOFFSET_TIME                        ( ( portTickType ) 3 )\r
100 \r
101 /* The time the Comms Rx task should wait to receive a character.  This should\r
102 be slightly longer than the time between transmissions.  If we do not receive\r
103 a character after this time then there must be an error in the transmission or\r
104 the timing of the transmission. */\r
105 #define commsRX_DELAY                   ( commsMAX_TX_DELAY + 20 )\r
106 \r
107 \r
108 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;\r
109 \r
110 /* The queue used to pass characters out of the ISR. */\r
111 static xQueueHandle xCommsQueue;\r
112 \r
113 /* The next character to transmit. */\r
114 static portCHAR cNextChar;\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 void vSerialInit( void )\r
119 {\r
120         /* Create the queue used to communicate between the UART ISR and the Comms\r
121         Rx task. */\r
122         xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( portCHAR ) );\r
123 \r
124         /* Enable the UART.  GPIOA has already been initialised. */\r
125         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);\r
126 \r
127         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the\r
128         UART signals. */\r
129         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );\r
130 \r
131         /* Configure the UART for 8-N-1 operation. */\r
132         UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );\r
133 \r
134         /* We dont want to use the fifo.  This is for test purposes to generate\r
135         as many interrupts as possible. */\r
136         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;\r
137 \r
138         /* Enable both Rx and Tx interrupts. */\r
139         HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );\r
140         IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );\r
141         IntEnable( INT_UART0 );\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
146 {\r
147 portTickType xDelayPeriod;\r
148 static unsigned portLONG *pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
149 \r
150         /* Co-routine MUST start with a call to crSTART. */\r
151         crSTART( xHandle );\r
152 \r
153         for(;;)\r
154     {   \r
155                 /* Was the previously transmitted string received correctly? */\r
156                 if( uxCommsErrorStatus != pdPASS )\r
157                 {\r
158                         /* An error was encountered so set the error LED. */\r
159                         vParTestSetLED( commsFAIL_LED, pdTRUE );\r
160                 }\r
161 \r
162                 /* The next character to Tx is the first in the string. */\r
163                 cNextChar = commsFIRST_TX_CHAR;\r
164 \r
165                 UARTIntDisable( UART0_BASE, UART_INT_TX );\r
166                 {\r
167                         /* Send the first character. */\r
168                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
169                         {\r
170                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
171                         }\r
172 \r
173                         /* Move the variable to the char to Tx on so the ISR transmits\r
174                         the next character in the string once this one has completed. */\r
175                         cNextChar++;\r
176                 }\r
177                 UARTIntEnable(UART0_BASE, UART_INT_TX);\r
178 \r
179                 /* Toggle the LED to show a new string is being transmitted. */\r
180         vParTestToggleLED( commsTX_LED );\r
181 \r
182                 /* Delay before we start the string off again.  A pseudo-random delay\r
183                 is used as this will provide a better test. */\r
184                 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );\r
185 \r
186                 pulRandomBytes++;\r
187                 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )\r
188                 {\r
189                         pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
190                 }\r
191 \r
192                 /* Make sure we don't wait too long... */\r
193                 xDelayPeriod &= commsMAX_TX_DELAY;\r
194 \r
195                 /* ...but we do want to wait. */\r
196                 if( xDelayPeriod < commsMIN_TX_DELAY )\r
197                 {\r
198                         xDelayPeriod = commsMIN_TX_DELAY;\r
199                 }\r
200 \r
201                 /* Block for the random(ish) time. */\r
202                 crDELAY( xHandle, xDelayPeriod );\r
203     }\r
204 \r
205         /* Co-routine MUST end with a call to crEND. */\r
206         crEND();\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 void vUART_ISR( void )\r
211 {\r
212 unsigned portLONG ulStatus;\r
213 portCHAR cRxedChar;\r
214 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
215 \r
216         /* What caused the interrupt. */\r
217         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );\r
218 \r
219         /* Clear the interrupt. */\r
220         UARTIntClear( UART0_BASE, ulStatus );\r
221 \r
222         /* Was an Rx interrpt pending? */\r
223         if( ulStatus & UART_INT_RX )\r
224         {\r
225                 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )\r
226                 {\r
227                         /* Get the char from the buffer and post it onto the queue of\r
228                         Rxed chars.  Posting the character should wake the task that is\r
229                         blocked on the queue waiting for characters. */\r
230                         cRxedChar = ( portCHAR ) HWREG( UART0_BASE + UART_O_DR );\r
231                         xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );\r
232                 }               \r
233         }\r
234 \r
235         /* Was a Tx interrupt pending? */\r
236         if( ulStatus & UART_INT_TX )\r
237         {\r
238                 /* Send the next character in the string.  We are not using the FIFO. */\r
239                 if( cNextChar <= commsLAST_TX_CHAR )\r
240                 {\r
241                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
242                         {\r
243                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
244                         }\r
245                         cNextChar++;\r
246                 }\r
247         }\r
248         \r
249         /* If a task was woken by the character being received then we force\r
250         a context switch to occur in case the task is of higher priority than\r
251         the currently executing task (i.e. the task that this interrupt\r
252         interrupted.) */\r
253         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vCommsRxTask( void * pvParameters )\r
258 {\r
259 static portCHAR cRxedChar, cExpectedChar;\r
260 \r
261         /* Set the char we expect to receive to the start of the string. */\r
262         cExpectedChar = commsFIRST_TX_CHAR;\r
263 \r
264         for( ;; )\r
265         {\r
266                 /* Wait for a character to be received. */\r
267                 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );\r
268 \r
269                 /* Was the character recived (if any) the expected character. */\r
270                 if( cRxedChar != cExpectedChar )\r
271                 {\r
272                         /* Got an unexpected character.  This can sometimes occur when\r
273                         reseting the system using the debugger leaving characters already\r
274                         in the UART regsters. */\r
275                         uxCommsErrorStatus = pdFAIL;\r
276 \r
277                         /* Resync by waiting for the end of the current string. */\r
278                         while( cRxedChar != commsLAST_TX_CHAR )\r
279                         {\r
280                                 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );\r
281                         }\r
282 \r
283                         /* The next expected character is the start of the string again. */\r
284                         cExpectedChar = commsFIRST_TX_CHAR;\r
285                 }\r
286                 else\r
287                 {\r
288                         if( cExpectedChar == commsLAST_TX_CHAR )\r
289                         {\r
290                                 /* We have reached the end of the string - we now expect to\r
291                                 receive the first character in the string again.   The LED is\r
292                                 toggled to indicate that the entire string was received without\r
293                                 error. */\r
294                                 vParTestToggleLED( commsRX_LED );\r
295                                 cExpectedChar = commsFIRST_TX_CHAR;\r
296                         }\r
297                         else\r
298                         {\r
299                                 /* We got the expected character, we now expect to receive the\r
300                                 next character in the string. */\r
301                                 cExpectedChar++;\r
302                         }\r
303                 }\r
304         }\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 unsigned portBASE_TYPE uxGetCommsStatus( void )\r
309 {\r
310         return uxCommsErrorStatus;\r
311 }\r