]> begriffs open source - cmsis-driver-validation/blob - Source/DV_USART.c
Configuration split per component and reworked SPI driver validation
[cmsis-driver-validation] / Source / DV_USART.c
1 /*
2  * Copyright (c) 2015-2020 Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the License); you may
7  * not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * -----------------------------------------------------------------------------
19  *
20  * Project:     CMSIS-Driver Validation
21  * Title:       Universal Synchronous Asynchronous Receiver/Transmitter (USART) 
22  *              Driver Validation tests
23  *
24  * -----------------------------------------------------------------------------
25  */
26
27
28 #include "cmsis_dv.h" 
29 #include "DV_USART_Config.h"
30 #include "DV_Framework.h"
31 #include "Driver_USART.h"
32 #include <stdio.h>
33 #include <stdlib.h> 
34 #include <string.h> 
35
36 // USART buffer type
37 #if (USART_DATA_BITS>8U)
38 typedef uint16_t buf_t;
39 #else
40 typedef uint8_t buf_t;
41 #endif
42
43 // USART buffer pointers
44 static buf_t *buffer_out; 
45 static buf_t *buffer_in; 
46
47 // USART data bits
48 #if   (USART_DATA_BITS==5U)
49 #define USART_DATA_BITS_CTRL_CODE ARM_USART_DATA_BITS_5
50 #elif (USART_DATA_BITS==6U)
51 #define USART_DATA_BITS_CTRL_CODE ARM_USART_DATA_BITS_6
52 #elif (USART_DATA_BITS==7U)
53 #define USART_DATA_BITS_CTRL_CODE ARM_USART_DATA_BITS_7
54 #elif (USART_DATA_BITS==8U)
55 #define USART_DATA_BITS_CTRL_CODE ARM_USART_DATA_BITS_8
56 #elif (USART_DATA_BITS==9U)
57 #define USART_DATA_BITS_CTRL_CODE ARM_USART_DATA_BITS_9
58 #endif
59
60 // USART baudrates
61 static const uint32_t USART_BR[] = {
62 #if (USART_BR_1>0)
63   USART_BR_1,
64 #endif  
65 #if (USART_BR_2>0)
66   USART_BR_2,
67 #endif 
68 #if (USART_BR_3>0)
69   USART_BR_3,
70 #endif 
71 #if (USART_BR_4>0)
72   USART_BR_4,
73 #endif 
74 #if (USART_BR_5>0)
75   USART_BR_5,
76 #endif 
77 #if (USART_BR_6>0)
78   USART_BR_6,
79 #endif 
80 };
81 static const uint32_t USART_BR_NUM = ARRAY_SIZE(USART_BR);
82
83 // Register Driver_USART#
84 extern ARM_DRIVER_USART CREATE_SYMBOL(Driver_USART, DRV_USART);
85 static ARM_DRIVER_USART *drv = &CREATE_SYMBOL(Driver_USART, DRV_USART);
86 static ARM_USART_CAPABILITIES capab;   
87
88 static char str[128];
89
90 // Event flags
91 static uint8_t volatile Event;  
92
93 // USART event
94 static void USART_DrvEvent (uint32_t event) {
95   Event |= event;
96 }
97
98 // USART asynchronous transfer
99 int8_t USART_RunTransfer (void *out, void *in, uint32_t cnt);
100 int8_t USART_RunTransfer (void *out, void *in, uint32_t cnt) {
101   uint32_t tick;
102   
103   Event &= ~ARM_USART_EVENT_RECEIVE_COMPLETE;
104       
105   drv->Receive (in, cnt);
106   drv->Send (out, cnt);
107     
108   tick = GET_SYSTICK();
109   do {
110     if (Event & ARM_USART_EVENT_RECEIVE_COMPLETE) {
111       return 0;
112     }
113   }
114   while ((GET_SYSTICK() - tick) < SYSTICK_MICROSEC(USART_TRANSFER_TIMEOUT));
115
116   drv->Control(ARM_USART_ABORT_TRANSFER, 0);
117   return -1;
118 }
119
120 // USART send with callback
121 int8_t USART_RunSend (void *out, uint32_t cnt);
122 int8_t USART_RunSend (void *out, uint32_t cnt) {
123   uint32_t tick;
124   
125   Event &= ~ARM_USART_EVENT_SEND_COMPLETE;
126       
127   drv->Send (out, cnt);
128     
129   tick = GET_SYSTICK();
130   do {
131     if (Event & ARM_USART_EVENT_SEND_COMPLETE) {
132       return 0;
133     }
134   }
135   while ((GET_SYSTICK() - tick) < SYSTICK_MICROSEC(USART_TRANSFER_TIMEOUT));
136
137   drv->Control(ARM_USART_ABORT_SEND, 0);
138   return -1;
139 }
140
141 // USART send without callback
142 int8_t USART_RunSend_NoCallback (void *out, uint32_t cnt);
143 int8_t USART_RunSend_NoCallback (void *out, uint32_t cnt) {
144   uint32_t tick;
145         
146   drv->Send (out, cnt);
147     
148   tick = GET_SYSTICK();
149   do {
150     if (drv->GetTxCount() == cnt) {
151       return 0;
152     }
153   }
154   while ((GET_SYSTICK() - tick) < SYSTICK_MICROSEC(USART_TRANSFER_TIMEOUT));
155
156   drv->Control(ARM_USART_ABORT_SEND, 0);
157   return -1;
158 }
159
160 // USART receive with callback
161 int8_t USART_RunReceive (void *out, void *in, uint32_t cnt);
162 int8_t USART_RunReceive (void *out, void *in, uint32_t cnt) {
163   uint32_t tick;
164   
165   Event &= ~ARM_USART_EVENT_RECEIVE_COMPLETE;
166       
167   drv->Receive (in,  cnt);
168   drv->Send    (out, cnt);              // Send data that will be received with loopback
169
170   tick = GET_SYSTICK();
171   do {
172     if (Event & ARM_USART_EVENT_RECEIVE_COMPLETE) {
173       return 0;
174     }
175   }
176   while ((GET_SYSTICK() - tick) < SYSTICK_MICROSEC(USART_TRANSFER_TIMEOUT));
177
178   drv->Control(ARM_USART_ABORT_RECEIVE, 0);
179   return -1;
180 }
181
182 // USART receive without callback
183 int8_t USART_RunReceive_NoCallback (void *out, void *in, uint32_t cnt);
184 int8_t USART_RunReceive_NoCallback (void *out, void *in, uint32_t cnt) {
185   uint32_t tick;
186         
187   drv->Receive (in,  cnt);
188   drv->Send    (out, cnt);              // Send data that will be received with loopback
189
190   tick = GET_SYSTICK();
191   do {
192     if (drv->GetRxCount() == cnt) {
193       return 0;
194     }
195   }
196   while ((GET_SYSTICK() - tick) < SYSTICK_MICROSEC(USART_TRANSFER_TIMEOUT));
197
198   drv->Control(ARM_USART_ABORT_RECEIVE, 0);
199   return -1;
200 }
201
202
203 /*-----------------------------------------------------------------------------
204  *      Tests
205  *----------------------------------------------------------------------------*/
206
207  
208 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
209 /**
210 \defgroup dv_usart USART Validation
211 \brief USART driver validation
212 \details
213 The USART validation test performs the following checks:
214 - API interface compliance.
215 - Data communication with various transfer sizes and communication parameters.
216 - Transfer speed of the data communication.
217 - Loopback communication.
218
219 \anchor usart_loop_back_setup
220 Loopback Communication Setup
221 ----------------------------
222
223 To perform loopback communication tests, it is required to connect the USART's \b TX signal to the \b RX signal (refer to the
224 schematics of your target hardware for detailed pinout information).
225
226 \defgroup usart_tests Tests
227 \ingroup dv_usart
228
229 @{
230 */
231
232 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
233 /**
234 \brief Function: USART_GetCapabilities
235 \details
236 The test function \b USART_GetCapabilities verifies the function \b GetCapabilities.
237 */
238 void USART_GetCapabilities (void) {
239   /* Get USART capabilities */
240   capab = drv->GetCapabilities();
241   TEST_ASSERT(&capab != NULL); 
242 }
243
244 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
245 /**
246 \brief  Function: USART_Initialization
247 \details
248 The test function \b USART_Initialization verifies the USART functions with the sequence:
249   - \b Initialize without callback
250   - \b Uninitialize
251   - \b Initialize with callback
252   - \b Uninitialize
253 */
254 void USART_Initialization (void) {
255   
256   /* Initialize without callback */
257   TEST_ASSERT(drv->Initialize(NULL) == ARM_DRIVER_OK); 
258     
259   /* Uninitialize */
260   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
261   
262   /* Initialize with callback */
263   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
264
265   /* Uninitialize */
266   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
267 }
268
269 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
270 /**
271 \brief  Function: USART_CheckInvalidInit
272 \details
273 The test function \b USART_CheckInvalidInit verifies the driver behaviour when receiving an invalid initialization sequence:
274   - \b Uninitialize
275   - \b PowerControl with Power off
276   - \b PowerControl with Power on
277   - \b Control 
278   - \b PowerControl with Power off
279   - \b Uninitialize
280 */
281 void USART_CheckInvalidInit (void) {
282   
283   /* Uninitialize */
284   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
285   
286   /* Power off */
287   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
288   
289   /* Try to power on */
290   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) != ARM_DRIVER_OK); 
291   
292   /* Try to set configuration */
293   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
294     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) != ARM_DRIVER_OK);
295   
296   /* Power off */
297   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
298  
299   /* Uninitialize */
300   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
301 }
302
303 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
304 /**
305 \brief  Function: USART_PowerControl
306 \details
307 The test function \b USART_PowerControl verifies the \b PowerControl function with the sequence:
308  - Initialize with callback
309  - Power on
310  - Power low
311  - Power off
312  - Uninitialize
313 */
314 void USART_PowerControl (void) {
315   int32_t val;
316   
317   /* Initialize with callback */
318   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
319   
320   /* Power on */
321   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK);  
322     
323   /* Power low */
324   val = drv->PowerControl (ARM_POWER_LOW);
325   if (val == ARM_DRIVER_ERROR_UNSUPPORTED) { TEST_MESSAGE("[WARNING] Low power is not supported"); }
326   else { TEST_ASSERT(val == ARM_DRIVER_OK); }
327    
328   /* Power off */
329   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
330   
331   /* Uninitialize */
332   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
333 }  
334
335 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
336 /**
337 \brief  Function: USART_Config_PolarityPhase
338 \details
339 The test function \b USART_Config_PolarityPhase verifies the \b Control function with the sequence:
340  - Initialize with callback
341  - Power on
342  - Set basic SPI bus configuration
343  - Change polarity
344  - Change phase 
345  - Change polarity and phase
346  - Power off
347  - Uninitialize
348 */
349 void USART_Config_PolarityPhase (void) { 
350   
351   /* Initialize with callback and power on */
352   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
353   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK);  
354   
355   /* Set basic SPI bus configuration*/
356   TEST_ASSERT(drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_CPOL0 | ARM_USART_CPHA0, USART_BR[0]) == ARM_DRIVER_OK);
357   
358   /* Change polarity */
359   TEST_ASSERT(drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_CPOL1 | ARM_USART_CPHA0, USART_BR[0]) == ARM_DRIVER_OK);
360   
361   /* Change phase */
362   TEST_ASSERT(drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_CPOL0 | ARM_USART_CPHA1, USART_BR[0]) == ARM_DRIVER_OK);
363   
364   /* Change polarity and phase */
365   TEST_ASSERT(drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_CPOL1 | ARM_USART_CPHA1, USART_BR[0]) == ARM_DRIVER_OK);  
366   
367   /* Power off and uninitialize */
368   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
369   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
370 }
371   
372 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
373 /**
374 \brief  Function: USART_Config_DataBits
375 \details
376 The test function \b USART_Config_DataBits verifies the \b Control function with the sequence:
377  - Initialize with callback
378  - Power on
379  - Data bits = \token{5}
380  - Data bits = \token{6}
381  - Data bits = \token{7}
382  - Data bits = \token{8}
383  - Data bits = \token{9}
384  - Power off
385  - Uninitialize
386 */
387 void USART_Config_DataBits (void) { 
388   int32_t val;  
389   
390   /* Initialize with callback and power on */
391   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
392   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
393   
394   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_5, USART_BR[0]);
395   if (val == ARM_USART_ERROR_DATA_BITS) { TEST_MESSAGE("[WARNING] Data Bits = 5 are not supported"); }
396   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
397   
398   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_6, USART_BR[0]);
399   if (val == ARM_USART_ERROR_DATA_BITS) { TEST_MESSAGE("[WARNING] Data Bits = 6 are not supported"); }
400   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
401   
402   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_7, USART_BR[0]);
403   if (val == ARM_USART_ERROR_DATA_BITS) { TEST_MESSAGE("[WARNING] Data Bits = 7 are not supported"); }
404   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
405   
406   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8, USART_BR[0]);
407   if (val == ARM_USART_ERROR_DATA_BITS) { TEST_MESSAGE("[WARNING] Data Bits = 8 are not supported"); }
408   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
409   
410   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_9, USART_BR[0]);
411   if (val == ARM_USART_ERROR_DATA_BITS) { TEST_MESSAGE("[WARNING] Data Bits = 9 are not supported"); }
412   else { TEST_ASSERT(val == ARM_DRIVER_OK); }  
413   
414   /* Power off and uninitialize */
415   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
416   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK); 
417 }
418
419 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
420 /**
421 \brief  Function: USART_Config_StopBits
422 \details
423 The test function \b USART_Config_StopBits verifies the \b Control function with the sequence:
424  - Initialize with callback
425  - Power on
426  - Stop bits = \token{1}
427  - Stop bits = \token{2}
428  - Stop bits = \token{1.5}
429  - Stop bits = \token{0.5}
430  - Power off
431  - Uninitialize
432 */
433 void USART_Config_StopBits (void) { 
434   int32_t val;  
435   
436   /* Initialize with callback and power on */
437   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
438   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
439   
440   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_STOP_BITS_1, USART_BR[0]);
441   if (val == ARM_USART_ERROR_STOP_BITS) { TEST_MESSAGE("[WARNING] Stop Bits = 1 are not supported"); }
442   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
443   
444   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_STOP_BITS_2, USART_BR[0]);
445   if (val == ARM_USART_ERROR_STOP_BITS) { TEST_MESSAGE("[WARNING] Stop Bits = 2 are not supported"); }
446   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
447   
448   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_STOP_BITS_1_5, USART_BR[0]);
449   if (val == ARM_USART_ERROR_STOP_BITS) { TEST_MESSAGE("[WARNING] Stop Bits = 1.5 are not supported"); }
450   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
451   
452   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_STOP_BITS_0_5, USART_BR[0]);
453   if (val == ARM_USART_ERROR_STOP_BITS) { TEST_MESSAGE("[WARNING] Stop Bits = 0.5 are not supported"); }
454   else { TEST_ASSERT(val == ARM_DRIVER_OK); }   
455   
456   /* Power off and uninitialize */
457   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
458   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
459 }
460
461 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
462 /**
463 \brief  Function: USART_Config_Parity
464 \details
465 The test function \b USART_Config_Parity verifies the \b Control function with the sequence:
466  - Initialize with callback
467  - Power on
468  - Sets parity bits: even parity
469  - Sets parity bits: no parity
470  - Sets parity bits: odd parity
471  - Power off
472  - Uninitialize
473 */
474 void USART_Config_Parity (void) { 
475   int32_t val;  
476   
477   /* Initialize with callback and power on */
478   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
479   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
480   
481   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_PARITY_EVEN, USART_BR[0]);
482   if (val == ARM_USART_ERROR_PARITY) { TEST_MESSAGE("[WARNING] Even parity is not supported"); }
483   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
484   
485   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_PARITY_NONE, USART_BR[0]);
486   if (val == ARM_USART_ERROR_PARITY) { TEST_MESSAGE("[WARNING] No parity is not supported"); }
487   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
488   
489   val = drv->Control(ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_PARITY_ODD, USART_BR[0]);
490   if (val == ARM_USART_ERROR_PARITY) { TEST_MESSAGE("[WARNING] Odd parity not supported"); }
491   else { TEST_ASSERT(val == ARM_DRIVER_OK); } 
492   
493   /* Power off and uninitialize */
494   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
495   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
496 }
497
498 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
499 /**
500 \brief  Function: USART_Config_Baudrate
501 \details
502 The test function \b USART_Config_Baudrate verifies the \b Control function and configures various baudrates with the sequence:
503  - Initialize with callback
504  - Power on
505  - Change bus speed 
506  - Power off
507  - Uninitialize
508  
509 \note This test needs to pass to be able to transfer data via the USART correctly. Usually, USART communication is set to a
510 certain baudrate with a defined tolerance. If the driver is not able to set the baudrate correctly, data exchange will not be
511 possible.
512 */
513 void USART_Config_Baudrate (void) { 
514   uint32_t speed;  
515
516   /* Initialize with callback and power on */
517   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
518   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK);   
519   
520   /* Change baud rate*/
521   for (speed=0; speed<USART_BR_NUM; speed++) {
522     
523     TEST_ASSERT (drv->Control(ARM_USART_MODE_ASYNCHRONOUS, USART_BR[speed]) 
524        == ARM_DRIVER_OK);   
525   }
526   
527   /* Power off and uninitialize */
528   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
529   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
530 }
531
532 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
533 /**
534 \brief  Function: USART_Loopback_CheckBaudrate
535 \details
536 The test function \b USART_Loopback_CheckBaudrate verifies the \b Control function, configures various baudrates, and measures
537 the transfer time with this sequence:
538
539  - Initialize with callback
540  - Power on
541  - Change baud rate with specific control parameter
542  - Measure transfer time
543  - Power off
544  - Uninitialize
545 */
546 void USART_Loopback_CheckBaudrate (void) { 
547   uint16_t cnt;
548   uint32_t speed;          
549   uint32_t ticks_measured;
550   uint32_t ticks_expected;  
551   double rate;
552   
553   /* Allocate buffer */
554   buffer_out = malloc(BUFFER_SIZE_BR*sizeof(buf_t));
555   TEST_ASSERT(buffer_out != NULL);
556   buffer_in = malloc(BUFFER_SIZE_BR*sizeof(buf_t));
557   TEST_ASSERT(buffer_in != NULL);
558   
559   /* Initialize with callback, power on and configure USART bus */
560   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
561   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
562   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
563     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) == ARM_DRIVER_OK); 
564   
565   /* Test baudrates */
566   for (speed=0; speed<USART_BR_NUM; speed++) {
567     
568     /* Set output buffer with random data*/
569     srand(GET_SYSTICK());
570     for (cnt = 0; cnt<BUFFER_SIZE_BR; cnt++) {  
571       buffer_out[cnt] = (buf_t)rand()&((1U<<USART_DATA_BITS)-1U);
572     }  
573     
574     /* Change baud rate*/
575     TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,0) == ARM_DRIVER_OK);
576     TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,0) == ARM_DRIVER_OK);
577     TEST_ASSERT(drv->Control(ARM_USART_MODE_ASYNCHRONOUS, USART_BR[speed]) 
578        == ARM_DRIVER_OK);       
579     TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,1) == ARM_DRIVER_OK);
580     TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,1) == ARM_DRIVER_OK);
581         
582     /* Set Local Loopback */
583     USART_LOCAL_LOOPBACK(); 
584         
585     /* Measure transfer time */
586     ticks_measured = GET_SYSTICK();
587     USART_RunTransfer(buffer_out, buffer_in, BUFFER_SIZE_BR);
588     ticks_measured = GET_SYSTICK() - ticks_measured;  
589     ticks_expected = SYSTICK_MICROSEC(BUFFER_SIZE_BR*10000/(USART_BR[speed]/100));
590     ticks_expected *= USART_DATA_BITS+2;
591     
592     rate = (double)ticks_measured/ticks_expected;
593        
594     if ((rate>(1.0+(double)TOLERANCE_BR/100))||(rate<(1.0-(double)TOLERANCE_BR/100))) {
595       snprintf(str,sizeof(str),"[WARNING] At %dbps: measured time is %f x expected time", USART_BR[speed], rate);
596       TEST_MESSAGE(str);
597     } else TEST_PASS();     
598     
599     /* Check received data against sent data */
600     if (memcmp(buffer_in, buffer_out, BUFFER_SIZE_BR)!=0) {
601       snprintf(str,sizeof(str),"[FAILED] At %dbps: fail to check block of %d bytes", USART_BR[speed], BUFFER_SIZE_BR);
602       TEST_FAIL_MESSAGE(str);
603     } else TEST_PASS();  
604   }
605   
606   /* Power off and uninitialize */
607   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
608   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
609   
610   /* Free buffer */
611   free(buffer_out);   
612   free(buffer_in); 
613 }
614
615 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
616 /**
617 \brief  Function: USART_Config_CommonParams
618 \details
619 The test function \b USART_Config_CommonParams verifies the \b Control function with the sequence:
620  - Initialize with callback
621  - Power on
622  - Configure USART bus
623  - Set transmitter
624  - Set receiver
625  - Power off
626  - Uninitialize
627 */
628 void USART_Config_CommonParams (void) {
629   
630   /* Initialize with callback and power on */
631   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
632   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
633   
634   /* Configure USART bus*/
635   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
636     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) == ARM_DRIVER_OK); 
637   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,1) == ARM_DRIVER_OK); 
638   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,1) == ARM_DRIVER_OK); 
639   
640   /* Power off and uninitialize */
641   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
642   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
643 }
644
645 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
646 /**
647 \brief  Function: USART_Send
648 \details
649 The test function \b USART_Send verifies the \b Send function with the sequence:
650  - Initialize with callback
651  - Power on
652  - Send data using callback
653  - Send data without callback
654  - Power off
655  - Uninitialize
656 */
657 void USART_Send (void) { 
658   uint16_t cnt;
659   
660   /* Allocate buffer */
661   buffer_out = malloc(BUFFER[BUFFER_NUM-1]*sizeof(buf_t));
662   TEST_ASSERT(buffer_out != NULL);
663   
664   /* Initialize with callback, power on and configure USART bus */
665   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
666   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
667   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
668     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) == ARM_DRIVER_OK); 
669   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,1) == ARM_DRIVER_OK); 
670   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,1) == ARM_DRIVER_OK); 
671   
672   /* Send data chunks */
673   for (cnt = 0; cnt<BUFFER_NUM; cnt++) { 
674     
675     /* Send using callback */
676     if (USART_RunSend(buffer_out, BUFFER[cnt]) != ARM_DRIVER_OK) {
677       snprintf(str,sizeof(str),"[FAILED] Fail to send %d bytes",BUFFER[cnt]);
678       TEST_FAIL_MESSAGE(str);
679     } else TEST_PASS();   
680     
681     /* Send without callback */
682     if (USART_RunSend_NoCallback(buffer_out, BUFFER[cnt]) != ARM_DRIVER_OK) {   
683       snprintf(str,sizeof(str),"[FAILED] Fail to send without callback %d bytes",BUFFER[cnt]);
684       TEST_FAIL_MESSAGE(str);
685     } else TEST_PASS();    
686   } 
687   
688   /* Power off and uninitialize */
689   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
690   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
691   
692   /* Free buffer */
693   free(buffer_out);    
694 }
695
696 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
697 /**
698 \brief  Function: USART_AsynchronousReceive
699 \details
700 The test function \b USART_AsynchronousReceive verifies the \b Receive function with the sequence:
701  - Initialize with callback
702  - Power on
703  - Send data using callback
704  - Send data without callback
705  - Power off
706  - Uninitialize
707 */
708 void USART_AsynchronousReceive (void) { 
709   uint16_t cnt;
710   
711   /* Allocate buffer */
712   buffer_in  = malloc(BUFFER[BUFFER_NUM-1]*sizeof(buf_t));
713   buffer_out = malloc(BUFFER[BUFFER_NUM-1]*sizeof(buf_t));
714   TEST_ASSERT(buffer_in  != NULL);
715   TEST_ASSERT(buffer_out != NULL);
716   
717   /* Initialize with callback, power on and configure USART bus */
718   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
719   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
720   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
721     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) == ARM_DRIVER_OK); 
722   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,1) == ARM_DRIVER_OK); 
723   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,1) == ARM_DRIVER_OK); 
724   
725   /* Receive data chunks */
726   for (cnt = 0; cnt<BUFFER_NUM; cnt++) { 
727     
728     /* Receive using callback */
729     if (USART_RunReceive(buffer_out, buffer_in, BUFFER[cnt]) != ARM_DRIVER_OK) {
730       snprintf(str,sizeof(str),"[FAILED] Fail to receive %d bytes",BUFFER[cnt]);
731       TEST_FAIL_MESSAGE(str);
732     } else TEST_PASS();   
733     
734     /* Receive without callback */
735     if (USART_RunReceive_NoCallback(buffer_out, buffer_in, BUFFER[cnt]) != ARM_DRIVER_OK) {   
736       snprintf(str,sizeof(str),"[FAILED] Fail to send without callback %d bytes",BUFFER[cnt]);
737       TEST_FAIL_MESSAGE(str);
738     } else TEST_PASS();    
739   } 
740   
741   /* Power off and uninitialize */
742   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
743   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
744   
745   /* Free buffer */
746   free(buffer_in);    
747 }
748
749 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
750 /**
751 \brief  Function: USART_Loopback_Transfer
752 \details
753 The test function \b USART_Loopback_Transfer verifies the \b Transfer function with the sequence:
754  - Initialize with callback
755  - Power on
756  - Clear input buffer
757  - Transfer data
758  - Check received data against sent data 
759  - Power off
760  - Uninitialize
761 */
762 void USART_Loopback_Transfer (void) { 
763   uint16_t cnt, i;
764   uint8_t pattern[] = BUFFER_PATTERN;
765   
766   /* Allocate buffer */
767   buffer_out = malloc(BUFFER[BUFFER_NUM-1]*sizeof(buf_t));
768   TEST_ASSERT(buffer_out != NULL);
769   buffer_in = malloc(BUFFER[BUFFER_NUM-1]*sizeof(buf_t));
770   TEST_ASSERT(buffer_in != NULL);
771   
772   /* Initialize with callback, power on and configure USART bus */
773   TEST_ASSERT(drv->Initialize(USART_DrvEvent) == ARM_DRIVER_OK); 
774   TEST_ASSERT(drv->PowerControl (ARM_POWER_FULL) == ARM_DRIVER_OK); 
775   TEST_ASSERT(drv->Control (ARM_USART_MODE_ASYNCHRONOUS | USART_DATA_BITS_CTRL_CODE | ARM_USART_PARITY_NONE | 
776     ARM_USART_STOP_BITS_1 | ARM_USART_FLOW_CONTROL_NONE, USART_BR[0]) == ARM_DRIVER_OK); 
777   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_TX,1) == ARM_DRIVER_OK); 
778   TEST_ASSERT(drv->Control (ARM_USART_CONTROL_RX,1) == ARM_DRIVER_OK); 
779   
780   /* Set Local Loopback */
781   USART_LOCAL_LOOPBACK();  
782                                                 
783   /* Set output buffer pattern*/
784   for (cnt = 0; cnt<BUFFER[BUFFER_NUM-1];) {  
785     for (i = 0; i<ARRAY_SIZE(pattern);) {
786       buffer_out[cnt] = pattern[i++];
787       if (USART_DATA_BITS>8U) buffer_out[cnt] |= pattern[i++]<<8U;
788       buffer_out[cnt++] &= (1U<<USART_DATA_BITS)-1U;  
789     }
790   } 
791
792   /* Transfer data chunks */
793   for (cnt = 0; cnt<BUFFER_NUM; cnt++) {      
794     /* Clear input buffer*/
795     memset(buffer_in,0,BUFFER[cnt]);    
796     if (USART_RunTransfer(buffer_out, buffer_in, BUFFER[cnt]) != ARM_DRIVER_OK) {
797       snprintf(str,sizeof(str),"[FAILED] Fail to transfer block of %d bytes",BUFFER[cnt]);
798       TEST_FAIL_MESSAGE(str);
799     } else TEST_PASS();     
800     if (memcmp(buffer_in, buffer_out, BUFFER[cnt])!=0) {
801       snprintf(str,sizeof(str),"[FAILED] Fail to check block of %d bytes",BUFFER[cnt]);
802       TEST_FAIL_MESSAGE(str);
803     } else TEST_PASS();     
804   } 
805   
806   /* Set output buffer with random data*/
807   srand(GET_SYSTICK());
808   for (cnt = 0; cnt<BUFFER[BUFFER_NUM-1]; cnt++) {  
809     buffer_out[cnt] = (buf_t)rand()&((1U<<USART_DATA_BITS)-1U);
810   }   
811
812   /* Transfer data chunks */
813   for (cnt = 0; cnt<BUFFER_NUM; cnt++) {      
814     /* Clear input buffer*/
815     memset(buffer_in,0,BUFFER[cnt]);    
816     if (USART_RunTransfer(buffer_out, buffer_in, BUFFER[cnt]) != ARM_DRIVER_OK) {
817       snprintf(str,sizeof(str),"[FAILED] Fail to transfer block of %d bytes",BUFFER[cnt]);
818       TEST_FAIL_MESSAGE(str);
819     } else TEST_PASS();     
820     if (memcmp(buffer_in, buffer_out, BUFFER[cnt])!=0) {
821       snprintf(str,sizeof(str),"[FAILED] Fail to check block of %d bytes",BUFFER[cnt]);
822       TEST_FAIL_MESSAGE(str);
823     } else TEST_PASS();     
824   } 
825   
826   /* Power off and uninitialize */
827   TEST_ASSERT(drv->PowerControl (ARM_POWER_OFF) == ARM_DRIVER_OK);
828   TEST_ASSERT(drv->Uninitialize() == ARM_DRIVER_OK);
829   
830   /* Free buffer */
831   free(buffer_out);   
832   free(buffer_in);     
833 }
834   
835 /**
836 @}
837 */ 
838 // end of group dv_usart
839
840   
841