modm-io / modm

modm: a C++23 library generator for AVR and ARM Cortex-M devices
https://modm.io
Mozilla Public License 2.0
748 stars 132 forks source link

[example] Adding SpiFlash FatFs compatibility test #1057

Closed rasmuskleist closed 1 year ago

rasmuskleist commented 1 year ago

I have experimented with integrating FatFs with the BdSpiBlockDevice using fibers as discussed in #893 and #814. The example is for testing the compatibility of the physical drive with FatFs and is adapted from elm-chan. I have tested the example on the STM32G491MET6 on my local setup, but have added the example in the same location as the remaining flash examples.

Currently, the example blocks at disk_read in the "Single sector write test (unaligned buffer address)". I have tried to debug this issue and have found that pbuff+3 in dr = disk_write(pdrv, pbuff+3, lba, 1); causes the subsequent call to disk_read to block inside spiOperation() and specifically at RF_CALL(Spi::transfer(const_cast<uint8_t*>(tx), rx, length));. I must admit that I am quite dumbfounded by this bug and do not really know where to start. Do you have any suggestions to why this bug is occring? At this point I am a little bit lost.

rasmuskleist commented 1 year ago

I am not quite sure what the problem was, but my suggestion is that my local cmake setup did not add the ffconf_local.hpp to overwrite the FF_MAX_SS. As a consequence the buffer size was only 512b although I expected 4096. Why this first caused problems when adding to the buffer pointer, is probably because I had allocated a 4096 byte stack for the fiber, but this is just guessing. At least the example is now working and should demonstrate that FatFs can be used resumably with fibers!

Also, It occured to me that the flipping of the waitWhileBusy() in #1054 is wrong. It does not wait for program/erase to finish if multiple erase/program are performed in the loops. The test I wrote does not catch this mistake because it programs the flash in page size blocks... Sorry for this!

rasmuskleist commented 1 year ago

Thanks for the nice comments! Yeah I am quite happy it was so smoothless. Next step will be to mount the drive and read/write files. I can possibly contribute a rudimentary example of this in a later PR if you wish?

salkinium commented 1 year ago

I can possibly contribute a rudimentary example of this in a later PR if you wish?

Sure, that would be great!