hideakitai / ESP32SPISlave

SPI Slave library for ESP32
MIT License
50 stars 15 forks source link

Arduino nano esp32, functionality with GPIO pins? #36

Closed Jubei-VA closed 5 months ago

Jubei-VA commented 5 months ago

Hey, thanks for the useful project.

I am using an Arduino ESP32 Nano, platformio

I want to use GPIO pins for this, not the standard pins. I am definitely seeing signals on the scope from my 3.3V SPI Master. However, I see no data from MISO.

I am not totally what BUFFER_SIZE represents here: const size_t received_bytes = slave.transfer(tx_buf, rx_buf, BUFFER_SIZE);

Does this mean to expect CS low during this entire 64 bit transaction?

Regardless, I tried setting it to 1. I still see MISO stuck at 1.

Any tips? Thanks!

#include <Arduino.h>
#include <ESP32SPISlave.h>

/* SPI Slave */
ESP32SPISlave slave;
static constexpr size_t BUFFER_SIZE = 8;
static constexpr size_t QUEUE_SIZE = 1;
uint8_t tx_buf[BUFFER_SIZE]{0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7};
uint8_t rx_buf[BUFFER_SIZE]{0, 0, 0, 0, 0, 0, 0, 0};

// put function declarations here:
int myFunction(int, int);

void setup()
{   
    /* SPI Slave */
    #define SLAVE_SCK D4
    #define SLAVE_MISO D5
    #define SLAVE_MOSI D7
    #define SLAVE_CS D8

    pinMode(SLAVE_SCK, INPUT);
    pinMode(SLAVE_MISO, OUTPUT);
    pinMode(SLAVE_MOSI, INPUT);
    pinMode(SLAVE_CS, INPUT);
    slave.setDataMode(SPI_MODE0);      // default: SPI_MODE0
    slave.setQueueSize(QUEUE_SIZE);    // default: 1
    slave.begin(SPI3_HOST, SLAVE_SCK, SLAVE_MISO, SLAVE_MOSI, SLAVE_CS);
}

void loop()
{
    const size_t received_bytes = slave.transfer(tx_buf, rx_buf, BUFFER_SIZE);
}
hideakitai commented 5 months ago

The buffer size is the size of the data transferred from the master in one transaction. It looks like there is no problem with BUFFER_SIZE.

You don't need to call pinMode() for four pins.

Please refer to this document for IO_MUX.

https://docs.espressif.com/projects/esp-idf/en/v4.4.7/esp32/api-reference/peripherals/spi_slave.html#gpio-matrix-and-io-mux

hideakitai commented 5 months ago

If you still need help, please fell free to reopen this issue.