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