Remapped SPI1_MOSI on STM32 doesn't seem to work

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Remapped SPI1_MOSI on STM32 doesn't seem to work

Post by Badger »

It turns out that whilst on average I can write data to the SD card fast enough during the I2c down time, when the FatFS code creates new sectors / clusters the write operation takes too long :cry:. I've been trying to think of a way of getting around this by having one thread which performs the SD card writing using a IOQueue, and somehow having my I2C thread pause the SD thread whilst it uses I2C1, but I can't see any way in Chibios to pause a thread (from outside that thread), only to wake one - is there a way to do that?

For reference, I'm running my I2c reading loop at 1ms intervals. 9/10 times I2C1 just reads one sensor which takes 250us. 1/10 times it reads 2 sensors which takes ~ 500us. My average write time for 26byte data packet is about 170us, so I should be well within my required data rate if I can think up a clever way of pausing FatFS operations! I've tried playing with the FAT block size but I still have the problem.

Any help on this would be much appreciated!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Remapped SPI1_MOSI on STM32 doesn't seem to work

Post by Giovanni »

You may feed an SD-writer thread using a mailbox, this way you would have a queue of pending transaction and only the average speed would matter. I would make a memory pool of "write transaction objects" and a mailbox. The client thread(s) gets an object from the pool and posts it on the mailbox, the server thread fetches the objects from the mailbox, performs the write on FatFs and then returns the object into the pool.
This would decouple the data writers from the FS.

About synchronization between FatFs and I2S, there is a mutex in the diskio.c module of FatFs, you may lock it before accessing I2C, this would prevent FatFs from using the SPI, then you would release the mutex. You may need to make the mutex public, I don't remember if it is declared static. I also suggest to use the latest diskio.c from the repository, I made improvements.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Remapped SPI1_MOSI on STM32 doesn't seem to work

Post by Badger »

Hi Giovanni,

Thanks for those suggestions, they sound like they'll work well. I see that there is a diskio.c in /ext and also one in the fatfs.zip; which one is the current version? I can't seem to find a mutex in either!

I've been experimenting with pre-allocating the file on the SD card but so far with no luck. Another option is to somehow pause the SPI DMA transfer, but I'm not sure if that is possible. I suppose it is datasheet time :D
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Remapped SPI1_MOSI on STM32 doesn't seem to work

Post by Giovanni »

Hi,

The one in the zip file, the other one is a file i "parked" there, I will remove it.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Remapped SPI1_MOSI on STM32 doesn't seem to work

Post by Badger »

I've been doing some experimenting with the MMC module, and found that with my card the majority of the MMC write time was spent waiting on the card, which was needlessly blocking the thread (obviously in most cases this isn't really a problem as the thread would be dedicated to it). I got around it by implementing mmcStartWrite, mmcWrite and mmcStopWrite which are very similar to the existing code except they only do single block writes (which makes sense when using fatfs; as far as i can tell it will only ever write one block at a time).

I then altered mmcWrite so that it doesn't call wait(). Instead, wait() is called at the start of disk_write/disk_read in diskio.c, so essentially the wait checking is delayed until the last possible moment. I'm also using the latest version of FatFS which seems to work fine with very similar patches to previous versions.
Post Reply