Skip to content

SPI.transfer16 - does not properly handle word size. #13

Closed
@KurtE

Description

@KurtE

Describe the bug
SPI.transfer16(0x2ff) - with MSBFIRST defined in the transaction, outputs the bytes in LSB, MSB order

The issue, is that the code, does not update the word size from 8 to 16 in the config.operation field.

Optional: attach the sketch

#include <SPI.h>
void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    SPI.begin();
    SPI.beginTransaction(SPISettings(30000000, MSBFIRST, SPI_MODE0));
    pinMode(53, OUTPUT);
    digitalWrite(53, HIGH);
}

uint16_t loop_count = 0;

void loop() {
    loop_count++;
    digitalWrite(53, LOW);
    SPI.transfer(loop_count >> 8);
    SPI.transfer(loop_count & 0xff);

    delayMicroseconds(5);
    SPI.transfer16(loop_count);
}


Additional context
I thought I could fix this, by simply editing the Transfer16 method to:

uint16_t arduino::ZephyrSPI::transfer16(uint16_t data) {
  int ret;
  uint16_t rx;
  const struct spi_buf tx_buf = {.buf = &data, .len = sizeof(data)};
  const struct spi_buf_set tx_buf_set = {
      .buffers = &tx_buf,
      .count = 1,
  };
  const struct spi_buf rx_buf = {.buf = &rx, .len = sizeof(rx)};
  const struct spi_buf_set rx_buf_set = {
      .buffers = &rx_buf,
      .count = 1,
  };

  spi_operation_t operation_save = config.operation;
  config.operation = (operation_save &= ~(SPI_WORD_SET(0x3f))) | SPI_WORD_SET(16);

  ret = spi_transceive(spi_dev, &config, &tx_buf_set, &rx_buf_set);
  config.operation = operation_save;
  if (ret < 0) {
    return 0;
  }

  return rx;
}

But running it faulted:


uart:~$ sketch
[00:00:12.771,000] <err> os: ***** USAGE FAULT *****
[00:00:12.777,000] <err> os:   Attempt to execute undefined instruction
[00:00:12.784,000] <err> os: r0/a1:  0x00000000  r1/a2:  0x00000000  r2/a3:  0x240008b0
[00:00:12.793,000] <err> os: r3/a4:  0x240008b0 r12/ip:  0x0805b2a0 r14/lr:  0x0804e8f9
[00:00:12.801,000] <err> os:  xpsr:  0x61000000
[00:00:12.807,000] <err> os: s[ 0]:  0x2400eb6d  s[ 1]:  0x2400eba3  s[ 2]:  0x2400f05d  s[ 3]:  0x2400d714
[00:00:12.817,000] <err> os: s[ 4]:  0x2400eb89  s[ 5]:  0x08048dfd  s[ 6]:  0x2400d714  s[ 7]:  0x2400eb89
[00:00:12.827,000] <err> os: s[ 8]:  0x00000000  s[ 9]:  0x00000000  s[10]:  0x00000000  s[11]:  0x00000000
[00:00:12.838,000] <err> os: s[12]:  0x00003a48  s[13]:  0x08042adf  s[14]:  0xaaaaaaaa  s[15]:  0xaaaaaaaa
[00:00:12.848,000] <err> os: fpscr:  0xaaaaaaaa
[00:00:12.853,000] <err> os: Faulting instruction address (r15/pc): 0x2400e750
[00:00:12.861,000] <err> os: >>> ZEPHYR FATAL ERROR 36: Unknown error on CPU 0
[00:00:12.869,000] <err> os: Current thread: 0x240008b0 (shell_uart)
[00:00:12.876,000] <err> os: Halting system

I probably missed something, but thought I would at least first mention this as an issue.

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