9 years, 3 months ago.

How to Configure two F401RE boards to communicate via SPI interface and with interrupt capabilities?

Hi

I am new to Arm developer environment. I am trying to establish two way communication link among F401RE boards via SPI interface, where one board is a master and other is a slave- see below my implemented code for each board. My challenges:

1) I am unable to transfer negative integers from master to slave. I think, slave is displaying data on a PC in two's complement form- How could I receive the data it in the right format?

2) Instead of polling data, i.e. member function "receive" of slave, is it possible to have an interrupt confirming that the data is received on its respective side?

I will appreciate your help.

//======================== SPI Master====================================
#include "mbed.h"
 
Serial pc(USBTX, USBRX); 
SPI deviceMaster (PB_5, PB_4, PB_3);// mosi, miso, sclk
DigitalOut cs(PA_9);

int main() {
    
    pc.printf("Communicating with PC...\n");
    cs = 1; // Chip must be deselected
 
    deviceMaster.format(8,2);
    deviceMaster.frequency(1000000);
    int values[5] = {0, 5, -15, 10, 55};   
    int i = 0;
    int val = 0;
    
    while(1) {
        if (i < 4){
            i++;
            }
            else
            {
                i = 0;
                }
        val = values[i];        
        cs = 0; // Select the device by seting chip select low  
        deviceMaster.write(val);
        cs = 1;
                
        //int value = deviceMaster.read();
            
            pc.printf("Master Transmission...\n");
            pc.printf("%d  \r\n", val); 
            wait_us(1000000); 
    }
}

//======================== SPI Slave ====================================

// Reply to a SPI master as slave
 
 #include "mbed.h"
 #include <SPISlave.h>

Serial pc(USBTX, USBRX); 
SPISlave device(PB_5, PB_4, PB_3, PA_4); // mosi, miso, sclk, ssel

 int main() {
     pc.printf("Communicating with PC...\n");
     int v = 0;
     device.format(8,2);
    device.frequency(1000000);     
     device.reply(0x00);              // Prime SPI with first reply
     while(1) {
         if(device.receive() == 1) {
             v = device.read();   // Read byte from master
             //device.reply(v);         // Make this the next reply 
            pc.printf("Slave Transmission...\n");
            pc.printf("%d  \r\n", v);        
         }
     }
 }

1 Answer

9 years, 3 months ago.

Please use <<code>> and <</code>> tags around your posted code on separate lines to keep it readable.

The problem in the code above is that you try to send integer values (32 bit), but the SPI port has been formatted for 8bit mode. The result is that you will only send and receive the LSB byte. You could try to format the port for 32bit, but this is not supported by all mbed platforms. Better solution is to send the int as 4 bytes and reconstruct them into an int again on the slave side.