I want to set up ESP32' SPI as an slave so that it always listens to the master and wait for probable incoming messages. in an provided example in IDE, hspi->transfer() function is used.
I could not find corresponding explanations for this function on web. where should I find the implementation of this function , explanations about it or the SPI.c or SPI.C++ library?
I checked the usual paths in my computer (docs, user/arduino) but I could not find such I file like SPI.c
Hi Idahowalker, I read all you wrote for the SPI ESP32 tutorial. For some reason I cannot get the simplest output from SPI to work.
In your tutorial you haven't given any working example of a simple application of your SPI method. Would you mind giving one? I just need to have at least a send message with the sclk kicking. Otherwise' it's pretty discouraging. I'm trying to get the ADS1256 ADC ful-duplex to work, so no having sclk is pretty discouraging.
In my case I'm using the HSPI. And already, your tip about the TMS and TDI was really useful (I knew I couldn't program it while the ADC plugged in, but I didn't know why.
So yeah, just a little working example in Arduino that sends a Byte, that would be really golden.
Right now I get a (1043031) spi_master: check_trans_valid(676): invalid dev handle
I'll post a program I write to test out a LSM9DS1 using the ESP32's SPI API. I figure you can sort through the program. There will be code from 2 tabs that use the ESP32's SPI API.
The ESP32_SPI_API.h tab's code is
#include <driver/spi_master.h>
#include "sdkconfig.h"
#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
////////////////////////////////////
//
//#define MAGTYPE true
//#define XGTYPE false
//////////////////////////
///////////////////////////
//
////////////////////////////
uint8_t GetLowBits();
int8_t GetHighBits();
int fReadSPIdata16bits( spi_device_handle_t &h, int address );
int fWriteSPIdata8bits( spi_device_handle_t &h, int address, int sendData );
int fInitializeSPI_Devices( spi_device_handle_t &h, int csPin);
// spi_device_handle_t fInitializeSPI_Devices( int csPin);
int fInitializeSPI_Channel( int spiCLK, int spiMOSI, int spiMISO, spi_host_device_t SPI_Host, bool EnableDMA);
Thanks for your response. I got a question regarding the SPI.h driver which is available in Arduino IDE examples. it seems there is only a function for transmission and there is no function for receiving data using SPI.
uint8_t transfer(uint8_t data);
which is defined in this class:
uint8_t SPIClass::transfer(uint8_t data)
{
if(_inTransaction){
return spiTransferByteNL(_spi, data);
}
return spiTransferByte(_spi, data);
}
and here is the implemention of the function:
uint8_t spiTransferByte(spi_t * spi, uint8_t data)
{
if(!spi) {
return 0;
}
SPI_MUTEX_LOCK();
spi->dev->mosi_dlen.usr_mosi_dbitlen = 7;
spi->dev->miso_dlen.usr_miso_dbitlen = 7;
spi->dev->data_buf[0] = data; #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3
spi->dev->cmd.update = 1;
while (spi->dev->cmd.update); #endif
spi->dev->cmd.usr = 1;
while(spi->dev->cmd.usr);
data = spi->dev->data_buf[0] & 0xFF;
SPI_MUTEX_UNLOCK();
return data;
}
Is the value returned by this function, data, the byte sent by SPI Slave ??
I mean is the buf[0] & 0xFF value the received value from the slave side?
it should be so strange if the SPI.h driver does not have a function to receive the value from the Slave side.