×

Due to the impacts of the coronavirus outbreak, we are experiencing longer than normal lead times on certain products. We encourage back-ordering out-of-stock items to receive them as soon as possible.

Member #1573123

Member Since: January 20, 2020

Country: United States

  • No hard feelings, but this seems like a lot of effort to work around a single line of code. As the article shows, Arduino's library contains a single line that writes back into the provided buffer. It was a bad design on Arduino's part, but I suspect they are stuck with that API/functionality across the product line now.

    To give us some flexibility, I'd write a new SPI transfer function. This is untested, but here's my idea.

    void spi_transfer(SPIClass & dev, void *buf_mosi, void *buf_miso, size_t nbytes)
    {
      uint8_t *buffer_mosi = reinterpret_cast<uint8_t *>(buf_mosi);
      uint8_t *buffer_miso = reinterpret_cast<uint8_t *>(buf_miso);
      byte tmp_byte;
      for (size_t i=0; i < nbytes; i++) {
    
        if (nullptr != buf_mosi)
        {
          tmp_byte = dev.transfer(*buffer_mosi++);
        }
        else
        {
          tmp_byte = dev.transfer(0);
        }
    
        if (nullptr != buf_miso)
        {
          *buffer_miso++ = tmp_byte;
        }
      }
    }
    

    This would allow for either full duplex (both directions) or half-duplex (one direction) communication, by simply setting one of the pointers to NULL (or "nullptr" in C++).

No public wish lists :(