PX4 Firmware
PX4 Autopilot Software http://px4.io
test_uart_break.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_break.c
36  * @author Lorenz Meier <lorenz@px4.io>
37  * @author David Sidrane <david_s5@nscdg.com>
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/defines.h>
42 
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <termios.h>
52 #include <string.h>
53 
54 #include <arch/board/board.h>
55 
56 #include "tests_main.h"
57 
58 #include <math.h>
59 #include <float.h>
60 
61 int test_uart_break(int argc, char *argv[])
62 {
63  int uart2_nwrite0 = 0;
64  int uart2_nwrite1 = 0;
65  int uart2_buffer_size = 0;
66 
67  /* assuming NuttShell is on UART1 (/dev/ttyS0) */
68  int uart2 = open("/dev/ttyS1", O_RDWR | O_NONBLOCK | O_NOCTTY); //
69 
70  if (uart2 < 0) {
71  printf("ERROR opening UART2, aborting..\n");
72  return uart2;
73  }
74 
75  struct termios uart2_config;
76 
77  struct termios uart2_config_original;
78 
79  int termios_state = 0;
80 
81  int ret;
82 
83  /* let the line settle */
84 
85  usleep(100000);
86 
87  /* Read the buffer length */
88 
89  ioctl(uart2, FIONSPACE, (unsigned long)&uart2_buffer_size);
90 
91 
92 #define UART_BREAK_RUNTIME_CONF
93 #ifdef UART_BREAK_RUNTIME_CONF
94 
95  if ((termios_state = tcgetattr(uart2, &uart2_config)) < 0) {
96  printf("ERROR getting termios config for UART2: %d\n", termios_state);
97  ret = termios_state;
98  goto cleanup;
99  }
100 
101  memcpy(&uart2_config_original, &uart2_config, sizeof(struct termios));
102 
103  /* Set baud rate */
104  if (cfsetispeed(&uart2_config, B9600) < 0 || cfsetospeed(&uart2_config, B9600) < 0) {
105  printf("ERROR setting termios config for UART2: %d\n", termios_state);
106  ret = ERROR;
107  goto cleanup;
108  }
109 
110  if ((termios_state = tcsetattr(uart2, TCSANOW, &uart2_config)) < 0) {
111  printf("ERROR setting termios config for UART2\n");
112  ret = termios_state;
113  goto cleanup;
114  }
115 
116  /* let the line settle */
117 
118  usleep(100000);
119 
120  /* Signal on Console for rough timing */
121 
122  printf("1");
123  fflush(stdout);
124 
125  /* Start Break */
126 
127  ioctl(uart2, TIOCSBRK, 0);
128  usleep(250000);
129 
130  /* End Break */
131 
132  ioctl(uart2, TIOCCBRK, 0);
133 
134  /* Signal on Console for rough timing */
135 
136  printf("0");
137  fflush(stdout);
138 
139  /* let the line settle */
140 
141  usleep(100000);
142 
143 
144 #endif
145 
146  uint8_t sample_uart2[] = {'U', 'A', 'R', 'T', '2', ' ', '#', 0xff};
147 
148  int messages = uart2_buffer_size / sizeof(sample_uart2);
149  messages = messages / 2;
150  int i, r;
151 
152  for (i = 0; i < messages; i++) {
153  /* uart2 -> */
154  r = write(uart2, sample_uart2, sizeof(sample_uart2));
155 
156  if (r > 0) {
157  uart2_nwrite0 += r;
158  }
159  }
160 
161  /* Ensure we are sending */
162 
163  usleep(100000);
164 
165  /* Signal on Console for rough timing */
166 
167  printf("1");
168  fflush(stdout);
169 
170  /* Start Break */
171 
172  ioctl(uart2, TIOCSBRK, 0);
173  usleep(250000);
174 
175  /* End Break */
176 
177  ioctl(uart2, TIOCCBRK, 0);
178 
179  /* Signal on Console for rough timing */
180 
181  printf("0");
182  fflush(stdout);
183 
184  /* Begin writing again*/
185 
186  for (i = 0; i < messages; i++) {
187  /* uart2 -> */
188  r = write(uart2, sample_uart2, sizeof(sample_uart2));
189 
190  if (r > 0) {
191  uart2_nwrite1 += r;
192  }
193  }
194 
195  int left = -1;
196  int wait = 0;
197 
198  for (wait = 0; wait < 1000 && left != uart2_buffer_size; wait++) {
199  ioctl(uart2, FIONSPACE, (unsigned long)&left);
200  usleep(250000);
201  }
202 
203 #define UART_BREAK_RUNTIME_CONF
204 #ifdef UART_BREAK_RUNTIME_CONF
205 
206  /* Set back to original settings */
207  if ((termios_state = tcsetattr(uart2, TCSANOW, &uart2_config_original)) < 0) {
208  printf("ERROR setting termios config for UART2\n");
209  ret = termios_state;
210  goto cleanup;
211  }
212 
213 #endif
214  close(uart2);
215 
216  printf("uart2_buffer_size %d wait %d uart2_nwrite0 %d uart2_nwrite1 %d\n", uart2_buffer_size, wait, uart2_nwrite0,
217  uart2_nwrite1);
218 
219  return OK;
220 cleanup:
221  close(uart2);
222  return ret;
223 
224 }
Tests declaration file.
static void write(bootloader_app_shared_t *pshared)
#define OK
Definition: uavcan_main.cpp:71
int test_uart_break(int argc, char *argv[])