Chamud / LEGEND-TAKIJIRI-RSC-2024

0 stars 1 forks source link

SPI #5

Open INA0017 opened 6 months ago

INA0017 commented 6 months ago

MASTER

#include <SPI.h>

SPIClass master(VSPI);// VSPI = CS:  5, CLK: 18, MOSI: 23, MISO: 19

#define SLAVE_SS 5

#define SLAVE_buf 8
uint8_t tx_SLAVE_array[SLAVE_buf] {1, 2, 3, 4, 5, 6, 7, 8};
uint8_t rx_SLAVE_array[SLAVE_buf] {0};

void Transfer(const uint8_t SS, const uint8_t data_tx[], const uint32_t data_size, uint8_t data_rx[], const uint32_t clock, bool msb, const uint8_t spi_mode){
master.beginTransaction(SPISettings(clock, msb, spi_mode));
   digitalWrite(SS, LOW);
      master.transferBytes(data_tx, data_rx, data_size);
   digitalWrite(SS, HIGH);
master.endTransaction();
}

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

    pinMode(SLAVE_SS, OUTPUT);

    digitalWrite(SLAVE_SS, HIGH);

    master.begin();
}

void loop(){

Transfer(SLAVE_SS, tx_SLAVE_array, SLAVE_buf, rx_SLAVE_array, 4000000, 1, 0);

       for (size_t i = 0; i < SLAVE_buf; ++i){
            Serial.print(rx_SLAVE_array[i]);
            Serial.print(" ");
        }
       Serial.println();

        memset(rx_SLAVE_array, 0, SLAVE_buf);

    delay(10);
}
INA0017 commented 6 months ago

SLAVE

#include <ESP32SPISlave.h>

ESP32SPISlave slave;

static constexpr uint8_t VSPI_SS {SS};

#define buf 8
uint8_t tx_master_array[buf] {9, 10, 11, 12, 13, 14, 15, 16};
uint8_t rx_master_array[buf] {0};

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

    slave.setDataMode(SPI_MODE0);
    slave.begin(VSPI);// VSPI = CS:  5, CLK: 18, MOSI: 23, MISO: 19
}

void loop(){

    slave.queue(rx_master_array, tx_master_array, 10);

    while (slave.available()){

  for (size_t i = 0; i < buf; ++i){
            Serial.print(rx_master_array[i]);
            Serial.print(" ");
        }
        Serial.println();

      memset(rx_master_array, 0, buf);
      slave.pop();
    }

    delay(10);
}