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