SPI read, write and command

Posted on
  • Hi,

    I've been looking at this code to try and convert to run an e-ink display (M5Paper) (https://github.com/gnzzz/IT8951/blob/89d­505b26f130539644117522a0fdc128ffe44c1/li­b/it8951.ts)

    I'm pretty new to SPI so have been trying to figure it out.

    looking through the forums I've managed to piece together 3 methods for reading, writing and sending commands

    var read = function(reg,len) { // read
      return SPI1.send([reg|SPI_PREAMBLE.READ,new Uint8Array(len)], cs).slice(1);
    };
    
    var write = function(reg,data) { // write
        return SPI1.write(reg, data, cs);
    };
    
    var command = function(c,d) {
        SPI1.write(c, cs);
        if (d!==undefined) {
          dc.set();
          SPI1.write(d, cs);
        }
      }
    

    But it doesn't work. So for example in the code I'm looking at, this gets called

     this.setRegister(SYS_REG.I80CPCR, 0x0001);
     this.spi.writeWords(
                Uint16Array.from([SPI_PREAMBLE.CMD, USDEF_I80_CMD.GET_DEV_INFO])
     );
     const data = this.spi.readWords(20);
    

    I've converted this to:

    write(SPI_PREAMBLE.WRITE|SYS_REG.I80CPCR­, 0x0001);
    let info = read(SPI_PREAMBLE.CMD|USDEF_I80_CMD.GET_­DEV_INFO,20)
    

    but info is just full of zeros.

    I'm not sure if I'm totally on the wrong track or just missing some key bit of information.

    Thanks,

    Rob Smart

  • Tue 2021.03.09

    Hi @Ruprect99 Rob, I do not have that hardware to test, but a few ideas do come to mind:

    'but info is just full of zeros.'

    If the commands going out are correct, it sounds like they aren't received, so no data transfer occurs. Is it possible MISO and MOSI are wired backwards?

    https://en.wikipedia.org/wiki/Serial_Per­ipheral_Interface

    This was a fundamental struggle I had, with my first experience with SPI. Your snippets do not show how SPI is setup, so I'd first double check to make sure the code definitions match the actual wiring. Also, make sure of a solid ground between the uP, the e-Ink display and the power supply.

    Has an attempt been made to mirror the code shown in another e-Ink example:

    https://www.espruino.com/SSD1606


    Using the available SPI functions:

    http://www.espruino.com/Reference#SPI
    http://www.espruino.com/SPI

    The snippets shown in post #1 don't quite mirror the SSD1606 e-Ink example. I had a similar experience and the community helped with SPI examples and snippets:

    Have you tried hard coding the register values in place of the constants, to verify that the constants are as they should be. e.g. 32bit vs 16bit, actual hex value and not a string representation, etc.

    Examples snippets

    http://forum.espruino.com/conversations/­336724/#comment14838658



    For what it's worth, after hours of struggle with SPI, I2C and UARTS, I eventually buckled and picked up a logic analyzer ~$20 that provides immediate feedback on what is on the data lines. I had a floating CS chip Select that rounded out the leading edge, so the receiving device never started the data receive, and thus nothing during the expected read was seen. Here are some sample images, but with a UART:

    JPG Images

    http://forum.espruino.com/comments/14687­689/

    Note the protocol analyzer output that allows the conversion of Hi-Lo to actual ASCII we may read



    Here is a full example by @urish, follow the gist link:

    http://forum.espruino.com/comments/14303­452/

  • Just a guess, in this code

     var command = function(c,d) {
     
     SPI1.write(c, cs);
     if (d!==undefined) {
       dc.set();
       SPI1.write(d, cs);
     }
     }
    

    you are not sending command+data as one block with cs low, not sure what device protocol there is but typically cs should be low for whole piece that belongs together. When cs goes high the device state machine typically goes to beginning (even to sleep) so forgets its state. But that's just a guess, don't know this device.

    EDIT:
    also don't see dc.clear(); before sending the command

  • Hi,

    I hadn't seen that other e-ink display. I can certainly use that for some help.

    I need to map out the manufacturers process in the c code they supply as I think there might be some more steps (like actually powering the display on first) that I might be missing.

    I was aware of the getting the spi lines round the wrong way so did try it both ways around.

    I will update on progress when I make some!

    Thanks

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

SPI read, write and command

Posted by Avatar for Ruprect99 @Ruprect99

Actions