emaq
Member level 4
How to manually toggle the chip select of Raspberry Pi 3 SPI in the following code?
My objective is to call transfer() twice for 16-bit transaction.
My objective is to call transfer() twice for 16-bit transaction.
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;
}