PX4 Firmware
PX4 Autopilot Software http://px4.io
test_uart_baudchange.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_baudchange.c
36  * @author Lorenz Meier <lorenz@px4.io>
37  */
38 
39 #include <px4_platform_common/px4_config.h>
40 #include <px4_platform_common/defines.h>
41 
42 #include <sys/types.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <termios.h>
50 #include <string.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_baudchange(int argc, char *argv[])
60 {
61  int uart2_nwrite = 0;
62 
63  /* assuming NuttShell is on UART1 (/dev/ttyS0) */
64  int uart2 = open("/dev/ttyS2", O_RDWR | O_NONBLOCK | O_NOCTTY); //
65 
66  if (uart2 < 0) {
67  printf("ERROR opening UART2, aborting..\n");
68  return uart2;
69  }
70 
71  struct termios uart2_config;
72 
73  struct termios uart2_config_original;
74 
75  int termios_state = 0;
76 
77  int ret;
78 
79  if ((termios_state = tcgetattr(uart2, &uart2_config)) < 0) {
80  printf("ERROR getting termios config for UART2: %d\n", termios_state);
81  ret = termios_state;
82  goto cleanup;
83  }
84 
85  if ((termios_state = tcgetattr(uart2, &uart2_config_original)) < 0) {
86  printf("ERROR getting termios config for UART2: %d\n", termios_state);
87  ret = termios_state;
88  goto cleanup;
89  }
90 
91  /* Set baud rate */
92  if (cfsetispeed(&uart2_config, B9600) < 0 || cfsetospeed(&uart2_config, B9600) < 0) {
93  printf("ERROR setting termios config for UART2: %d\n", termios_state);
94  ret = ERROR;
95  goto cleanup;
96  }
97 
98  if ((termios_state = tcsetattr(uart2, TCSANOW, &uart2_config)) < 0) {
99  printf("ERROR setting termios config for UART2\n");
100  ret = termios_state;
101  goto cleanup;
102  }
103 
104  /* Set back to original settings */
105  if ((termios_state = tcsetattr(uart2, TCSANOW, &uart2_config_original)) < 0) {
106  printf("ERROR setting termios config for UART2\n");
107  ret = termios_state;
108  goto cleanup;
109  }
110 
111  uint8_t sample_uart2[] = {'U', 'A', 'R', 'T', '2', ' ', '#', 0, '\n'};
112 
113  int i, r;
114 
115  for (i = 0; i < 100; i++) {
116  /* uart2 -> */
117  r = write(uart2, sample_uart2, sizeof(sample_uart2));
118 
119  if (r > 0) {
120  uart2_nwrite += r;
121  }
122  }
123 
124  close(uart2);
125 
126  printf("uart2_nwrite %d\n", uart2_nwrite);
127 
128  return OK;
129 cleanup:
130  close(uart2);
131  return ret;
132 
133 }
Tests declaration file.
static void write(bootloader_app_shared_t *pshared)
int test_uart_baudchange(int argc, char *argv[])
#define OK
Definition: uavcan_main.cpp:71