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
|
|
|
|
|
2021-04-24 19:37:29 +02:00
|
|
|
|
// qt includes
|
|
|
|
|
#include <QDir>
|
|
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
|
namespace {
|
|
|
|
|
const bool verbose = false;
|
|
|
|
|
|
|
|
|
|
// SPI discovery service
|
|
|
|
|
const char DISCOVERY_DIRECTORY[] = "/dev/";
|
|
|
|
|
const char DISCOVERY_FILEPATTERN[] = "spidev*";
|
|
|
|
|
|
|
|
|
|
} //End of constants
|
|
|
|
|
|
2020-08-08 00:21:19 +02:00
|
|
|
|
ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig)
|
|
|
|
|
: LedDevice(deviceConfig)
|
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-07-12 20:27:56 +02:00
|
|
|
|
bool isInitOK = false;
|
2016-12-02 12:07:24 +01:00
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
// Initialise sub-class
|
|
|
|
|
if ( LedDevice::init(deviceConfig) )
|
|
|
|
|
{
|
|
|
|
|
_deviceName = deviceConfig["output"].toString(_deviceName);
|
|
|
|
|
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
|
|
|
|
|
_spiMode = deviceConfig["spimode"].toInt(_spiMode);
|
|
|
|
|
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
|
|
|
|
|
|
|
|
|
|
Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms);
|
|
|
|
|
Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode);
|
2020-07-12 18:27:24 +02:00
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
isInitOK = true;
|
|
|
|
|
}
|
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;
|
2020-07-12 20:27:56 +02:00
|
|
|
|
_isDeviceReady = false;
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
const int bitsPerWord = 8;
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
|
2013-11-01 23:48:39 +01:00
|
|
|
|
|
2020-07-12 20:27:56 +02: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)
|
2020-02-10 15:21:58 +01:00
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
retval = -2;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
|
2020-02-10 15:21:58 +01:00
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
retval = -4;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MAX_SPEED_HZ, &_baudRate_Hz) == -1 || ioctl(_fid, SPI_IOC_RD_MAX_SPEED_HZ, &_baudRate_Hz) == -1)
|
2020-02-10 15:21:58 +01:00
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
retval = -6;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
// Everything OK -> enable device
|
|
|
|
|
_isDeviceReady = true;
|
|
|
|
|
retval = 0;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( retval < 0 )
|
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retval);
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
if ( retval < 0 )
|
|
|
|
|
{
|
|
|
|
|
this->setInError( errortext );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 15:21:58 +01:00
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
int ProviderSpi::close()
|
2020-02-10 15:21:58 +01:00
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
// LedDevice specific closing activities
|
|
|
|
|
int retval = 0;
|
|
|
|
|
_isDeviceReady = false;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
|
2020-07-12 20:27:56 +02:00
|
|
|
|
// Test, if device requires closing
|
2020-02-10 15:21:58 +01:00
|
|
|
|
if ( _fid > -1 )
|
2013-11-01 23:48:39 +01:00
|
|
|
|
{
|
2020-07-12 20:27:56 +02:00
|
|
|
|
// Close device
|
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) );
|
2020-07-12 20:27:56 +02:00
|
|
|
|
retval = -1;
|
2020-02-10 15:21:58 +01:00
|
|
|
|
}
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
2020-07-12 20:27:56 +02:00
|
|
|
|
return retval;
|
2013-11-01 23:48:39 +01:00
|
|
|
|
}
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
|
int ProviderSpi::writeBytes(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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 10:31:56 +02:00
|
|
|
|
uint8_t * newdata {nullptr};
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-09-15 10:31:56 +02:00
|
|
|
|
newdata = static_cast<uint8_t *>(malloc(size));
|
2016-08-13 19:54:08 +02:00
|
|
|
|
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
|
|
|
|
|
2021-09-15 10:31:56 +02:00
|
|
|
|
free (newdata);
|
|
|
|
|
|
2013-11-02 06:51:41 +01:00
|
|
|
|
return retVal;
|
|
|
|
|
}
|
2021-04-24 19:37:29 +02:00
|
|
|
|
|
|
|
|
|
QJsonObject ProviderSpi::discover(const QJsonObject& /*params*/)
|
|
|
|
|
{
|
|
|
|
|
QJsonObject devicesDiscovered;
|
|
|
|
|
devicesDiscovered.insert("ledDeviceType", _activeDeviceType );
|
|
|
|
|
|
|
|
|
|
QJsonArray deviceList;
|
|
|
|
|
|
|
|
|
|
QDir deviceDirectory (DISCOVERY_DIRECTORY);
|
|
|
|
|
QStringList deviceFilter(DISCOVERY_FILEPATTERN);
|
|
|
|
|
deviceDirectory.setNameFilters(deviceFilter);
|
|
|
|
|
deviceDirectory.setSorting(QDir::Name);
|
|
|
|
|
QFileInfoList deviceFiles = deviceDirectory.entryInfoList(QDir::System);
|
|
|
|
|
|
|
|
|
|
QFileInfoList::const_iterator deviceFileIterator;
|
|
|
|
|
for (deviceFileIterator = deviceFiles.constBegin(); deviceFileIterator != deviceFiles.constEnd(); ++deviceFileIterator)
|
|
|
|
|
{
|
|
|
|
|
QJsonObject deviceInfo;
|
|
|
|
|
deviceInfo.insert("deviceName", (*deviceFileIterator).fileName().remove(0,6));
|
|
|
|
|
deviceInfo.insert("systemLocation", (*deviceFileIterator).absoluteFilePath());
|
|
|
|
|
deviceList.append(deviceInfo);
|
|
|
|
|
}
|
|
|
|
|
devicesDiscovered.insert("devices", deviceList);
|
|
|
|
|
|
|
|
|
|
DebugIf(verbose,_log, "devicesDiscovered: [%s]", QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
|
|
|
|
|
|
|
|
|
return devicesDiscovered;
|
|
|
|
|
}
|