Skip to content

transfer16 equivalence #1

Description

@rtek1000

Hello, nice job!

The arduino's current hardware library has a 16-bit transfer routine. Would it be possible to add this routine to the library via software?

This seems to be the original code (Arduino IDE source):

  inline static uint16_t transfer16(uint16_t data) {
    union { uint16_t val; struct { uint8_t lsb; uint8_t msb; }; } in, out;
    in.val = data;
    if (!(SPCR & _BV(DORD))) {
      SPDR = in.msb;
      asm volatile("nop"); // See transfer(uint8_t) function
      while (!(SPSR & _BV(SPIF))) ;
      out.msb = SPDR;
      SPDR = in.lsb;
      asm volatile("nop");
      while (!(SPSR & _BV(SPIF))) ;
      out.lsb = SPDR;
    } else {
      SPDR = in.lsb;
      asm volatile("nop");
      while (!(SPSR & _BV(SPIF))) ;
      out.lsb = SPDR;
      SPDR = in.msb;
      asm volatile("nop");
      while (!(SPSR & _BV(SPIF))) ;
      out.msb = SPDR;
    }
    return out.val;
  }

The equivalent could be this way, as below?

inline static uint16_t transfer16(uint16_t data, bool MSB1ST = true) {
  union {
    uint16_t val;
    struct {
      uint8_t lsb;
      uint8_t msb;
    };
  } in, out;
  in.val = data;

  if (MSB1ST) {
    out.msb = mySPI.transfer(in.msb);
    out.lsb = mySPI.transfer(in.lsb);
  } else {
    out.lsb = mySPI.transfer(in.lsb);
    out.msb = mySPI.transfer(in.msb);
  }

  return out.val;
}

Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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