SPI mbed to mbed connection issue

29 Jan 2012

Hello everyone,

I have an issue regarding an SPI connection between two mbeds.

Basically, what I want to do is to receive 4 values from the slave every 5 seconds.

The following code snippets are made to describe the problem.

The first mbed is the master and uses the following code:

#include "mbed.h"

SPI spi(p11, p12, p13); 
DigitalOut chipSelect(p29);

Serial pc(USBTX, USBRX); 

uint8_t readSPIByte() 
{   
    chipSelect.write(0);   
    uint8_t value = spi.write(0xFF);  
    chipSelect.write(1);
   
    return value;
}

int main() 
{
    pc.baud(115200);
  
    spi.format(8, 3);        
    spi.frequency(1000000); 

    pc.printf("======================================================\r\n");
    pc.printf("Press any key to start...\r\n");
    pc.getc();  
                   
    while (1)
    {        
        uint8_t dataFromSlave1 = readSPIByte();       
        uint8_t dataFromSlave2 = readSPIByte();     
        uint8_t dataFromSlave3 = readSPIByte();      
        uint8_t dataFromSlave4 = readSPIByte(); 
        
        pc.printf("Returns %d\r\n", dataFromSlave1);
        pc.printf("Returns %d\r\n", dataFromSlave2);
        pc.printf("Returns %d\r\n", dataFromSlave3);
        pc.printf("Returns %d\r\n", dataFromSlave4);
        
        wait(5);      
    }
}

The second mbed is the slave and uses the following code:

#include "mbed.h"

SPISlave device(p5, p6, p7, p8); 

Serial pc(USBTX, USBRX); 

int main() 
{    
    pc.baud(115200);

    device.format(8, 3);        
    device.frequency(1000000); 
    
    device.reply(0x00);

    int reply = 1;    
    
    pc.printf("======================================================\r\n");    

    while (1) 
    {      
        if (device.receive()) 
        {      
            int valueFromMaster = device.read();          
            device.reply(reply);
            pc.printf("Received value (%d) reply %d \r\n", valueFromMaster, reply); 
            reply++;         
        }     
    }
}

The output of the following programs is not what I expected from the master and the slave side.

Master outputs:

Quote:

Press any key to start...

Returns 0

Returns 0

Returns 0

Returns 0

Returns 2

Returns 0

Returns 0

Returns 0

Returns 4

Returns 0

Returns 0

Returns 0

Returns 6

Returns 0

Returns 0

Returns 0

Returns 8

Returns 1

Returns 2

Returns 2

Returns 10

Returns 3

Returns 4

Returns 4

Returns 12

Returns 5

Returns 6

Returns 6

Slave outputs:

Quote:

Received value (255) reply 1

Received value (255) reply 2

Received value (255) reply 3

Received value (255) reply 4

Received value (255) reply 5

Received value (255) reply 6

Received value (255) reply 7

Received value (255) reply 8

Received value (255) reply 9

Received value (255) reply 10

Received value (255) reply 11

Received value (255) reply 12

Received value (255) reply 13

Received value (255) reply 14

The problem seems to be solved by using wait commands between each readSPIByte() calls. But is this the only way to make this work? If yes, what should be the minimum amount of waiting time? Is this a case of trial and error until the minimum delay time is determined or there is specific delay value?

05 May 2016

this code was perfect working. try to use single slave you can get output.

05 May 2016

include the mbed library with this snippet

#include "mbed.h"
 
SPI spi(p11, p12, p13); 
DigitalOut chipSelect(p29);
AnalogIn mypot1(p19); 
float z1; 
 
Serial pc(USBTX, USBRX); 
 
uint8_t readSPIByte() 
{   
    chipSelect.write(0);   
        z1=mypot1.read();
        z1=z1*100;
    uint8_t value = spi.write(z1);  
    chipSelect.write(1);
   
    return value;
}
 
int main() 
{
    pc.baud(115200);
  
    spi.format(8, 3);        
    spi.frequency(1000000); 
 
    pc.printf("======================================================\r\n");
    pc.printf("Press any key to start...\r\n");
    pc.getc();  
                   
    while (1)
    {        
        uint8_t dataFromSlave1 = readSPIByte();    
        
        pc.printf("Returns %d\r\n", dataFromSlave1);
        
        wait(1);      
    }
}