Clean up the I2C code.
This commit is contained in:
parent
d16a06db07
commit
03682b000a
|
@ -44,6 +44,8 @@ Time=24
|
||||||
# Port=/dev/ttyACM0
|
# Port=/dev/ttyACM0
|
||||||
# Port=/dev/ttyAMA0
|
# Port=/dev/ttyAMA0
|
||||||
Port=\\.\COM3
|
Port=\\.\COM3
|
||||||
|
Protocol=uart
|
||||||
|
# Address=0x22
|
||||||
TXInvert=1
|
TXInvert=1
|
||||||
RXInvert=0
|
RXInvert=0
|
||||||
PTTInvert=0
|
PTTInvert=0
|
||||||
|
|
|
@ -1076,10 +1076,9 @@ bool CMMDVMHost::createModem()
|
||||||
|
|
||||||
LogInfo("Modem Parameters");
|
LogInfo("Modem Parameters");
|
||||||
LogInfo(" Port: %s", port.c_str());
|
LogInfo(" Port: %s", port.c_str());
|
||||||
if (protocol == "i2c"){
|
|
||||||
LogInfo(" Protocol: %s", protocol.c_str());
|
LogInfo(" Protocol: %s", protocol.c_str());
|
||||||
LogInfo(" i2c Address: %u", address);
|
if (protocol == "i2c")
|
||||||
}
|
LogInfo(" i2c Address: %02X", address);
|
||||||
LogInfo(" RX Invert: %s", rxInvert ? "yes" : "no");
|
LogInfo(" RX Invert: %s", rxInvert ? "yes" : "no");
|
||||||
LogInfo(" TX Invert: %s", txInvert ? "yes" : "no");
|
LogInfo(" TX Invert: %s", txInvert ? "yes" : "no");
|
||||||
LogInfo(" PTT Invert: %s", pttInvert ? "yes" : "no");
|
LogInfo(" PTT Invert: %s", pttInvert ? "yes" : "no");
|
||||||
|
|
|
@ -94,8 +94,6 @@ const unsigned int BUFFER_LENGTH = 2000U;
|
||||||
|
|
||||||
CModem::CModem(const std::string& port, const std::string& protocol, unsigned int address, bool duplex, bool rxInvert, bool txInvert, bool pttInvert, unsigned int txDelay, unsigned int dmrDelay, bool trace, bool debug) :
|
CModem::CModem(const std::string& port, const std::string& protocol, unsigned int address, bool duplex, bool rxInvert, bool txInvert, bool pttInvert, unsigned int txDelay, unsigned int dmrDelay, bool trace, bool debug) :
|
||||||
m_port(port),
|
m_port(port),
|
||||||
m_protocol(protocol),
|
|
||||||
m_address(address),
|
|
||||||
m_dmrColorCode(0U),
|
m_dmrColorCode(0U),
|
||||||
m_ysfLoDev(false),
|
m_ysfLoDev(false),
|
||||||
m_ysfTXHang(4U),
|
m_ysfTXHang(4U),
|
||||||
|
|
2
Modem.h
2
Modem.h
|
@ -96,8 +96,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_port;
|
std::string m_port;
|
||||||
std::string m_protocol;
|
|
||||||
unsigned int m_address;
|
|
||||||
unsigned int m_dmrColorCode;
|
unsigned int m_dmrColorCode;
|
||||||
bool m_ysfLoDev;
|
bool m_ysfLoDev;
|
||||||
unsigned int m_ysfTXHang;
|
unsigned int m_ysfTXHang;
|
||||||
|
|
|
@ -44,6 +44,8 @@
|
||||||
CSerialController::CSerialController(const std::string& device, SERIAL_SPEED speed, const std::string& protocol, unsigned int address, bool assertRTS) :
|
CSerialController::CSerialController(const std::string& device, SERIAL_SPEED speed, const std::string& protocol, unsigned int address, bool assertRTS) :
|
||||||
m_device(device),
|
m_device(device),
|
||||||
m_speed(speed),
|
m_speed(speed),
|
||||||
|
m_protocol(protocol),
|
||||||
|
m_address(address),
|
||||||
m_assertRTS(assertRTS),
|
m_assertRTS(assertRTS),
|
||||||
m_handle(INVALID_HANDLE_VALUE)
|
m_handle(INVALID_HANDLE_VALUE)
|
||||||
{
|
{
|
||||||
|
@ -247,132 +249,132 @@ bool CSerialController::open()
|
||||||
LogError("Cannot open device - %s", m_device.c_str());
|
LogError("Cannot open device - %s", m_device.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::ioctl(m_fd, I2C_TENBIT, 0) < 0) {
|
if (::ioctl(m_fd, I2C_TENBIT, 0) < 0) {
|
||||||
LogError("CI2C: failed to set 7bitaddress");
|
LogError("CI2C: failed to set 7bitaddress");
|
||||||
|
::close(m_fd);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::ioctl(m_fd, I2C_SLAVE, m_address) < 0) {
|
if (::ioctl(m_fd, I2C_SLAVE, m_address) < 0) {
|
||||||
LogError("CI2C: Failed to acquire bus access/talk to slave 0x%u", m_address);
|
LogError("CI2C: Failed to acquire bus access/talk to slave 0x%02X", m_address);
|
||||||
::close(m_fd);
|
::close(m_fd);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
LogError("Conntected to Modem via i2c");
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); /*open in block mode under OSX*/
|
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); /*open in block mode under OSX*/
|
||||||
#else
|
#else
|
||||||
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY, 0);
|
m_fd = ::open(m_device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY, 0);
|
||||||
#endif
|
#endif
|
||||||
if (m_fd < 0) {
|
if (m_fd < 0) {
|
||||||
LogError("Cannot open device - %s", m_device.c_str());
|
LogError("Cannot open device - %s", m_device.c_str());
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (::isatty(m_fd) == 0) {
|
|
||||||
LogError("%s is not a TTY device", m_device.c_str());
|
|
||||||
::close(m_fd);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
termios termios;
|
|
||||||
if (::tcgetattr(m_fd, &termios) < 0) {
|
|
||||||
LogError("Cannot get the attributes for %s", m_device.c_str());
|
|
||||||
::close(m_fd);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
termios.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
|
|
||||||
termios.c_cflag &= ~CSIZE;
|
|
||||||
termios.c_cflag |= CS8; /* 8-bit characters */
|
|
||||||
termios.c_cflag &= ~PARENB; /* no parity bit */
|
|
||||||
termios.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
|
|
||||||
termios.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
|
|
||||||
|
|
||||||
/* setup for non-canonical mode */
|
|
||||||
termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
|
||||||
termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
|
||||||
termios.c_oflag &= ~OPOST;
|
|
||||||
|
|
||||||
/* fetch bytes as they become available */
|
|
||||||
termios.c_cc[VMIN] = 1;
|
|
||||||
termios.c_cc[VTIME] = 1;
|
|
||||||
#else
|
|
||||||
termios.c_lflag &= ~(ECHO | ECHOE | ICANON | IEXTEN | ISIG);
|
|
||||||
termios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
|
|
||||||
termios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CRTSCTS);
|
|
||||||
termios.c_cflag |= CS8;
|
|
||||||
termios.c_oflag &= ~(OPOST);
|
|
||||||
termios.c_cc[VMIN] = 0;
|
|
||||||
termios.c_cc[VTIME] = 10;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch (m_speed) {
|
|
||||||
case SERIAL_1200:
|
|
||||||
::cfsetospeed(&termios, B1200);
|
|
||||||
::cfsetispeed(&termios, B1200);
|
|
||||||
break;
|
|
||||||
case SERIAL_2400:
|
|
||||||
::cfsetospeed(&termios, B2400);
|
|
||||||
::cfsetispeed(&termios, B2400);
|
|
||||||
break;
|
|
||||||
case SERIAL_4800:
|
|
||||||
::cfsetospeed(&termios, B4800);
|
|
||||||
::cfsetispeed(&termios, B4800);
|
|
||||||
break;
|
|
||||||
case SERIAL_9600:
|
|
||||||
::cfsetospeed(&termios, B9600);
|
|
||||||
::cfsetispeed(&termios, B9600);
|
|
||||||
break;
|
|
||||||
case SERIAL_19200:
|
|
||||||
::cfsetospeed(&termios, B19200);
|
|
||||||
::cfsetispeed(&termios, B19200);
|
|
||||||
break;
|
|
||||||
case SERIAL_38400:
|
|
||||||
::cfsetospeed(&termios, B38400);
|
|
||||||
::cfsetispeed(&termios, B38400);
|
|
||||||
break;
|
|
||||||
case SERIAL_115200:
|
|
||||||
::cfsetospeed(&termios, B115200);
|
|
||||||
::cfsetispeed(&termios, B115200);
|
|
||||||
break;
|
|
||||||
case SERIAL_230400:
|
|
||||||
::cfsetospeed(&termios, B230400);
|
|
||||||
::cfsetispeed(&termios, B230400);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
LogError("Unsupported serial port speed - %d", int(m_speed));
|
|
||||||
::close(m_fd);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (::tcsetattr(m_fd, TCSANOW, &termios) < 0) {
|
if (::isatty(m_fd) == 0) {
|
||||||
LogError("Cannot set the attributes for %s", m_device.c_str());
|
LogError("%s is not a TTY device", m_device.c_str());
|
||||||
::close(m_fd);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_assertRTS) {
|
|
||||||
unsigned int y;
|
|
||||||
if (::ioctl(m_fd, TIOCMGET, &y) < 0) {
|
|
||||||
LogError("Cannot get the control attributes for %s", m_device.c_str());
|
|
||||||
::close(m_fd);
|
::close(m_fd);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
y |= TIOCM_RTS;
|
termios termios;
|
||||||
|
if (::tcgetattr(m_fd, &termios) < 0) {
|
||||||
if (::ioctl(m_fd, TIOCMSET, &y) < 0) {
|
LogError("Cannot get the attributes for %s", m_device.c_str());
|
||||||
LogError("Cannot set the control attributes for %s", m_device.c_str());
|
|
||||||
::close(m_fd);
|
::close(m_fd);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
setNonblock(false);
|
termios.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
|
||||||
|
termios.c_cflag &= ~CSIZE;
|
||||||
|
termios.c_cflag |= CS8; /* 8-bit characters */
|
||||||
|
termios.c_cflag &= ~PARENB; /* no parity bit */
|
||||||
|
termios.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
|
||||||
|
termios.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
|
||||||
|
|
||||||
|
/* setup for non-canonical mode */
|
||||||
|
termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
||||||
|
termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||||
|
termios.c_oflag &= ~OPOST;
|
||||||
|
|
||||||
|
/* fetch bytes as they become available */
|
||||||
|
termios.c_cc[VMIN] = 1;
|
||||||
|
termios.c_cc[VTIME] = 1;
|
||||||
|
#else
|
||||||
|
termios.c_lflag &= ~(ECHO | ECHOE | ICANON | IEXTEN | ISIG);
|
||||||
|
termios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF | IXANY);
|
||||||
|
termios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CRTSCTS);
|
||||||
|
termios.c_cflag |= CS8;
|
||||||
|
termios.c_oflag &= ~(OPOST);
|
||||||
|
termios.c_cc[VMIN] = 0;
|
||||||
|
termios.c_cc[VTIME] = 10;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
switch (m_speed) {
|
||||||
|
case SERIAL_1200:
|
||||||
|
::cfsetospeed(&termios, B1200);
|
||||||
|
::cfsetispeed(&termios, B1200);
|
||||||
|
break;
|
||||||
|
case SERIAL_2400:
|
||||||
|
::cfsetospeed(&termios, B2400);
|
||||||
|
::cfsetispeed(&termios, B2400);
|
||||||
|
break;
|
||||||
|
case SERIAL_4800:
|
||||||
|
::cfsetospeed(&termios, B4800);
|
||||||
|
::cfsetispeed(&termios, B4800);
|
||||||
|
break;
|
||||||
|
case SERIAL_9600:
|
||||||
|
::cfsetospeed(&termios, B9600);
|
||||||
|
::cfsetispeed(&termios, B9600);
|
||||||
|
break;
|
||||||
|
case SERIAL_19200:
|
||||||
|
::cfsetospeed(&termios, B19200);
|
||||||
|
::cfsetispeed(&termios, B19200);
|
||||||
|
break;
|
||||||
|
case SERIAL_38400:
|
||||||
|
::cfsetospeed(&termios, B38400);
|
||||||
|
::cfsetispeed(&termios, B38400);
|
||||||
|
break;
|
||||||
|
case SERIAL_115200:
|
||||||
|
::cfsetospeed(&termios, B115200);
|
||||||
|
::cfsetispeed(&termios, B115200);
|
||||||
|
break;
|
||||||
|
case SERIAL_230400:
|
||||||
|
::cfsetospeed(&termios, B230400);
|
||||||
|
::cfsetispeed(&termios, B230400);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LogError("Unsupported serial port speed - %d", int(m_speed));
|
||||||
|
::close(m_fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (::tcsetattr(m_fd, TCSANOW, &termios) < 0) {
|
||||||
|
LogError("Cannot set the attributes for %s", m_device.c_str());
|
||||||
|
::close(m_fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_assertRTS) {
|
||||||
|
unsigned int y;
|
||||||
|
if (::ioctl(m_fd, TIOCMGET, &y) < 0) {
|
||||||
|
LogError("Cannot get the control attributes for %s", m_device.c_str());
|
||||||
|
::close(m_fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
y |= TIOCM_RTS;
|
||||||
|
|
||||||
|
if (::ioctl(m_fd, TIOCMSET, &y) < 0) {
|
||||||
|
LogError("Cannot set the control attributes for %s", m_device.c_str());
|
||||||
|
::close(m_fd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
setNonblock(false);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ enum SERIAL_SPEED {
|
||||||
|
|
||||||
class CSerialController : public ISerialPort {
|
class CSerialController : public ISerialPort {
|
||||||
public:
|
public:
|
||||||
CSerialController(const std::string& device, SERIAL_SPEED speed, const std::string& protocol = "uart", unsigned int address = 0x22, bool assertRTS = false);
|
CSerialController(const std::string& device, SERIAL_SPEED speed, const std::string& protocol = "uart", unsigned int address = 0x22U, bool assertRTS = false);
|
||||||
virtual ~CSerialController();
|
virtual ~CSerialController();
|
||||||
|
|
||||||
virtual bool open();
|
virtual bool open();
|
||||||
|
|
Loading…
Reference in a new issue