Why SPI master can't receive data from SPI slave?

I am using Arduino Mega 2560 as SPI master, and ESP32 as SPI slave. Problem is ESP32 could receive data from Mega, but Mega could't get data from ESP32.

code of Mega:

#include <SPI.h>           // version of Arduino IDE is 2.3.3-nightly-20240316

static constexpr size_t BUFFER_SIZE = 8;
uint8_t rx_buf[BUFFER_SIZE];

void setup() {
  // put your setup code here, to run once:


  Serial.begin(115200);
  // 初始化 SPI 接口
  SPI.begin();

  SPI.setBitOrder(MSBFIRST);

  // 设置 SS 引脚为输出模式
  pinMode(SS, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);

  // 将 SS 引脚置为高电平
  digitalWrite(SS, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

  uint8_t tx_buf[BUFFER_SIZE]{ 6, 45, 3, 5, 4, 5, 7, 34 };

  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1));

  // 选择 ESP32 从机
  digitalWrite(SS, LOW);

  for (int i = 0; i < BUFFER_SIZE; i++) {
    uint8_t ack = SPI.transfer(tx_buf[i]);
    Serial.print(ack);
    Serial.print(" ");
  }
  Serial.println();

  // 取消选择 ESP32 从机
  digitalWrite(SS, HIGH);

  SPI.endTransaction();

  delay(3000);
}

code of ESP32:

#include <ESP32SPISlave.h>            // version of ESP32SPISlave is 0.6.0
#include <SPI.h>

ESP32SPISlave slave;

static constexpr size_t BUFFER_SIZE = 8;
static constexpr size_t QUEUE_SIZE = 1;

uint8_t rx_buf[BUFFER_SIZE]{ 1, 2,3,4,5,6,7,8 };
uint8_t tx_buf[BUFFER_SIZE]{ 1, 2,3,4,5,6,7,8 };


void setup() {

  Serial.begin(115200);
  Serial.print(1);
  SPI.setFrequency(4000000);

  slave.setDataMode(SPI_MODE1);  // default: SPI_MODE0 怎么换成SPI_MODE1数据就不丢失了???

  slave.setQueueSize(QUEUE_SIZE);  // default: 1

  // begin() after setting
  slave.begin(VSPI);  // default: HSPI (please refer README for pin assignments)
  pinMode(SS, INPUT);
  pinMode(MISO, OUTPUT);
  pinMode(MOSI, INPUT);
}

void loop() {

  // 以下生意0.6.0的代码
  const size_t received_bytes = slave.transfer(tx_buf, rx_buf, BUFFER_SIZE);
  Serial.print("received_bytes: ");
  Serial.println((int)received_bytes);

  for (int i = 0; i < (int)received_bytes; i++) {
    Serial.print(rx_buf[i]);
    Serial.print(" ");
  }
  Serial.println();
}

data received by ESP32 and Mega:

Anyone could help me?

Hi, @foreinyel

Mega is 5V logic.
ESP32 is 3V3 logic.

The ESP32 is not 5V logic input tolerant, so Mega to ESP32 is not okay.
You are probably lucky that you have not caused a fault in your ESP32.
But 3V3 from the ESP32 may not be high enough for the 5V input logic of the Mega.

Tom... :smiley: :+1: :coffee: :australia:

Having both doesn't help.

The pinModes probably don't help there either. I believe they're attached (for you).

SPI Buses and SPI Pins --
ESP32SPISlave/README.md at main · hideakitai/ESP32SPISlave · GitHub

You are correct? As per data sheets of MEGA, the VIH must be >= 3.5V (Fig-1). The OP is required to put level shifter (Fig-2) between MEGA and ESP32 in addition to simplify the Master/Slave codes.


Figure-1:


Figure-2:

Master/Slave is determined by who is generating the clock. A slave cannot transmit data to a master, the master has to clock the data into itself. It does this concurrently while sending data. If you do not have anything send zeros, it does not matter.

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