• Hey πŸ‘‹,

    I'm playing around with the SX127x library in a large project, and have some problems using the HW SPI with the SX127x module.

    I've boiled an example down based on the example code.

    This works (soft SPI):

    // Pins
    const PIN_SCK = D5;
    const PIN_MISO = D18;
    const PIN_MOSI = D23;
    const PIN_CS = D19;                               // NSS
    const PIN_RESET = D21;
    
    // Configuration
    const txConfig = {
        bandwidth: 0,
        freq: 434330000,
        power: 17,
        forcePaBoost: true,
    };
    
    // Setup
    const spi = new SPI();
    spi.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    var sx = require("SX127x").connect({ spi: spi, cs: PIN_CS, rst: PIN_RESET });
    
    // Poll DIO0 for interrupts
    setInterval(function () { sx.onIRQ(); }, 100);
    
    sx.setTxConfig(txConfig);
    
    // This one uses an interval, to show that it works continuously
    setInterval(() => {
      sx.send("Hello", function () {
          console.log("TX done");
      });
    }, 500);
    

    But this doesn't (HW SPI, same txConfig, same pins):

    // Setup
    SPI1.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    var sx = require("SX127x").connect({ spi: SPI1, cs: PIN_CS, rst: PIN_RESET });
    
    // Poll DIO0 for interrupts
    setInterval(function () { sx.onIRQ(); }, 100);
    
    sx.setTxConfig(txConfig);
    
    // This one is just "locked" in a state of constant transmission (see screen capture)
    sx.send("Hello", function () {
        console.log("TX done");
    });
    

    My LoRa module is the AI Thinker RA-02.

    Anyone have an idea as to why πŸ™‚?

    I've attached examples of the captured radio outputs, but I think I've boiled it down to the HW SPI being off in some way, and not setting the radio up properly to begin with.


    2 Attachments

    • HW SPI.gif
    • SW SPI.gif
  • hi,

    the hardware SPI’s are only avaiable on specific pins:

    SPI1: SCLK D14, MISO D12, MOSI D13

    SPI2: SCLK D18, MISO D19, MOSI D23

  • Great, I'll try that out πŸ™‚! I did think about that many, many, many, times, but I just could not track down the information, where did you find it?

  • I just know it and it should be added to the docs as mentioned in this issue.

    More details about the ESP32 SPI can be found here.

  • I just know it

    to be honest, it took me several hours to figure this out, when I tried to use it the first time ;-)

  • Which hardware are you using to captured the radio outputs?

  • to be honest, it took me several hours to figure this out, when I tried to use it the first time ;-)

    Glad to know I'm not alone 😁

    Which hardware are you using to captured the radio outputs?

    I'm using the HackRF One I bought a few years back, it's been priceless! When a signal is not received, it's invaluable to know that the transmitter at least tries to do something--that's a good half of the setup I don't have to debug right there ⭐⭐⭐.

  • OK, so I've replaced the pins with the ones you mention--but still struggle a little πŸ™‚!

    Setup now looks like this (only the pins has changed):

    const PIN_SCK = D18;
    const PIN_MISO = D19;
    const PIN_MOSI = D23;
    const PIN_CS = D5;        // NSS
    const PIN_RESET = D21;
    
    // Configuration
    const txConfig = {
        bandwidth: 0,
        freq: 434330000,
        power: 17,
        forcePaBoost: true,
    };
    

    And the code like this:

    // Setup
    const spi = new SPI();
    spi.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    const sx = require("SX127x").connect({ spi: spi, cs: PIN_CS, rst: PIN_RESET });
    
    // HW SPI (replace the three lines above)
    // SPI2.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    // const sx = require("SX127x").connect({ spi: SPI2, cs: PIN_CS, rst: PIN_RESET });
    
    // Poll DIO0 for interrupts
    setInterval(function () { sx.onIRQ(); }, 100);
    sx.setTxConfig(txConfig);
    
    // Transmit
    sx.send("Hello", function () {
        console.log("TX done");
    });
    

    I've attached two screenshots of the SCK, MOSI and MISO, and it looks like nothing is happening on the MOSI line when using HW SPI with this example--I'm sure I just missed something obvious, I hope someone can spot it πŸ˜¬πŸ™Œ?

    (are there restrictions to the CS/RESET pins as well perhaps? Do I need to set the I/O "mode" for some of the pins manually? Do I need to specify more parameters when using HW- as opposed to SW SPI?)


    2 Attachments

    • HW SPI.png
    • SW SPI.png
  • Do you what use soft or hard SPI (SPI1 and SPI2 is hardware)?
    In the sample of post #8 is using software.

    What about the baud rate, don't you need a specific one?

  • Sorry, the example was not clear enough πŸ™‚. I just made an example where I could easily switch between using SW spi (the stuff with ".. new SPI ()... "), and HW SPI (the one with "SPI2 ... ") for this demonstration.

    The problem occurs when I try to use the HW spi (the block I commented out in the example of #8).

    Yes, setting the baud rate was what started this endeavour in the very first rate--but first I need HW SPI to work though πŸ™‚.

  • So to be completely clear, this is what's not working for me πŸ™‚:

    const PIN_SCK = D18;
    const PIN_MISO = D19;
    const PIN_MOSI = D23;
    const PIN_CS = D5;        // NSS
    const PIN_RESET = D21;
    
    // Configuration
    const txConfig = {
        bandwidth: 0,
        freq: 434330000,
        power: 17,
        forcePaBoost: true,
    };
    
    // HW SPI (replace the three lines above)
    SPI2.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI });
    const sx = require("SX127x").connect({ spi: SPI2, cs: PIN_CS, rst: PIN_RESET });
    
    // Poll DIO0 for interrupts
    setInterval(function () { sx.onIRQ(); }, 100);
    sx.setTxConfig(txConfig);
    
    // Transmit
    sx.send("Hello", function () {
        console.log("TX done");
    });
    

    (Edit: Perhaps I should try to remove the SX127x stuff, and boil it down to only using the SPI, I'll be back.)

  • OK, perhaps I'm on to something (my own mistake 😬).

    I'm using a chip called AI Thinker-A1S, which I'm sure I've read somewhere should be compatible with the Espressif ESP32-WROVER (which uses the IOs MaBe mentioned), but I can't find that information now.

    Reading the datasheet of the AI Thinker-A1S, it looks like the IOs used for SPI is a little different though; MISO is IO21 and not IO19 (although they use a terminology like VSPIIDHS1STROBE instead of VSPID as the WROVER does).

    Is there anything I can do to make HW SPI work on this device?

  • In any case this is what I see on the SPI lines for HW and SW SPI of this example code (nothing on MOSI):

    const PIN_SCK = D18;
    const PIN_MISO = D19;
    const PIN_MOSI = D23;
    
    // HW SPI
    SPI2.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI});
    SPI2.write([1,2,3]);
    
    // SW SPI
    // const spi = new SPI();
    // spi.setup({ sck: PIN_SCK, miso: PIN_MISO, mosi: PIN_MOSI});
    // spi.write([1,2,3]);
    

    2 Attachments

    • HW SPI, SPI only example.png
    • SW SPI, SPI only example.png
  • OK, I think I got to the heart of the problem, SPI1 is working beautifully (I haven't tested with the SX library and all that, but I get data through MOSI now).

    Apparantly capacitors on the AI Thinker ESP32-A1S prevents us from using GPIOs 5, 18, 19 and 23 for SPI (according to this comment (Edit: may be incorrect, the comment is referring to a dev. kit using the same chip)).

    Thank you MaBe for your help!

    Edit: still having issues, posting a new question πŸ™‚

  • Which SX127x devices are you using for send and receive?

  • AI Thinker Ra-02

  • Yes. If you tell me what you're up to, I might be able to provide you with better answers.

  • mainly I like to build up some LoRa knowledge.

    Some sender and a gateway

    Edit: Using Espruino as firmware.

  • Ah ok, I wasn't sure if you were still in debugging mode. Great to have more Espruino guys using this πŸ‘.

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

Unexpected behavior using SW vs. HW SPI on ESP32 with the SX127x LoRa module

Posted by Avatar for rj @rj

Actions