Refactor spidev and ftdi spi providers into separate classes
This commit is contained in:
parent
97538fb1ef
commit
43b864b92e
|
@ -44,6 +44,18 @@ endif()
|
||||||
|
|
||||||
if ( ENABLE_DEV_SPI OR ENABLE_DEV_FTDI )
|
if ( ENABLE_DEV_SPI OR ENABLE_DEV_FTDI )
|
||||||
FILE ( GLOB Leddevice_SPI_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/*.h" "${CURRENT_SOURCE_DIR}/dev_spi/*.cpp")
|
FILE ( GLOB Leddevice_SPI_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/*.h" "${CURRENT_SOURCE_DIR}/dev_spi/*.cpp")
|
||||||
|
|
||||||
|
FILE ( GLOB Leddevice_SPI_PROVIDERS_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/BaseProvider.*")
|
||||||
|
|
||||||
|
if ( ENABLE_DEV_FTDI )
|
||||||
|
file (GLOB FTDI_PROVIDER_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/ProviderFtdi.*")
|
||||||
|
list (APPEND Leddevice_SPI_PROVIDERS_SOURCES ${FTDI_PROVIDER_SOURCES})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if ( ENABLE_DEV_SPI )
|
||||||
|
file (GLOB SPIDEV_PROVIDER_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/providers/ProviderSpidev.*")
|
||||||
|
list (APPEND Leddevice_SPI_PROVIDERS_SOURCES ${SPIDEV_PROVIDER_SOURCES})
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if ( ENABLE_DEV_TINKERFORGE )
|
if ( ENABLE_DEV_TINKERFORGE )
|
||||||
|
@ -73,6 +85,7 @@ SET( Leddevice_SOURCES
|
||||||
${Leddevice_PWM_SOURCES}
|
${Leddevice_PWM_SOURCES}
|
||||||
${Leddevice_SERIAL_SOURCES}
|
${Leddevice_SERIAL_SOURCES}
|
||||||
${Leddevice_SPI_SOURCES}
|
${Leddevice_SPI_SOURCES}
|
||||||
|
${Leddevice_SPI_PROVIDERS_SOURCES}
|
||||||
${Leddevice_TINKER_SOURCES}
|
${Leddevice_TINKER_SOURCES}
|
||||||
${Leddevice_USB_HID_SOURCES}
|
${Leddevice_USB_HID_SOURCES}
|
||||||
)
|
)
|
||||||
|
|
|
@ -55,14 +55,14 @@ LedDevice* LedDeviceAPA104::construct(const QJsonObject &deviceConfig)
|
||||||
|
|
||||||
bool LedDeviceAPA104::init(const QJsonObject &deviceConfig)
|
bool LedDeviceAPA104::init(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
_baudRate_Hz = 2235000;
|
// _baudRate_Hz = 2235000;
|
||||||
|
|
||||||
bool isInitOK = false;
|
bool isInitOK = false;
|
||||||
|
|
||||||
// Initialise sub-class
|
// Initialise sub-class
|
||||||
if ( ProviderSpi::init(deviceConfig) )
|
if ( ProviderSpi::init(deviceConfig) )
|
||||||
{
|
{
|
||||||
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz);
|
// WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2470000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2470000)", _baudRate_Hz);
|
||||||
|
|
||||||
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ LedDevice* LedDeviceSk6812SPI::construct(const QJsonObject &deviceConfig)
|
||||||
|
|
||||||
bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig)
|
bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
_baudRate_Hz = 3000000;
|
// _baudRate_Hz = 3000000;
|
||||||
|
|
||||||
bool isInitOK = false;
|
bool isInitOK = false;
|
||||||
|
|
||||||
|
@ -40,7 +40,7 @@ bool LedDeviceSk6812SPI::init(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
Debug( _log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
|
Debug( _log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm));
|
||||||
|
|
||||||
WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
|
// WarningIf(( _baudRate_Hz < 2050000 || _baudRate_Hz > 4000000 ), _log, "SPI rate %d outside recommended range (2050000 -> 4000000)", _baudRate_Hz);
|
||||||
|
|
||||||
const int SPI_FRAME_END_LATCH_BYTES = 3;
|
const int SPI_FRAME_END_LATCH_BYTES = 3;
|
||||||
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
||||||
|
|
|
@ -57,14 +57,14 @@ LedDevice* LedDeviceSk6822SPI::construct(const QJsonObject &deviceConfig)
|
||||||
|
|
||||||
bool LedDeviceSk6822SPI::init(const QJsonObject &deviceConfig)
|
bool LedDeviceSk6822SPI::init(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
_baudRate_Hz = 2230000;
|
// _baudRate_Hz = 2230000;
|
||||||
|
|
||||||
bool isInitOK = false;
|
bool isInitOK = false;
|
||||||
|
|
||||||
// Initialise sub-class
|
// Initialise sub-class
|
||||||
if ( ProviderSpi::init(deviceConfig) )
|
if ( ProviderSpi::init(deviceConfig) )
|
||||||
{
|
{
|
||||||
WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz);
|
// WarningIf(( _baudRate_Hz < 2000000 || _baudRate_Hz > 2460000 ), _log, "SPI rate %d outside recommended range (2000000 -> 2460000)", _baudRate_Hz);
|
||||||
|
|
||||||
_ledBuffer.resize( (_ledRGBCount * SPI_BYTES_PER_COLOUR) + (_ledCount * SPI_BYTES_WAIT_TIME ) + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
_ledBuffer.resize( (_ledRGBCount * SPI_BYTES_PER_COLOUR) + (_ledCount * SPI_BYTES_WAIT_TIME ) + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
||||||
isInitOK = true;
|
isInitOK = true;
|
||||||
|
|
|
@ -54,14 +54,14 @@ LedDevice* LedDeviceWs2812SPI::construct(const QJsonObject &deviceConfig)
|
||||||
|
|
||||||
bool LedDeviceWs2812SPI::init(const QJsonObject &deviceConfig)
|
bool LedDeviceWs2812SPI::init(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
_baudRate_Hz = 2600000;
|
// _baudRate_Hz = 2600000;
|
||||||
|
|
||||||
bool isInitOK = false;
|
bool isInitOK = false;
|
||||||
|
|
||||||
// Initialise sub-class
|
// Initialise sub-class
|
||||||
if ( ProviderSpi::init(deviceConfig) )
|
if ( ProviderSpi::init(deviceConfig) )
|
||||||
{
|
{
|
||||||
WarningIf(( _baudRate_Hz < 2106000 || _baudRate_Hz > 3075000 ), _log, "SPI rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
|
// WarningIf(( _baudRate_Hz < 2106000 || _baudRate_Hz > 3075000 ), _log, "SPI rate %d outside recommended range (2106000 -> 3075000)", _baudRate_Hz);
|
||||||
|
|
||||||
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
_ledBuffer.resize(_ledRGBCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
|
||||||
|
|
||||||
|
|
|
@ -1,77 +1,69 @@
|
||||||
// Local Hyperion includes
|
#include "HyperionConfig.h"
|
||||||
#include "ProviderSpi.h"
|
#include "ProviderSpi.h"
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
#ifdef ENABLE_SPIDEV
|
||||||
// STL includes
|
#include "providers/ProviderSpidev.h"
|
||||||
#include <cstring>
|
|
||||||
#include <cstdio>
|
|
||||||
#include <iostream>
|
|
||||||
#include <cerrno>
|
|
||||||
// Linux includes
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
// qt includes
|
|
||||||
#include <QDir>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
|
|
||||||
#include <ftdi.h>
|
|
||||||
#include <libusb.h>
|
|
||||||
#include <utils/WaitTime.h>
|
|
||||||
|
|
||||||
#define FTDI_CHECK_RESULT(statement) if (statement) {setInError(ftdi_get_error_string(_ftdic)); return retVal;}
|
|
||||||
#define IS_FTDI_DEVICE_NAME(deviceName) (deviceName.startsWith("d:") || deviceName.startsWith("i:") || deviceName.startsWith("s:"))
|
|
||||||
#define ANY_FTDI_VENDOR 0x0
|
|
||||||
#define ANY_FTDI_PRODUCT 0x0
|
|
||||||
|
|
||||||
|
|
||||||
namespace Pin {
|
|
||||||
// enumerate the AD bus for convenience.
|
|
||||||
enum bus_t {
|
|
||||||
SK = 0x01, // ADBUS0, SPI data clock
|
|
||||||
DO = 0x02, // ADBUS1, SPI data out
|
|
||||||
CS = 0x08, // ADBUS3, SPI chip select, active low
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const unsigned char pinInitialState = Pin::CS;
|
|
||||||
// Use these pins as outputs
|
|
||||||
const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef ENABLE_FTDIDEV
|
||||||
|
#include "providers/ProviderFtdi.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Local HyperHDR includes
|
||||||
#include <utils/Logger.h>
|
#include <utils/Logger.h>
|
||||||
|
|
||||||
|
|
||||||
// Constants
|
namespace SPIProvider {
|
||||||
namespace {
|
SpiImplementation deviceNameToSpiImplementation(const QString &deviceName) {
|
||||||
const bool verbose = false;
|
if ((deviceName.startsWith("d:") || deviceName.startsWith("i:") || deviceName.startsWith("s:"))) {
|
||||||
#ifdef ENABLE_DEV_SPI
|
return SpiImplementation::FTDI;
|
||||||
// SPI discovery service
|
} else {
|
||||||
const char DISCOVERY_DIRECTORY[] = "/dev/";
|
return SpiImplementation::SPIDEV;
|
||||||
const char DISCOVERY_FILEPATTERN[] = "spidev*";
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseProvider *construct(const QJsonObject &deviceConfig) {
|
||||||
|
QString deviceName = deviceConfig["output"].toString("unknown");
|
||||||
|
switch (SPIProvider::deviceNameToSpiImplementation(deviceName)) {
|
||||||
|
#ifdef ENABLE_SPIDEV
|
||||||
|
case SPIDEV:
|
||||||
|
return dynamic_cast<BaseProvider *>(new ProviderSpidev(deviceConfig));
|
||||||
|
break;
|
||||||
#endif
|
#endif
|
||||||
} //End of constants
|
#ifdef ENABLE_FTDIDEV
|
||||||
|
case FTDI:
|
||||||
|
return dynamic_cast<BaseProvider *>(new ProviderFtdi(deviceConfig));
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return new BaseProvider(deviceConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray discover(const QJsonObject ¶ms) {
|
||||||
|
QJsonArray deviceList;
|
||||||
|
#ifdef ENABLE_SPIDEV
|
||||||
|
QJsonArray spidevDevices = (new ProviderSpidev(params))->discover(params);
|
||||||
|
for (const auto &item: spidevDevices){
|
||||||
|
deviceList += item;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENABLE_FTDIDEV
|
||||||
|
QJsonArray ftdiDevices = (new ProviderFtdi(params))->discover(params);
|
||||||
|
for (const auto &item: ftdiDevices){
|
||||||
|
deviceList += item;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return deviceList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig)
|
ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig)
|
||||||
: LedDevice(deviceConfig),
|
: LedDevice(deviceConfig),
|
||||||
_deviceName("/dev/spidev0.0"),
|
_spiProvider(nullptr) {
|
||||||
_baudRate_Hz(1000000)
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
|
||||||
, _fid(-1)
|
|
||||||
, _spiMode(SPI_MODE_0)
|
|
||||||
, _spiDataInvert(false)
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
, _ftdic(nullptr)
|
|
||||||
#endif
|
|
||||||
, _spiImplementation(SPIDEV) {
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
|
||||||
memset(&_spi, 0, sizeof(_spi));
|
|
||||||
_latchTime_ms = 1;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProviderSpi::~ProviderSpi() {
|
ProviderSpi::~ProviderSpi() {
|
||||||
|
@ -83,18 +75,8 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) {
|
||||||
// Initialise sub-class
|
// Initialise sub-class
|
||||||
if (LedDevice::init(deviceConfig)) {
|
if (LedDevice::init(deviceConfig)) {
|
||||||
_deviceName = deviceConfig["output"].toString(_deviceName);
|
_deviceName = deviceConfig["output"].toString(_deviceName);
|
||||||
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
|
Debug(_log, "_deviceName %s", QSTRING_CSTR(_deviceName));
|
||||||
#ifdef ENABLE_DEV_SPI
|
_spiProvider = SPIProvider::construct(deviceConfig);
|
||||||
_spiMode = deviceConfig["spimode"].toInt(_spiMode);
|
|
||||||
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
|
|
||||||
Debug(_log, "_spiDataInvert [%d], _spiMode [%d]", _spiDataInvert, _spiMode);
|
|
||||||
#endif
|
|
||||||
bool isFtdiImplementation = IS_FTDI_DEVICE_NAME(_deviceName);
|
|
||||||
_spiImplementation = isFtdiImplementation ? FTDI : SPIDEV;
|
|
||||||
|
|
||||||
Debug(_log, "_baudRate_Hz [%d], _latchTime_ms [%d]", _baudRate_Hz, _latchTime_ms);
|
|
||||||
|
|
||||||
|
|
||||||
isInitOK = true;
|
isInitOK = true;
|
||||||
}
|
}
|
||||||
return isInitOK;
|
return isInitOK;
|
||||||
|
@ -102,250 +84,42 @@ bool ProviderSpi::init(const QJsonObject &deviceConfig) {
|
||||||
|
|
||||||
int ProviderSpi::open() {
|
int ProviderSpi::open() {
|
||||||
int retVal = -1;
|
int retVal = -1;
|
||||||
QString errortext;
|
_isDeviceReady = true;
|
||||||
_isDeviceReady = false;
|
if ((retVal = _spiProvider->open())) {
|
||||||
if (_spiImplementation == SPIDEV) {
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
|
||||||
const int bitsPerWord = 8;
|
|
||||||
|
|
||||||
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
|
|
||||||
|
|
||||||
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
|
|
||||||
_isDeviceReady = true;
|
|
||||||
retVal = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ( retVal < 0 )
|
|
||||||
{
|
|
||||||
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retVal);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( retVal < 0 )
|
|
||||||
{
|
|
||||||
this->setInError( errortext );
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} else if (_spiImplementation == FTDI) {
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
|
|
||||||
_ftdic = ftdi_new();
|
|
||||||
|
|
||||||
Debug(_log, "Opening FTDI device=%s", QSTRING_CSTR(_deviceName));
|
|
||||||
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName))) < 0);
|
|
||||||
/* doing this disable resets things if they were in a bad state */
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_disable_bitbang(_ftdic)) < 0);
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_setflowctrl(_ftdic, SIO_DISABLE_FLOW_CTRL)) < 0);
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET)) < 0);
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0xff, BITMODE_MPSSE)) < 0);
|
|
||||||
|
|
||||||
|
|
||||||
double reference_clock = 60e6;
|
|
||||||
int divisor = (reference_clock / 2 / _baudRate_Hz) - 1;
|
|
||||||
std::vector<uint8_t> buf = {
|
|
||||||
DIS_DIV_5,
|
|
||||||
TCK_DIVISOR,
|
|
||||||
static_cast<unsigned char>(divisor),
|
|
||||||
static_cast<unsigned char>(divisor >> 8),
|
|
||||||
SET_BITS_LOW, // opcode: set low bits (ADBUS[0-7]
|
|
||||||
pinInitialState, // argument: inital pin state
|
|
||||||
pinDirection
|
|
||||||
};
|
|
||||||
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size());
|
|
||||||
|
|
||||||
_isDeviceReady = true;
|
_isDeviceReady = true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int ProviderSpi::close() {
|
int ProviderSpi::close() {
|
||||||
// LedDevice specific closing activities
|
// LedDevice specific closing activities
|
||||||
int retVal = 0;
|
int retVal = 0;
|
||||||
_isDeviceReady = false;
|
_isDeviceReady = false;
|
||||||
if (_spiImplementation == SPIDEV) {
|
if (_spiProvider != nullptr) {
|
||||||
#ifdef ENABLE_DEV_SPI
|
retVal = _spiProvider->close();
|
||||||
// Test, if device requires closing
|
|
||||||
if ( _fid > -1 )
|
|
||||||
{
|
|
||||||
// Close device
|
|
||||||
if ( ::close(_fid) != 0 )
|
|
||||||
{
|
|
||||||
Error( _log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno) );
|
|
||||||
retVal = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} else if (_spiImplementation == FTDI) {
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
if (_ftdic != nullptr) {
|
|
||||||
Debug(_log, "Closing FTDI device");
|
|
||||||
// Delay to give time to push color black from writeBlack() into the led,
|
|
||||||
// otherwise frame transmission will be terminated half way through
|
|
||||||
wait(30);
|
|
||||||
ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET);
|
|
||||||
ftdi_usb_close(_ftdic);
|
|
||||||
ftdi_free(_ftdic);
|
|
||||||
_ftdic = nullptr;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ProviderSpi::writeBytes(unsigned size, const uint8_t *data) {
|
int ProviderSpi::writeBytes(unsigned size, const uint8_t *data) {
|
||||||
int retVal = 0;
|
int retVal = 0;
|
||||||
if (_spiImplementation == SPIDEV) {
|
if ((retVal = _spiProvider->writeBytes(size, data)) < 0) {
|
||||||
#ifdef ENABLE_DEV_SPI
|
retVal = -1;
|
||||||
if (_fid < 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t * newdata {nullptr};
|
|
||||||
|
|
||||||
_spi.tx_buf = __u64(data);
|
|
||||||
_spi.len = __u32(size);
|
|
||||||
|
|
||||||
if (_spiDataInvert)
|
|
||||||
{
|
|
||||||
newdata = static_cast<uint8_t *>(malloc(size));
|
|
||||||
for (unsigned i = 0; i<size; i++) {
|
|
||||||
newdata[i] = data[i] ^ 0xff;
|
|
||||||
}
|
|
||||||
_spi.tx_buf = __u64(newdata);
|
|
||||||
}
|
|
||||||
|
|
||||||
retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
|
|
||||||
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno) );
|
|
||||||
|
|
||||||
free (newdata);
|
|
||||||
#endif
|
|
||||||
} else if (_spiImplementation == FTDI) {
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
int count_arg = size - 1;
|
|
||||||
std::vector<uint8_t> buf = {
|
|
||||||
SET_BITS_LOW,
|
|
||||||
pinInitialState & ~Pin::CS,
|
|
||||||
pinDirection,
|
|
||||||
MPSSE_DO_WRITE | MPSSE_WRITE_NEG,
|
|
||||||
static_cast<unsigned char>(count_arg),
|
|
||||||
static_cast<unsigned char>(count_arg >> 8),
|
|
||||||
// LED's data will be inserted here
|
|
||||||
SET_BITS_LOW,
|
|
||||||
pinInitialState | Pin::CS,
|
|
||||||
pinDirection
|
|
||||||
};
|
|
||||||
// insert before last SET_BITS_LOW command
|
|
||||||
// SET_BITS_LOW takes 2 arguments, so we're inserting data in -3 position from the end
|
|
||||||
buf.insert(buf.end() - 3, &data[0], &data[size]);
|
|
||||||
|
|
||||||
FTDI_CHECK_RESULT((retVal = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size());
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject ProviderSpi::discover(const QJsonObject & /*params*/) {
|
QJsonObject ProviderSpi::discover(const QJsonObject ¶ms) {
|
||||||
QJsonObject devicesDiscovered;
|
QJsonObject devicesDiscovered;
|
||||||
|
QJsonArray deviceList = SPIProvider::discover(params);
|
||||||
|
|
||||||
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
devicesDiscovered.insert("ledDeviceType", _activeDeviceType);
|
||||||
QJsonArray deviceList;
|
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
struct ftdi_device_list *devlist;
|
|
||||||
struct ftdi_context *ftdic;
|
|
||||||
|
|
||||||
ftdic = ftdi_new();
|
|
||||||
|
|
||||||
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0) {
|
|
||||||
struct ftdi_device_list *curdev = devlist;
|
|
||||||
QMap<QString, uint8_t> deviceIndexes;
|
|
||||||
|
|
||||||
while (curdev) {
|
|
||||||
libusb_device_descriptor desc;
|
|
||||||
int rc = libusb_get_device_descriptor(curdev->dev, &desc);
|
|
||||||
if (rc == 0) {
|
|
||||||
QString vendorIdentifier = QString("0x%1").arg(desc.idVendor, 4, 16, QChar{'0'});
|
|
||||||
QString productIdentifier = QString("0x%1").arg(desc.idProduct, 4, 16, QChar{'0'});
|
|
||||||
QString vendorAndProduct = QString("%1:%2")
|
|
||||||
.arg(vendorIdentifier)
|
|
||||||
.arg(productIdentifier);
|
|
||||||
uint8_t deviceIndex = deviceIndexes.value(vendorAndProduct, 0);
|
|
||||||
|
|
||||||
char serial_string[128] = {0};
|
|
||||||
char manufacturer_string[128] = {0};
|
|
||||||
char description_string[128] = {0};
|
|
||||||
ftdi_usb_get_strings2(ftdic, curdev->dev, manufacturer_string, 128, description_string, 128,
|
|
||||||
serial_string, 128);
|
|
||||||
|
|
||||||
QString serialNumber{serial_string};
|
|
||||||
QString ftdiOpenString;
|
|
||||||
if (!serialNumber.isEmpty()) {
|
|
||||||
ftdiOpenString = QString("s:%1:%2").arg(vendorAndProduct).arg(serialNumber);
|
|
||||||
} else {
|
|
||||||
ftdiOpenString = QString("i:%1:%2").arg(vendorAndProduct).arg(deviceIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
deviceList.push_back(QJsonObject{
|
|
||||||
{"deviceName", description_string},
|
|
||||||
{"systemLocation", ftdiOpenString}
|
|
||||||
});
|
|
||||||
deviceIndexes.insert(vendorAndProduct, deviceIndex + 1);
|
|
||||||
}
|
|
||||||
curdev = curdev->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ftdi_list_free(&devlist);
|
|
||||||
ftdi_free(ftdic);
|
|
||||||
#endif
|
|
||||||
devicesDiscovered.insert("devices", deviceList);
|
devicesDiscovered.insert("devices", deviceList);
|
||||||
DebugIf(verbose, _log, "devicesDiscovered: [%s]",
|
|
||||||
QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
Debug(_log, "SPI devices discovered: [%s]",
|
||||||
|
QString(QJsonDocument(devicesDiscovered).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||||
|
|
||||||
return devicesDiscovered;
|
return devicesDiscovered;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "HyperionConfig.h"
|
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
// HyperHDR includes
|
||||||
// Linux-SPI includes
|
|
||||||
#include <linux/spi/spidev.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
#include <ftdi.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Hyperion includes
|
|
||||||
#include <leddevice/LedDevice.h>
|
#include <leddevice/LedDevice.h>
|
||||||
|
#include <providers/BaseProvider.h>
|
||||||
|
|
||||||
|
|
||||||
enum SpiImplementation { SPIDEV, FTDI };
|
enum SpiImplementation { SPIDEV, FTDI };
|
||||||
|
|
||||||
|
@ -26,14 +15,14 @@ public:
|
||||||
///
|
///
|
||||||
/// Constructs specific LedDevice
|
/// Constructs specific LedDevice
|
||||||
///
|
///
|
||||||
ProviderSpi(const QJsonObject &deviceConfig);
|
ProviderSpi(const QJsonObject& deviceConfig);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Sets configuration
|
/// Sets configuration
|
||||||
///
|
///
|
||||||
/// @param deviceConfig the json device config
|
/// @param deviceConfig the json device config
|
||||||
/// @return true if success
|
/// @return true if success
|
||||||
bool init(const QJsonObject &deviceConfig) override;
|
bool init(const QJsonObject& deviceConfig) override;
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Destructor of the LedDevice; closes the output device if it is open
|
/// Destructor of the LedDevice; closes the output device if it is open
|
||||||
|
@ -43,14 +32,10 @@ public:
|
||||||
///
|
///
|
||||||
/// Opens and configures the output device
|
/// Opens and configures the output device
|
||||||
///
|
///
|
||||||
/// @return Zero on succes else negative
|
/// @return Zero on success else negative
|
||||||
///
|
///
|
||||||
int open() override;
|
int open() override;
|
||||||
|
|
||||||
/// @param[in] params Parameters used to overwrite discovery default behaviour
|
|
||||||
///
|
|
||||||
/// @return A JSON structure holding a list of devices found
|
|
||||||
///
|
|
||||||
QJsonObject discover(const QJsonObject& params) override;
|
QJsonObject discover(const QJsonObject& params) override;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
@ -70,31 +55,10 @@ protected:
|
||||||
///
|
///
|
||||||
/// @return Zero on success, else negative
|
/// @return Zero on success, else negative
|
||||||
///
|
///
|
||||||
int writeBytes(unsigned size, const uint8_t *data);
|
int writeBytes(unsigned size, const uint8_t* data);
|
||||||
|
|
||||||
/// The name of the output device
|
/// The name of the output device
|
||||||
QString _deviceName;
|
QString _deviceName;
|
||||||
|
|
||||||
/// The used baudrate of the output device
|
BaseProvider * _spiProvider;
|
||||||
int _baudRate_Hz;
|
|
||||||
#ifdef ENABLE_DEV_SPI
|
|
||||||
/// The File Identifier of the opened output device (or -1 if not opened)
|
|
||||||
int _fid;
|
|
||||||
|
|
||||||
/// which spi clock mode do we use? (0..3)
|
|
||||||
int _spiMode;
|
|
||||||
|
|
||||||
/// 1=>invert the data pattern
|
|
||||||
bool _spiDataInvert;
|
|
||||||
|
|
||||||
/// The transfer structure for writing to the spi-device
|
|
||||||
spi_ioc_transfer _spi;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ENABLE_DEV_FTDI
|
|
||||||
/// The Ftdi serial-device
|
|
||||||
struct ftdi_context *_ftdic;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SpiImplementation _spiImplementation;
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
#include "BaseProvider.h"
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
BaseProvider::BaseProvider(const QJsonObject& deviceConfig):
|
||||||
|
_deviceName("/dev/spidev0.0"),
|
||||||
|
_baudRate_Hz(1000000),
|
||||||
|
_activeDeviceType("UNSPECIFIED")
|
||||||
|
{
|
||||||
|
_activeDeviceType = deviceConfig["type"].toString(_activeDeviceType).toLower();
|
||||||
|
_log = Logger::getInstance("PROVIDER_" + _activeDeviceType.toUpper());
|
||||||
|
|
||||||
|
_deviceName = deviceConfig["output"].toString(_deviceName);
|
||||||
|
_baudRate_Hz = deviceConfig["rate"].toInt(_baudRate_Hz);
|
||||||
|
|
||||||
|
Debug(_log, "_deviceName [%s], _baudRate_Hz [%d]", QSTRING_CSTR(_deviceName), _baudRate_Hz);
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseProvider::~BaseProvider(){}
|
||||||
|
|
||||||
|
int BaseProvider::open() {
|
||||||
|
Debug(_log, "Open");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int BaseProvider::writeBytes(unsigned size, const uint8_t *data) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int BaseProvider::close() {
|
||||||
|
Debug(_log, "Close");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray BaseProvider::discover(const QJsonObject& /*params*/)
|
||||||
|
{
|
||||||
|
QJsonArray deviceList;
|
||||||
|
return deviceList;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#pragma once
|
||||||
|
#include "QObject"
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device.
|
||||||
|
///
|
||||||
|
class BaseProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
///
|
||||||
|
/// Constructs specific LedDevice
|
||||||
|
///
|
||||||
|
BaseProvider(const QJsonObject& deviceConfig);
|
||||||
|
///
|
||||||
|
/// Destructor of the LedDevice; closes the output device if it is open
|
||||||
|
///
|
||||||
|
~BaseProvider();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Opens and configures the output device
|
||||||
|
///
|
||||||
|
/// @return Zero on success else negative
|
||||||
|
///
|
||||||
|
virtual int open();
|
||||||
|
|
||||||
|
virtual QJsonArray discover(const QJsonObject& params);
|
||||||
|
|
||||||
|
virtual int close();
|
||||||
|
|
||||||
|
virtual int writeBytes(unsigned size, const uint8_t *data);
|
||||||
|
protected:
|
||||||
|
|
||||||
|
/// The common Logger instance for all LED-devices
|
||||||
|
Logger* _log;
|
||||||
|
|
||||||
|
/// Current device's type
|
||||||
|
QString _activeDeviceType;
|
||||||
|
|
||||||
|
/// The name of the output device
|
||||||
|
QString _deviceName;
|
||||||
|
|
||||||
|
/// The used baudrate of the output device
|
||||||
|
int _baudRate_Hz;
|
||||||
|
};
|
|
@ -0,0 +1,174 @@
|
||||||
|
#include "ProviderFtdi.h"
|
||||||
|
|
||||||
|
#include <utils/WaitTime.h>
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
#define ANY_FTDI_VENDOR 0x0
|
||||||
|
#define ANY_FTDI_PRODUCT 0x0
|
||||||
|
|
||||||
|
#include <ftdi.h>
|
||||||
|
#include <libusb.h>
|
||||||
|
#include <QEventLoop>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
namespace Pin {
|
||||||
|
// enumerate the AD bus for convenience.
|
||||||
|
enum bus_t {
|
||||||
|
SK = 0x01, // ADBUS0, SPI data clock
|
||||||
|
DO = 0x02, // ADBUS1, SPI data out
|
||||||
|
CS = 0x08, // ADBUS3, SPI chip select, active low
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#define FTDI_CHECK_RESULT(statement) if (statement) {Debug(_log, "FTDI ERROR: %s", ftdi_get_error_string(_ftdic)); return retVal;}
|
||||||
|
|
||||||
|
const unsigned char pinInitialState = Pin::CS;
|
||||||
|
// Use these pins as outputs
|
||||||
|
const unsigned char pinDirection = Pin::SK | Pin::DO | Pin::CS;
|
||||||
|
|
||||||
|
// Local HyperHDR includes
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
|
|
||||||
|
ProviderFtdi::ProviderFtdi(const QJsonObject &deviceConfig): BaseProvider(deviceConfig),
|
||||||
|
_ftdic(nullptr) {
|
||||||
|
Debug(_log, "Initialise ProviderFtdi");
|
||||||
|
}
|
||||||
|
|
||||||
|
ProviderFtdi::~ProviderFtdi() {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int ProviderFtdi::open() {
|
||||||
|
int retVal = -1;
|
||||||
|
_ftdic = ftdi_new();
|
||||||
|
|
||||||
|
Debug(_log, "Opening FTDI device=%s", QSTRING_CSTR(_deviceName));
|
||||||
|
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_usb_open_string(_ftdic, QSTRING_CSTR(_deviceName))) < 0);
|
||||||
|
/* doing this disable resets things if they were in a bad state */
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_disable_bitbang(_ftdic)) < 0);
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_setflowctrl(_ftdic, SIO_DISABLE_FLOW_CTRL)) < 0);
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET)) < 0);
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_set_bitmode(_ftdic, 0xff, BITMODE_MPSSE)) < 0);
|
||||||
|
|
||||||
|
|
||||||
|
double reference_clock = 60e6;
|
||||||
|
int divisor = (reference_clock / 2 / _baudRate_Hz) - 1;
|
||||||
|
std::vector<uint8_t> buf = {
|
||||||
|
DIS_DIV_5,
|
||||||
|
TCK_DIVISOR,
|
||||||
|
static_cast<unsigned char>(divisor),
|
||||||
|
static_cast<unsigned char>(divisor >> 8),
|
||||||
|
SET_BITS_LOW, // opcode: set low bits (ADBUS[0-7]
|
||||||
|
pinInitialState, // argument: inital pin state
|
||||||
|
pinDirection
|
||||||
|
};
|
||||||
|
|
||||||
|
FTDI_CHECK_RESULT((retVal = ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size());
|
||||||
|
Debug(_log, "Opened ftdi device=%s retVal=%d", QSTRING_CSTR(_deviceName), retVal);
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ProviderFtdi::close() {
|
||||||
|
int retVal = 0;
|
||||||
|
if (_ftdic != nullptr) {
|
||||||
|
Debug(_log, "Closing FTDI device");
|
||||||
|
// Delay to give time to push color black from writeBlack() into the led,
|
||||||
|
// otherwise frame transmission will be terminated half way through
|
||||||
|
wait(30);
|
||||||
|
ftdi_set_bitmode(_ftdic, 0x00, BITMODE_RESET);
|
||||||
|
ftdi_usb_close(_ftdic);
|
||||||
|
ftdi_free(_ftdic);
|
||||||
|
_ftdic = nullptr;
|
||||||
|
}
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ProviderFtdi::writeBytes(unsigned size, const uint8_t *data) {
|
||||||
|
int retVal = 0;
|
||||||
|
|
||||||
|
int count_arg = size - 1;
|
||||||
|
std::vector<uint8_t> buf = {
|
||||||
|
SET_BITS_LOW,
|
||||||
|
pinInitialState & ~Pin::CS,
|
||||||
|
pinDirection,
|
||||||
|
MPSSE_DO_WRITE | MPSSE_WRITE_NEG,
|
||||||
|
static_cast<unsigned char>(count_arg),
|
||||||
|
static_cast<unsigned char>(count_arg >> 8),
|
||||||
|
// LED's data will be inserted here
|
||||||
|
SET_BITS_LOW,
|
||||||
|
pinInitialState | Pin::CS,
|
||||||
|
pinDirection
|
||||||
|
};
|
||||||
|
// insert before last SET_BITS_LOW command
|
||||||
|
// SET_BITS_LOW takes 2 arguments, so we're inserting data in -3 position from the end
|
||||||
|
buf.insert(buf.end() - 3, &data[0], &data[size]);
|
||||||
|
|
||||||
|
FTDI_CHECK_RESULT((retVal = (ftdi_write_data(_ftdic, buf.data(), buf.size())) != buf.size()));
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray ProviderFtdi::discover(const QJsonObject & /*params*/) {
|
||||||
|
QJsonArray deviceList;
|
||||||
|
|
||||||
|
struct ftdi_device_list *devlist;
|
||||||
|
struct ftdi_context *ftdic;
|
||||||
|
|
||||||
|
ftdic = ftdi_new();
|
||||||
|
|
||||||
|
if (ftdi_usb_find_all(ftdic, &devlist, ANY_FTDI_VENDOR, ANY_FTDI_PRODUCT) > 0)
|
||||||
|
{
|
||||||
|
QMap<QString, uint8_t> deviceIndexes;
|
||||||
|
struct ftdi_device_list *curdev = devlist;
|
||||||
|
while (curdev)
|
||||||
|
{
|
||||||
|
char manufacturer[128] = {0}, serial_string[128] = {0};
|
||||||
|
ftdi_usb_get_strings(ftdic, curdev->dev, manufacturer, 128, NULL, 0, serial_string, 128);
|
||||||
|
|
||||||
|
libusb_device_descriptor desc;
|
||||||
|
libusb_get_device_descriptor(curdev->dev, &desc);
|
||||||
|
|
||||||
|
QString vendorAndProduct = QString("0x%1:0x%2")
|
||||||
|
.arg(desc.idVendor, 4, 16, QChar{'0'})
|
||||||
|
.arg(desc.idProduct, 4, 16, QChar{'0'});
|
||||||
|
|
||||||
|
QString serialNumber {serial_string};
|
||||||
|
QString ftdiOpenString;
|
||||||
|
if(!serialNumber.isEmpty())
|
||||||
|
{
|
||||||
|
ftdiOpenString = QString("s:%1:%2").arg(vendorAndProduct).arg(serialNumber);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uint8_t deviceIndex = deviceIndexes.value(vendorAndProduct, 0);
|
||||||
|
ftdiOpenString = QString("i:%1:%2").arg(vendorAndProduct).arg(deviceIndex);
|
||||||
|
deviceIndexes.insert(vendorAndProduct, deviceIndex + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString displayLabel = QString("%1 (%2)")
|
||||||
|
.arg(ftdiOpenString)
|
||||||
|
.arg(manufacturer);
|
||||||
|
|
||||||
|
deviceList.push_back(QJsonObject{
|
||||||
|
{"value", ftdiOpenString},
|
||||||
|
{"name", displayLabel}
|
||||||
|
});
|
||||||
|
|
||||||
|
curdev = curdev->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ftdi_list_free(&devlist);
|
||||||
|
ftdi_free(ftdic);
|
||||||
|
|
||||||
|
|
||||||
|
Debug(_log, "SPI devices discovered: [%s]",
|
||||||
|
QString(QJsonDocument(deviceList).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||||
|
|
||||||
|
return deviceList;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
#include <ftdi.h>
|
||||||
|
#include "providers/BaseProvider.h"
|
||||||
|
|
||||||
|
class ProviderFtdi : public BaseProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
///
|
||||||
|
/// Constructs specific LedDevice
|
||||||
|
///
|
||||||
|
ProviderFtdi(const QJsonObject& deviceConfig);
|
||||||
|
~ProviderFtdi();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Opens and configures the output device
|
||||||
|
///
|
||||||
|
/// @return Zero on success else negative
|
||||||
|
///
|
||||||
|
int open() override ;
|
||||||
|
|
||||||
|
QJsonArray discover(const QJsonObject& params) override;
|
||||||
|
|
||||||
|
int writeBytes(unsigned size, const uint8_t *data) override ;
|
||||||
|
|
||||||
|
int close() override ;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// The Ftdi serial-device
|
||||||
|
struct ftdi_context *_ftdic;
|
||||||
|
};
|
|
@ -0,0 +1,141 @@
|
||||||
|
#include "ProviderSpidev.h"
|
||||||
|
|
||||||
|
// STL includes
|
||||||
|
#include <cstring>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
|
// Linux includes
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <QDirIterator>
|
||||||
|
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
|
||||||
|
// Local HyperHDR includes
|
||||||
|
#include "utils/Logger.h"
|
||||||
|
|
||||||
|
|
||||||
|
ProviderSpidev::ProviderSpidev(const QJsonObject &deviceConfig)
|
||||||
|
: BaseProvider(deviceConfig),
|
||||||
|
_fid(-1),
|
||||||
|
_spiMode(SPI_MODE_0),
|
||||||
|
_spiDataInvert(false) {
|
||||||
|
memset(&_spi, 0, sizeof(_spi));
|
||||||
|
|
||||||
|
_spiMode = deviceConfig["spimode"].toInt(_spiMode);
|
||||||
|
_spiDataInvert = deviceConfig["invert"].toBool(_spiDataInvert);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProviderSpidev::~ProviderSpidev() {
|
||||||
|
}
|
||||||
|
|
||||||
|
int ProviderSpidev::open() {
|
||||||
|
int retVal = -1;
|
||||||
|
QString errortext;
|
||||||
|
const int bitsPerWord = 8;
|
||||||
|
|
||||||
|
_fid = ::open(QSTRING_CSTR(_deviceName), O_RDWR);
|
||||||
|
|
||||||
|
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
|
||||||
|
retVal = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (retVal < 0) {
|
||||||
|
errortext = QString("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (retVal < 0) {
|
||||||
|
// this->setInError(errortext);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ProviderSpidev::close() {
|
||||||
|
// LedDevice specific closing activities
|
||||||
|
int retVal = 0;
|
||||||
|
|
||||||
|
// Test, if device requires closing
|
||||||
|
if (_fid > -1) {
|
||||||
|
// Close device
|
||||||
|
if (::close(_fid) != 0) {
|
||||||
|
Error(_log, "Failed to close device (%s). Error message: %s", QSTRING_CSTR(_deviceName), strerror(errno));
|
||||||
|
retVal = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ProviderSpidev::writeBytes(unsigned size, const uint8_t *data) {
|
||||||
|
int retVal = 0;
|
||||||
|
|
||||||
|
uint8_t *newdata = nullptr;
|
||||||
|
|
||||||
|
if (_fid < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_spi.tx_buf = __u64(data);
|
||||||
|
_spi.len = __u32(size);
|
||||||
|
|
||||||
|
if (_spiDataInvert) {
|
||||||
|
newdata = (uint8_t *) malloc(size);
|
||||||
|
for (unsigned i = 0; i < size; i++) {
|
||||||
|
newdata[i] = data[i] ^ 0xff;
|
||||||
|
}
|
||||||
|
_spi.tx_buf = __u64(newdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
retVal = ioctl(_fid, SPI_IOC_MESSAGE(1), &_spi);
|
||||||
|
ErrorIf((retVal < 0), _log, "SPI failed to write. errno: %d, %s", errno, strerror(errno));
|
||||||
|
|
||||||
|
if (newdata != nullptr)
|
||||||
|
free(newdata);
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray ProviderSpidev::discover(const QJsonObject& /*params*/) {
|
||||||
|
QJsonArray deviceList;
|
||||||
|
QStringList files;
|
||||||
|
QDirIterator it("/dev", QStringList() << "spidev*", QDir::System);
|
||||||
|
|
||||||
|
while (it.hasNext())
|
||||||
|
files << it.next();
|
||||||
|
files.sort();
|
||||||
|
|
||||||
|
for (const auto &path: files)
|
||||||
|
deviceList.push_back(QJsonObject{
|
||||||
|
{"value", path},
|
||||||
|
{"name", path}});
|
||||||
|
|
||||||
|
Debug(_log, "SPI devices discovered: [%s]",
|
||||||
|
QString(QJsonDocument(deviceList).toJson(QJsonDocument::Compact)).toUtf8().constData());
|
||||||
|
|
||||||
|
return deviceList;
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
#pragma once
|
||||||
|
#include "providers/BaseProvider.h"
|
||||||
|
// Linux-SPI includes
|
||||||
|
#include <linux/spi/spidev.h>
|
||||||
|
#include "QObject"
|
||||||
|
|
||||||
|
///
|
||||||
|
/// The ProviderSpi implements an abstract base-class for LedDevices using the SPI-device.
|
||||||
|
///
|
||||||
|
class ProviderSpidev : public BaseProvider
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
///
|
||||||
|
/// Constructs specific LedDevice
|
||||||
|
///
|
||||||
|
ProviderSpidev(const QJsonObject& deviceConfig);
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Destructor of the LedDevice; closes the output device if it is open
|
||||||
|
///
|
||||||
|
~ProviderSpidev();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Opens and configures the output device
|
||||||
|
///
|
||||||
|
/// @return Zero on succes else negative
|
||||||
|
///
|
||||||
|
int open() override;
|
||||||
|
QJsonArray discover(const QJsonObject& params) override;
|
||||||
|
int close() override;
|
||||||
|
int writeBytes(unsigned size, const uint8_t* data) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// The File Identifier of the opened output device (or -1 if not opened)
|
||||||
|
int _fid;
|
||||||
|
|
||||||
|
/// which spi clock mode do we use? (0..3)
|
||||||
|
int _spiMode;
|
||||||
|
|
||||||
|
/// 1=>invert the data pattern
|
||||||
|
bool _spiDataInvert;
|
||||||
|
|
||||||
|
/// The transfer structure for writing to the spi-device
|
||||||
|
spi_ioc_transfer _spi;
|
||||||
|
};
|
Loading…
Reference in New Issue