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