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