• AVR Freaks

MDD file system struggles - trying to save simple text file to SD card

Author
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
2014/07/13 14:44:46 (permalink)
0

MDD file system struggles - trying to save simple text file to SD card

I am trying to save a simple text file to an SD card using pic18f4620 on a breadboard with C18 compiler and pickit2 with the MDD file system. I've edited hardwareprofile.h, fsconfig.h, and the linker file as appropriate to make them compatible with the 18f4620. My project builds and compiles. When it runs, it hangs up on FSInit(). I've spent hours trouble shooting this, including reviewing the forums and tracking down the line where it catches and have not been able to fix it. The code stops at the line   ----while (!SPI_INTERRUPT_FLAG)---- within the function MDD_SDSPI_ReadMedia(void). This function is in SD-SPI.c. The SPI_INTERRUPT_FLAG is defined in hardwareprofile.h as PIR1bits.SSPIF. Here is more context for the line where the code stops:
 
BYTE MDD_SDSPI_ReadMedia(void)
{
BYTE clear;
clear = SPIBUF;
SPI_INTERRUPT_FLAG = 0;
SPIBUF = 0xFF;
while (!SPI_INTERRUPT_FLAG);

return SPIBUF;
}
 
 
I've double and triple checked my hardware. I'm using a 3.3V regulator for the card, have decoupling caps in place, pins are all correct. Can anyone provide insight about why the code hangs up here? I really appreciate it, thanks...
 
#1
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/14 12:39:21 (permalink)
0
Strange: is the SPI setup correctly? Can you check the lines with a Scope?

GENOVA :D :D ! GODO
#2
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11423
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/14 12:54:19 (permalink)
+1 (1)
It sounds like SPI is not enabled.
#3
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/18 19:07:03 (permalink)
0
Thanks for the responses - I tracked the code to the point where it hangs up and SPI is enabled prior to that line. I do not have a scope to check all the pins. Considering picking up a cheap SD card breakout board to rule out hardware problems. Right now I just have an SD card adapter with soldered wires to the breadboard and I swap in a microSD to the adapter. No dice so far...
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11423
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/18 19:25:20 (permalink)
0
The MSSP doesn't know or care about anything external to the PIC if it's in SPI master mode.  Your SPI transaction should complete successfully even if nothing is connected to the SPI pins.  It still sounds like you don't have SPI master mode enabled.
#5
ric
Super Member
  • Total Posts : 24581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/18 21:46:09 (permalink)
0
+1 for the MSSP not being in the right mode.
What was the last value you wrote to SSPCON1 ?
If SSPM = 0011, is TMR2 running?

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!
#6
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/19 17:16:14 (permalink)
0
Thanks a lot for the help - I think I'm making progress understanding what's going on. Stepping through the MDD code right before it stops, the following bits are cleared/set:
 
-SSPSTAT = 0x0000;
-SSPCON1 = 0x0000;
-SSPCON1 | = sync_mode; (I'm running at 16Mhz, so the sync_mode is assigned by MDD to sync_mode_slow which is defined as 0x02. Therefore, at this point SSPCON1 = 00000010).
-SSPCON1 |= 0x80;
-SSPSTATbits.CKE = 1;
-SSPCON1bits.SSPEN = 1;
Therefore, at the end of this function (OpenSPIM()), the following are set:
 
SSPSTAT = 01000000;
SSPCON1 = 10100010;
 
Then,
-interrupt flag (PIR1bits.SSPIF) is cleared to 0
-SSPBUF = 0xFF
 
This is the point where the code stops, specifically at the line:
while (!SPI_INTERRUPT_FLAG);
 
So it looks like my SSPM is 0010. Should it be 0011? I haven't changed this code that came with the MDD system. Am I missing a configuration of the oscillator and/or TMR2 earlier in the code?
 
Thanks for your time
#7
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11423
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/19 17:40:54 (permalink)
0
Any of 00, 01, or 10 should work for SSPM without any further setup.
 
Why are you setting WCOL with SSPCON1 |= 0x80?  I don't know what that will do, but it might flag the MSSP to stop transmitting.
 
Edit:  or are you saying WCOL is getting set on its on its own?  That means a write to SSPBUF was attempted before the previous transfer finished.
post edited by jtemples - 2014/07/19 17:48:29
#8
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/19 18:46:27 (permalink)
0
Thanks jtemples - So as a disclaimer, this code is the MDD file system that I'm just trying to work through, I haven't written it myself, just trying to understand/troubleshoot it...
 
Stepping through the MDD code (omitting a lot of code dealing with initializing RAM, etc.), the FSInit() function calls OpenSPIM(): 
 
****
void OpenSPIM (unsigned char sync_mode)
{
SPISTAT = 0x0000; // power on state
SPICON1 = 0x0000; // power on state
SPICON1 |= sync_mode; // select serial mode 
SPICON1 |= 0x80;
SPISTATbits.CKE = 1;


SPICLOCK = 0;
SPIOUT = 0; // define SDO1 as output (master or slave)
SPIIN = 1; // define SDI1 as input (master or slave)
SPIENABLE = 1; // enable synchronous serial port
}
****
 
...And then WriteSPIM(0xFF) is called:
 
****
BYTE clear;
clear = SPIBUF;
SPI_INTERRUPT_FLAG = 0;
SPIBUF = data_out;
if (SPICON1 & 0x80)
return -1;
else
while (!SPI_INTERRUPT_FLAG);
return 0;
****
 
 
...And then SendMMCCmd() is called:
 
****
 MMC_RESPONSE SendMMCCmd(BYTE cmd, DWORD address)
{
WORD timeout = 0x8;
BYTE index;
MMC_RESPONSE response;
CMD_PACKET CmdPacket;

SD_CS = 0; //Card Select
// Copy over data
CmdPacket.cmd = sdmmc_cmdtable[cmd].CmdCode;
CmdPacket.address = address;
CmdPacket.crc = sdmmc_cmdtable[cmd].CRC; // Calc CRC here

CmdPacket.TRANSMIT_BIT = 1; //Set Tranmission bit

WriteSPIM(CmdPacket.cmd); //Send Command
WriteSPIM(CmdPacket.addr3); //Most Significant Byte
WriteSPIM(CmdPacket.addr2);
WriteSPIM(CmdPacket.addr1);
WriteSPIM(CmdPacket.addr0); //Least Significant Byte
WriteSPIM(CmdPacket.crc); //Send CRC
 
....
****
 
And then MDD_SDSPI_ReadMedia() is called:
 
****
BYTE clear;
clear = SPIBUF;
SPI_INTERRUPT_FLAG = 0;
SPIBUF = 0xFF;
while (!SPI_INTERRUPT_FLAG);
return SPIBUF;
****
 
 
and just before the return line, at <while (!SPI_INTERRUPT_FLAG);> the FSInit() function stalls...
 
#9
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11423
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/19 19:02:45 (permalink)
0
First get rid of SSPCON1 |= 0x80 and see what effect that has.
 
And the values you posted above are from inspecting SSPCON1 in the debugger?
#10
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/20 16:13:38 (permalink)
0
Commented out that line and no change in the results.
 
The values I posted above are from following the code from the point where FSInit() begins through various breakpoints in the debugger until the point where it stops. 
 
 
#11
ric
Super Member
  • Total Posts : 24581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/20 16:16:32 (permalink)
0
If you are using a debugger, then you can simply view what is in that register when it has stopped.

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!
#12
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/21 15:10:03 (permalink)
0
At the point it stops, the debugger shows:
 
SSPSTAT = 0x40 (01000000)
PIR1        = 0x00 
SSPCON1 = 0xA2 (10100010)
SSPBUF   = 0xFF 
#13
ric
Super Member
  • Total Posts : 24581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/21 15:38:51 (permalink)
+1 (1)
The WCOL bit in your SSPCON1 register is set, indicating something has tried to write to SSPBUF before the previous value finished sending.
Your WriteSPIM() function stops waiting for the byte to send if it sees that flag set.
 

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!
#14
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/21 17:20:16 (permalink)
0
getting rid of the line <SSPCON1 |= 0x80> (thus not setting the WCOL bit) causes the card to stop initializing. That line is written in the code only to be included on PIC18 devices. In OpenSPIM():
 
***
#ifdef __18CXX
SPICON1 |= 0x80;
SPISTATbits.CKE = 1;
....
***
 
Even clearing the WCOL bit prior to the lines:
 
***
SPIBUF = 0xFF;
while (!SPI_INTERRUPT_FLAG);
***
 
still results in the code hanging up. Frustrating...
 
#15
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11423
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/21 18:06:16 (permalink)
0 (1)
Your PIC's family has several errata associated with WCOL.  Check if your specific silicon is affected.
#16
gjb2
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2014/07/13 14:21:09
  • Location: 0
  • Status: offline
Re: MDD file system struggles - trying to save simple text file to SD card 2014/07/21 19:35:16 (permalink)
0
Thanks a lot for all the insight - I'll have to play around with this for a bit. I just pulled up the errata and you're right, there's a bunch of problems with the MSSP including WCOL. I'll let you know if I make any progress.
#17
Jump to:
© 2019 APG vNext Commercial Version 4.5