 |
 |
| View previous topic :: View next topic |
| Author |
Message |
mrtele
Joined: 26 Jun 2026 Posts: 2
|
| Help with SD Card and PIC24 |
Posted: Fri Jun 26, 2026 7:47 am |
|
|
Hello everyone,
I've been trying without much success to communicate with an sd card on a new project. I'm using a PIC24FJ512GB606, connected to the SD Card with the standard pinout for SPI communication.
Looking through the forum, I saw the pinned post "mmcsd.c and fat.c modified and fully functional", though unfortunately I was not able to use it as of yet, as my code is still using the setup_spi() and spi_read/write functions rather than the #use spi and spi_xfer option.
I've had some partial success with the driver from this page (https://simple-circuit.com/ccs-c-fat-library-for-mmc-sd-cards/), after modifying it, mostly to make sure all the variables are explicitly unsigned. I've made a few more changes to the init routine, according to the init sequences I've found online:
| Code: | MMCSD_err mmcsd_init()
{
uint8_t i, r1, r3[4];
g_CRC_enabled = TRUE;
g_mmcsdBufferAddress = 0;
output_drive(MMCSD_PIN_SELECT);
to_spi_m0v();
mmcsd_deselect();
delay_ms(250);
for(i = 0; i < 10; i++) // Send 80 cycles
MMCSD_SPI_XFER(0xFF);
/* begin initialization */
i = 0;
do
{
delay_ms(1);
mmcsd_select();
r1 = mmcsd_go_idle_state();
mmcsd_deselect();
i++;
if(i == 0xFF) {
if (r1 == 0)
return 1;
else
return r1;
}
} while(r1 != MMCSD_IDLE);
i = 0;
do
{
delay_ms(1);
mmcsd_select();
//! r1 = mmcsd_send_op_cond();
r1 = mmcsd_send_if_cond(r3);
mmcsd_deselect();
i++;
} while((r1 & MMCSD_IDLE) && i != 0xFF);
/* an mmc will return an 0x04 here */
if(r1 == 0x04)
g_card_type = MMC;
else {
g_card_type = SDSC;
i = 0;
do {
r1=mmcsd_app_cmd();
r1=mmcsd_sd_send_op_cond();
} while (r1 == 0x01 && i != 0xFF);
if (r1 == 0x01) {
return r1;
}
mmcsd_select();
r1 = mmcsd_read_ocr(r3);
mmcsd_deselect();
if(r1 != MMCSD_ILLEGAL_CMD) {
r1 = r3[3];
if(bit_test(r1, 6)) // If bit 30 of the OCR register is 1 (CCS is 1) ==> SDHC type
g_card_type = SDHC;
}
}
/* set block length to 512 bytes */
mmcsd_select();
r1 = mmcsd_set_blocklen(MMCSD_MAX_BLOCK_SIZE);
mmcsd_deselect();
if(r1 != MMCSD_GOODEC)
return r1;
/// this would be a good time to set a higher clock speed, 20MHz
to_spi_m0f();
// Turn OFF CRC check, some card return 0 (MMCSD_GOODEC) and some others return 0x04 (MMCSD_ILLEGAL_CMD)
mmcsd_select();
r1 = mmcsd_crc_on_off(FALSE);
mmcsd_deselect();
r1 = mmcsd_load_buffer();
return MMCSD_GOODEC;
} |
Through this section I get the expected responses, and the card is recognised as SDHC (it's a 4GB SD Card, which I tested on a Windows PC and seems to work fine; it was formatted to FAT32 using the "official" SD Card Formatter utility).
However, right after the call to mmcsd_init in fat_init,, there's a sequence of mmcsd_read_data calls that appear to work but read no data (all the variables pointed to remain at zero). The final one in the sequence (ec += mmcsd_read_data(offset_ + 0x3EC, 4, &Next_Free_Clust);) returns a 5, ending fat_init prematurely. As far as I can tell, this 5 originates as a response from a READ_SINGLE_BLOCK command deep within the read_data call.
Does anyone have any insight into the possible cause? It's my first time interfacing with an SD Card, so I might be missing some obvious stuff. |
|
 |
asmallri
Joined: 12 Aug 2004 Posts: 1670 Location: Perth, Australia
|
|
Posted: Mon Jun 29, 2026 8:22 pm |
|
|
If this is your own hardware then the first step is to validate the hardware configuration. The most obvious are, do you have the correct pull-up resistors - (e.g. 10K pull-up on the cards SDO output and the CS line)?
Do you have a low ESR capacitor (Ceramic or tantalum capacitor around 10uF) as close as practical to the power pins of the SD card socket? _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
 |
mrtele
Joined: 26 Jun 2026 Posts: 2
|
|
Posted: Tue Jun 30, 2026 7:54 am |
|
|
I don't think we have one actually, we'll be sure to add it for better stability.
In any case, I ended up testing the drivers from the pinned post. I had to make a number of modifications, but it seems to be working now (making directories, reading and writing some simple txt files). I'll post the files once I finish with some more tests. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|