Writing data to SD card using PIC18f26k80 over SPI

Discussion in 'Embedded Systems and Microcontrollers' started by Sangam Singh, Jul 6, 2015.

  1. Sangam Singh

    Thread Starter New Member

    Jun 23, 2015
    9
    0
    I am using a PIC18F26K80 and an XC8 compiler. I am trying to initialise an SD card and create a file. I have simply formatted the SD card on Windows to have a "FAT32" file system and an "Allocation unit size" of 512 bytes. The capacity of the SD card is 2GB. I am using the MDD library from the MLA Legacy version. My main is the following:

    Code (Text):
    1. FSFILE * file;
    2. char sendBuffer[22] = "This is test string 1";
    3.  
    4. //**************************************************
    5. // main function
    6. //**************************************************
    7.  
    8. int main()
    9. {
    10.     initIO();
    11.     LATBbits.LATB0 = 0;
    12.  
    13.     // Initialise SPI and SD-card
    14.     while ( !MDD_MediaDetect() );
    15.  
    16.     // Initialize the device
    17.     while ( !FSInit() );
    18.  
    19.     // Initialize
    20. #ifdef ALLOW_WRITES
    21.  
    22.     // Create a new file
    23.     file = FSfopenpgm ( "FILE.TXT", "w" );
    24.     if ( file == NULL )
    25.         while(1);
    26.  
    27.     // Write 21 1-byte objects from sendBuffer into the file
    28.     if ( FSfwrite ( (void *) sendBuffer, 1, 21, file ) != 21 )
    29.         while(1);
    30.  
    31.     // Close the file
    32.     if ( FSfclose ( file ) )
    33.         while(1);
    34.  
    35. #endif
    36.  
    37.     LATBbits.LATB0 = 1;         //LED
    38.  
    39.     while(1) {}
    40.  
    41.     return (0);
    42. }

    The program gets stuck inside the function "FSInit()" and the error I get from the function is "CE_BAD_PARTITION", which means "The boot record is bad".

    The "initIO()" function is the following:

    Code (Text):
    1. //==============================================================================
    2. // void initIO( void );
    3. //==============================================================================
    4. // Sets the pins on the PIC to input or output and determines the speed of the
    5. // internal oscilaltor
    6. // input: none
    7. // return: none
    8. //==============================================================================
    9. void initIO()
    10. {
    11.     OSCCON = 0x75;                  // Clock speed = 32MHz (4x8Mhz)
    12.  
    13.     TRISA = 0;
    14.     TRISB = 0;
    15.     TRISC = 0;
    16.  
    17.     TRISBbits.TRISB0 = 0;           //LED
    18.  
    19.     TRISCbits.TRISC3 = 0;           // set SCL pin as output
    20.     TRISCbits.TRISC4 = 1;           // set RC4 pin as input
    21.     TRISCbits.TRISC5 = 0;
    22.     TRISAbits.TRISA5 = 0;
    23. }
    The last two bytes of sector 0 are the boot signature and they are meant to be 0x55 and 0xAA and the picture I included confirms that. However, inside the function "LoadMBR" the following check is made:

    Code (Text):
    1. if((Partition->Signature0 != FAT_GOOD_SIGN_0) || (Partition->Signature1 != FAT_GOOD_SIGN_1))
    2. {
    3.     FSerrno = CE_BAD_PARTITION;
    4.     error = CE_BAD_PARTITION;
    5. }
    6. else
    7. {
    8.     ...
    9. }
    and although the bytes are the same the first condition is met and it returns with the "CE_BAD_PARTITION" error.

    [​IMG]

    I also downloaded the SD card formatter and used it to erase all the sections that normal format doesn't. However, I am still getting the same error. I have attached a zip file with the code. Could someone please have a look at it. I can't figure out what is wrong.
     
Loading...