2018-07-30 13:58:35 +00:00
|
|
|
/*
|
2020-12-15 16:21:07 +00:00
|
|
|
* Copyright (C) 2002-2004,2007-2011,2013,2014-2017,2020 by Jonathan Naylor G4KLX
|
2018-07-30 13:58:35 +00:00
|
|
|
* Copyright (C) 1999-2001 by Thomas Sailor HB9JNX
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
2020-12-15 16:21:07 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
|
2018-07-30 17:19:46 +00:00
|
|
|
#include "I2CController.h"
|
2018-07-30 13:58:35 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2018-07-30 17:19:46 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <linux/i2c-dev.h>
|
|
|
|
|
2020-12-15 16:21:07 +00:00
|
|
|
CI2CController::CI2CController(const std::string& device, unsigned int address) :
|
|
|
|
m_device(device),
|
|
|
|
m_address(address),
|
|
|
|
m_fd(-1)
|
2018-07-30 13:58:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:19:46 +00:00
|
|
|
CI2CController::~CI2CController()
|
2018-07-30 13:58:35 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:19:46 +00:00
|
|
|
bool CI2CController::open()
|
2018-07-30 13:58:35 +00:00
|
|
|
{
|
|
|
|
assert(m_fd == -1);
|
2018-07-30 17:19:46 +00:00
|
|
|
|
2018-07-30 13:58:35 +00:00
|
|
|
m_fd = ::open(m_device.c_str(), O_RDWR);
|
|
|
|
if (m_fd < 0) {
|
|
|
|
LogError("Cannot open device - %s", m_device.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::ioctl(m_fd, I2C_TENBIT, 0) < 0) {
|
2020-12-15 16:21:07 +00:00
|
|
|
LogError("I2C: failed to set 7bitaddress");
|
2018-07-30 13:58:35 +00:00
|
|
|
::close(m_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::ioctl(m_fd, I2C_SLAVE, m_address) < 0) {
|
2020-12-15 16:21:07 +00:00
|
|
|
LogError("I2C: Failed to acquire bus access/talk to slave 0x%02X", m_address);
|
2018-07-30 13:58:35 +00:00
|
|
|
::close(m_fd);
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-30 17:19:46 +00:00
|
|
|
|
2018-07-30 13:58:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:19:46 +00:00
|
|
|
int CI2CController::read(unsigned char* buffer, unsigned int length)
|
2018-07-30 13:58:35 +00:00
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(m_fd != -1);
|
|
|
|
|
|
|
|
if (length == 0U)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned int offset = 0U;
|
|
|
|
|
|
|
|
while (offset < length) {
|
|
|
|
ssize_t n = ::read(m_fd, buffer + offset, 1U);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
LogError("Error returned from read(), errno=%d", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
offset += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-07-30 17:19:46 +00:00
|
|
|
int CI2CController::write(const unsigned char* buffer, unsigned int length)
|
2018-07-30 13:58:35 +00:00
|
|
|
{
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(m_fd != -1);
|
|
|
|
|
|
|
|
if (length == 0U)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unsigned int ptr = 0U;
|
|
|
|
while (ptr < length) {
|
2020-12-15 16:21:07 +00:00
|
|
|
ssize_t n = ::write(m_fd, buffer + ptr, 1U);
|
2018-07-30 13:58:35 +00:00
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EAGAIN) {
|
|
|
|
LogError("Error returned from write(), errno=%d", errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0)
|
|
|
|
ptr += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2020-12-15 16:21:07 +00:00
|
|
|
void CI2CController::close()
|
|
|
|
{
|
|
|
|
assert(m_fd != -1);
|
|
|
|
|
|
|
|
::close(m_fd);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:58:35 +00:00
|
|
|
#endif
|