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