Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

SPI.transfer16 not working as expected #85

Closed
@rmlearney-digicatapult

Description

@rmlearney-digicatapult

One expects SPI.transfer16() to be functionally equivalent to 2x sequential calls to SPI.transfer().

However the following functions produce very different results on my Nano 33 BLE communicating with an ADS1118, despite identical appearance. In particular, the function using SPI.transfer16() does not work but the SPI.transfer() one does.

int16_t ADS1118::getData(void){
  uint8_t buffer[2];
  int16_t retVal;

  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
  buffer[0] = SPI.transfer(((_config >> 8) & 0xFF)); // MSB
  buffer[1] = SPI.transfer((_config & 0xFF)); // LSB
  SPI.transfer(0x00); // Have to do 32-bit write if !CS held low
  SPI.transfer(0x00);
  SPI.endTransaction();

  retVal = ((buffer[0] << 8) | buffer[1]);
  return retVal;
}
int16_t ADS1118::getData16(void){
  int16_t retVal;

  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));
  retVal = SPI.transfer16(_config);
  SPI.transfer16(0x0000); // Have to do 32-bit write if !CS held low
  SPI.endTransaction();

  return retVal;
}

This unexpected difference led to about 4 weeks of hardware debugging, and I advise anyone using SPI.transfer16() on the Nano BLE 33 to rewrite your drivers to use 2x 8-bit transfers via SPI.transfer() until this is solved.

Would really appreciate a review of MBed's SPI system to explain why these are not functionally equivalent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions