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