Skip to content

Instantly share code, notes, and snippets.

@shaielc
Last active April 28, 2023 10:18
Show Gist options
  • Save shaielc/e0937d68978b03b2544474b641328145 to your computer and use it in GitHub Desktop.
Save shaielc/e0937d68978b03b2544474b641328145 to your computer and use it in GitHub Desktop.
SlaveSPIClass_esp32_Arduino
#include<SlaveSPIClass.h>
int SlaveSPI::size = 0;
SlaveSPI** SlaveSPI::SlaveSPIVector = NULL;
void setupIntr(spi_slave_transaction_t * trans)
{
for(int i=0 ; i<SlaveSPI::size;i++)
{
if(SlaveSPI::SlaveSPIVector[i]->match(trans))
SlaveSPI::SlaveSPIVector[i]->setup_intr(trans);
}
}
void transIntr(spi_slave_transaction_t * trans)
{
for(int i=0 ; i<SlaveSPI::size;i++)
{
if(SlaveSPI::SlaveSPIVector[i]->match(trans))
SlaveSPI::SlaveSPIVector[i]->trans_intr(trans);
}
}
SlaveSPI::SlaveSPI()
{
SlaveSPI** temp = new SlaveSPI * [size+1];
for (int i=0;i<size;i++)
{
temp[i]=SlaveSPIVector[i];
}
temp[size] = this;
size++;
delete [] SlaveSPIVector;
SlaveSPIVector = temp;
buff = "";
transBuffer = "";
}
void SlaveSPI::begin(gpio_num_t so,gpio_num_t si,gpio_num_t sclk,gpio_num_t ss,size_t length,void(* ext)())
{
//should set to the minimum transaction length
t_size = length;
driver = new spi_slave_transaction_t{t_size * 8 , 0 , heap_caps_malloc(max(t_size,32), MALLOC_CAP_DMA), heap_caps_malloc(max(t_size,32), MALLOC_CAP_DMA),NULL};
spi_bus_config_t buscfg={
.mosi_io_num=si,
.miso_io_num=so,
.sclk_io_num=sclk
};
gpio_set_pull_mode(sclk, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(ss, GPIO_PULLUP_ONLY);
spi_slave_interface_config_t slvcfg={ss,0,1,0,setupIntr,transIntr};//check the IDF for further explenation
spi_slave_initialize(HSPI_HOST, &buscfg, &slvcfg,1); //DMA channel 1
spi_slave_queue_trans(HSPI_HOST,driver,portMAX_DELAY);//ready for input (no transmit)
exter_intr =ext;
}
void SlaveSPI::setup_intr(spi_slave_transaction_t *trans)
{//called when the trans is set in the queue
//didn't find use for it in the end.
}
void SlaveSPI::trans_intr(spi_slave_transaction_t *trans)
{//called when the trans has finished
for(int i=0;i<(driver->trans_len/8);i++)
{
buff += ((char*)driver->rx_buffer)[i];
((char*) driver->rx_buffer)[i] = (char)0;
size++;
}
setDriver();
exter_intr();
}
void SlaveSPI::trans_queue(String& transmission){
//used to queue data to transmit
for (int i=0;i<transmission.length();i++)
transBuffer += transmission[i];
}
inline bool SlaveSPI::match(spi_slave_transaction_t * trans)
{
return (this->driver == trans);
}
void SlaveSPI::setDriver()
{
driver->user = NULL;
int i=0;
for(;i<(driver->trans_len/8)&&i<transBuffer.length();i++)
{
((char*) driver->tx_buffer)[i] = transBuffer[i];
}
transBuffer = &(transBuffer[i]);
driver->length=t_size*8;
driver->trans_len = 0;
spi_slave_queue_trans(HSPI_HOST,driver,portMAX_DELAY);
}
char SlaveSPI::read()
{
char temp = buff[0];
buff.remove(0,1);
size--;
return temp;
}
#ifndef SLAVE_SPI_CLASS
#define SLAVE_SPI_CLASS
#include "Arduino.h"
#include "driver/spi_slave.h"
void setupIntr(spi_slave_transaction_t * trans);
void transIntr(spi_slave_transaction_t * trans);
class SlaveSPI
{
static SlaveSPI** SlaveSPIVector;
static int size;
friend void setupIntr(spi_slave_transaction_t * trans);
friend void transIntr(spi_slave_transaction_t * trans);
String buff;//used to save incoming data
String transBuffer;//used to buffer outgoing data !not tested!
spi_slave_transaction_t * driver;
void (*exter_intr)();//interrupt at the end of transmission , if u need to do something at the end of each transmission
size_t t_size;//length of transaction buffer, (should be set to maximum transition size)
public:
SlaveSPI();
int size;
void setup_intr(spi_slave_transaction_t *trans);//called when the trans is set in the queue
void trans_intr(spi_slave_transaction_t *trans);//called when the trans has finished
void begin(gpio_num_t so,gpio_num_t si,gpio_num_t sclk,gpio_num_t ss,size_t length=128,void(* ext)() = NULL);
void trans_queue(String& transmission);//used to queue data to transmit
inline char* operator[](int i){return (&buff[i]);}
inline void flush(){buff = "";size=0;}
inline bool match(spi_slave_transaction_t * trans);
void setDriver();
char read();
inline String* getBuff(){return &buff;}
};
#endif
#include <SlaveSPIClass.h>
#include <SPI.h>
#define MO 22
#define MI 23
#define MCLK 19
#define MS 18
#define SO 32
#define SI 25
#define SCLK 27
#define SS 34
SlaveSPI slave;
SPIClass master(VSPI);
void test();
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
master.begin(MCLK,MI,MO);
slave.begin((gpio_num_t)SO,(gpio_num_t)SI,(gpio_num_t)SCLK,(gpio_num_t)SS,8,test);//seems to work with groups of 4 bytes
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
pinMode(MS,OUTPUT);
}
String txt = "";
String cmd ="";
void loop() {
// put your main code here, to run repeatedly:
if(slave.getBuff()->length()&&digitalRead(SS)==HIGH)
{
while(slave.getBuff()->length())
txt+=slave.read();
Serial.println("slave input:");
Serial.println(txt);
}
while(Serial.available())
{
cmd +=(char) Serial.read();
}
while(txt.length()>0)
{
slave.trans_queue(txt);
Serial.println("slave output:");
Serial.println(txt);
txt ="";
}
while(cmd.length()>0)
{
Serial.println("input:");
Serial.println(cmd);
Serial.println("Master input:");
digitalWrite(MS,LOW);
for(int i=0;i<cmd.length();i++)
{
cmd[i] = master.transfer(cmd[i]); //ERROR : gives the transmitted data <<1
}
digitalWrite(MS,HIGH);
for(int i=0;i<cmd.length();i++)
Serial.print(cmd[i],HEX);
cmd ="";
}
}
void test()
{
//Serial.println("test");
//Serial.println(slave[0]);
}
@andrewjaykeller
Copy link

@shaielc how did you pick these pins for spi slave?

#define SO 32
#define SI 25
#define SCLK 27
#define SS 34

@shaielc
Copy link
Author

shaielc commented Jan 7, 2018

hi didn't see you commented here git apparently doesn't notify me

and it uses the GPIO mux so just pick whatever you need!

@andrewjaykeller
Copy link

@shaielc

What is match for?
How do i get data from the SPISlave rx buffer? I see the test function is called on data.

@andrewjaykeller
Copy link

@shaielc could you explain what's happening in these lines of code:

SlaveSPI::SlaveSPI()
{
	SlaveSPI** temp = new SlaveSPI * [size+1];
	for (int i=0;i<size;i++)
	{
		temp[i]=SlaveSPIVector[i];
	}
	temp[size] = this;
	size++;
	delete [] SlaveSPIVector;
	SlaveSPIVector = temp;
	buff = "";
	transBuffer = "";
}

@cfausn
Copy link

cfausn commented Apr 9, 2018

I believe temp is a pointer to a pointer, and it looks like it's effectively pointing to a list of other pointers and appending (size++;). Then clearing the original and setting SlaveSPIVector to the new pointer of pointers with the appended value, then clearing the buffers. If it's not appending directly it could just be increasing the buffer size for an incoming append, or reorganizing the pointers. Could be wrong but that's my guess!

Edit: After some further thinking it looks like it's appending to SlaveSPIVector at index size the new value, so yes I do think it's appending something. Basically going from:
Old SlaveSPIVector : SlaveSPIVector[size] -> SlaveSPIVector [i] -> (some data)
New SlaveSPIVector: SlaveSPIVector[size++] -> Old SlaveSPIVector -> yadda yadda

Let me know if I'm on the right track here I'd be interested in finding out if I'm still sharp with C ;)

@Mahmoudomar91
Copy link

I tried this code & the library on the Arduino IDE and it didn't work on the ESP32
And i got the following error.
could you please help me with that?

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159/arduino.ar" -lgcc -lopenssl -lbtdm_app -lfatfs -lwps -lcoexist -lwear_levelling -lhal -lnewlib -ldriver -lbootloader_support -lpp -lmesh -lsmartconfig -ljsmn -lwpa -lethernet -lphy -lapp_trace -lconsole -lulp -lwpa_supplicant -lfreertos -lbt -lmicro-ecc -lcxx -lxtensa-debug-module -lmdns -lvfs -lsoc -lcore -lsdmmc -lcoap -ltcpip_adapter -lc_nano -lrtc -lspi_flash -lwpa2 -lesp32 -lapp_update -lnghttp -lspiffs -lespnow -lnvs_flash -lesp_adc_cal -llog -lexpat -lm -lc -lheap -lmbedtls -llwip -lnet80211 -lpthread -ljson -lstdc++ -Wl,--end-group -Wl,-EL -o "C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159/SlaveSPITEST.ino.elf" C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function SPIClass::SPIClass(unsigned char)':

C:\Users\Mahmoud\Documents\Arduino\hardware\espressif\esp32\libraries\SPI\src/SPI.cpp:24: multiple definition of `SPIClass::SPIClass(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch/SPI.cpp:24: first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::SPIClass(unsigned char)':

SPI.cpp:(.text._ZN8SPIClassC2Eh+0x0): multiple definition of `SPIClass::SPIClass(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClassC2Eh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::begin(signed char, signed char, signed char, signed char)':

SPI.cpp:(.text._ZN8SPIClass5beginEaaaa+0x0): multiple definition of `SPIClass::begin(signed char, signed char, signed char, signed char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass5beginEaaaa+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::setHwCs(bool)':

SPI.cpp:(.text._ZN8SPIClass7setHwCsEb+0x0): multiple definition of `SPIClass::setHwCs(bool)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass7setHwCsEb+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::end()':

SPI.cpp:(.text._ZN8SPIClass3endEv+0x0): multiple definition of `SPIClass::end()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass3endEv+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::setFrequency(unsigned int)':

SPI.cpp:(.text._ZN8SPIClass12setFrequencyEj+0x0): multiple definition of `SPIClass::setFrequency(unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass12setFrequencyEj+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::setClockDivider(unsigned int)':

SPI.cpp:(.text._ZN8SPIClass15setClockDividerEj+0x0): multiple definition of `SPIClass::setClockDivider(unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass15setClockDividerEj+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::setDataMode(unsigned char)':

SPI.cpp:(.text._ZN8SPIClass11setDataModeEh+0x0): multiple definition of `SPIClass::setDataMode(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass11setDataModeEh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::setBitOrder(unsigned char)':

SPI.cpp:(.text._ZN8SPIClass11setBitOrderEh+0x0): multiple definition of `SPIClass::setBitOrder(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass11setBitOrderEh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::beginTransaction(SPISettings)':

SPI.cpp:(.text._ZN8SPIClass16beginTransactionE11SPISettings+0x0): multiple definition of `SPIClass::beginTransaction(SPISettings)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass16beginTransactionE11SPISettings+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::endTransaction()':

SPI.cpp:(.text._ZN8SPIClass14endTransactionEv+0x0): multiple definition of `SPIClass::endTransaction()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass14endTransactionEv+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::write(unsigned char)':

SPI.cpp:(.text._ZN8SPIClass5writeEh+0x0): multiple definition of `SPIClass::write(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass5writeEh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::transfer(unsigned char)':

SPI.cpp:(.text._ZN8SPIClass8transferEh+0x0): multiple definition of `SPIClass::transfer(unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass8transferEh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::write16(unsigned short)':

SPI.cpp:(.text._ZN8SPIClass7write16Et+0x0): multiple definition of `SPIClass::write16(unsigned short)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass7write16Et+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::transfer16(unsigned short)':

SPI.cpp:(.text._ZN8SPIClass10transfer16Et+0x0): multiple definition of `SPIClass::transfer16(unsigned short)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass10transfer16Et+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::write32(unsigned int)':

SPI.cpp:(.text._ZN8SPIClass7write32Ej+0x0): multiple definition of `SPIClass::write32(unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass7write32Ej+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::transfer32(unsigned int)':

SPI.cpp:(.text._ZN8SPIClass10transfer32Ej+0x0): multiple definition of `SPIClass::transfer32(unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass10transfer32Ej+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::transferBits(unsigned int, unsigned int*, unsigned char)':

SPI.cpp:(.text._ZN8SPIClass12transferBitsEjPjh+0x0): multiple definition of `SPIClass::transferBits(unsigned int, unsigned int*, unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass12transferBitsEjPjh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::writeBytes(unsigned char*, unsigned int)':

SPI.cpp:(.text._ZN8SPIClass10writeBytesEPhj+0x0): multiple definition of `SPIClass::writeBytes(unsigned char*, unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass10writeBytesEPhj+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::writePixels(void const*, unsigned int)':

SPI.cpp:(.text._ZN8SPIClass11writePixelsEPKvj+0x0): multiple definition of `SPIClass::writePixels(void const*, unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass11writePixelsEPKvj+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::transferBytes(unsigned char*, unsigned char*, unsigned int)':

SPI.cpp:(.text._ZN8SPIClass13transferBytesEPhS0_j+0x0): multiple definition of `SPIClass::transferBytes(unsigned char*, unsigned char*, unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass13transferBytesEPhS0_j+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::writePattern_(unsigned char*, unsigned char, unsigned char)':

SPI.cpp:(.text.ZN8SPIClass13writePattern_EPhhh+0x0): multiple definition of `SPIClass::writePattern(unsigned char*, unsigned char, unsigned char)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass13writePattern_EPhhh+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o: In function `SPIClass::writePattern(unsigned char*, unsigned char, unsigned int)':

SPI.cpp:(.text._ZN8SPIClass12writePatternEPhhj+0x0): multiple definition of `SPIClass::writePattern(unsigned char*, unsigned char, unsigned int)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:SPI.cpp:(.text._ZN8SPIClass12writePatternEPhhj+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SPI.cpp.o:(.bss.SPI+0x0): multiple definition of `SPI'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SPI.cpp.o:(.bss.SPI+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `setupIntr(spi_slave_transaction_t*)':

C:\Users\Mahmoud\Documents\Arduino\hardware\espressif\esp32\libraries\SPI\src/SlaveSPIClass.cpp:5: multiple definition of `setupIntr(spi_slave_transaction_t*)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch/SlaveSPIClass.cpp:5: first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o:(.bss._ZN8SlaveSPI4sizeE+0x0): multiple definition of `SlaveSPI::size'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:(.bss._ZN8SlaveSPI4sizeE+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o:(.bss._ZN8SlaveSPI14SlaveSPIVectorE+0x0): multiple definition of `SlaveSPI::SlaveSPIVector'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:(.bss._ZN8SlaveSPI14SlaveSPIVectorE+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::SlaveSPI()':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPIC2Ev+0x0): multiple definition of `SlaveSPI::SlaveSPI()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPIC2Ev+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::SlaveSPI()':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPIC2Ev+0x0): multiple definition of `SlaveSPI::SlaveSPI()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPIC2Ev+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `transIntr(spi_slave_transaction_t*)':

SlaveSPIClass.cpp:(.text._Z9transIntrP23spi_slave_transaction_t+0x0): multiple definition of `transIntr(spi_slave_transaction_t*)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._Z9transIntrP23spi_slave_transaction_t+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::begin(gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t, unsigned int, void (*)())':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI5beginE10gpio_num_tS0_S0_S0_jPFvvE+0x0): multiple definition of `SlaveSPI::begin(gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t, unsigned int, void (*)())'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI5beginE10gpio_num_tS0_S0_S0_jPFvvE+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::setup_intr(spi_slave_transaction_t*)':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI10setup_intrEP23spi_slave_transaction_t+0x0): multiple definition of `SlaveSPI::setup_intr(spi_slave_transaction_t*)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI10setup_intrEP23spi_slave_transaction_t+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::trans_queue(String&)':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI11trans_queueER6String+0x0): multiple definition of `SlaveSPI::trans_queue(String&)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI11trans_queueER6String+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::setDriver()':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI9setDriverEv+0x0): multiple definition of `SlaveSPI::setDriver()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI9setDriverEv+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::trans_intr(spi_slave_transaction_t*)':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI10trans_intrEP23spi_slave_transaction_t+0x0): multiple definition of `SlaveSPI::trans_intr(spi_slave_transaction_t*)'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI10trans_intrEP23spi_slave_transaction_t+0x0): first defined here

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\libraries\SPI\SlaveSPIClass.cpp.o: In function `SlaveSPI::read()':

SlaveSPIClass.cpp:(.text._ZN8SlaveSPI4readEv+0x0): multiple definition of `SlaveSPI::read()'

C:\Users\Mahmoud\AppData\Local\Temp\arduino_build_410159\sketch\SlaveSPIClass.cpp.o:SlaveSPIClass.cpp:(.text._ZN8SlaveSPI4readEv+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

Using library SPI at version 1.0 in folder: C:\Users\Mahmoud\Documents\Arduino\hardware\espressif\esp32\libraries\SPI
exit status 1
Error compiling for board ESP32 Dev Module.`

@iPAS
Copy link

iPAS commented Jul 23, 2018

I've found that the DMA channel need to be 0, disable, so the code will work.

@shaielc
Copy link
Author

shaielc commented Aug 31, 2018

don't know if that is still relevant @cfausn yeah it is an array off initiated SPIslaves (it is static ) so it increases in size whenever you call the class constructor.

@euquiq
Copy link

euquiq commented Sep 2, 2018

@iPAS DMA to 0 will work only if the data packet is less than 32 bytes, according to IDF documentation.

@shaielc My device configures itself as a master and sends a frame of 17 bytes (when reporting a remote control button being pressed).

So I need to be attentive to an incoming data message at any time, and I need to read it as a SLAVE.

The device, after sending each data packet the device reverts into slave mode, in order to listen for any command I may send into it.

My simplified code, posted earlier, gets the 17 bytes ok. What I don't like of your library is that you are using a String as a buffer and returning data as a string ... Maybe it is just me, but at SPI interfacing level, device will talk in bytes, and the String thing is a weird, multibyte per character thingy, with lots of caveats.

Actually I even think I read on another thread about a tinyGSM library, which used String objects inside it, that it was a No-no in particular with esp32 devices because of RTOS and some incompatibility regarding moving strings inside a task, which may very well be a usage case for this too.

I will retry your library, and post about it, anyway, give me some hours and I will post back.

@shaielc
Copy link
Author

shaielc commented Sep 2, 2018

Explanation about the string :
The string is just there to manage large transactions the idea was to try and mimic the behavior of the other arduino drivers (UART & SPI master) which get the data inside a DMA buffer from the hardware and than saves it in a software buffer.
The reason why you would do that is this creates an abstraction for the user the user doesn't handle the "low level" allocations he just uses the data that got in.

Now I will try and check how they did it with the master and try to mimic the behavior.

@Markusenz
Copy link

cannot compile (with ESP32 Arduino core v1.0.1) , get the following error:

/Users/markus/Documents/Arduino/libraries/SlaveSPI/SlaveSPIClass.h:22:6: error: 'int SlaveSPI::size' conflicts with a previous declaration
  int size;
      ^
/Users/markus/Documents/Arduino/libraries/SlaveSPI/SlaveSPIClass.h:11:13: note: previous declaration 'int SlaveSPI::size'
  static int size;
             ^

@mat1554
Copy link

mat1554 commented Mar 15, 2019

I got an error in functions max() and min() parameters, I "fixed it" casting max_buffer_size as int, but when I executed and wrote some data in serial monitor I got another an error, I think it is something about invalid memory...

I am new using arduino ide and esp32 and need to write a code to read from a SPI master some temperature data (16 bits), nothing more than that, I don't need to write to master neither gpios. How could I change this example to do that?

Memory error (?):

13:51:41.956 -> master output (cmd): 31 A , input (return): 0 0 CORRUPT HEAP: Bad head at 0x3ffba5d4. Expected 0xabba1234 got 0x3ffba6f0
13:51:42.049 -> assertion "head != NULL" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/heap/multi_heap_poisoning.c", line 214, function: multi_heap_free
13:51:42.049 -> abort() was called at PC 0x400d71eb on core 1
13:51:42.049 -> 
13:51:42.049 -> ELF file SHA256: 0000000000000000000000000000000000000000000000000000000000000000
13:51:42.049 -> 
13:51:42.049 -> Backtrace: 0x40085fd0:0x3ffb1df0 0x40086245:0x3ffb1e10 0x400d71eb:0x3ffb1e30 0x4008a999:0x3ffb1e60 0x400846d6:0x3ffb1e80 0x40084b1d:0x3ffb1ea0 0x4000bec7:0x3ffb1ec0 0x400e9745:0x3ffb1ee0 0x400e9b15:0x3ffb1f00 0x400d11d3:0x3ffb1f20 0x400d0ee5:0x3ffb1f40 0x400d117e:0x3ffb1f70 0x400d2a29:0x3ffb1fb0 0x4008a1a1:0x3ffb1fd0
13:51:42.049 -> 
13:51:42.049 -> Rebooting...
13:51:42.049 -> ets Jun  8 2016 00:22:57
13:51:42.049 -> 
13:51:42.049 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
13:51:42.049 -> configsip: 0, SPIWP:0xee
13:51:42.049 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
13:51:42.049 -> mode:DIO, clock div:1
13:51:42.049 -> load:0x3fff0018,len:4
13:51:42.049 -> load:0x3fff001c,len:1100
13:51:42.049 -> load:0x40078000,len:10312
13:51:42.049 -> load:0x40080400,len:6460
13:51:42.049 -> entry 0x400806a4

Max and Min error:

Arduino: 1.8.8 (Windows 10), Board: "ESP32 Dev Module, Disabled, Default, 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

sketch\SlaveSPI.cpp: In member function 'esp_err_t SlaveSPI::begin(gpio_num_t, gpio_num_t, gpio_num_t, gpio_num_t, size_t, int (*)())':

SlaveSPI.cpp:52:74: error: no matching function for call to 'max(size_t&, int)'

     tx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3463:5: note: candidate: template<class _Tp, class _Compare> _Tp std::max(std::initializer_list<_Tp>, _Compare)

     max(initializer_list<_Tp> __l, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3463:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:52:74: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     tx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3457:5: note: candidate: template<class _Tp> _Tp std::max(std::initializer_list<_Tp>)

     max(initializer_list<_Tp> __l)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3457:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:52:74: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     tx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Compare)

     max(const _Tp& __a, const _Tp& __b, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:265:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:52:74: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     tx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:219:5: note: candidate: template<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)

     max(const _Tp& __a, const _Tp& __b)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:219:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:52:74: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     tx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

SlaveSPI.cpp:53:74: error: no matching function for call to 'max(size_t&, int)'

     rx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3463:5: note: candidate: template<class _Tp, class _Compare> _Tp std::max(std::initializer_list<_Tp>, _Compare)

     max(initializer_list<_Tp> __l, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3463:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:53:74: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     rx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3457:5: note: candidate: template<class _Tp> _Tp std::max(std::initializer_list<_Tp>)

     max(initializer_list<_Tp> __l)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3457:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:53:74: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     rx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> const _Tp& std::max(const _Tp&, const _Tp&, _Compare)

     max(const _Tp& __a, const _Tp& __b, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:265:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:53:74: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     rx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:219:5: note: candidate: template<class _Tp> const _Tp& std::max(const _Tp&, const _Tp&)

     max(const _Tp& __a, const _Tp& __b)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:219:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:53:74: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     rx_buffer       = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

                                                                          ^

sketch\SlaveSPI.cpp: In member function 'esp_err_t SlaveSPI::initTransmissionQueue()':

SlaveSPI.cpp:167:59: error: no matching function for call to 'min(size_t&, int)'

     int size = min(max_buffer_size, output_stream.length());                  // NOT over the buffer's size.

                                                           ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3451:5: note: candidate: template<class _Tp, class _Compare> _Tp std::min(std::initializer_list<_Tp>, _Compare)

     min(initializer_list<_Tp> __l, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3451:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:167:59: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     int size = min(max_buffer_size, output_stream.length());                  // NOT over the buffer's size.

                                                           ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:62:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3445:5: note: candidate: template<class _Tp> _Tp std::min(std::initializer_list<_Tp>)

     min(initializer_list<_Tp> __l)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algo.h:3445:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:167:59: note:   mismatched types 'std::initializer_list<_Tp>' and 'unsigned int'

     int size = min(max_buffer_size, output_stream.length());                  // NOT over the buffer's size.

                                                           ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:243:5: note: candidate: template<class _Tp, class _Compare> const _Tp& std::min(const _Tp&, const _Tp&, _Compare)

     min(const _Tp& __a, const _Tp& __b, _Compare __comp)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:243:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:167:59: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     int size = min(max_buffer_size, output_stream.length());                  // NOT over the buffer's size.

                                                           ^

In file included from c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\algorithm:61:0,

                 from C:\Program Files (x86)\Arduino\hardware\espressif\esp32\cores\esp32/Arduino.h:148,

                 from sketch\SlaveSPI.h:4,

                 from sketch\SlaveSPI.cpp:9:

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:195:5: note: candidate: template<class _Tp> const _Tp& std::min(const _Tp&, const _Tp&)

     min(const _Tp& __a, const _Tp& __b)

     ^

c:\users\matheus.nogueira\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits\stl_algobase.h:195:5: note:   template argument deduction/substitution failed:

sketch\SlaveSPI.cpp:167:59: note:   deduced conflicting types for parameter 'const _Tp' ('unsigned int' and 'int')

     int size = min(max_buffer_size, output_stream.length());                  // NOT over the buffer's size.

                                                           ^

exit status 1
no matching function for call to 'max(size_t&, int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

@EstevanTH
Copy link

Hey there,
I could fix the compile errors and warnings, which turned out to be just 2 tiny problems.

At the moment I do not know if it makes the program work.

Fix #1

The same name is used for a static member and an instance member.

In SlaveSPIClass.h @ line 2

static int size;

becomes

static int size_;

In SlaveSPIClass.cpp @ lines 2, 6, 14: replace occurrences of SlaveSPI::size with SlaveSPI::size_.

Fix #2

The compiler messes up the implicit conversion from integer literal to t_size.

SlaveSPIClass.cpp @ line 41

driver = new spi_slave_transaction_t{t_size * 8 , 0 , heap_caps_malloc(max(t_size,32), MALLOC_CAP_DMA), heap_caps_malloc(max(t_size,32), MALLOC_CAP_DMA),NULL};

becomes

driver = new spi_slave_transaction_t{(size_t)(t_size * 8) ,	0 ,	heap_caps_malloc(max(t_size,(size_t)32), MALLOC_CAP_DMA),	heap_caps_malloc(max(t_size,(size_t)32), MALLOC_CAP_DMA),NULL};

@EstevanTH
Copy link

EstevanTH commented Mar 25, 2019

I feel like something else is wrong with size and size_.

Fix #3

It does not make sense that an instance value is used in the constructor SlaveSPI::SlaveSPI() because size is only used to affect the static member SlaveSPIVector.

SlaveSPIClass.cpp @ lines 20 to 33: replace every size with SlaveSPI::size_.

Possible fix

The instance's size is not explicitly initialized. It looks like a buffer count. It should probably be initialized to 0 in the member initialization part of the constructor. The default value is to be checked.

EDIT #1

Yes! My fixes #1 to #3 made it work somehow! I was receiving nothing at all without the fix #3. Now I'm receiving a corrupted message but at least I have a correct piece of it.

@MateoSegura
Copy link

I am using platform IO, and after implementing all the fixes above, I cannot compile:

My error is inline 29 of .cpp file: tx_buffer = (uint8_t *)heap_caps_malloc(max(max_buffer_size, 32), SPI_MALLOC_CAP);

the error is: no instance of max(size_t,int)

any help or suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment