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