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