Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transfer16 equivalence #1

Open
rtek1000 opened this issue Apr 26, 2019 · 1 comment
Open

transfer16 equivalence #1

rtek1000 opened this issue Apr 26, 2019 · 1 comment

Comments

@rtek1000
Copy link

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!

@Riflio
Copy link

Riflio commented Jul 18, 2019

#2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants