esp32-slave-spi icon indicating copy to clipboard operation
esp32-slave-spi copied to clipboard

External Master

Open mat1554 opened this issue 6 years ago • 0 comments

Hi, thank you for the project! I'm new on esp32 and arduino ide, and your project had helped me a lot to understand many things. But for my project I need to use esp32 as slave and another micro as master. I tried to change your code but I'm getting a stranger behavior, when the master sends data I see the data on serial monitor but after that I got a memory error. I can't figure out what could be. I was sending a 8 bits command (0x02) and one 8bit word (0x05). Could you please, share the changes that I gonna need to do to just receive data from master?

/**
 * SlaveSPI Class
 * - adopt from gist:shaielc/SlaveSPIClass.cpp at https://gist.github.com/shaielc/e0937d68978b03b2544474b641328145
 */
#include "SlaveSPI.h"
#include <SPI.h>

#define SO   (gpio_num_t)12
#define SI   (gpio_num_t)13
#define SCLK (gpio_num_t)14
#define SS   (gpio_num_t)15

SlaveSPI slave(HSPI_HOST);  // VSPI_HOST

// ----------------------------------------------------------------------------
#include "SimpleArray.h"
typedef SimpleArray<uint8_t, int> array_t;

array_t master_msg(SPI_DEFAULT_MAX_BUFFER_SIZE);
array_t slave_msg(SPI_DEFAULT_MAX_BUFFER_SIZE);

// ----------------------------------------------------------------------------
void printHex(array_t arr);
void printlnHex(array_t arr);


/******************************************************************************
 * Auxiliary
 */
void printHex(array_t arr) {
    for (int i = 0; i < arr.length(); i++) {
        Serial.print(arr[i], HEX);
        Serial.print(" ");
    }
}

void printlnHex(array_t arr) {
    printHex(arr);
    Serial.println();
}

int callback_after_slave_tx_finish() {
    // Serial.println("[slave_tx_finish] slave transmission has been finished!");
    // Serial.println(slave[0]);

    return 0;
}

/******************************************************************************
 * Setup
 */
void setup() {
    Serial.begin(115200);
    
    // Setup Slave-SPI
    // slave.begin(SO, SI, SCLK, SS, 8, callback_after_slave_tx_finish);  // seems to work with groups of 4 bytes
    // slave.begin(SO, SI, SCLK, SS, 4, callback_after_slave_tx_finish);
    slave.begin(SO, SI, SCLK, SS, 2, callback_after_slave_tx_finish);
    // slave.begin(SO, SI, SCLK, SS, 1, callback_after_slave_tx_finish);  // at least 2 word in an SPI frame
}

/******************************************************************************
 * Loop 
 */
void loop() {
    if (slave.getInputStream()->length() && digitalRead(SS) == HIGH) {  // Slave SPI has got data in.
        while (slave.getInputStream()->length()) {
            slave.readToArray(slave_msg);  // Not the sample read() as Serial
        }
        Serial.print("slave input: ");
        printlnHex(slave_msg);
    }
}

mat1554 avatar Mar 14 '19 20:03 mat1554