Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
UART.cpp
Go to the documentation of this file.
1 /*
2  * for uart programming on linux see the following tutorial
3  * https://en.wikibooks.org/wiki/Serial_Programming/termios
4  */
5 
6 #include "com/UART.hpp"
7 
8 /* file control options */
9 #include <fcntl.h>
10 /* needed for memset */
11 #include <cstring>
12 /* for uart baud rate settings etc. */
13 #include <termios.h>
14 #include <unistd.h>
15 #include <iostream>
16 /* for size_t */
17 #include <cstddef>
18 
20 }
21 
23  /* TODO: how do we know, that we have to listen on THS1 instead of THS0 e.g.
24  * we need a reference here and also a description of how or where to find
25  * that information
26  * https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them
27  * Description: pinmux is needed by the kernel to know the HW setup
28  *
29  * THS = Tegra High Speed
30  * ls /dev/ttyS* and
31  * ls /dev/ttyTHS* are the same UART HW but different driver
32  *
33  * on agx:
34  * "The initial pinmux should set all of the these pins, except for the power,
35  * UART RX TX and two I2C busses, to GPIO at boot. The UART and I2C busses
36  * should not be used for GPIO."
37  *
38  * pinmux manually on agx:
39  * https://www.elinux.org/Jetson/AGX_Xavier_Update_Pinmux_Manually
40  *
41  * for all uart pinmuxs:
42  * sudo cat /sys/kernel/debug/tegra_pinctrl_reg
43  *
44  * Linux setserial tool:
45  * https://www.cyberciti.biz/faq/find-out-linux-serial-ports-with-setserial/
46  *
47  * Jetson AGX Header Pinout:
48  * https://www.jetsonhacks.com/nvidia-jetson-agx-xavier-gpio-header-pinout/
49  * taken from:
50  * http://developer.nvidia.com/embedded/dlc/jetson-xavier-developer-kit-carrier-board-spec
51  *
52  * Xavier Forum Links:
53  * https://devtalk.nvidia.com/default/topic/1058946/
54  *
55  * from
56  * https://devtalk.nvidia.com/default/topic/1062552/jetson-agx-xavier/accesseing-uart2-in-tegra/post/5380741/#5380741
57  * device tree determines routing of serial devices or what function a GPIO
58  * will take
59  * DT is included in BSP -> HW changes, but drivers stay the same
60  *
61  * ls -l /dev/ttyS* /dev/ttyTHS*
62  * if group name is dialout -> it is connected to some serial HW
63  *
64  * Additional Information:
65  * reverse DTB
66  * https://forum.digilentinc.com/topic/2427-how-to-decompile-dtb-file/
67  *
68  * DT reference: https://elinux.org/Device_Tree_Reference
69  *
70  * devicetree.org
71  *
72  */
73 #ifndef JETSON_DEVICE
74 #error "Please define JETSON_DEVICE variable"
75 #endif
76 #if JETSON_DEVICE == 0
77  const char* ttyPath = "/dev/ttyTHS0";
78 #else
79  const char* ttyPath = "/dev/ttyTHS1";
80 #endif
81  speed_t speed = B1000000; /* 1MBps Baudrate */
82  tty_fd = open(ttyPath, O_RDWR | O_NOCTTY | O_SYNC);
83 
84  if(tty_fd < 0) {
85  std::cout << "ERROR: Could not open tty " << *ttyPath;
86  return;
87  }
88 
89  struct termios tty;
90  memset(&tty, 0, sizeof tty);
91 
92  tty.c_iflag = 0;
93  tty.c_oflag = 0;
94  tty.c_cflag = (CS8 | CLOCAL | CREAD);
95  tty.c_lflag = 0;
96  tty.c_cc[VMIN] = 1;
97  tty.c_cc[VTIME] = 5;
98 
99  cfsetospeed(&tty, speed);
100  cfsetispeed(&tty, speed);
101 
102  tcsetattr(tty_fd, TCSANOW, &tty);
103 
104  tcflush(tty_fd, TCIOFLUSH);
105 
106  m_connected = true;
107 }
108 
110  if(tty_fd < 0) {
111  return;
112  }
113  close(tty_fd);
114 
115  m_connected = false;
116 }
117 
118 size_t UART::VSendData(void* pData, const size_t len) {
119  size_t written = write(tty_fd, pData, len);
120  return written;
121 }
122 
123 size_t UART::VReceiveData(void* pData, const size_t len) {
124  size_t bytesread = read(tty_fd, pData, len);
125  return bytesread;
126 }
127 /*vim:set et sw=2 ts=2 tw=80:*/
UART::VConnect
void VConnect() override
Definition: UART.cpp:22
UART::VSendData
size_t VSendData(void *, const size_t) override
Definition: UART.cpp:118
UART::tty_fd
int tty_fd
Definition: UART.hpp:43
UART::VDisconnect
void VDisconnect() override
Definition: UART.cpp:109
UART.hpp
ICommunication::m_connected
bool m_connected
Definition: ICommunication.h:7
UART::~UART
~UART() override
Definition: UART.cpp:19
UART::VReceiveData
size_t VReceiveData(void *, const size_t) override
Definition: UART.cpp:123