Goal Transfer Data Between UNO and ESP8266 using SPI.
I am using Arduino IDE for both the UNO and the ESP8266.
I have established communication using the SPISlave exanples provided by the ESP8266 Library and with some modifications and it works extremely well (removed Delay's form the Master code UNO side )
(I do not want to use serial communications I am saving this for other purposes)
**Where I need Help: **
The chunk of data that is sent over SPI is always sent is 32 Bytes.
//ESP8266 slave side
void SPISlaveClass::setData(uint8_t * data, size_t len){
if(len > 32) {
len = 32; //<<<I believe theis is a Hardware Limit in the ESP8266 using SPI
}
hspi_slave_setData(data, len);
}
I have found nothing at this moment to give me proper direction as to how to convert this back to longer char arrays, int's float's or other data structures. I have some ideas but I would appreciate examples, sketches, web links, for transferring large amounts of data and or data structures.
My Goal is to: provide a easy way to transfer a chunk of data this could be strings (Not that I use Strings) char[] int[]'s floats[] and even structures of data with multiple types.
This is my concept code for UNO: Please advise me on how I can process this in a better way. My demo code uses strings fro ease of visualization, Any Data type and structures is my ultimate goal.
union BufferX {
byte Byte[164]; // same as uint8_t data type
char Char[164]; // same as int8_t data type
unsigned UInt[82]; // same as uint16_t data type
int Int[82]; // same as int16_t data type
unsigned long ULong[41]; // same as uint16_t data type
long Long[41]; // same as int16_t data type
};
union TransferBuffer {
byte Byte[33]; // same as uint8_t data type
char Char[33]; // same as int8_t data type
};
TransferBuffer SPI_TransferBuffer; // per code this buffer is exactly 32 bytes
BufferX BufferUNO1;
BufferX BufferUNO2;
void setup() {
Serial.begin(115200);
char test[] = "w123456789abcdefx123456789abcdefy123456789abcdefz123456789abcdef@123456789abcdef#123456789abcdef$123456789abcdef^123456789abcdef";
BufferUNO1.Char[161] = '\0';// Truncate strings for testing
memcpy(BufferUNO1.Char, test, strlen(test) + 1); // Load the Data to be transfered
SPI_TransferBuffer.Char[32] = '\0';
Serial.println("Data to Send:");
Serial.println(BufferUNO1.Char);
}
void loop() {
int Recieved = 0;
while (Recieved < 160) {
SendBytes(); // This Code is a representation of the ESP8266 Sending Data to the UNO
Recieved = ReceiveBytes(); // This code is a representation of the UNO receiving data from the ESP8266
Serial.print("Recieved Bytes: ");
Serial.println(Recieved);
Serial.println(BufferUNO2.Char);
delay(100);
}
Serial.println("Complte");
Serial.println(BufferUNO2.Char);
while (1) {};
}
void SendBytes() {
static int offsetSend = 0;
setData(BufferUNO1.Byte + offsetSend, 160 - offsetSend );
offsetSend += 32;
}
int ReceiveBytes() {
static int offsetReceive = 0;
byte TempBuffer[32];
readData(TempBuffer);
memcpy( BufferUNO2.Byte + offsetReceive, TempBuffer, 32);
offsetReceive += 32;
return (offsetReceive);
}
// Code Below this point is for imitation of the SPI Transfer Process
// Slave Code ESP8266
// commented line is the origional code from SPISlave.cpp
void setData(uint8_t * data, size_t len) {
if (len > 32) {
len = 32; //<<<I believe theis is a Hardware Limit in the ESP8266 using SPI
}
//hspi_slave_setData(data, len);
memcpy(SPI_TransferBuffer.Byte, data, 32);
}
// Master code for UNO
// Commented lines are part of the code in the ESPSafeMaster.h (I created from the ESPSafeMaster sketch)
void readData(uint8_t * data)
{
//_pulseSS();
//SPI.transfer(0x03);
//SPI.transfer(0x00);
for (uint8_t i = 0; i < 32; i++) {
// data[i] = SPI.transfer(0);
data[i] = transfer(0);
}
//_pulseSS();
}
uint8_t transfer(int x) {
static int ptr = 0;
if (ptr == 32) ptr = 0;
return (SPI_TransferBuffer.Byte[ptr++]);
}
Thanks in advance for any and all suggestions and help
P.S.
I will provide additional code/schematics upon request but I thought it would be a distraction from the purpose of this post.