7 years, 6 months ago.

SD card + SPI (as a Slave) on FRDM-K64F - spi.receive() never triggered (works without SD card!?)

Hello,

my intention is to read the data from SD card and send it over to another board using SPI (FRDM in a Slave mode). I have two separate programs:

  1. Read the data from SD card and print it over Serial - works perfectly.
  2. Send some data over SPI (as a slave) to second board - second board receives all the data correctly.

Here comes the problem, once I merge these two programs, I can buffer up the data from SD card just fine, but when I get to the point where I'm about to send the data, the:

if (spi.receive()) {

is never triggered, and I used a scope to see that I'm getting the clock from other board and also some data is pushed to the slave... Could anyone shed some light on this for me, please?

Hardware setup

  • FRDM-K64F <-> SD card slot - 32 GB micro SD card type 10
  • Connections FRDM-K64F (Slave) <-> Nucleo-F746ZG (Master):
    • PTD5 (SCK) <-> PE_2 (SPI4_SCLK)
    • PTD4 (CS) <-> PE_4 (SPI4_SSEL)
    • PTD6 (MOSI) <-> PE_5 (SPI4_MISO)
    • PTD7 (MISO) <-> PE_6 (SPI4_MOSI)
    • (initially I thought MISO should link with MISO, and the same for MOSI, but that didn't work at all).
  • both boards have common ground.

Code

#define SIZEOFDATA  96000

#include "mbed.h"
#include "SDFileSystem.h"

Serial pc(USBTX, USBRX);
int16_t data[SIZEOFDATA];

int main()
{
    pc.printf("The beginning it is\r\n");

    if (true) {  // trying strange things with variable scope
        SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
    
        if(sd.card_present())
            pc.printf("\r\nCard Detected was...\r\n");
        //Mount the filesystem
        sd.mount();
    
        FILE *fp = fopen("/sd/test_file.bin", "r");
        if(fp == NULL) {
            error("Could not open file for reading\r\n");
        } 
    
        int result = fread(&data,sizeof(data), 1, fp);
        if( result  > 0 ) //while we can still read from the file pull in 16bits
        {
            printf("60000 Samples Read...\r\n");
        }
        else
        {
            printf("Hmmm Didn't Read Anything :<\r\n");
        }
        printf("Read: %d Newdat size: %d Value of Sample 0: %d\r\n", result, sizeof(data), data[0]);
    
        fclose(fp);
        sd.unmount();
    }

    // establish SPI link
    SPISlave spi(PTD6, PTD7, PTD5, PTD4); // mosi, miso, sclk, ssel
    spi.format(16,0);        // Setup:  bit data and mode (polarity, phase)
    spi.frequency(1000000); // 1MHz
 
    int reply = 8080;
    // try with some fake data
    spi.reply(reply);
    spi.reply(reply);
    pc.printf("SPI enabled!\r\n");
    int counter = 0;
   while (1) {
        if (spi.receive()) {
            int data_in = device.read();
            spi.reply(data[counter++]);
            if (counter >= SIZEOFDATA) {
                counter = 0;
            }
            spi.reply(++reply);
            pc.printf("Received %d; sending %d\r\n", data_in, reply);
        } 
    }        
}

As far as I can tell, everything runs fine until line 53. But the inner parts of conditional statement never gets executed...

Any insights much appreciated! Thank you.

1 Answer

4 years, 7 months ago.

Hi, What happens in your Master ( Nucleo-F746ZG)? Is it sending something to SPI? Line 53 is never true if nobody is sending nothing to the Slave (K64F). If scope is showing that chip select, Mosi and clock are working on the correct lines I would check the spi.format.