Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 5 months ago.
First data transfer from Master is invalid (FRDM-K64F boards)
Hi,
I am new to mbed and i have a question regarding master-slave SPI communication. I have two FRDM-K64F boards connected to each other using SPI pins. My connection looks as below:
SPI Master
#include "mbed.h" SPI spi(PTD2, PTD3, PTD1); // mosi, miso, sclk, ssel DigitalOut chipSelect(PTD0); Serial pc(USBTX, USBRX); // tx, rx int main() { int valueToSendToSlave = 1; chipSelect = 1; // Deselect device spi.format(8,3); spi.frequency(1000000); pc.printf("=================== SPI Master ===================================\r\n"); pc.printf("Press any key to start...\r\n"); pc.getc(); while (1) { chipSelect = 0; int dataFromSlave = spi.write(valueToSendToSlave); chipSelect = 1; // Deselect device pc.printf("Sent value:%d Received value:%d\r\n", valueToSendToSlave, dataFromSlave); valueToSendToSlave++; wait(10); } }
SPI Slave
#include "mbed.h" #include <stdio.h> SPISlave device(PTD2, PTD3, PTD1, PTD0); // mosi, miso, sclk, ssel Serial pc(USBTX, USBRX); // tx, rx int main() { pc.printf("=================== SPI Slave ===================================\r\n"); device.format(8,3); device.frequency(1000000); // 1MHz int reply = 0x00; device.reply(reply); // Prime SPI with first reply while (1) { if (device.receive()) { int valueFromMaster = device.read(); pc.printf("Received value:%d Sent value:%d\r\n", valueFromMaster, reply); device.reply(++reply); // Prime SPI with next reply } } }
Output is as below
COM15 is master and COM18 is slave. I have two questions regarding this ouput
- First data which is sent from master is 1 but on the slave the received value is 128. Why is the first data from the master received incorrectly on the slave.
- First reply from slave should be 0 which is correct but the second reply from the slave should be 1. The slave reply is set to 1 in it's second reply but the master is receiving 0. This goes on in the subsequent loops. Is there a reason why slave sends same reply twice and then starts sending the correct reply
I am using keil uvision v5.27 to compile and run the program. The master and slave spi examples were imported from mbed.
1 Answer
5 years, 4 months ago.
Hi Ravi,
In Master code, you should set cs to spi object, and don't use another DigitalOut to control cs. SPI mater will control cs itself when doing write operation. Please refer to below modefication.
SPI Master
#include "mbed.h" SPI spi(PTD2, PTD3, PTD1, PTD0); // mosi, miso, sclk, ssel Serial pc(USBTX, USBRX); // tx, rx int main() { int valueToSendToSlave = 1; spi.format(8,3); spi.frequency(1000000); pc.printf("=================== SPI Master ===================================\r\n"); pc.printf("Press any key to start...\r\n"); pc.getc(); while (1) { int dataFromSlave = spi.write(valueToSendToSlave); pc.printf("Sent value:%d Received value:%d\r\n", valueToSendToSlave, dataFromSlave); valueToSendToSlave++; wait(10); } }
Thanks,
Desmond