Author Topic: SPI.transfer16 problem  (Read 3282 times)

postuma

  • Jr. Member
  • **
  • Posts: 64
SPI.transfer16 problem
« on: November 29, 2016, 05:12:44 pm »
Just a head's up for anyone using the SPI.transfer16 function: the current implementation is bugged - it uses little-endian where it should use big-endian transfers, i.e. the LSBFIRST and MSBFIRST sections are reversed. Should read:

Code: [Select]
uint16_t SPIClass::transfer16(uint16_t data) {
    union {
            uint16_t val;
            struct {
                    uint8_t lsb;
                    uint8_t msb;
            };
    } in, out;
    in.val = data;

    if((SPI1C & (SPICWBO | SPICRBO))) {
        //LSBFIRST
        out.lsb = transfer(in.lsb);
        out.msb = transfer(in.msb);
    } else {
        //MSBFIRST
        out.msb = transfer(in.msb);
        out.lsb = transfer(in.lsb);
    }
    return out.val;
}

as per the latest ESP8266 Arduino code at https://github.com/esp8266/Arduino/blob/master/libraries/SPI/SPI.cpp

Caused some major issues until I figured it out. Working on a touch-screen implementation I plan to post soon.
« Last Edit: November 29, 2016, 05:15:53 pm by postuma »

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: SPI.transfer16 problem
« Reply #1 on: November 29, 2016, 11:31:10 pm »
Thanks for that... I'll put a pull request in with the corrections. Hopefully in the near future the Oak stuff can essentially be a variant of the Arduino ESP8266 core, then we'll get the benefits of all their changes as they come through.

postuma

  • Jr. Member
  • **
  • Posts: 64
Re: SPI.transfer16 problem
« Reply #2 on: December 12, 2016, 03:43:35 pm »
So I've got the touch screen up and running - more at https://digistump.com/board/index.php/topic,2508.msg11933.html#msg11933