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