Struggling with Electron as SPI slave


#1

Dear all,

I’m currently struggling with a RM186 LoRa/BLE module which only supports SPI master role (bad) and smartBASIC as “programming language” (worse).

I’ll try to keep it short.

On the RM186 side, the code looks basically like this:

GpioSetFunc(SPI_CS_PIN,2,1) // configures CS as output with initial state HI
GpioWrite(SPI_CS_PIN,1)  // sets CS HI, just to be sure
spiTX$ = "\03\01\80\DA\DA" // data to send, 0x020180ff
SpiOpen(0, 125000, 0, SPIHandle) // open SPI in mode 0, 125khz, MSB

// code for transfer in function "SPI_Send()"
GpioWrite(SPI_CS_PIN,0) // set CS LO
SpiWrite(spiTX$) // write our 5 bytes
GpioWrite(SPI_CS_PIN,1) // CS HI

And here’s the Electron side as a slave

SPI.onSelect(slaveSelect);
SPI.setClockSpeed(125, KHZ);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.begin(SPI_MODE_SLAVE, SS_PIN);

void slaveSelect(uint8_t state) {
    if (state) {
        SPI.transfer(sendValues, rcvdValues, NUM_VALUES, slaveCallback);
    }
}

void slaveCallback() {
    int rxCount = SPI.available();
    Serial.print("Slave callback, rxCount:");
    Serial.println(rxCount);
    for(int i=0; i<NUM_VALUES; i++)
        Serial.printf("%02x ", rcvdValues[i]);

    Serial.print("End slave callback");
    gotValue = true;
}

Now, what happens is, that the first transmission seems to be empty and I have to call SPI_Send() on master side a second time. Here’s the Electron’s log:

Slave callback, rxCount:0
00 00 00 00 00
End slave callback
Slave callback, rxCount:5
03 01 80 da da
End slave callback

each subsequent call to SPI_Send() results in no call to slave callback at Electron’s side

I tried to play with adding a SpiRead (with 5 bytes buffer) on the RM186 BEFORE SpiWrite, afterwards AFTER SpiWrite. But then things got really strange and it looked like my buffer got totally mixed up

Any advice?