Clean up the I2C controller code.

This commit is contained in:
Jonathan Naylor 2020-05-23 16:49:21 +01:00
parent 303a0163d3
commit 0b185a0900
7 changed files with 38 additions and 63 deletions

View file

@ -17,6 +17,8 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#if defined(__linux__)
#include "I2CController.h" #include "I2CController.h"
#include "Log.h" #include "Log.h"
@ -24,52 +26,18 @@
#include <cassert> #include <cassert>
#include <sys/types.h> #include <sys/types.h>
#if defined(_WIN32) || defined(_WIN64)
#include <setupapi.h>
#include <winioctl.h>
CI2CController::CI2CController(const std::string& device, unsigned int speed, unsigned int address, bool assertRTS) :
CSerialController(device, speed, assertRTS),
m_address(address)
{
}
CI2CController::~CI2CController()
{
}
bool CI2CController::open()
{
return CSerialController::open();
}
int CI2CController::read(unsigned char* buffer, unsigned int length)
{
return CSerialController::read(buffer, length);
}
int CI2CController::write(const unsigned char* buffer, unsigned int length)
{
return CSerialController::write(buffer, length);
}
#else
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cerrno> #include <cerrno>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#if defined(__linux__)
#include <linux/i2c-dev.h> #include <linux/i2c-dev.h>
#endif
CI2CController::CI2CController(const std::string& device, unsigned int speed, unsigned int address, bool assertRTS) : CI2CController::CI2CController(const std::string& device, unsigned int address) :
CSerialController(device, speed, assertRTS), m_device(device),
m_address(address) m_address(address),
m_fd(-1)
{ {
} }
@ -81,7 +49,6 @@ bool CI2CController::open()
{ {
assert(m_fd == -1); assert(m_fd == -1);
#if defined(__linux__)
m_fd = ::open(m_device.c_str(), O_RDWR); m_fd = ::open(m_device.c_str(), O_RDWR);
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());
@ -99,9 +66,6 @@ bool CI2CController::open()
::close(m_fd); ::close(m_fd);
return false; return false;
} }
#else
#warning "I2C controller supports Linux only"
#endif
return true; return true;
} }
@ -117,7 +81,6 @@ int CI2CController::read(unsigned char* buffer, unsigned int length)
unsigned int offset = 0U; unsigned int offset = 0U;
while (offset < length) { while (offset < length) {
#if defined(__linux__)
ssize_t n = ::read(m_fd, buffer + offset, 1U); ssize_t n = ::read(m_fd, buffer + offset, 1U);
if (n < 0) { if (n < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
@ -128,7 +91,6 @@ int CI2CController::read(unsigned char* buffer, unsigned int length)
if (n > 0) if (n > 0)
offset += n; offset += n;
#endif
} }
return length; return length;
@ -144,10 +106,7 @@ int CI2CController::write(const unsigned char* buffer, unsigned int length)
unsigned int ptr = 0U; unsigned int ptr = 0U;
while (ptr < length) { while (ptr < length) {
ssize_t n = 0U; ssize_t n = ::write(m_fd, buffer + ptr, 1U);
#if defined(__linux__)
n = ::write(m_fd, buffer + ptr, 1U);
#endif
if (n < 0) { if (n < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
LogError("Error returned from write(), errno=%d", errno); LogError("Error returned from write(), errno=%d", errno);
@ -162,4 +121,12 @@ int CI2CController::write(const unsigned char* buffer, unsigned int length)
return length; return length;
} }
void CI2CController::close()
{
assert(m_fd != -1);
::close(m_fd);
m_fd = -1;
}
#endif #endif

View file

@ -20,11 +20,13 @@
#ifndef I2CController_H #ifndef I2CController_H
#define I2CController_H #define I2CController_H
#include "SerialController.h" #if defined(__linux__)
class CI2CController : public CSerialController { #include "SerialPort.h"
class CI2CController : public ISerialPort {
public: public:
CI2CController(const std::string& device, unsigned int speed, unsigned int address = 0x22U, bool assertRTS = false); CI2CController(const std::string& device, unsigned int address = 0x22U);
virtual ~CI2CController(); virtual ~CI2CController();
virtual bool open(); virtual bool open();
@ -33,8 +35,14 @@ public:
virtual int write(const unsigned char* buffer, unsigned int length); virtual int write(const unsigned char* buffer, unsigned int length);
virtual void close();
private: private:
std::string m_device;
unsigned int m_address; unsigned int m_address;
int m_fd;
}; };
#endif #endif
#endif

View file

@ -1238,10 +1238,13 @@ bool CMMDVMHost::createModem()
LogInfo("Modem Parameters"); LogInfo("Modem Parameters");
LogInfo(" Port: %s", port.c_str()); LogInfo(" Port: %s", port.c_str());
#if defined(__linux__)
LogInfo(" Protocol: %s", protocol.c_str()); LogInfo(" Protocol: %s", protocol.c_str());
if (protocol == "i2c") if (protocol == "i2c")
LogInfo(" I2C Address: %02X", address); LogInfo(" I2C Address: %02X", address);
LogInfo(" Speed: %u", speed); else
#endif
LogInfo(" Speed: %u", speed);
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");

View file

@ -187,7 +187,6 @@
<ClInclude Include="Golay24128.h" /> <ClInclude Include="Golay24128.h" />
<ClInclude Include="Hamming.h" /> <ClInclude Include="Hamming.h" />
<ClInclude Include="DMRLookup.h" /> <ClInclude Include="DMRLookup.h" />
<ClInclude Include="I2CController.h" />
<ClInclude Include="IIRDirectForm1Filter.h" /> <ClInclude Include="IIRDirectForm1Filter.h" />
<ClInclude Include="LCDproc.h" /> <ClInclude Include="LCDproc.h" />
<ClInclude Include="Log.h" /> <ClInclude Include="Log.h" />
@ -285,7 +284,6 @@
<ClCompile Include="Golay2087.cpp" /> <ClCompile Include="Golay2087.cpp" />
<ClCompile Include="Golay24128.cpp" /> <ClCompile Include="Golay24128.cpp" />
<ClCompile Include="Hamming.cpp" /> <ClCompile Include="Hamming.cpp" />
<ClCompile Include="I2CController.cpp" />
<ClCompile Include="IIRDirectForm1Filter.cpp" /> <ClCompile Include="IIRDirectForm1Filter.cpp" />
<ClCompile Include="LCDproc.cpp" /> <ClCompile Include="LCDproc.cpp" />
<ClCompile Include="Log.cpp" /> <ClCompile Include="Log.cpp" />

View file

@ -275,9 +275,6 @@
<ClInclude Include="POCSAGDefines.h"> <ClInclude Include="POCSAGDefines.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="I2CController.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MobileGPS.h"> <ClInclude Include="MobileGPS.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
@ -547,9 +544,6 @@
<ClCompile Include="POCSAGControl.cpp"> <ClCompile Include="POCSAGControl.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="I2CController.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MobileGPS.cpp"> <ClCompile Include="MobileGPS.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>

View file

@ -16,7 +16,10 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
#include "SerialController.h"
#if defined(__linux__)
#include "I2CController.h" #include "I2CController.h"
#endif
#include "DStarDefines.h" #include "DStarDefines.h"
#include "DMRDefines.h" #include "DMRDefines.h"
#include "YSFDefines.h" #include "YSFDefines.h"
@ -224,9 +227,11 @@ CModem::~CModem()
void CModem::setSerialParams(const std::string& protocol, unsigned int address, unsigned int speed) void CModem::setSerialParams(const std::string& protocol, unsigned int address, unsigned int speed)
{ {
// Create the serial controller instance according the protocol specified in conf. // Create the serial controller instance according the protocol specified in conf.
#if defined(__linux__)
if (protocol == "i2c") if (protocol == "i2c")
m_serial = new CI2CController(m_port, speed, address, true); m_serial = new CI2CController(m_port, address);
else else
#endif
m_serial = new CSerialController(m_port, speed, true); m_serial = new CSerialController(m_port, speed, true);
} }

View file

@ -19,7 +19,7 @@
#ifndef MODEM_H #ifndef MODEM_H
#define MODEM_H #define MODEM_H
#include "SerialController.h" #include "SerialPort.h"
#include "RingBuffer.h" #include "RingBuffer.h"
#include "Defines.h" #include "Defines.h"
#include "Timer.h" #include "Timer.h"
@ -156,7 +156,7 @@ private:
bool m_fmEnabled; bool m_fmEnabled;
int m_rxDCOffset; int m_rxDCOffset;
int m_txDCOffset; int m_txDCOffset;
CSerialController* m_serial; ISerialPort* m_serial;
unsigned char* m_buffer; unsigned char* m_buffer;
unsigned int m_length; unsigned int m_length;
unsigned int m_offset; unsigned int m_offset;