Hi Folks,
After a weeks trial and error, I have now got 2 MBEDS talking to each other via SPI, and I want to share this with others trying to do the same. In addition, I have a question on the SPI Slave behaviour - please see later in mail.
What I've done...
1. Connect two mbeds :- p5->p11, p6->p12, p7->p13 and p8->p14 (note that first is master, 2nd slave). Note that I connect straight, I do not cross MOSI and MISO. If I do cross these, I get zero values from the Slave.
2. Ensure that both mbeds have common earth. Not sure if this is necessary, but it did seem to help :-/
3. I am using breadboard, and I have found that (v. occasionally) it was necessary to re-seat the wires, or occasionally jiggle them around when it didn't work. Think this needs better contact work - which I intend to do when I do my real project. Also working at lower frequencies helps. Anyhow, it does work.
The code...
Master
#include "mbed.h"
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut chipSelect(p8);
Serial pc(USBTX, USBRX); // tx, rx
int main() {
int valueToSendToSlave = 20; // Starting value only, this increments
spi.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture
spi.frequency(1000000); //1MHz
pc.printf("======================================================\r\n");
pc.printf("Press any key to start...\r\n");
pc.getc(); // wait for keyboard
int counter = 1;
while (1) {
pc.printf("%d Value to send = %d ", counter++, valueToSendToSlave);
chipSelect = 0; // Select device
int dataFromSlave = spi.write(valueToSendToSlave);
chipSelect = 1; // Deselect device
pc.printf(" returns %d\r\n", dataFromSlave);
valueToSendToSlave++;
wait(5); // Wait for 5 seconds for readability only
}
}
Slave
#include "mbed.h"
SPISlave device(p11, p12, p13, p14); // mosi, miso, sclk, ssel
Serial pc(USBTX, USBRX); // tx, rx
int main() {
int counter = 1;
device.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture
device.frequency(1000000); // 1MHz
int reply = 99;
device.reply(reply); // Prime SPI with first reply
device.reply(reply); // Prime SPI with first reply, again
pc.printf("======================================================\r\n");
pc.printf("Startup Next reply will be %d\r\n", reply);
while (1) {
if (device.receive()) {
int valueFromMaster = device.read();
pc.printf("%d Something rxvd, and should have replied with %d\n\r", counter++, reply);
device.reply(++reply); // Prime SPI with next reply
pc.printf(" Received value from Master (%d) Next reply will be %d \r\n", valueFromMaster, reply);
}
}
}
Output
Master
Press any key to start...
1 Value to send = 20 returns 0
2 Value to send = 21 returns 99
3 Value to send = 22 returns 100
4 Value to send = 23 returns 101
5 Value to send = 24 returns 102
6 Value to send = 25 returns 103
Slave
Startup Next reply will be 99
1 Something rxvd, and should have replied with 99
Received value from Master (20) Next reply will be 100
2 Something rxvd, and should have replied with 100
Received value from Master (21) Next reply will be 101
3 Something rxvd, and should have replied with 101
Received value from Master (22) Next reply will be 102
4 Something rxvd, and should have replied with 102
Received value from Master (23) Next reply will be 103
5 Something rxvd, and should have replied with 103
Received value from Master (24) Next reply will be 104
QUESTION: I think the first value returned from Slave should be 99, not 0. I dont understand why the first Master 'write()' doesn't provoke a Slave 'device.received()'?
Note the 2 'reply()' in the initial setups of the Slave. If you remove one of these, which I think is correct, the output is...
Master
Press any key to start...
1 Value to send = 20 returns 0
2 Value to send = 21 returns 100
3 Value to send = 22 returns 100
4 Value to send = 23 returns 102
5 Value to send = 24 returns 103
6 Value to send = 25 returns 104
7 Value to send = 26 returns 105
Slave
Startup Next reply will be 99
1 Something rxvd, and should have replied with 99
Received value from Master (20) Next reply will be 100
2 Something rxvd, and should have replied with 100
Received value from Master (21) Next reply will be 101
3 Something rxvd, and should have replied with 101
Received value from Master (22) Next reply will be 102
4 Something rxvd, and should have replied with 102
Received value from Master (23) Next reply will be 103
5 Something rxvd, and should have replied with 103
Received value from Master (24) Next reply will be 104
6 Something rxvd, and should have replied with 104
Received value from Master (25) Next reply will be 105
7 Something rxvd, and should have replied with 105
Received value from Master (26) Next reply will be 106
Notice how the Master never receives the initial value of 99.
I hope my logic is correct here.
Can anybody help please?
Many Thanks
Ian Hunter
Hi Folks,
After a weeks trial and error, I have now got 2 MBEDS talking to each other via SPI, and I want to share this with others trying to do the same. In addition, I have a question on the SPI Slave behaviour - please see later in mail.
What I've done...
1. Connect two mbeds :- p5->p11, p6->p12, p7->p13 and p8->p14 (note that first is master, 2nd slave). Note that I connect straight, I do not cross MOSI and MISO. If I do cross these, I get zero values from the Slave.
2. Ensure that both mbeds have common earth. Not sure if this is necessary, but it did seem to help :-/
3. I am using breadboard, and I have found that (v. occasionally) it was necessary to re-seat the wires, or occasionally jiggle them around when it didn't work. Think this needs better contact work - which I intend to do when I do my real project. Also working at lower frequencies helps. Anyhow, it does work.
The code...
Master
Slave
Output
Master
Slave
QUESTION: I think the first value returned from Slave should be 99, not 0. I dont understand why the first Master 'write()' doesn't provoke a Slave 'device.received()'?
Note the 2 'reply()' in the initial setups of the Slave. If you remove one of these, which I think is correct, the output is...
Master
Slave
Startup Next reply will be 99
Notice how the Master never receives the initial value of 99.
I hope my logic is correct here. Can anybody help please?
Many Thanks Ian Hunter