Shop OBEX P1 Docs P2 Docs Learn Events
OBEX 619 (SD Card): File System Corrupted — Parallax Forums

OBEX 619 (SD Card): File System Corrupted

DavidZemonDavidZemon Posts: 2,973
edited 2013-03-01 18:19 in Propeller 1
I'm giving SD card reading a try for the first time. I downloaded object 619 (http://obex.parallax.com/objects/619) and have written the following code:
{{ File:    Tachometer.spin
   Author:  David Zemon
   Project: Learn SD

   Description: Learn how to work with an SD card
}}

CON
  _CLKMODE      = XTAL1 + PLL16X
  _CLKFREQ      = 80_000_000

  BAUD_RATE     = 115_200

  LED_PIN0      = 16
  LED_PIN7      = 23

  CS            = 0
  DO            = 1
  CLK           = 2
  DI            = 3

OBJ
  pst   :       "Parallax Serial Terminal"
  sd    :       "SD-MMC_FATEngine"

PUB Main | count, sdSuccess
  pst.start(BAUD_RATE)

  dira[LED_PIN0..LED_PIN7] := $ff

  waitcnt(CLKFREQ*5 + cnt)

  ' Wait for user to be ready
  outa[LED_PIN0] := 1
  pst.Str(String("Hello world!", pst#NL))
  pst.Str(String("Press enter when ready to start...", pst#NL))
  pst.CharIn

  ' Connect to the SD card
  outa[LED_PIN0 + 1] := 1
  sdSuccess := sd.FATEngineStart(DO, CLK, DI, CS, -1, -1, -1, -1, -1)
  outa[LED_PIN0 + 2] := 1
  if (True == sdSuccess)
    pst.Str(String("Successfully started! Press enter to format...", pst#NL))
    pst.CharIn
  else
    pst.Str(String("Failed to start. Aborting.", pst#NL))
    abort

  ' Mount a partition
  outa[LED_PIN0 + 3] := 1
  pst.Str(String("Formatting partition...", pst#NL))
  sdSuccess := sd.formatPartition(0)
  outa[LED_PIN0 + 4] := 1
  if (False == sdSuccess)
    pst.Str(String("Successfully formatted FAT32 partition!!! Press enter to continue", pst#NL))
    pst.CharIn
  else
    pst.Str(String("Failed: "))
    pst.Str(sdSuccess)
    pst.Str(String(pst#NL))
    abort

  outa[LED_PIN0 + 5] := 1

  ' Unmount and stop the SD card
  sd.unmountPartition
  sd.FATEngineStop

I noticed, however, that when an error occurs it doesn't actually return the error code - it kills the cog (via the abort command) so I changed "abort" to "return" in abortError() and set the the calling function that is failing (formatPartition) to return abortError() instead of just calling it.

Anyway, my questions is, how do I go about finding out what "File System Corrupted" means or other errors that I may run into later? The card is currently unformatted.

Of course, if there is a different object that you think I should be using, I'm open to suggestions. My requirements are very simple and very static - read from and write to a single file in the root directory.

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2013-02-24 23:57
    You have to catch an abort, otherwise it will end your program. To catch you have to preceed the function call with a /

    sdSuccess := /sd.formatPartition(0)

    I never formatted a SD card with the propeller. Just do it with your PC.
  • RaymanRayman Posts: 14,826
    edited 2013-02-25 05:43
    I recently figured out that fsrw does the same thing...

    I always use a slash now when doing a "mount", otherwise it shuts down the cog on failure...

    Personally, I don't use the "abort" features, so it's easy to get surprised by this behavior...
  • KyeKye Posts: 2,200
    edited 2013-02-25 06:09
    Reading the API would solve this problem... every function in the FATEngine says this... :)

    Do a low level reformat on the PC to fix the file system corrupted error. The FATEngine does not like it when there are any problems with the media. (If it gives you a file system corrupted error there is a problem with the media, other drivers just choose to ignore the problem).

    Thanks,
  • DavidZemonDavidZemon Posts: 2,973
    edited 2013-02-27 14:27
    Okay - I switched the code back to abort and am using the '\' now. Thanks for the tip. Unfortunately, now I'm getting "Disk I/O Error".

    @Kye
    I'm not sure what you mean by "Do a low level reformat". I'm running both Ubuntu and Windows so I can use whatever tool you think would work. I tried partitioning with GParted by: delete existing partition, Create Partition Table, recreate FAT32 partition. there were no options for the partition table or FAT32 partition like block or cluster size.

    Thanks for the help.
  • KyeKye Posts: 2,200
    edited 2013-02-27 17:39
    Disk I/O error just means the driver can't interface with the SD card.

    This is a problem having to do with the block driver not getting the correct responses from the SD card on time.

    The two solutions for this are to use a different card then or use the FSRW driver also on the OBEX... it has a different block driver that may be suitable.

    Thanks,
  • DavidZemonDavidZemon Posts: 2,973
    edited 2013-02-27 20:44
    Kye,

    I'm using the 4GB version of this SD card http://www.kingston.com/us/flash/sd_cards#sd4 if that helps.

    I tried fsrw as well and upon mount_explicit, -1 is returned. Attached is my updated code.

    I also reformatted with FAT16 this time (didn't change anything with either library).

    Thanks,
    David
    {{ File:    LearnSD.spin   Author:  David Zemon
       Project: Learn SD
    
    
       Description: Learn how to work with an SD card
    }}
    
    
    CON
      _CLKMODE      = XTAL1 + PLL16X
      _CLKFREQ      = 80_000_000
    
    
      BAUD_RATE     = 115_200
    
    
      LED_PIN0      = 16
      LED_PIN7      = 23
    
    
      CS            = 0
      DO            = 1
      CLK           = 2
      DI            = 3
    
    
    OBJ
      pst   :       "Parallax Serial Terminal"
      sd    :       "SD-MMC_FATEngine"
      fsrw  :       "fsrw"
    
    
    VAR
      long count
    
    
    PUB Main
      pst.start(BAUD_RATE)
    
    
      dira[LED_PIN0..LED_PIN7] := $ff
      count := 0
    
    
      waitcnt(CLKFREQ*5 + cnt)
    
    
      SD_fsrw
    
    
    PUB SD_fsrw | sdSuccess
      FuncComplete
      pst.Str(String("Hello!", pst#NL))
      pst.Str(String("Ready to mount when you are! Press enter...", pst#NL))
      pst.CharIn
      sdSuccess := \fsrw.mount_explicit(DO, CLK, DI, CS)
      if sdSuccess
        pst.Str(String("Mount failed... sad day! :( "))
        pst.Dec(sdSuccess)
        pst.Str(String(pst#NL))
      else
        pst.Str(String("Mount succeeded! Yay!!! :D", pst#NL))
      FuncComplete
    
    
    PUB SD_MMC_FAT | sdSuccess
      ' Wait for user to be ready
      FuncComplete
      pst.Str(String("Hello world!", pst#NL))
      pst.Str(String("Press enter when ready to start...", pst#NL))
      pst.CharIn
    
    
      ' Connect to the SD card
      FuncComplete
      sdSuccess := \sd.FATEngineStart(DO, CLK, DI, CS, -1, -1, -1, -1, -1)
      FuncComplete
      if (True == sdSuccess)
        pst.Str(String("Successfully started! Press enter to mount...", pst#NL))
        pst.CharIn
      else
        pst.Str(String("Failed to start. Aborting.", pst#NL))
        abort
    
    
      ' Mount a partition
      FuncComplete
      pst.Str(String("Mounting partition...", pst#NL))
      sdSuccess := \sd.mountPartition(0)
      FuncComplete
      if (False == sdSuccess)
        pst.Str(String("Successfully mounted partition!!! Press enter to continue", pst#NL))
        pst.CharIn
      else
        pst.Str(String("Failed: "))
        pst.Str(sdSuccess)
        pst.Str(String(pst#NL))
        abort
    
    
      FuncComplete
    
    
      ' Unmount and stop the SD card
      sd.unmountPartition
      sd.FATEngineStop
    
    
    PUB FuncComplete
      outa[LED_PIN0 + count] := 1
    
  • AribaAriba Posts: 2,690
    edited 2013-02-28 00:10
    Error -1 means the SD card can not be Reseted. This is the first thing the drivers try to do, so it looks like something with the SD card connection is wrong.
    Are you sure you have the SD pins connected the way you define it in the CON section?:
    CS            = 0
      DO            = 1
      CLK           = 2
      DI            = 3
    

    Most boards (QuickStart HMI, PropellerPlatform for example) have the pins in this order:
    DO            = 0
      CLK           = 1
      DI            = 2
      CS            = 3
    

    Andy
  • DavidZemonDavidZemon Posts: 2,973
    edited 2013-02-28 00:18
    I will double check the pins tomorrow and post a picture. I'm using it on a breadboard so that's why the pin configuration doesn't look familiar
  • KyeKye Posts: 2,200
    edited 2013-02-28 07:23
    Note, DO is the data out from the SD card, not the propeller...
  • DavidZemonDavidZemon Posts: 2,973
    edited 2013-02-28 19:31
    I'm extremely confused why DO is not data out from the propeller, but that was my problem. Thanks for the tip.

    ### SD Card Profile ###


    Disk Signature: 0x000B78B6


    Partition 0 - 0xF0F27818
    FAT16


    512 - Bytes Per Sector
    128 - Sectors Per Cluster
    7623936 - Total Sectors
    59562 - Total Clusters
    256 - Used Sectors
    7623680 - Free Sectors


    32 KB Stride Speed Test:


    writeByte - 3 KBs
    writeShort - 6 KBs
    writeLong - 11 KBs
    writeData - 207 KBs
    readByte - 3 KBs
    readShort - 6 KBs
    readLong - 12 KBs
    readData - 263 KBs
  • RaymanRayman Posts: 14,826
    edited 2013-03-01 05:36
    After getting confused myself from time to time, I now try to label the pins how they should be labelled: MISO and MOSI instead of DO and DI.
    MISO is Master-in-Slave-Out and MOSI is Master-Out-Slave-In.
    I think it's pretty clear that the Propeller is the master in this case and the SD card is the slave.

    This labelling makes things a lot more clear, IMHO.
  • KyeKye Posts: 2,200
    edited 2013-03-01 07:31
    I just followed how FSRW did it. Ask them...
  • RaymanRayman Posts: 14,826
    edited 2013-03-01 18:19
    I know a lot of people use DI and DO, but I think using MOSI and MISO is more clear...

    Here's the wikipedia info on it:
    http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
Sign In or Register to comment.