Skip to content

Instantly share code, notes, and snippets.

@SimedruF
Created November 26, 2023 11:55
Show Gist options
  • Save SimedruF/57a35a6b0f9ad99b5eea4fd6c91084d0 to your computer and use it in GitHub Desktop.
Save SimedruF/57a35a6b0f9ad99b5eea4fd6c91084d0 to your computer and use it in GitHub Desktop.
ESP32 SPI data send to RaspberryPi4
/*; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
upload_port = COM9
monitor_port = COM9
monitor_speed = 115200
lib_deps = hideakitai/ESP32DMASPI@^0.3.0
*/
#include <Arduino.h>
#include <ESP32DMASPISlave.h>
ESP32DMASPI::Slave slave;
// HallSensor Analog value is connected to GPIO 35 (Analog ADC1_CH7)
const int hallSensorPin = 35;
const int hallSensorDPin = 13;
int i = 0;
static const uint32_t BUFFER_SIZE = 8;
uint8_t *spi_slave_tx_buf;
uint8_t *spi_slave_rx_buf;
void set_buffer()
{
for (uint32_t i = 0; i < BUFFER_SIZE; i++)
{
spi_slave_tx_buf[i] = i+ 0x31;
}
//memset(spi_slave_rx_buf, 0, BUFFER_SIZE);
}
void setup()
{
Serial.begin(115200);
// to use DMA buffer, use these methods to allocate buffer
spi_slave_tx_buf = slave.allocDMABuffer(BUFFER_SIZE);
spi_slave_rx_buf = slave.allocDMABuffer(BUFFER_SIZE);
set_buffer();
delay(5000);
// slave device configuration
slave.setDataMode(SPI_MODE0);
slave.setMaxTransferSize(BUFFER_SIZE);
// begin() after setting
slave.begin(VSPI); // HSPI = CS: 15, CLK: 14, MOSI: 13, MISO: 12 -> default
// VSPI (CS: 5, CLK: 18, MOSI: 23, MISO: 19)
pinMode(hallSensorDPin, INPUT);
Serial.println("Boot finalized!");
}
void loop()
{
i=i+4;
uint8_t hallSensorDValue = digitalRead(hallSensorDPin);
uint16_t HallSensorValue = analogRead(hallSensorPin);
Serial.print("Analog Data: ");
Serial.println(HallSensorValue);
uint8_t val_hall = map(HallSensorValue, 0,4095, 0, 255);
if(i==7)
i = 0;
spi_slave_tx_buf[0] = val_hall;
spi_slave_tx_buf[1] = 0xff;
// if there is no transaction in queue, add transaction
if (slave.remained() == 0)
{
slave.queue(spi_slave_rx_buf, spi_slave_tx_buf, BUFFER_SIZE);
}
// if transaction has completed from master,
// available() returns size of results of transaction,
// and buffer is automatically updated
while (slave.available())
{
//Serial.printf("Slave received: %s\n\r", spi_slave_rx_buf);
//Serial.printf("Slave Transmitted: %s\n\r", spi_slave_tx_buf);
slave.pop();
}
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment