PX4 Firmware
PX4 Autopilot Software http://px4.io
test_uart_console.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  * Tests the uart console.
37  *
38  * @author Lorenz Meier <lorenz@px4.io>
39  * @author David Sidrane <david_s5@nscdg.com>
40  */
41 
42 #include <px4_platform_common/px4_config.h>
43 #include <px4_platform_common/tasks.h>
44 
45 #include <sys/types.h>
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 
53 #include <arch/board/board.h>
54 
55 #include "tests_main.h"
56 
57 #include <math.h>
58 #include <float.h>
59 #include <drivers/drv_hrt.h>
60 
61 static void *receive_loop(void *arg)
62 {
63  int uart_usb = open("/dev/ttyACM0", O_RDONLY | O_NOCTTY);
64 
65  while (1) {
66  char c;
67  read(uart_usb, &c, 1);
68  printf("%c", c);
69  fflush(stdout);
70  }
71 
72  return NULL;
73 }
74 
75 int test_uart_console(int argc, char *argv[])
76 {
77  /* assuming NuttShell is on UART1 (/dev/ttyS0) */
78  int uart_usb = open("/dev/ttyACM0", O_WRONLY | O_NOCTTY);
79 
80  if (uart_usb < 0) {
81  printf("ERROR opening /dev/ttyACM0. Do you need to run sercon first? Aborting..\n");
82  return uart_usb;
83  }
84 
85  uint8_t sample_uart_usb[] = {'S', 'A', 'M', 'P', 'L', 'E', ' ', '\n'};
86 
87  pthread_t receive_thread;
88 
89  pthread_create(&receive_thread, NULL, receive_loop, NULL);
90 
91  //wait for threads to complete:
92  pthread_join(receive_thread, NULL);
93 
94  for (int i = 0; i < 30; i++) {
95  write(uart_usb, sample_uart_usb, sizeof(sample_uart_usb));
96  printf(".");
97  fflush(stdout);
98  px4_sleep(1);
99  }
100 
101 // uint64_t start_time = hrt_absolute_time();
102 //
103 //// while (true)
104 // for (int i = 0; i < 1000; i++)
105 // {
106 // //write(uart_usb, sample_uart_usb, sizeof(sample_uart_usb));
107 // int nread = 0;
108 // char c;
109 // do {
110 // nread = read(uart_usb, &c, 1);
111 // if (nread > 0)
112 // {
113 // printf("%c", c);
114 // }
115 // } while (nread > 0);
116 //
117 // do {
118 // nread = read(uart_console, &c, 1);
119 // if (nread > 0)
120 // {
121 // if (c == 0x03)
122 // {
123 // close(uart_usb);
124 // close(uart_console);
125 // exit(OK);
126 // }
127 // else
128 // {
129 // write(uart_usb, &c, 1);
130 // }
131 // }
132 // } while (nread > 0);
133 // usleep(10000);
134 // }
135 //
136 // int interval = hrt_absolute_time() - start_time;
137 
138  close(uart_usb);
139 // close(uart_console);
140 
141  return 0;
142 }
High-resolution timer with callouts and timekeeping.
static void read(bootloader_app_shared_t *pshared)
Tests declaration file.
int test_uart_console(int argc, char *argv[])
static void write(bootloader_app_shared_t *pshared)
static void * receive_loop(void *arg)