Skip to content

Commit

Permalink
fix(SDHC)!: Fix SDHC Formats Card Size Incorrectly, Update FatFS to R…
Browse files Browse the repository at this point in the history
…0.15, Add SDXC and exFAT Support (#720)

Co-authored-by: Brian Hindman <[email protected]>
Co-authored-by: Jake-Carter <[email protected]>
  • Loading branch information
3 people authored and EricB-ADI committed Oct 26, 2023
1 parent cf16465 commit 081e8f2
Show file tree
Hide file tree
Showing 125 changed files with 31,824 additions and 95 deletions.
13 changes: 3 additions & 10 deletions Examples/MAX32572/SDHC_FAT/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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 Down Expand Up @@ -565,15 +567,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
13 changes: 3 additions & 10 deletions Examples/MAX32650/SDHC_FAT/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,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 Down Expand Up @@ -542,15 +544,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
2 changes: 2 additions & 0 deletions Examples/MAX32650/SDHC_FAT/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ SBT=0

# Enable the SDHC library
LIB_SDHC=1
# Use FatFS R0.15
FATFS_VERSION = ff15
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
2 changes: 2 additions & 0 deletions Examples/MAX32665/SDHC_FAT/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ SBT=0

# Enable SDHC library
LIB_SDHC = 1
# Use FatFS version R0.15
FATFS_VERSION = ff15
1 change: 1 addition & 0 deletions Examples/MAX78000/CNN/faceid_evkit/db_single/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/
4 changes: 3 additions & 1 deletion Examples/MAX78000/CNN/facial_recognition/SDHC_weights/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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ ifneq ($(BOARD),FTHR_RevA)
$(error ERR_NOTSUPPORTED: This project is only supported on the MAX78000FTHR. (see https://analog-devices-msdk.github.io/msdk/USERGUIDE/#board-support-packages))
endif

# Enable SDHC library
LIB_SDHC = 1
# Use FatFS R0.15
FATFS_VERSION = ff15

3 changes: 2 additions & 1 deletion Examples/MAX78000/CNN/facial_recognition/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ endif

# Enable the SDHC library
LIB_SDHC = 1

# Use FatFS R0.15
FATFS_VERSION = ff15

4 changes: 3 additions & 1 deletion Examples/MAX78000/CNN/facial_recognition/src/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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 Down
4 changes: 3 additions & 1 deletion Examples/MAX78000/CNN/kws20_demo/sd_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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 Down
4 changes: 2 additions & 2 deletions Examples/MAX78000/SDHC_FTHR/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"${config:MAXIM_PATH}/Libraries/MiscDrivers/PushButton",
"${config:MAXIM_PATH}/Libraries/MiscDrivers/Touchscreen",
"${config:MAXIM_PATH}/Libraries/SDHC/Include",
"${config:MAXIM_PATH}/Libraries/SDHC/ff13/Source",
"${config:MAXIM_PATH}/Libraries/SDHC/ff15/source",
"${config:MAXIM_PATH}/Libraries/CLI/inc"
],
"C_Cpp.default.browse.path": [
Expand All @@ -69,7 +69,7 @@
"${config:MAXIM_PATH}/Libraries/MiscDrivers/PushButton",
"${config:MAXIM_PATH}/Libraries/MiscDrivers/Touchscreen",
"${config:MAXIM_PATH}/Libraries/MiscDrivers",
"${config:MAXIM_PATH}/Libraries/SDHC/ff13/Source",
"${config:MAXIM_PATH}/Libraries/SDHC/ff15/source",
"${config:MAXIM_PATH}/Libraries/CLI/src"
],
"C_Cpp.default.defines": [
Expand Down
2 changes: 2 additions & 0 deletions Examples/MAX78000/SDHC_FTHR/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ endif

# Enable SDHC library
LIB_SDHC = 1
# Use FatFS version R0.15
FATFS_VERSION = ff15

# Enable CLI library
LIB_CLI = 1
5 changes: 3 additions & 2 deletions Examples/MAX78000/SDHC_FTHR/src/sdhc.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ int formatSDHC()

printf("FORMATTING DRIVE\n");

if ((err = f_mkfs("", FM_ANY, 0, work, sizeof(work))) !=
FR_OK) { //Format the default drive to FAT32
MKFS_PARM format_options = { .fmt = FM_ANY };

if ((err = f_mkfs("", &format_options, work, sizeof(work))) != FR_OK) {
printf("Error formatting SD card: %s\n", FF_ERRORS[err]);
} else {
printf("Drive formatted.\n");
Expand Down
13 changes: 3 additions & 10 deletions Examples/MAX78002/SDHC_FAT/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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 Down Expand Up @@ -545,15 +547,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
3 changes: 3 additions & 0 deletions Examples/MAX78002/SDHC_FAT/project.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@

# Add your config here!

# Enable SDHC library
LIB_SDHC = 1
# Use FatFS R0.15
FATFS_VERSION = ff15

12 changes: 6 additions & 6 deletions Libraries/PeriphDrivers/Include/MAX32570/sdhc.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ int MXC_SDHC_Shutdown(void);
/**
* @brief Set clock divider
* @param clk_div Divider setting
* s
* @returns #E_NO_ERROR upon success, @ref MXC_Error_Codes "error" if
* unsuccessful.
*/
void MXC_SDHC_Set_Clock_Config(unsigned int clk_div);

/**
* @brief Get clock divider
* @return Clock divider setting
* s
* @returns #E_NO_ERROR SDHC shutdown successfully, @ref MXC_Error_Codes "error" if
* unsuccessful.
*/
unsigned int MXC_SDHC_Get_Clock_Config(void);

/**
* @brief Get the input clock frequency for the SDHC peripheral.
* @return Input clock frequency in Hz
*/
unsigned int MXC_SDHC_Get_Input_Clock_Freq(void);

/**
* @brief Send Command, <em>blocking</em>.
*
Expand Down
Loading

0 comments on commit 081e8f2

Please sign in to comment.