]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/comtest.c
Change version numbers ready for V8.0.0 release candidate 1 tag.
[freertos] / FreeRTOS / Demo / Common / Minimal / comtest.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 /*\r
68  * This version of comtest. c is for use on systems that have limited stack\r
69  * space and no display facilities.  The complete version can be found in\r
70  * the Demo/Common/Full directory.\r
71  *\r
72  * Creates two tasks that operate on an interrupt driven serial port.  A\r
73  * loopback connector should be used so that everything that is transmitted is\r
74  * also received.  The serial port does not use any flow control.  On a\r
75  * standard 9way 'D' connector pins two and three should be connected together.\r
76  *\r
77  * The first task posts a sequence of characters to the Tx queue, toggling an\r
78  * LED on each successful post.  At the end of the sequence it sleeps for a\r
79  * pseudo-random period before resending the same sequence.\r
80  *\r
81  * The UART Tx end interrupt is enabled whenever data is available in the Tx\r
82  * queue.  The Tx end ISR removes a single character from the Tx queue and\r
83  * passes it to the UART for transmission.\r
84  *\r
85  * The second task blocks on the Rx queue waiting for a character to become\r
86  * available.  When the UART Rx end interrupt receives a character it places\r
87  * it in the Rx queue, waking the second task.  The second task checks that the\r
88  * characters removed from the Rx queue form the same sequence as those posted\r
89  * to the Tx queue, and toggles an LED for each correct character.\r
90  *\r
91  * The receiving task is spawned with a higher priority than the transmitting\r
92  * task.  The receiver will therefore wake every time a character is\r
93  * transmitted so neither the Tx or Rx queue should ever hold more than a few\r
94  * characters.\r
95  *\r
96  */\r
97 \r
98 /* Scheduler include files. */\r
99 #include <stdlib.h>\r
100 #include "FreeRTOS.h"\r
101 #include "task.h"\r
102 \r
103 /* Demo program include files. */\r
104 #include "serial.h"\r
105 #include "comtest.h"\r
106 #include "partest.h"\r
107 \r
108 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE\r
109 #define comTX_LED_OFFSET                        ( 0 )\r
110 #define comRX_LED_OFFSET                        ( 1 )\r
111 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )\r
112 \r
113 /* The Tx task will transmit the sequence of characters at a pseudo random\r
114 interval.  This is the maximum and minimum block time between sends. */\r
115 #define comTX_MAX_BLOCK_TIME            ( ( portTickType ) 0x96 )\r
116 #define comTX_MIN_BLOCK_TIME            ( ( portTickType ) 0x32 )\r
117 #define comOFFSET_TIME                          ( ( portTickType ) 3 )\r
118 \r
119 /* We should find that each character can be queued for Tx immediately and we\r
120 don't have to block to send. */\r
121 #define comNO_BLOCK                                     ( ( portTickType ) 0 )\r
122 \r
123 /* The Rx task will block on the Rx queue for a long period. */\r
124 #define comRX_BLOCK_TIME                        ( ( portTickType ) 0xffff )\r
125 \r
126 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */\r
127 #define comFIRST_BYTE                           ( 'A' )\r
128 #define comLAST_BYTE                            ( 'X' )\r
129 \r
130 #define comBUFFER_LEN                           ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )\r
131 #define comINITIAL_RX_COUNT_VALUE       ( 0 )\r
132 \r
133 /* Handle to the com port used by both tasks. */\r
134 static xComPortHandle xPort = NULL;\r
135 \r
136 /* The transmit task as described at the top of the file. */\r
137 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );\r
138 \r
139 /* The receive task as described at the top of the file. */\r
140 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );\r
141 \r
142 /* The LED that should be toggled by the Rx and Tx tasks.  The Rx task will\r
143 toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task will toggle LED\r
144 ( uxBaseLED + comTX_LED_OFFSET ). */\r
145 static unsigned portBASE_TYPE uxBaseLED = 0;\r
146 \r
147 /* Check variable used to ensure no error have occurred.  The Rx task will\r
148 increment this variable after every successfully received sequence.  If at any\r
149 time the sequence is incorrect the the variable will stop being incremented. */\r
150 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )\r
155 {\r
156         /* Initialise the com port then spawn the Rx and Tx tasks. */\r
157         uxBaseLED = uxLED;\r
158         xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );\r
159 \r
160         /* The Tx task is spawned with a lower priority than the Rx task. */\r
161         xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );\r
162         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static portTASK_FUNCTION( vComTxTask, pvParameters )\r
167 {\r
168 char cByteToSend;\r
169 portTickType xTimeToWait;\r
170 \r
171         /* Just to stop compiler warnings. */\r
172         ( void ) pvParameters;\r
173 \r
174         for( ;; )\r
175         {\r
176                 /* Simply transmit a sequence of characters from comFIRST_BYTE to\r
177                 comLAST_BYTE. */\r
178                 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )\r
179                 {\r
180                         if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )\r
181                         {\r
182                                 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
183                         }\r
184                 }\r
185 \r
186                 /* Turn the LED off while we are not doing anything. */\r
187                 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );\r
188 \r
189                 /* We have posted all the characters in the string - wait before\r
190                 re-sending.  Wait a pseudo-random time as this will provide a better\r
191                 test. */\r
192                 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
193 \r
194                 /* Make sure we don't wait too long... */\r
195                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
196 \r
197                 /* ...but we do want to wait. */\r
198                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
199                 {\r
200                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
201                 }\r
202 \r
203                 vTaskDelay( xTimeToWait );\r
204         }\r
205 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static portTASK_FUNCTION( vComRxTask, pvParameters )\r
209 {\r
210 signed char cExpectedByte, cByteRxed;\r
211 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;\r
212 \r
213         /* Just to stop compiler warnings. */\r
214         ( void ) pvParameters;\r
215 \r
216         for( ;; )\r
217         {\r
218                 /* We expect to receive the characters from comFIRST_BYTE to\r
219                 comLAST_BYTE in an incrementing order.  Loop to receive each byte. */\r
220                 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )\r
221                 {\r
222                         /* Block on the queue that contains received bytes until a byte is\r
223                         available. */\r
224                         if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )\r
225                         {\r
226                                 /* Was this the byte we were expecting?  If so, toggle the LED,\r
227                                 otherwise we are out on sync and should break out of the loop\r
228                                 until the expected character sequence is about to restart. */\r
229                                 if( cByteRxed == cExpectedByte )\r
230                                 {\r
231                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
232                                 }\r
233                                 else\r
234                                 {\r
235                                         xResyncRequired = pdTRUE;\r
236                                         break; /*lint !e960 Non-switch break allowed. */\r
237                                 }\r
238                         }\r
239                 }\r
240 \r
241                 /* Turn the LED off while we are not doing anything. */\r
242                 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );\r
243 \r
244                 /* Did we break out of the loop because the characters were received in\r
245                 an unexpected order?  If so wait here until the character sequence is\r
246                 about to restart. */\r
247                 if( xResyncRequired == pdTRUE )\r
248                 {\r
249                         while( cByteRxed != comLAST_BYTE )\r
250                         {\r
251                                 /* Block until the next char is available. */\r
252                                 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );\r
253                         }\r
254 \r
255                         /* Note that an error occurred which caused us to have to resync.\r
256                         We use this to stop incrementing the loop counter so\r
257                         sAreComTestTasksStillRunning() will return false - indicating an\r
258                         error. */\r
259                         xErrorOccurred++;\r
260 \r
261                         /* We have now resynced with the Tx task and can continue. */\r
262                         xResyncRequired = pdFALSE;\r
263                 }\r
264                 else\r
265                 {\r
266                         if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )\r
267                         {\r
268                                 /* Increment the count of successful loops.  As error\r
269                                 occurring (i.e. an unexpected character being received) will\r
270                                 prevent this counter being incremented for the rest of the\r
271                                 execution.   Don't worry about mutual exclusion on this\r
272                                 variable - it doesn't really matter as we just want it\r
273                                 to change. */\r
274                                 uxRxLoops++;\r
275                         }\r
276                 }\r
277         }\r
278 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
282 {\r
283 portBASE_TYPE xReturn;\r
284 \r
285         /* If the count of successful reception loops has not changed than at\r
286         some time an error occurred (i.e. a character was received out of sequence)\r
287         and we will return false. */\r
288         if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )\r
289         {\r
290                 xReturn = pdFALSE;\r
291         }\r
292         else\r
293         {\r
294                 xReturn = pdTRUE;\r
295         }\r
296 \r
297         /* Reset the count of successful Rx loops.  When this function is called\r
298         again we expect this to have been incremented. */\r
299         uxRxLoops = comINITIAL_RX_COUNT_VALUE;\r
300 \r
301         return xReturn;\r
302 }\r
303 \r