Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
UART Class Reference

#include <UART.hpp>

Inheritance diagram for UART:
Collaboration diagram for UART:

Public Member Functions

 UART ()=default
 
 ~UART () override
 
void VConnect () override
 
void VDisconnect () override
 
size_t VSendData (void *, const size_t) override
 
size_t VReceiveData (void *, const size_t) override
 
- Public Member Functions inherited from ICommunication
virtual ~ICommunication ()
 
virtual void VInitialize ()
 
bool IsConnected () const
 

Protected Attributes

int tty_fd
 
- Protected Attributes inherited from ICommunication
bool m_connected
 

Detailed Description

Definition at line 6 of file UART.hpp.

Constructor & Destructor Documentation

◆ UART()

UART::UART ( )
default

◆ ~UART()

UART::~UART ( )
override

Definition at line 19 of file UART.cpp.

19  {
20 }

Member Function Documentation

◆ VConnect()

void UART::VConnect ( )
overridevirtual

Implements ICommunication.

Definition at line 22 of file UART.cpp.

22  {
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 }

References ICommunication::m_connected, and tty_fd.

◆ VDisconnect()

void UART::VDisconnect ( )
overridevirtual

Implements ICommunication.

Definition at line 109 of file UART.cpp.

109  {
110  if(tty_fd < 0) {
111  return;
112  }
113  close(tty_fd);
114 
115  m_connected = false;
116 }

References ICommunication::m_connected, and tty_fd.

◆ VReceiveData()

size_t UART::VReceiveData ( void *  pData,
const size_t  len 
)
overridevirtual

Implements ICommunication.

Definition at line 123 of file UART.cpp.

123  {
124  size_t bytesread = read(tty_fd, pData, len);
125  return bytesread;
126 }

References tty_fd.

◆ VSendData()

size_t UART::VSendData ( void *  pData,
const size_t  len 
)
overridevirtual

Implements ICommunication.

Definition at line 118 of file UART.cpp.

118  {
119  size_t written = write(tty_fd, pData, len);
120  return written;
121 }

References tty_fd.

Member Data Documentation

◆ tty_fd

int UART::tty_fd
protected

Definition at line 43 of file UART.hpp.

Referenced by VConnect(), VDisconnect(), VReceiveData(), and VSendData().


The documentation for this class was generated from the following files:
UART::tty_fd
int tty_fd
Definition: UART.hpp:43
ICommunication::m_connected
bool m_connected
Definition: ICommunication.h:7