2020-02-10 15:21:58 +01:00
|
|
|
|
|
2013-11-01 23:48:39 +01:00
|
|
|
|
// STL includes
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <iostream>
|
2016-06-25 14:44:52 +02:00
|
|
|
|
#include <cerrno>
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
|
|
|
|
// Linux includes
|
|
|
|
|
#include <fcntl.h>
|
2020-02-10 15:21:58 +01:00
|
|
|
|
#include <unistd.h>
|
2013-11-01 23:48:39 +01:00
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
|
|
// Local Hyperion includes
|
2016-08-28 07:12:48 +02:00
|
|
|
|
#include "ProviderSpi.h"
|
2016-06-25 14:44:52 +02:00
|
|
|
|
#include <utils/Logger.h>
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
|
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
|
ProviderSpi::ProviderSpi()
|
2016-08-23 20:07:12 +02:00
|
|
|
|
: LedDevice()
|
2016-10-08 08:14:36 +02:00
|
|
|
|
, _deviceName("/dev/spidev0.0")
|
|
|
|
|
, _baudRate_Hz(1000000)
|
2016-08-14 10:46:44 +02:00
|
|
|
|
, _fid(-1)
|
2016-10-08 08:14:36 +02:00
|
|
|
|
, _spiMode(SPI_MODE_0)
|
|
|
|
|
, _spiDataInvert(false)
|
2013-11-01 23:48:39 +01:00
|
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
|
memset(&_spi, 0, sizeof(_spi));
|
2017-04-09 22:28:32 +02:00
|
|
|
|
_latchTime_ms = 1;
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
|
ProviderSpi::~ProviderSpi()
|
2013-11-01 23:48:39 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
|
bool ProviderSpi::init(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
|
bool isInitOK = LedDevice::init(deviceConfig);
|
2016-12-02 12:07:24 +01:00
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
|
_deviceName = deviceConfig["output"].toString(_deviceName);
|
2016-10-13 21:59:58 +02:00
|
|
|
|
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
|
|
|
|
|
_spiMode = deviceConfig["spimode"].toInt(_spiMode);
|
|
|
|
|
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
|
2016-12-02 12:07:24 +01:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
return isInitOK;
|
2016-08-23 20:07:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
|
int ProviderSpi::open()
|
2013-11-01 23:48:39 +01:00
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
|
int retval = -1;
|
|
|
|
|
QString errortext;
|
|
|
|
|
_deviceReady = false;
|
2016-09-04 14:28:06 +02:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
if ( init(_devConfig) )
|
|
|
|
|
{
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
Debug(_log, "_baudRate_Hz %d, _latchTime_ns %d", _baudRate_Hz, _latchTime_ms);
|
|
|
|
|
Debug(_log, "_spiDataInvert %d, _spiMode %d", _spiDataInvert, _spiMode);
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
const int bitsPerWord = 8;
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
if (_fid < 0)
|
|
|
|
|
{
|
|
|
|
|
errortext = QString ("Failed to open device (%1). Error message: %2").arg(_deviceName, strerror(errno));
|
|
|
|
|
retval = -1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1)
|
|
|
|
|
{
|
|
|
|
|
retval = -2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
|
|
|
|
|
{
|
|
|
|
|
retval = -4;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1)
|
|
|
|
|
{
|
|
|
|
|
retval = -6;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Everything OK -> enable device
|
|
|
|
|
_deviceReady = true;
|
|
|
|
|
setEnable(true);
|
|
|
|
|
retval = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
|
{
|
2020-03-26 18:49:44 +01:00
|
|
|
|
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retval);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
|
{
|
|
|
|
|
this->setInError( errortext );
|
|
|
|
|
}
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProviderSpi::close()
|
|
|
|
|
{
|
|
|
|
|
LedDevice::close();
|
|
|
|
|
|
|
|
|
|
// Device specific closing activites
|
|
|
|
|
if ( _fid > -1 )
|
2013-11-01 23:48:39 +01:00
|
|
|
|
{
|
2020-02-10 15:21:58 +01:00
|
|
|
|
if ( ::close(_fid) != 0 )
|
|
|
|
|
{
|
|
|
|
|
Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) );
|
|
|
|
|
}
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
|
int ProviderSpi::writeBytes(const unsigned size, const uint8_t * data)
|
2013-11-02 06:51:41 +01:00
|
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
|
if (_fid < 0)
|
2013-11-02 06:51:41 +01:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
|
_spi.tx_buf = __u64(data);
|
|
|
|
|
_spi.len = __u32(size);
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
|
if (_spiDataInvert)
|
|
|
|
|
{
|
2016-08-13 19:54:08 +02:00
|
|
|
|
uint8_t * newdata = (uint8_t *)malloc(size);
|
|
|
|
|
for (unsigned i = 0; i<size; i++) {
|
|
|
|
|
newdata[i] = data[i] ^ 0xff;
|
|
|
|
|
}
|
2016-08-14 10:46:44 +02:00
|
|
|
|
_spi.tx_buf = __u64(newdata);
|
2016-08-13 19:54:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
|
int retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
|
2016-08-31 09:51:50 +02:00
|
|
|
|
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno) );
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|