emaq
Member level 4
I am wondering how the WiringPi SPI works without using the chip select/enable in the following code.
It seems if I send two consecutive bytes (16-bit data) with WiringPi it will be transmitted as synchronized with clock and the corresponding 16-bit data will be received. But in that case do I have to pull down the CE pin of the device connected to raspberry pi SPI (spidev0.0) by manually connecting it to the ground permanently?
A similar following SPI code does not use chip select/enable.
Can someone please explain how this works?
Code:
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
{
struct spi_ioc_transfer spi ;
channel &= 1 ;
memset (&spi, 0, sizeof (spi)) ;
spi.tx_buf = (unsigned long)data ;
spi.rx_buf = (unsigned long)data ;
spi.len = len ;
spi.delay_usecs = spiDelay ;
spi.speed_hz = spiSpeeds [channel] ;
spi.bits_per_word = spiBPW ;
return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
}
It seems if I send two consecutive bytes (16-bit data) with WiringPi it will be transmitted as synchronized with clock and the corresponding 16-bit data will be received. But in that case do I have to pull down the CE pin of the device connected to raspberry pi SPI (spidev0.0) by manually connecting it to the ground permanently?
A similar following SPI code does not use chip select/enable.
Code:
int
SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
{
if ((send == nullptr) && (recv == nullptr)) {
return -EINVAL;
}
// set write mode of SPI
int result = ::ioctl(_fd, SPI_IOC_WR_MODE, &_mode);
if (result == -1) {
PX4_ERR("can’t set spi mode");
return PX4_ERROR;
}
spi_ioc_transfer spi_transfer{};
spi_transfer.tx_buf = (uint64_t)send;
spi_transfer.rx_buf = (uint64_t)recv;
spi_transfer.len = len;
spi_transfer.speed_hz = _frequency;
spi_transfer.bits_per_word = 8;
result = ::ioctl(_fd, SPI_IOC_MESSAGE(1), &spi_transfer);
if (result != (int)len) {
PX4_ERR("write failed. Reported %d bytes written (%s)", result, strerror(errno));
return PX4_ERROR;
}
return PX4_OK;
}
Last edited: