Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Nucleo module as slave
Using Nucleo modules as slave was not easy to start with. Many thing could go wrong. I provide two projects that works and can send a float number between them.
Still I get sometimes packet lost, but it is working in general. You might modify it so it will be more secure to communicate between the modules.
I used SPI 16 bits mode to allow sending some kind of checksum or control code at the MSByte .. The LSByte should be always the data. Using this technique, you can make a quite reliable communication between the two module.
Master code is as follow :
include the mbed library with this snippet
SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
typedef unsigned char byte;
typedef union {
float sensor;
byte SPI_Packet[sizeof(float)];
}SPI_Packet_t;
unsigned short tem;
int i;
unsigned int paSize;
SPI_Packet_t spipacket;
DigitalOut cs(SPI_CS);
#define SPI_DUMMY 0X0000
#define SPI_CMD 0XADAD
#define SPI_FAULT 0xDD
Serial pc(SERIAL_TX, SERIAL_RX);
short sendToSlave(unsigned short value )
{
short result=0;
cs=1;
wait_us(500);
cs=0;
device.write(value);
wait_us(500);
cs=1;
wait_ms(40);
cs=0;
wait_us(500);
result=device.write(0x0000);
wait_us(500);
cs=1;
wait_ms(40);
return result;
}
int main()
{
device.format(16,0);
device.frequency(1000000);
tem=0;
while(1) {
tem=sendToSlave(SPI_CMD); //RETURNED BYT IS DUMMY
paSize=(tem & 0xFF00)>>8;
if(paSize>0){
spipacket.SPI_Packet[0]=(byte)(tem & 0x00FF);
pc.printf("Packet=%x\n",spipacket.SPI_Packet[0]);
pc.printf("packet size=%i\n",paSize);
for (i=1; i<paSize; i++) {
cs=1;
wait_us(500);
cs=0;
tem=(unsigned short)device.write(SPI_DUMMY);
wait_us(500);
cs=1;
wait_ms(40);
if( ((tem & 0xFF00)>>8)!=SPI_FAULT){
spipacket.SPI_Packet[i]=(byte)(tem & 0x00FF);
pc.printf("Packet=%x\n",spipacket.SPI_Packet[i]);
}
else {
pc.printf("wrong packet\n");
break; //go out from for loop.
}
}
pc.printf("Temp= %04.2f\n",spipacket.sensor);
pc.printf("------------------------------\n");
}
spipacket.sensor=0.0;// Reset the variable.
wait(1);
}
}
And the Slave side is as follow :
include the mbed library with this snippet
Serial pc(USBTX,USBRX);
#define SPI_DUMMY 0X0000
#define SPI_CMD 0XADAD //Anything as a CMD code.
#define SPI_FAULT 0xDD00 //Anything as a part of 16 bit as control byte
#ifdef _DEBUG
extern Serial pc;
#define DEBUG_MESSAGE(...) pc.printf(__VA_ARGS__)
#else
#define DEBUG_MESSAGE(...)
#endif
typedef unsigned char byte;
SPISlave slave(SPI_MOSI, SPI_MISO, SPI_SCK ,PA_4);
int res=0;
float d,c,e;
/*This will be the float number we wish to transfer.
You can make a struct of multiple variable and send it in the same way*/
typedef union {
float sensor;
byte SPI_Packet[sizeof(float)];
}SPI_Packet_t;
SPI_Packet_t packet;
int main()
{
pc.printf("Testing is started from slave side\n");
slave.format(16,0);
slave.frequency(1000000);
unsigned short vs=0;
slave.reply(SPI_DUMMY); // Prime reply
unsigned short tempVal=0;
packet.sensor=1000.02; //Value of the float number we wish to communicate
while(1) {
if(slave.receive()) {
tempVal=(unsigned short) slave.read(); //Real command
printf("----------------\n%x\n",tempVal);
/*
The switch is not necessary if you have only one command between
master-slave. This is useful if you wish to send different
object depending on the command.
*/
switch(tempVal){
case SPI_CMD: { /*Use this if you have different commands
for sending different objects to the master.*/
tempVal=(sizeof(float))<<8| packet.SPI_Packet[0];
pc.printf("Packet=%x\n",tempVal);
slave.reply(tempVal);
for(unsigned int i=1; i<sizeof(float); i++){
while(!(slave.receive()))
wait_ms(1);
vs=slave.read(); //Real command
if (vs==SPI_DUMMY){
//Continue sending bytes
{
tempVal=0x00FF & packet.SPI_Packet[i];
pc.printf("Packet=%x\n",tempVal);
slave.reply(tempVal);
}
}
else {
slave.reply(SPI_FAULT); //ERROR CODE
break;
}
}
}
break;
}
}
}
}
Note:
Slave communication is shifted by one byte. To respond for ex for a START byte, you need to put the ACK byte prepared on the bus even before having the START byte received by the slave. As the Master write the SPI bus the START Command, the prepared byte on the slave SPI bus will be transferred to the Master immediately. So, you need to prepare the Slave device to shift the bytes by one byte as in the following example.