How to have HIGH signal on MOSI SPI

Hello everyone,

I have an ESP32 in order to communicate in SPI mode.

For my project I need to have the MOSI output (GPIO 23) in HIGH level while I don't send a message.

Currently, GPIO 23 is alwas to 0V (except during the message of course).

How I can do that ?

Here is my code:

#include <ESP32SPISlave.h>

static constexpr uint8_t VSPI_SS {SS};  // default: GPIO 5
SPIClass master(VSPI);
ESP32SPISlave slave;


static constexpr uint16_t BUFFER_SIZE {5};
uint8_t spi_master_tx_buf[BUFFER_SIZE];
uint8_t spi_master_rx_buf[BUFFER_SIZE];
uint8_t spi_slave_tx_buf[BUFFER_SIZE];
uint8_t spi_slave_rx_buf[BUFFER_SIZE];


const byte EN_gpio = 25;


void setup() {
    Serial.begin(115200);
    delay(2000);

    pinMode(EN_gpio, OUTPUT);
    digitalWrite(EN_gpio, HIGH);

     // SPI Master
    // VSPI = CS: 5, CLK: 18, MOSI: 23, MISO: 19
    pinMode(VSPI_SS, OUTPUT);
    digitalWrite(VSPI_SS, HIGH);
    master.begin();

    // begin() after setting
    // HSPI = CS: 15, CLK: 14, MOSI: 13, MISO: 12
    // VSPI = CS: 5, CLK: 18, MOSI: 23, MISO: 19
    slave.setDataMode(SPI_MODE3);
    slave.setQueueSize(1);  // transaction queue size
    slave.begin();          // default SPI is HSPI
   

    // clear buffers
    memset(spi_slave_tx_buf, 0, BUFFER_SIZE);
    memset(spi_slave_rx_buf, 0, BUFFER_SIZE);
}

void loop() {

  spi_master_tx_buf[0]=0x00;
  spi_master_tx_buf[1]=0x01;
  spi_master_tx_buf[2]=0x3B;
  spi_master_tx_buf[3]=0x11;

    master.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
    digitalWrite(VSPI_SS, LOW);
    master.transferBytes(spi_master_tx_buf, spi_master_rx_buf, BUFFER_SIZE);
    digitalWrite(VSPI_SS, HIGH);
    master.endTransaction();

    // block until the transaction comes from master
    

    delay(2000);
 }

Thanks a lot for your help!

What is wrong with using digitalWrite(23, HIGH) ?

it doesn't work... MOSI signal still is at low lvl in idle

You can send a continous stream of ones on the line to a fake SPI connected device.

You might be able to use the ESP32's SPI API to bypass the Arduino Core, SPI Master Driver - ESP32 - — ESP-IDF Programming Guide latest documentation to get your desired results. I prefer to use the ESP32's SPI API over the ESP32's Arduino core. The API offers more options of optimization when trying to do non-standard things.

Good idea,

could you help me to send a continous stream of ones when I don't send a true message please

Sure post your best coding attempts and I'll see if I can help with the non-standard way of doing things.

You could also use hardware to cause the MOSI line to presentin a non-standard way as being high when not engaged.

Why do you need the MOSI line to not follow conventional standards for SPI MOSI signal?

Did you try a pull-up resistor on the MOSI line to create the non-standard condition?

Thanks for help!

Yes, I tried to add a pull-up, unfortunately, the SPI settings seem to force the signal to low in any case...

I tried to use an arduno mega, with it, the MOSI idle is HIGH, unfortunately, the clock has a break every octet, so I can't use in this way...

I found something here:
https://esp32.com/viewtopic.php?t=22496

It seems I have to add following lines in my code:

   esp_rom_gpio_connect_out_signal(mosi, spi_periph_signal[host_device].spid_out, invert, false);
   esp_rom_gpio_connect_in_signal(mosi, spi_periph_signal[host_device].spid_in, invert);

But of course, if I just copy-past those, I get error...

An idea ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.