You should work on Example of post #12 rather than on your own codes which you can't make working!
Do you understand why syc pattern is needed while transmitting struct type data?
You should work on Example of post #12 rather than on your own codes which you can't make working!
Do you understand why syc pattern is needed while transmitting struct type data?
I'm controlling a stepper motor (it can also be a sensor). The Master periodically "pings" the Slave and it responds with his own id, state and speed.
struct __attribute__((packed)) SlaveStatusPacket {
int8_t id; //Doesn't change
int8_t state; //1 = ready, 5 = busy
float rpm; //value between 0 and 1500
};
Additionally Master can send these commands to Slave
struct __attribute__((packed)) SlaveCommandPacket {
int8_t operation; //flag that indicates a new package of command has arrived.
int8_t command; //Command to the motor. 0=stop 1 = run
float position;
float rpm;
int8_t direction;
float advance;
};
and the slave must again respond to the master with his own id, state and speed.
for example:
from Slave to Master [1,1,60]
from Master to Slave [1,1,20,200,1,2]
Master sends A0, garbage comes from Slave, Slave receives A0, Slave loads B0
Master sends A1, B0 comes from Slave, Slave receives A1, Slave loads B1
Master sends A2, B1 comes from Slave, Slave receives A2, Slave loads B2
Master sends A3, B2 comes from Slave, Slave receives A3, Slave loads B3
Master sends A0, B3 comes from Slave, Slave receives A0, Slave loads B0
.........
You see that there is 1-byte off which you must take care while re-building received data at the Master side.
You may visualize the 1-byte off concept from the following simple example; where, 2-byte data are being exchnaged between Master and Slave using SPI Port.
Master (UNO) Sketch: save as spiP711MUno (tested)
#include <SPI.h>
byte myCommandData[] = {0x12, 0x34};
//0x12 goes, comes D-;
//0x34 goes, 0x56 comes; 0x12 goes, comes 0x78
byte myData[2];
int i = 0;
void setup ()
{
Serial.begin(115200);
SPI.begin();
digitalWrite(SS, LOW); //Slave NANO is selceted
}
void loop()
{
for (int i = 0; i < 2; i++)
{
myData[i] = SPI.transfer(myCommandData[i]);
//12/D-; 34/56; 12/78; 34/56
Serial.println(myData[i], HEX);
}
byte lsByte = myData[0]; //0x78
byte msByte = myData[1]; //0x56
int y = (int)msByte<<8|(int)lsByte;
Serial.print("Received from Slave: ");
Serial.println(y, HEX);
Serial.println("=========================");
delay(2000);
}
Slave (NANO)Sketch: save as spiP711SNano (tested)
#include <SPI.h>
byte myData[] = {0x56, 0x78}; //data to be sent to Master
volatile bool flag = false;
byte recData[2]; //data received from master would be saved here
int i = 0;
void setup ()
{
Serial.begin(115200);
pinMode(SS, INPUT_PULLUP); // ensure SS stays high for now
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT_PULLUP); //Slave is kept deselceted
bitSet(SPCR, SPE); //SPI Port is enabled
bitSet(SPCR, MSTR); //Arduino is Slave
SPI.attachInterrupt(); //interrupt logic is enabled
SPDR = 0x13; //D-
}
void loop()
{
if (flag == true)
{
recData[i] = SPDR;
SPDR = myData[i];
i++;
if (i == 2)
{
i = 0;
byte y0 = recData[0];
byte y1 = recData[1];
int y = (int)y0<<8|(int)y1;
Serial.print("Data received from Master: ");
Serial.println(y, HEX);
}
flag = false;
}
}
ISR(SPI_STC_vect)
{
flag = true;
}
That's exactly what I said in post #23. ![]()
But, you are not able to correct it.
You may visualize the 1-byte off concept and the mechanism of re-building correct data at Master side from the following simple example; where, 2-byte data are being exchnaged between Master and Slave using SPI Port.
Master (UNO) Sketch : save as spiP711MUno (tested)
#include <SPI.h>
byte myCommandData[] = {0x12, 0x34};
//0x12 goes, comes D-;
//0x34 goes, 0x56 comes; 0x12 goes, comes 0x78
byte myData[2];
int i = 0;
void setup ()
{
Serial.begin(115200);
SPI.begin();
digitalWrite(SS, LOW); //Slave NANO is selceted
}
void loop()
{
for (int i = 0; i < 2; i++)
{
myData[i] = SPI.transfer(myCommandData[i]);
//12/D-; 34/56; 12/78; 34/56
Serial.println(myData[i], HEX);
}
byte lsByte = myData[0]; //0x78
byte msByte = myData[1]; //0x56
int y = (int)msByte<<8|(int)lsByte;
Serial.print("Received from Slave: ");
Serial.println(y, HEX);
Serial.println("=========================");
delay(2000);
}
Slave (NANO)Sketch : save as spiP711SNano (tested)
#include <SPI.h>
byte myData[] = {0x56, 0x78}; //data to be sent to Master
volatile bool flag = false;
byte recData[2]; //data received from master would be saved here
int i = 0;
void setup ()
{
Serial.begin(115200);
pinMode(SS, INPUT_PULLUP); // ensure SS stays high for now
pinMode(MISO, OUTPUT);
pinMode(SS, INPUT_PULLUP); //Slave is kept deselceted
bitSet(SPCR, SPE); //SPI Port is enabled
bitSet(SPCR, MSTR); //Arduino is Slave
SPI.attachInterrupt(); //interrupt logic is enabled
SPDR = 0x13; //D-
}
void loop()
{
if (flag == true)
{
recData[i] = SPDR;
SPDR = myData[i];
i++;
if (i == 2)
{
i = 0;
byte y0 = recData[0];
byte y1 = recData[1];
int y = (int)y0<<8|(int)y1;
Serial.print("Data received from Master: ");
Serial.println(y, HEX);
}
flag = false;
}
}
ISR(SPI_STC_vect)
{
flag = true;
}
The following diagram (Fig-1) may be helpful for you to understand the 1-byte off event in SPI communication.
Figure-1:
I have a start transmission marker that I can use to know the beginning of the transmission. I will implement it. Or even better I will implement the synchronization method exposed in another post.
Additionally, your example makes me the thing more clear and how can I implement the reading/ writing process inside the loop instead of inside the ISR.
I will try it and let you know.
Thank you
Read the theory of SPI communication and write simple-friendly-logical codes.
A cat is a cat -- it does not matter its color. If the process works in ISR(), keep it here.
Example of post #25 has shown the process of reading/writing in the loop() function.
You missed that now the nextbyte from txBuffer is prepared for send but actually is sent only later.
Do it as everybody does it:
Everything else will be more bloated, for no reason, or won't work as it should.
Actually it's what I said here
@GolamMostafa @Wawa @sonofcy @BitSeeker @DrDiettrich
Thank everybody for your support. I found the issue.
The problem was that on the slave I was preloading SPDR = -1 for the first transmission. That was causing that Master and Slave's buffers lose synchronization because the last byte of Slave was never read by Master.
I solved it doing a dummy transmission that throws away the first received byte by Master.
That way the buffers keep synchronized.
SPI.transfer(0xFF);
delayMicroseconds(20);
for (int i = 0; i < bufferSize; i++) {
rxBuffer[i] = SPI.transfer(txBuffer[i]);
delayMicroseconds(10);
}
This problem unfortunately still remains. ![]()
Do you have a shared ground between the boards?
Leo..
Yes. I do
The circuit is like this:
220AC->600W power supply -> 24DCV to 5DCV converter-> electronic boards.
Each board has its own +5V/ground cables directly wired to the converter.