• AVR Freaks

SD card SPI 6012A troubleshooting

Page: 12 > Showing page 1 of 2
Author
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
2015/01/18 10:27:38 (permalink)
0

SD card SPI 6012A troubleshooting

Solved: Skip to the post  #21 where tutorial for SD card on dspic30f library editing and adjustments is given!
 
 
Hi guys, i'm trying to use sdcard on dspic30f6012A SPI1
 
The socket for sd card have arrived. looking like this: http://cdn.instructables.com/F6V/F1OJ/H5ENQ08B/F6VF1OJH5ENQ08B.MEDIUM.jpg
 
Connected:
socket CS to chip SS (via 2k2 and 3k3 to ground)
socket MOSI to chip SDO (via 2k2 and 3k3 to ground)
socket SCK to chip SCK (via 2k2 and 3k3 to ground)
socket MISO to chip SDI (direct)
Ok so far?
 
Used FSconfig.h, FSIO.h, HardwareProfile.h,SD-SPI.h, FSIO.c, SD-SPI.c from dspicwaveplayer found online.
Cant find the exact site anymore so ill upload the files on request.
Copied example read crate write delete file from AN1045 and adapted it slightly to make it work. There were some errors.(i added * to pNewFile)
Added write to LCD lines with delay to debug where the problem is.
And the problem is that chip resets on FSinit.
 
FSFILE *pOldFile, *pNewFile;
char myData[20];
char bfr [6];
int bytesRead, bytesWritten;
//char newFile[] = "newfile.txt";
//char writeArg = "w";
// Must initialize the FAT16/FAT32 library. It also initializes SPI and other related pins.
WritePageLCD("FSFILE", myData, myData,myData, 2);
if( !FSInit() )
// Failed to initialize FAT16 ? do something?
return 1; // Card not present or wrong format
WritePageLCD("FSINIT", "newfile", myData,myData, 2);
// Create a new file
pNewFile = FSfopen ("newfile.txt", "w");
WritePageLCD("FSINIT", "newfile", myData,myData, 2);
// Open an existing file to read
pOldFile = FSfopen ("test.txt", "r");
WritePageLCD("FSINIT", "newfile", "oldfile",myData, 2);
if ( pOldFile == NULL )
// Either file is not present or card is not present
return 1;

// Read 10 bytes of data from the file.
bytesRead = FSfread((void*)myData, 10, 1, pOldFile);
__delay_ms(2000);
// read bfrSize (5) items (of size 1 byte). returns items count
bytesRead = FSfread( (void *)bfr, 1, bfrSize, pOldFile );
__delay_ms(2000);
// Write those fifteen bytes to the new file
bytesWritten = FSfwrite ((void *) myData, 10, 1, pNewFile);
__delay_ms(2000);
bytesWritten = FSfwrite ((void *) bfr, 1, bfrSize, pNewFile);
__delay_ms(2000);
// After processing, close the file.
FSfclose( pOldFile );
FSfclose (pNewFile);
__delay_ms(2000);
//Delete the old file
FSremove ("test.txt");
__delay_ms(2000);

 
Any taughts why chip resets on FSInit?
I never used sc-card with pic so at the moment i dont have idea where to star looking for the problem?
HardwareProfile maybe? was i suposed to select pins somewhere?  any troubleshooting tips for sd-card on spi?
 
any help appreciated.
post edited by Miroslav Segvic - 2015/01/20 17:59:00
#1
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 11:08:47 (permalink)
0
Ok i found section with pins for SCK SDI SDO and CS, and i will change that  but what are CD and WE?
        #define SD_CS PORTFbits.RF1
        #define SD_CS_TRIS TRISFbits.TRISF1
        
        #define SD_CD 0
        #define SD_CD_TRIS TRISCbits.TRISC15
        
        #define SD_WE 0
        #define SD_WE_TRIS TRISCbits.TRISC15
        
        // Registers for the SPI module you want to use
        #define SPICON1 SPI1CON
        #define SPISTAT SPI1STAT
        #define SPIBUF SPI1BUF
        #define SPISTAT_RBF SPI1STATbits.SPIRBF
        #define SPICON1bits SPI1CONbits
        #define SPISTATbits SPI1STATbits
        // Tris pins for SCK/SDI/SDO lines
        #define SPICLOCK TRISFbits.TRISF6
        #define SPIIN TRISFbits.TRISF2
        #define SPIOUT TRISFbits.TRISF3

#2
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 11:11:23 (permalink)
3 (1)
there changed CS to SS1 pin on 6012A
 
#define SD_CS PORTBbits.RB2
#define SD_CS_TRIS TRISBbits.TRISB2
 
Still resting on FSopen
post edited by Miroslav Segvic - 2015/01/18 11:14:03
#3
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 11:49:04 (permalink)
5 (1)
Write Enable and Card Detect :)
you can skip them...

GENOVA :D :D ! GODO
#4
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 12:08:39 (permalink)
0
Thanks, still no progress on FSopen resting the chip.
I didn't set the ports as input or output. My guess is that library does that automatically when pins are assigned? is it needed for SPI?
#5
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 13:44:35 (permalink)
3 (1)
Ok, simple question:
 
Do i need to setup ports or SPI itself?
Anyone used sdcard on SPI on dsPIC30f series? does anyone have good reference link to working example?
I'm trying to read from txt file on sd card. nothing more.
 
#6
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 18:56:15 (permalink)
0
Ok, i found example on this web site: http://kibacorp.com/kit-applications/implement-file-io
 
It is for pic24 and pic32. I adapted it slightly to compile with dspic30f. If someone experienced with porting wants the files to maybe finish this process, i am willing to email.
 
I no longer have the problem of chip reseting on FSinit, but the FSinit returns error.
Seems that it is error no 6. From FSDefs, i can see that 6th error description is FS initialization failed.
 
Any ideas? someone willing to help with this library port?
#7
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 19:00:50 (permalink)
0
Also this is a sandisk 2 gb card and when i try to format it with FAT it offers minimum allocation unit size of 16Kbytes.
With FAT32 it offers 512bytes.
 
Shouldn't i be able to format with FAT16 and lower alocation size like 2 Kbytes?
 
#8
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 19:39:59 (permalink)
4 (1)
Surely that IS FAT16.
FAT16 only allows 65536 clusters, so 16kB clusters with FAT16 only gives you 1GB total storage anyway.
You need 32kB clusters to get 2GB.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 20:17:22 (permalink)
0
Sorry my mistake, yes it is 1 gb card. So to work 512byte sectors with FAT16 i should get 32 mb card?
 
I'm asking this because i can't use 16kbytes of ram for sector allocation. And i don't know will these libraries work with FAT32.
 
Thanks
#10
ric
Super Member
  • Total Posts : 24593
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/18 20:32:34 (permalink)
3 (1)
I haven't used the FSIO package, but in general "sector size" and "cluster size" do NOT have to be equal.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 03:48:48 (permalink)
0
And yes the PIN direction has to be set, and usually also any analog feature at pins has to be removed (for some PICs the library will take care of that, but...)
 
That could've been a good reason for resetting, along with a Watchdog maybe.

GENOVA :D :D ! GODO
#12
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 07:02:51 (permalink)
0
well the pins have been specified in hardwareprofile.h
 
 
  // Description: SD-SPI Chip Select Output bit
        #define SD_CS PORTBbits.RB2
        // Description: SD-SPI Chip Select TRIS bit
        #define SD_CS_TRIS TRISBbits.TRISB2
        
        // Description: SD-SPI Card Detect Input bit
        #define SD_CD PORTFbits.RF0
        // Description: SD-SPI Card Detect TRIS bit
        #define SD_CD_TRIS TRISFbits.TRISF0
        
        // Description: SD-SPI Write Protect Check Input bit
        #define SD_WE PORTFbits.RF1
        // Description: SD-SPI Write Protect Check TRIS bit
        #define SD_WE_TRIS TRISFbits.TRISF1
        
        // Registers for the SPI module you want to use

        // Description: The main SPI control register
        #define SPICON1 SPI1CON1
        // Description: The SPI status register
        #define SPISTAT SPI1STAT
        // Description: The SPI Buffer
        #define SPIBUF SPI1BUF
        // Description: The receive buffer full bit in the SPI status register
        #define SPISTAT_RBF SPI1STATbits.SPIRBF
        // Description: The bitwise define for the SPI control register (i.e. _____bits)
        #define SPICON1bits SPI1CON1bits
        // Description: The bitwise define for the SPI status register (i.e. _____bits)
        #define SPISTATbits SPI1STATbits
        // Description: The enable bit for the SPI module
        #define SPIENABLE SPISTATbits.SPIEN

        // Tris pins for SCK/SDI/SDO lines

        // Description: The TRIS bit for the SCK pin
        #define SPICLOCK TRISFbits.TRISF6
        // Description: The TRIS bit for the SDI pin
        #define SPIIN TRISFbits.TRISF2
        // Description: The TRIS bit for the SDO pin
        #define SPIOUT TRISFbits.TRISF3

 
So i guess that takes care of that. 
 
What about clock. i had to specify clock also, but i'm running at 120Mhz and i'm not sure card can be used with this frequency?
#elif defined (__C30__)

    #define GetSystemClock() 120000000
    #define GetPeripheralClock() GetSystemClock()
    #define GetInstructionClock() (GetSystemClock() / 2)

    // Clock values
    #define MILLISECONDS_PER_TICK 10 // Definition for use with a tick timer
    #define TIMER_PRESCALER TIMER_PRESCALER_8 // Definition for use with a tick timer
    #define TIMER_PERIOD 20000 // Definition for use with a tick timer

 
#13
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 07:37:17 (permalink)
3 (1)
Just update, for those who didn't get it. The chip isn't resenting any more since i'm using the library from another example project. I edited the library to support dspic. Unfortunately the FSinit function returns error no 6. failed to initialize. 
FSinit function calls this function and somewhere in there is the reason why it fails to initialize.
 
 
BYTE MDD_SDSPI_MediaInitialize(void)
{
    WORD timeout;
    BYTE status = TRUE;
    MMC_RESPONSE response;
#if defined __C30__ || defined __C32__
    WORD spiconvalue = 0x0003;
#endif
    
    SD_CS = 1; //Initialize Chip Select line
    
    //Media powers up in the open-drain mode and cannot handle a clock faster
    //than 400kHz. Initialize SPI port to slower than 400kHz
#if defined __C30__ || defined __C32__
#ifdef __PIC32MX__
    SPIBRG = SPICalutateBRG(GetPeripheralClock(), 400000);
    SPICON1bits.MSTEN = 1;
    OpenSPIM (MASTER_ENABLE_ON);
    
#else
    // Calculate the prescaler needed for the clock
    timeout = GetSystemClock() / 400000;
    // if timeout is less than 400k and greater than 100k use a 1:1 prescaler
    if (timeout == 0)
    {
        OpenSPIM (MASTER_ENABLE_ON | PRI_PRESCAL_1_1 | SEC_PRESCAL_1_1);
    }
    while (timeout != 0)
    {
        if (timeout > 8)
        {
            spiconvalue--;
            // round up
            if ((timeout % 4) != 0)
                timeout += 4;
            timeout /= 4;
        }
        else
        {
            timeout = 0;
        }
    }
    
    OpenSPIM (MASTER_ENABLE_ON | spiconvalue | ((~(timeout << 2)) & 0x1C));
#endif


    // let the card power on and initialize
    Delayms(1);
    
    //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
    for(timeout=0; timeout<10; timeout++)
        mSend8ClkCycles();

    SD_CS = 0;
    
    Delayms(1);
    
    // Send CMD0 to reset the media
    response = SendMMCCmd(GO_IDLE_STATE,0x0);
    
    if((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
    {
        status = FALSE; // we have not got anything back from the card
        SD_CS = 1; // deselect the devices

        return status;
    }

    // According to spec cmd1 must be repeated until the card is fully initialized
    timeout = 0xFFF;
    
    do
    {
        response = SendMMCCmd(SEND_OP_COND,0x0);
        timeout--;
    }while(response.r1._byte != 0x00 && timeout != 0);

    // see if it failed
    if(timeout == 0)
    {
        status = FALSE; // we have not got anything back from the card
        
        SD_CS = 1; // deselect the devices
    }
    else
    {

#else
    
    // let the card power on and initialize
    Delayms(1);
    
    #if (GetSystemClock() < 25600000)

        #if (GetSystemClock() < 1600000)
            OpenSPIM (SYNC_MODE_FAST, BUS_MODE, SMP_PHASE);
        #elif (GetSystemClock() < 6400000)
            OpenSPIM (SYNC_MODE_MED, BUS_MODE, SMP_PHASE);
        #else
            OpenSPIM (SYNC_MODE_SLOW, BUS_MODE, SMP_PHASE);
        #endif
        
        // let the card power on and initialize
        Delayms(1);
        
        //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
        for(timeout=0; timeout<10; timeout++)
            mSend8ClkCycles();
    
        SD_CS = 0;
        
        Delayms(1);
        
        // Send CMD0 to reset the media
        response = SendMMCCmd(GO_IDLE_STATE,0x0);
        
        if((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
        {
            status = FALSE; // we have not got anything back from the card
            SD_CS = 1; // deselect the devices

            return status;
        }
    
        // According to spec cmd1 must be repeated until the card is fully initialized
        timeout = 0xFFF;
        
        do
        {
            response = SendMMCCmd(SEND_OP_COND,0x0);
            timeout--;
        }while(response.r1._byte != 0x00 && timeout != 0);

    #else

        // Make sure the SPI module doesn't control the bus
        SPICON1 = 0x00;

        //Media requires 80 clock cycles to startup [8 clocks/BYTE * 10 us]
        for(timeout=0; timeout<10; timeout++)
            WriteSPIManual(0xFF);
    
        SD_CS = 0;
        
        Delayms(1);
    
        // Send CMD0 to reset the media
        response = SendMMCCmdManual (GO_IDLE_STATE, 0x0);

        if ((response.r1._byte == MMC_BAD_RESPONSE) || ((response.r1._byte & 0xF7) != 0x01))
        {
            status = FALSE; // we have not got anything back from the card
            SD_CS = 1; // deselect the devices

            return status;
        }

        // According to the spec cmd1 must be repeated until the card is fully initialized
        timeout = 0xFFF;
    
        do
        {
            response = SendMMCCmdManual (SEND_OP_COND, 0x0);
            timeout--;
        }while(response.r1._byte != 0x00 && timeout != 0);
    #endif

    // see if it failed
    if (timeout == 0)
    {
        status = FALSE; // we have not got anything back from the card

        SD_CS = 1; // deselect the devices

    }
    else
    {
#endif

        Delayms (2);

        #ifdef __PIC32MX__
            #if (GetSystemClock() <= 20000000)
                SPIBRG = SPICalutateBRG(GetPeripheralClock(), 10000);
            #else
                SPIBRG = SPICalutateBRG(GetPeripheralClock(), 20000000); // SPI Speed is 20MHz
            #endif
            SPICON1 = 0x0000C060;
            SPICON1bits.MSTEN = 1;
        #else
            OpenSPIM(SYNC_MODE_FAST);
        #endif

        // Turn off CRC7 if we can, might be an invalid cmd on some cards (CMD59)
        response = SendMMCCmd(CRC_ON_OFF,0x0);

        // Now set the block length to media sector size. It should be already
        response = SendMMCCmd(SET_BLOCKLEN,MEDIA_SECTOR_SIZE);
        
        for(timeout = 0xFF; timeout > 0 && MDD_SDSPI_SectorRead(0x0,NULL) != TRUE; timeout--)
        {;}

        // see if we had an issue
        if(timeout == 0)
        {
            status = FALSE;
            SD_CS = 1; // deselect the devices

        }
    }

    return(status);
}//end MediaInitialize

 
As i can see it uses data from hardware profile which i posted above, to determine the prescaler for SPI and i'm using 120Mhz clock. It also uses these timings, to wait for chip to initialize, that i copy pasted and i'm not sure they are right for my chip and setup?
 
    // Clock values
    #define MILLISECONDS_PER_TICK 10 // Definition for use with a tick timer
    #define TIMER_PRESCALER TIMER_PRESCALER_8 // Definition for use with a tick timer
    #define TIMER_PERIOD 20000 // Definition for use with a tick timer

 
I just can't believe that nobody on this forum used SD-card with PIC or dsPIC.
#14
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 07:39:21 (permalink)
0
I'm not sure which timer does it use, since i'm using timer1 already for something else, could this be an issue.
 
#15
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 07:44:59 (permalink)
0
I'd say that the Code does take care of the Clock etc, but NOT of analog features and the rest. You may try setting ANSEL or equivalent register anyway.
 
As for the clock, anyway, you could try lowering the system clock, just to check.

GENOVA :D :D ! GODO
#16
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 08:36:50 (permalink)
3 (1)
Tried lowering clock to 15Mhz. Didn't help.
Tried changing the timer values to get 100Hz, and changed hardware profile.
 
    // Clock values
    #define MILLISECONDS_PER_TICK 10 // Definition for use with a tick timer
    #define TIMER_PRESCALER TIMER_PRESCALER_256 // Definition for use with a tick timer
    #define TIMER_PERIOD 1152 // Definition for use with a tick timer

 
All the pins are selected as digital already.
Maybe direction. I'll try later. have to go now.
 
 
#17
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/19 17:07:54 (permalink)
0
Hi i'm trying to adapt the example from latest MLA. Must say source files are scattered around the MLA nicely.
 
Anyway i had to disable the SPI Enhanced Buffer to make it compile. It was enabled by default but it is trying to set bits that don't exist in the registers:
 
 
#ifndef DRV_SPI_CONFIG_ENHANCED_BUFFER_DISABLE
        DRV_SPI_CON2bits(1).SPIBEN = 1;
#endif

...

#ifndef DRV_SPI_CONFIG_ENHANCED_BUFFER_DISABLE
    #ifdef DRV_SPI_CONFIG_CHANNEL_1_ENABLE
    static inline __attribute__((__always_inline__)) void DRV_SPI_WaitForDataByte1 (void)
    {
        while (DRV_SPI_STATbits(1).SRXMPT);
    }
    #endif
...

 
Question is, do i need SPI enhanced buffer. I get it is enhanced but is it necessary for sd card interfacing? and what are the concequences of disabling it?
 
Thanks
 
#18
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/20 05:35:52 (permalink)
0
Ok, dumped the two lines that wouldn't compile and enabled enhanced buffer. I can't mount the drive. I get error 6 again. Failed to initialise.
 
Still the same problem form beginning.
#19
Miroslav Segvic
Senior Member
  • Total Posts : 167
  • Reward points : 0
  • Joined: 2013/05/27 06:53:43
  • Location: Zagreb, Croatia
  • Status: offline
Re: SD card SPI 6012A troubleshooting 2015/01/20 07:15:06 (permalink)
0
Update: Found a lot of resources on web. It seems to be a hardware problem with the module i use. These SD card modules have lines connected to 3.3V via 10k resistor, and sending 3.3V shifted signal from PIC does nothing on the lines. Will try the method with reversed diodes between the chip and SD module and report back.
 
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2019 APG vNext Commercial Version 4.5