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