mbed to mbed SPI

21 Jun 2011

I'm trying to connect two mbed devices together via SPI to do some benchmarking. Below is the code I've got on my two devices, my problem is that the master and slave seem to be receiving corrupted data from each other. If I hook the master up in a loopback configuration it gets the expected characters.

    int i=0;
    bool run=true;
    char send[15]="MASTER HELLO\r\n";
    csel=1;
    csel=0;
    while (run){

        if (pc.readable() && pc.getc()=='s') run=false;
        csel=1;
        csel=0;
        int response = spi.write((int)send[i]);
        csel=1;
        pc.printf("Sending: %c\r\n",send[i]);
        pc.printf("Response: %c\r\n",(char)response);
        wait_us(1000);
        if(i>=sizeof(send))i=0;
        else i++;
    }

SLAVE:

spi.format(8,0);
    spi.frequency(100000);
    
    pc.baud(115200);
    pc.printf("SPI Slave: Now starting ITG-3200 test...\r\n");
    
    char send[14]="SLAVE HELLO\r\n";

    char i=0;
    spi.reply(i);
    while (true) {
        if (spi.receive()) {
            
            int c = spi.read();
            pc.printf("%c",(char)c);
           
            i++;
            if(i>=sizeof(send))i=0;
            spi.reply(send[i]);
            
        }
        wait_us(1000);
    }