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