wits end: FatFS, Pic32MX460 and SD

Discussion in 'Embedded Systems and Microcontrollers' started by chrisw1990, Jan 31, 2014.

  1. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    ok,
    so..
    iv been trying to find time to do fatFS implementation of USB and SD interfaces..
    finally got round to it, and for the LIFE of me, the SD card interface just will not work.. i had the usb working, but when i put the sd and usb together, the fatfs library said the usb was write protected, and wouldnt return ok when opening a file on the SD card..
    so i started over, fresh project.. and i used the microchip sample code, where they implement an SD interface for pic32..
    however... i still cannot get it to work, i dont know why.. i have attached the code, its an mplab x project, but if someone could check it.. let me know if it works, or have a look and spot obvious mistakes it would be much appreciated, also any help trying to debug this issue as im currently very stuck in a very deep rut...
     
  2. Kermit2

    AAC Fanatic!

    Feb 5, 2010
    3,772
    931
    a ZIP file?

    You have a long wait before someone gets brave enough to open that. Perhaps if they borrow a not so good friends computer, they might risk it.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    In addition, who has the time to open it and go through your code?

    That is your job. Lots of folks around here willing to help but no one is going to do your job for you.

    Not to mention is that you are the one with all of the hardware. Are you certain the SD is hooked up correctly? What have you done to make sure you are sending the proper data to the SD and to the right pin? Is the SD responding at all?

    My advice is to forget the USB interface for now. Get you MCU to interface directly to the SD card.

    You mentioned FatFS. I assume you mean from here:

    http://elm-chan.org/

    If not you should check it out. The code does work. Took me a while to get it working but it does work.
     
  4. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    i know, i put the code in a zip because it keeps everything together - includes etc, and its a single upload.. sorry!
    i did get rid of the usb interface, that code is just the SD, but for the life of me i cant figure it out..
    it is the code from the elmchan site, i know it is a long shot.. but iv been working on it for 4 days and just trying to get some suggestions :)
    sorry to have rocked the boat! wasnt my intention.
     
  5. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    oh and iv been using my logic analyser to see the bus.. but.. the code seems intermittent, i used code from microchip just to test the spi, to make sure the hardware wasnt labelled wrong (spi1 when its actually spi 2) but its not.. just trying to figure it out
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I actually opened the zip file, no dire consequences. Just a standard MPLAB-X project that opens no trouble.

    However, I've never got fatFS to work. I only briefly tried it as a smaller alternative to Microchip's file system, which works just fine for me.

    I've created several projects based on Microchip's "USB Device - Mass Storage - SD Card reader" for the device closest to mine. First get that to work on your hardware, then fold back in the Microchip file system (FSIO.c and SD-SPI.c, plus headers); I don't know what the USB uses for a file sys and I've never found it so I just add my own.

    I've always wondered how the USB and my app can both access the same SD card at the same time, but I've actually never had that occurrence or it is not a problem.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    There is no telling what is in a zip file. Most people won't open them from unknown sources. And you can stop rocking the boat by using a capital at the beginning of your sentence and stop all of those periods at the end. Lots of old people around here that find it difficult to read and sometimes just ignore messages like that.

    Is the SD responding?


    As I recall, I had to make a bunch of changes to get FATFS to work for me. I have a working example but have not touched it in some time. It might take a while to drag it out and get it working again

    I had a heck of a time with the Microchip version. I never did get it working. But if you say it works I will have to give it another try. Maybe the OP should give it a try too? I sort of like the idea of using Microchip's code anyway rather than some anonymous person out there on the internet.
     
    Last edited: Feb 1, 2014
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The only issue with Microchip is getting a correct HardwareProfile.h file to describe my target. I've never touched the source code to get USB-SD transfers, or use the SD as a drive from my programs. That was using either the C18 or the C32 compiler.
     
  9. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    iv had usb and SD working with the microchip library before, but i needed them to work together (thats usb host and sd card) in the same application, which is not supported by the MDDFS currently. unless they have changed it =]
    i didnt realise people had such difficult getting it to initially work, thats encouraging and makes me want to persist! haha :)
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    I have been trying all day to get that code working I spoke of. I am certain I had it working. So far no luck.

    Probably something wired wrong. I had it all bread boarded, I really wish I would have created a prototype board to work with the SD memory.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Finally got it working. Part of my problem was I was trying to write a file that was already there and it was getting an error. Doh!

    I am attaching the project. Unzip at your own risk. :)

    The project assumes your Pic has a hardware SPI. The file that you will need to mess with is SDSPI_CONFIG.h. You shouldn't need to touch anything else.


    I think what I will do now is to work on a demo board for an SD chip so I don't have to have issues with wiring if I ever pick up an SD project again or if someone asked about it. :)
     
    Last edited: Feb 1, 2014
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    On a scale of 1 to 10 I had about a 2 getting this to work the first time, and that was because I'd never sone SPI before and connected SDI to SDI and SDO to SDO.

    And if you follow what I posted you can make "usb host and sd card in the same application" work just fine.
     
  13. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Yeah I was going to mention that to the OP. I would not think it would matter. I would think that the Pic would be the interface between the SD chip and the outside world via USB.

    I going to whip up a SD demo board and then then to tackle the microchip library.

    I think I have a USB capable chip around here somewhere, I was going to give that a try too. But I need to order some micro usb connectors.
     
  14. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    well if you have any luck that would be great, im still cracking on, its just finding the best way of implementing the lower levels (do i use the sample fatfs code from microchip for example?)
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    Look at the code I posted, it works.
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    One thing to keep in mind is the SD card itself hold state information; it must as it accepts or output a stream of data over the single SPI connection.

    Thus no pair of devices can have complete control over the single SD card, they MUST have some sort of arbitration over which has control and which needs to wait, and probably re-establish it's connection.

    In my apps I've tended to ignore that as the USB access is infrequent and done outside of the local app's use of the card: the USB is only used to read and write files very occasionally, and never during the local app's usage.

    The local app can re-establish control simply by calling FSInit() again... I do not know where or how the "USB Device - Mass Storage - SD Card reader" app does such, though it must be in there.
     
  17. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    that might be why the microchip demo code utilises a timer interrupt, which seems to refresh the state of the sd card.. i assume like a keep alive function. and i guess this needs that functionality because fatfs works a little differently to the mdd system, perhaps.
    anyway, thanks for all the feedback, iv been busy past couple of days so i can now sit down and read things through and give them some thought!
     
  18. chrisw1990

    Thread Starter Active Member

    Oct 22, 2011
    543
    41
    Iv been looking at the code you attached, and am i right in thinking you have the petit fatfs in the folder too? I was eventually looking to use that one for an sd bootloader.. eventually haha!
     
Loading...