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