5 years, 11 months ago.

Read file via SPI

Hi,

I'm working with a Cortex-M7 and I need some external storage to be able to load some data at run-time. My idea is to use a microSD card connected to the Cortex via SPI (I'm using a Nucleo-144 board). I'm not very experienced in the SPI world but I believe what I'm trying to do should be doable. I have a few questions regarding this:

  • What's the most convenient way to store the data in the micoSD card so it can be easily read via SPI?
  • I'm aware of the doc and example here, but I was wondering if anyone could share a more complete example of how to read data from a file via SPI. (or point me to one)
  • Is SPI the wrong way of doing this? (I'd like to perform this reading using a little memory and latency as possible)

Thanks!

Javier

I managed to solve this. These are the steps I followed:

  • 1. Clone mbed-os-example-filesystem repo.
  • 2. Checkout branch "override-storage-components"
  • 3. Replace the SPIFBlockDevice bd(...) line with a SDBlockDevice declaration.
  • 4. (optional) replace `LittleFileSystem` with `FATFileSystem` if you want to read/write data to SD card and make it visible from your PC.
  • 5. Connect PINS as shown in this repo. You don't need that precise SD-card shield. Most SD-card shields will work.
  • 6. Compile it with `mbed compile -m auto -t GCC_ARM -c -f`. Once it's flashed use `mbed sterm` to see the logs.
posted by javier fernandez 18 Jan 2019

1 Answer

5 years, 11 months ago.

Hi Javier,

I use FATFileSystem together with SDBlockDevice in my K64F. See the example at the bottom of:

https://os.mbed.com/docs/mbed-os/v5.11/apis/fatfilesystem.html

If you want to use the on board SD Flash with other devices (i.e. readable from a PC), do not use the LittleFileSystem. You also don't need SPIFBlockDevice or HeapBlockDevice.

Both of the items up top are embedded in the mbed-os library. You just need to "include" them in your code.

...kevin

Hi Kevin, thanks for your reply.

I've gone through the documentation and example here but can't make it work. I only get "Hardfault" messages when trying to run it on my NUCLEO-F767ZI. I'm following the same pin connection setup as in the sd-driver example (I know now it's deprecated). I'm wondering if you have a full working example of some minimal file read/write to an sd card. Thanks!

posted by javier fernandez 15 Jan 2019

Or maybe how to run the example in the TEST directory here. Thank you.

posted by javier fernandez 15 Jan 2019

Sorry Javier, I was assuming you were using a K64F. ST does non-standard things sometimes. I have a DISCO-L475VG-IOT01A. They have a custom QSPIFBlockDevice driver that I had to use instead of SDBlockDevice. I had to do some digging in order to get things working. ST is slow to answer questions. At least now QSPIFBlockDevice is part of mbed-os.

This board also has a lot of I2C devices AND they don't use the standard mbed I2C driver either.

posted by Kevin Braun 15 Jan 2019

No worries. I managed to make it work. This is what I did:

  • 1. Clone mbed-os-example-filesystem repo.
  • 2. Checkout branch "override-storage-components"
  • 3. Replace the SPIFBlockDevice bd(...) line with a SDBlockDevice declaration.
  • 4. (optional) replace `LittleFileSystem` with `FATFileSystem` if you want to read/write data to SD card and make it visible from your PC.
  • 5. Connect PINS as shown in this repo. You don't need that precise SD-card shield. Most SD-card shields will work.
  • 6. Compile it with `mbed compile -m auto -t GCC_ARM -c -f`. Once it's flashed use `mbed sterm` to see the logs.

Hope this is helpful for someone. Because of rules in this forum, I can't post an answer to my own question, so please feel free to post one with the steps above.

posted by javier fernandez 16 Jan 2019