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