PX4 Firmware
PX4 Autopilot Software http://px4.io
test_uart_loopback.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_uart_loopback.c
36  * Tests the uart outputs.
37  *
38  * @author Lorenz Meier <lorenz@px4.io>
39  */
40 
41 #include <px4_platform_common/px4_config.h>
42 
43 #include <sys/types.h>
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <stdint.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 
52 #include <arch/board/board.h>
53 
54 #include "tests_main.h"
55 
56 #include <math.h>
57 #include <float.h>
58 
59 int test_uart_loopback(int argc, char *argv[])
60 {
61 
62  int uart5_nread = 0;
63  int uart2_nread = 0;
64  int uart5_nwrite = 0;
65  int uart2_nwrite = 0;
66 
67  /* opening stdout */
68  int stdout_fd = 1;
69 
70  int uart2 = open("/dev/ttyS1", O_RDWR | O_NONBLOCK | O_NOCTTY);
71 
72  if (uart2 < 0) {
73  printf("ERROR opening UART2, aborting..\n");
74  return uart2;
75  }
76 
77  int uart5 = open("/dev/ttyS2", O_RDWR | O_NONBLOCK | O_NOCTTY);
78 
79  if (uart5 < 0) {
80  if (uart2 >= 0) {
81  close(uart2);
82  }
83 
84  printf("ERROR opening UART5, aborting..\n");
85  return 1;
86  }
87 
88  uint8_t sample_stdout_fd[] = {'C', 'O', 'U', 'N', 'T', ' ', '#', '\n'};
89  uint8_t sample_uart2[] = {'C', 'O', 'U', 'N', 'T', ' ', '#', 0};
90  uint8_t sample_uart5[] = {'C', 'O', 'U', 'N', 'T', ' ', '#', 0};
91 
92  int i, r;
93 
94  for (i = 0; i < 1000; i++) {
95 // printf("TEST #%d\n",i);
96  write(stdout_fd, sample_stdout_fd, sizeof(sample_stdout_fd));
97 
98  /* uart2 -> uart5 */
99  r = write(uart2, sample_uart2, sizeof(sample_uart2));
100 
101  if (r > 0) {
102  uart2_nwrite += r;
103  }
104 
105 // printf("TEST #%d\n",i);
106  write(stdout_fd, sample_stdout_fd, sizeof(sample_stdout_fd));
107 
108  /* uart2 -> uart5 */
109  r = write(uart5, sample_uart5, sizeof(sample_uart5));
110 
111  if (r > 0) {
112  uart5_nwrite += r;
113  }
114 
115 // printf("TEST #%d\n",i);
116  write(stdout_fd, sample_stdout_fd, sizeof(sample_stdout_fd));
117 
118  /* try to read back values */
119  do {
120  r = read(uart5, sample_uart2, sizeof(sample_uart2));
121 
122  if (r > 0) {
123  uart5_nread += r;
124  }
125  } while (r > 0);
126 
127 // printf("TEST #%d\n",i);
128  write(stdout_fd, sample_stdout_fd, sizeof(sample_stdout_fd));
129 
130  do {
131  r = read(uart2, sample_uart5, sizeof(sample_uart5));
132 
133  if (r > 0) {
134  uart2_nread += r;
135  }
136  } while (r > 0);
137 
138 // printf("TEST #%d\n",i);
139 // write(stdout_fd, sample_stdout_fd, sizeof(sample_uart5));
140  }
141 
142  for (i = 0; i < 200000; i++) {
143 
144  /* try to read back values */
145  r = read(uart5, sample_uart2, sizeof(sample_uart2));
146 
147  if (r > 0) {
148  uart5_nread += r;
149  }
150 
151  r = read(uart2, sample_uart5, sizeof(sample_uart5));
152 
153  if (r > 0) {
154  uart2_nread += r;
155  }
156 
157  if ((uart2_nread == uart2_nwrite) && (uart5_nread == uart5_nwrite)) {
158  break;
159  }
160  }
161 
162 
163  close(stdout_fd);
164  close(uart2);
165  close(uart5);
166 
167  printf("uart2_nwrite %d\n", uart2_nwrite);
168  printf("uart5_nwrite %d\n", uart5_nwrite);
169  printf("uart2_nread %d\n", uart2_nread);
170  printf("uart5_nread %d\n", uart5_nread);
171 
172 
173  return 0;
174 }
int test_uart_loopback(int argc, char *argv[])
static void read(bootloader_app_shared_t *pshared)
Tests declaration file.
static void write(bootloader_app_shared_t *pshared)