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>
|
|
|
|
#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)
|
|
|
|
, _latchTime_ns(0)
|
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));
|
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-08-14 10:46:44 +02:00
|
|
|
// close(_fid);
|
2013-11-01 23:48:39 +01:00
|
|
|
}
|
|
|
|
|
2016-10-08 08:14:36 +02:00
|
|
|
bool ProviderSpi::init(const Json::Value &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2016-10-08 08:14:36 +02:00
|
|
|
_deviceName = deviceConfig.get("output",_deviceName).asString();
|
|
|
|
_baudRate_Hz = deviceConfig.get("rate",_baudRate_Hz).asInt();
|
|
|
|
_latchTime_ns = deviceConfig.get("latchtime",_latchTime_ns).asInt();
|
|
|
|
_spiMode = deviceConfig.get("spimode",_spiMode).asInt();
|
|
|
|
_spiDataInvert = deviceConfig.get("invert",_spiDataInvert).asBool();
|
2016-08-23 20:07:12 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:12:48 +02:00
|
|
|
int ProviderSpi::open()
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
2016-09-04 14:28:06 +02:00
|
|
|
Debug(_log, "_baudRate_Hz %d, _latchTime_ns %d", _baudRate_Hz, _latchTime_ns);
|
|
|
|
Debug(_log, "_spiDataInvert %d, _spiMode %d", _spiDataInvert, _spiMode);
|
|
|
|
|
2013-11-01 23:48:39 +01:00
|
|
|
const int bitsPerWord = 8;
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
_fid = ::open(_deviceName.c_str(), O_RDWR);
|
2013-11-01 23:48:39 +01:00
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (_fid < 0)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
Error( _log, "Failed to open device (%s). Error message: %s", _deviceName.c_str(), strerror(errno) );
|
2013-11-01 23:48:39 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (ioctl(_fid, SPI_IOC_WR_MODE, &_spiMode) == -1 || ioctl(_fid, SPI_IOC_RD_MODE, &_spiMode) == -1)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (ioctl(_fid, SPI_IOC_WR_BITS_PER_WORD, &bitsPerWord) == -1 || ioctl(_fid, SPI_IOC_RD_BITS_PER_WORD, &bitsPerWord) == -1)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -4;
|
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +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)
|
2013-11-01 23:48:39 +01:00
|
|
|
{
|
|
|
|
return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
if (retVal == 0 && _latchTime_ns > 0)
|
2013-11-02 06:51:41 +01:00
|
|
|
{
|
|
|
|
// The 'latch' time for latching the shifted-value into the leds
|
|
|
|
timespec latchTime;
|
|
|
|
latchTime.tv_sec = 0;
|
2016-08-14 10:46:44 +02:00
|
|
|
latchTime.tv_nsec = _latchTime_ns;
|
2013-11-02 06:51:41 +01:00
|
|
|
|
|
|
|
// Sleep to latch the leds (only if write succesfull)
|
|
|
|
nanosleep(&latchTime, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|