Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SDHC): The patch repairs getSize() in main.c of SDHC_FAT to properly calculate SD Card disk capacity #3

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 29 additions & 21 deletions Examples/MAX32665/SDHC_FAT/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ int formatSDHC()

printf("FORMATTING DRIVE\n");

if ((err = f_mkfs("", FM_ANY, 0, work, sizeof(work))) !=
MKFS_PARM format_options = { .fmt = FM_ANY };

if ((err = f_mkfs("", &format_options, work, sizeof(work))) !=
FR_OK) { //Format the default drive to FAT32
printf("Error formatting SD card: %s\n", FF_ERRORS[err]);
} else {
Expand All @@ -147,7 +149,7 @@ int formatSDHC()

mount();

if ((err = f_setlabel("MAXIM")) != FR_OK) {
if ((err = f_setlabel("Analog")) != FR_OK) {
printf("Error setting drive label: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
}
Expand All @@ -159,6 +161,9 @@ int formatSDHC()

int getSize()
{
QWORD disksize = 0;
QWORD available_bytes = 0;

if (!mounted) {
mount();
}
Expand All @@ -170,9 +175,12 @@ int getSize()

sectors_total = (fs->n_fatent - 2) * fs->csize;
sectors_free = clusters_free * fs->csize;
disksize = (QWORD)(sectors_total / 2) *
(QWORD)(1024); // for cards over 3GB, we need QWORD to hold size
available_bytes = (QWORD)(sectors_free / 2) * (QWORD)(1024);

printf("Disk Size: %u bytes\n", sectors_total / 2);
printf("Available: %u bytes\n", sectors_free / 2);
printf("Disk Size: %llu bytes\n", disksize);
printf("Available: %llu bytes\n", available_bytes);

return err;
}
Expand Down Expand Up @@ -335,6 +343,7 @@ int cd()
return err;
}

// save directory
printf("Changed to %s\n", directory);
f_getcwd(cwd, sizeof(cwd));

Expand Down Expand Up @@ -367,6 +376,10 @@ int example()
{
unsigned int length = 256;

if (!mounted) {
mount();
}

if ((err = formatSDHC()) != FR_OK) {
printf("Error Formatting SD Card: %s\n", FF_ERRORS[err]);
return err;
Expand All @@ -379,7 +392,7 @@ int example()
}
printf("SD Card Opened!\n");

if ((err = f_setlabel("MAXIM")) != FR_OK) {
if ((err = f_setlabel("Analog")) != FR_OK) {
printf("Error setting drive label: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
return err;
Expand Down Expand Up @@ -426,32 +439,32 @@ int example()
return err;
}

err = f_stat("MaximSDHC", &fno);
err = f_stat("AnalogSDHC", &fno);
if (err == FR_NO_FILE) {
printf("Creating Directory...\n");
if ((err = f_mkdir("MaximSDHC")) != FR_OK) {
if ((err = f_mkdir("AnalogSDHC")) != FR_OK) {
printf("Error creating directory: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
return err;
}
}

printf("Renaming File...\n");
if ((err = f_rename("0:HelloWorld.txt", "0:MaximSDHC/HelloMaxim.txt")) !=
if ((err = f_rename("0:HelloWorld.txt", "0:AnalogSDHC/HelloAnalog.txt")) !=
FR_OK) { //cr: clearify 0:file notation
printf("Error moving file: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
return err;
}

if ((err = f_chdir("/MaximSDHC")) != FR_OK) {
if ((err = f_chdir("/AnalogSDHC")) != FR_OK) {
printf("Error in chdir: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
return err;
}

printf("Attempting to read back file...\n");
if ((err = f_open(&file, "HelloMaxim.txt", FA_READ)) != FR_OK) {
if ((err = f_open(&file, "HelloAnalog.txt", FA_READ)) != FR_OK) {
printf("Error opening file: %s\n", FF_ERRORS[err]);
f_mount(NULL, "", 0);
return err;
Expand Down Expand Up @@ -480,14 +493,16 @@ int example()
if ((err = f_mount(NULL, "", 0)) != FR_OK) {
printf("Error unmounting volume: %s\n", FF_ERRORS[err]);
return err;
} else {
mounted = 0;
}

return 0;
}

/******************************************************************************/
int main(void)
{
MXC_Delay(MXC_DELAY_SEC(2));
mxc_sdhc_cfg_t cfg;

FF_ERRORS[0] = "FR_OK";
Expand All @@ -511,7 +526,7 @@ int main(void)
FF_ERRORS[18] = "FR_TOO_MANY_OPEN_FILES";
FF_ERRORS[19] = "FR_INVALID_PARAMETER";
srand(12347439);
int run = 1, input = -1;
int run = 1, input = 0;

printf("\n\n***** " TOSTRING(TARGET) " SDHC FAT Filesystem Example *****\n");

Expand Down Expand Up @@ -555,15 +570,6 @@ int main(void)
printf("Card type: MMC/eMMC\n");
}

/* Configure for fastest possible clock, must not exceed 52 MHz for eMMC */
if (SystemCoreClock > 96000000) {
printf("SD clock ratio (at card) 4:1\n");
MXC_SDHC_Set_Clock_Config(1);
} else {
printf("SD clock ratio (at card) 2:1\n");
MXC_SDHC_Set_Clock_Config(0);
}

while (run) {
f_getcwd(cwd, sizeof(cwd));

Expand Down Expand Up @@ -627,11 +633,13 @@ int main(void)
err = -1;
break;
}
/*
if (err >= 0 && err <= 20) {
printf("Function Returned with code: %d\n", FF_ERRORS[err]);
} else {
printf("Function Returned with code: %d\n", err);
}
*/
MXC_TMR_Delay(MXC_TMR0, MXC_DELAY_MSEC(500));
}
printf("End of example, please try to read the card.\n");
Expand Down