1
0
Fork 0
mirror of https://github.com/iNavFlight/inav.git synced 2025-07-26 17:55:28 +03:00

Implement busTransferMultiple abstraction layer

This commit is contained in:
Konstantin Sharlaimov (DigitalEntity) 2017-12-04 01:44:40 +10:00
parent 15455c253d
commit 0ae6cb02d9
3 changed files with 44 additions and 16 deletions

View file

@ -206,25 +206,40 @@ void busDeviceWriteScratchpad(busDevice_t * dev, uint32_t value)
bool busTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length) bool busTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length)
{ {
#ifdef USE_SPI
// busTransfer function is only supported on SPI bus
if (dev->busType == BUSTYPE_SPI) {
busTransferDescriptor_t dsc = {
.rxBuf = rxBuf,
.txBuf = txBuf,
.length = length
};
return spiBusTransferMultiple(dev, &dsc, 1);
}
#else
UNUSED(rxBuf); UNUSED(rxBuf);
UNUSED(txBuf); UNUSED(txBuf);
UNUSED(length); UNUSED(length);
switch (dev->busType) {
case BUSTYPE_SPI:
#ifdef USE_SPI
return spiBusTransfer(dev, rxBuf, txBuf, length);
#else
return false;
#endif #endif
case BUSTYPE_I2C:
// Raw transfer operation is not supported on I2C
return false; return false;
}
default: bool busTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * dsc, int count)
return false; {
#ifdef USE_SPI
// busTransfer function is only supported on SPI bus
if (dev->busType == BUSTYPE_SPI) {
return spiBusTransferMultiple(dev, dsc, count);
} }
#else
UNUSED(rxBuf);
UNUSED(txBuf);
UNUSED(length);
#endif
return false;
} }
bool busWriteBuf(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length) bool busWriteBuf(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length)

View file

@ -220,6 +220,13 @@ extern const busDeviceDescriptor_t __busdev_registry_end[];
BUSDEV_REGISTER_I2C_F(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags) BUSDEV_REGISTER_I2C_F(_name, _devHw, _i2cBus, _devAddr, _irqPin, _tag, _flags)
// busTransfer and busTransferMultiple are supported only on full-duplex SPI bus
typedef struct busTransferDescriptor_s {
uint8_t * rxBuf;
const uint8_t * txBuf;
uint32_t length;
} busTransferDescriptor_t;
/* Internal abstraction function */ /* Internal abstraction function */
bool i2cBusWriteBuffer(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length); bool i2cBusWriteBuffer(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length);
bool i2cBusWriteRegister(const busDevice_t * dev, uint8_t reg, uint8_t data); bool i2cBusWriteRegister(const busDevice_t * dev, uint8_t reg, uint8_t data);
@ -228,7 +235,7 @@ bool i2cBusReadRegister(const busDevice_t * dev, uint8_t reg, uint8_t * data);
void spiBusSetSpeed(const busDevice_t * dev, busSpeed_e speed); void spiBusSetSpeed(const busDevice_t * dev, busSpeed_e speed);
bool spiBusTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length); bool spiBusTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * dsc, int count);
bool spiBusWriteBuffer(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length); bool spiBusWriteBuffer(const busDevice_t * dev, uint8_t reg, const uint8_t * data, uint8_t length);
bool spiBusWriteRegister(const busDevice_t * dev, uint8_t reg, uint8_t data); bool spiBusWriteRegister(const busDevice_t * dev, uint8_t reg, uint8_t data);
bool spiBusReadBuffer(const busDevice_t * dev, uint8_t reg, uint8_t * data, uint8_t length); bool spiBusReadBuffer(const busDevice_t * dev, uint8_t reg, uint8_t * data, uint8_t length);
@ -248,8 +255,10 @@ void busDeviceWriteScratchpad(busDevice_t * dev, uint32_t value);
void busSetSpeed(const busDevice_t * dev, busSpeed_e speed); void busSetSpeed(const busDevice_t * dev, busSpeed_e speed);
bool busTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length);
bool busWriteBuf(const busDevice_t * busdev, uint8_t reg, const uint8_t * data, uint8_t length); bool busWriteBuf(const busDevice_t * busdev, uint8_t reg, const uint8_t * data, uint8_t length);
bool busReadBuf(const busDevice_t * busdev, uint8_t reg, uint8_t * data, uint8_t length); bool busReadBuf(const busDevice_t * busdev, uint8_t reg, uint8_t * data, uint8_t length);
bool busRead(const busDevice_t * busdev, uint8_t reg, uint8_t * data); bool busRead(const busDevice_t * busdev, uint8_t reg, uint8_t * data);
bool busWrite(const busDevice_t * busdev, uint8_t reg, uint8_t data); bool busWrite(const busDevice_t * busdev, uint8_t reg, uint8_t data);
bool busTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length);
bool busTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * buffers, int count);

View file

@ -37,12 +37,16 @@ void spiBusSetSpeed(const busDevice_t * dev, busSpeed_e speed)
} }
bool spiBusTransfer(const busDevice_t * dev, uint8_t * rxBuf, const uint8_t * txBuf, int length) bool spiBusTransferMultiple(const busDevice_t * dev, busTransferDescriptor_t * dsc, int count)
{ {
SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus); SPI_TypeDef * instance = spiInstanceByDevice(dev->busdev.spi.spiBus);
IOLo(dev->busdev.spi.csnPin); IOLo(dev->busdev.spi.csnPin);
spiTransfer(instance, rxBuf, txBuf, length);
for (int n = 0; n < count; n++) {
spiTransfer(instance, dsc[n].rxBuf, dsc[n].txBuf, dsc[n].length);
}
IOHi(dev->busdev.spi.csnPin); IOHi(dev->busdev.spi.csnPin);
return true; return true;