0

I am wondering how the WiringPi SPI works without using the chip select/enable spi.cs_change = true; in the following 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.

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;
}

Can someone please explain how this works?

2

What you do with the chip-select or chip-enable depends largely on what kind of a device you are communicating with. Some devices, such as programmable LEDs, may not use a chip-select at all. Some devices may allow you to permanently connect the chip-select so that it is always asserted. Of course, without using chip-selects you are generally limited to a single device on the SPI bus.

Some devices, notably some analog-to-digital converters, use the chip-select signal to cause some desired behavior to occur and require that the chip-select be deasserted between readings.

In order to know what you need to do, you need to study the datasheet for your peripheral device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.