SPI SD Card Writes using PIC18F27K40 w MPLAB X-IDE

SPI SD Card Writes using PIC18F27K40 w MPLAB X-IDE

Postby anightin » Fri Jul 09, 2021 6:27 pm

Dear All,

I hope you are all well in these strange times.

I have an issue with getting a PIC18F27K40 to write SD card sectors correctly.

I am currently using MPLAX X IDE 5.45 with XC8 2.32 and MCC v4 on Windows 10.

I've set up my PIC18F with an internal 16MHz clock, and a SPI connection to a standard SD card adapter and I'm running the SPI clock at 1Mhz using the FatFs (R0.13) interface on top of the SD_SPI driver. All files apart from my main.c code are generated using MCC. I am debugging via an FDDI to USB serial cable and programming using a PCKit3.

In terms of my main.c code, I can initialize, read and write to the card and do things such as create files and list directory contents etc.

I was about to start celebrating when I then tried the f_mkdir() function that executes multiple sector writes.

Here I spotted an issue. When putting the SD card back into my PC, it failed to delete the directory I had created. On further inspection chkdsk/f gave:

\testdir Errors in . and/or .. corrected.
Removing nonvalid long folder entry from \testdir...
I found that the directory entry contained invalid data. At first I thought this was an issue with FafFs code so even ported the same code to an ESP8266 I had, and it worked fine with the same SD card on that platform (using the Arduino SPI driver implementation for the ESP).

The function I am calling on the PIC is SD_SPI_SectorWrite(). No errors are reported at any time.

However, I found by experimentation that if I put a SD_SPI_SectorRead() command after the SD_SPI_SectorWrite(), everything works as expected without any errors.

Clearly this creates a sub-optimal performance from the card, and I'd love to get to the bottom of it. I can only imagine that the SD card is continuing to write it's data and the PIC code is not giving enough time for that to complete before the next sector write. I have tried inserting time delays after the write operation, and even sending null commands but I can't seem to get the behavior to change with these.

I am quite new to PIC programming so starting to run out of tricks here other than get the oscilloscope out and trying to make sense of what's going on at the hardware level.


If you can help me shed any light on the above I would much appreciate it.

Many thanks in advance.

Andy
anightin
 
Posts: 2
Joined: Fri Jul 09, 2021 6:20 pm
PIC experience: Experienced Hobbyist

Re: SPI SD Card Writes using PIC18F27K40 w MPLAB X-IDE

Postby ric » Fri Jul 09, 2021 11:34 pm

This is only a guess, I haven't used that library.
Is it possible the last write is still sitting in RAM. and not yet flushed to the SD card?
Possibly adding the SD_SPI_SectorRead() call forces it to be written.
Is there some sort of "flush" or "eject" function available?
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: SPI SD Card Writes using PIC18F27K40 w MPLAB X-IDE

Postby anightin » Sat Jul 10, 2021 12:09 am

Thanks, I guess I could try only doing the read on the last write to see if that still has the same effect. I'll try anything at this stage :)
anightin
 
Posts: 2
Joined: Fri Jul 09, 2021 6:20 pm
PIC experience: Experienced Hobbyist


Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 10 guests

cron