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