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