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

my SD/MMC no longer works from version 5.0 (IDFGH-11818) #12912

Closed
3 tasks done
SybexX opened this issue Jan 2, 2024 · 7 comments
Closed
3 tasks done

my SD/MMC no longer works from version 5.0 (IDFGH-11818) #12912

SybexX opened this issue Jan 2, 2024 · 7 comments
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF

Comments

@SybexX
Copy link

SybexX commented Jan 2, 2024

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

framework-espidf@3.50002.230601

Espressif SoC revision.

ESP32-S

Operating System used.

Windows

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

Development Kit.

AI Thinker ESP32-CAM

Power Supply used.

USB

What is the expected behavior?

older SDs without ATA-TRIM support do not work

What is the actual behavior?

SD initialization is aborted

Steps to reproduce.

Using an older SD card

Debug Logs.

No response

More Information.

As of version 5.0, FF_USE_TRIM was set from 0 to 1 and since then my card has not been recognized.
This is because it does not support ATA-TRIM^^

You have added a function esp_err_t sdmmc_can_trim(sdmmc_card_t* card) for this, but it has no effect.
I searched for the error overnight and found a solution for it.

After I modified the files as follows, everything works as it should again:

fatfs/diskio/diskio_sdmmc.c

DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff), at lines 106 to 110 changed from:

#if FF_USE_TRIM
        case CTRL_TRIM:
            return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
                    (*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
#endif //FF_USE_TRIM

to:

#if (FF_USE_TRIM)
        case CTRL_TRIM:
            if(FF_CAN_TRIM){
                return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
                    (*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
            }
            else{
                return RES_ERROR;
            }
#endif //FF_USE_TRIM

fatfs/src/ff.c

added:

#include "sdmmc_cmd.h"

static FRESULT remove_chain(FFOBJID* obj, DWORD clst, DWORD pclst), at lines 1437 to 1454 changed from:

#if FF_FS_EXFAT || FF_USE_TRIM
		if (ecl + 1 == nxt) {	/* Is next cluster contiguous? */
			ecl = nxt;
		} else {				/* End of contiguous cluster block */
#if FF_FS_EXFAT
			if (fs->fs_type == FS_EXFAT) {
				res = change_bitmap(fs, scl, ecl - scl + 1, 0);	/* Mark the cluster block 'free' on the bitmap */
				if (res != FR_OK) return res;
			}
#endif
#if FF_USE_TRIM
			rt[0] = clst2sect(fs, scl);					/* Start of data area to be freed */
			rt[1] = clst2sect(fs, ecl) + fs->csize - 1;	/* End of data area to be freed */
			disk_ioctl(fs->pdrv, CTRL_TRIM, rt);		/* Inform storage device that the data in the block may be erased */
#endif
			scl = ecl = nxt;
		}
#endif

to:

#if FF_FS_EXFAT || FF_USE_TRIM
		if(FF_FS_EXFAT || FF_CAN_TRIM){
			if (ecl + 1 == nxt) {	/* Is next cluster contiguous? */
				ecl = nxt;
			} 
			else {				/* End of contiguous cluster block */
#if FF_FS_EXFAT
				if (fs->fs_type == FS_EXFAT) {
					res = change_bitmap(fs, scl, ecl - scl + 1, 0);	/* Mark the cluster block 'free' on the bitmap */
					if (res != FR_OK) return res;
				}
#endif
#if FF_USE_TRIM
				if(FF_CAN_TRIM){
					rt[0] = clst2sect(fs, scl);					/* Start of data area to be freed */
					rt[1] = clst2sect(fs, ecl) + fs->csize - 1;	/* End of data area to be freed */
					disk_ioctl(fs->pdrv, CTRL_TRIM, rt);		/* Inform storage device that the data in the block may be erased */
				}
#endif				
				scl = ecl = nxt;
			}
		}
#endif

FRESULT f_mkfs(const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len), at lines 5946 to 5949 changed from:

#if FF_USE_TRIM
		lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;	/* Inform storage device that the volume area may be erased */
		disk_ioctl(pdrv, CTRL_TRIM, lba);
#endif

to:

#if FF_USE_TRIM
		if(FF_CAN_TRIM){
			lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;	/* Inform storage device that the volume area may be erased */
			disk_ioctl(pdrv, CTRL_TRIM, lba);
		}
#endif

FRESULT f_mkfs(const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len), at lines 6175 to 6178 changed from:

#if FF_USE_TRIM
		lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;	/* Inform storage device that the volume area may be erased */
		disk_ioctl(pdrv, CTRL_TRIM, lba);
#endif

to:

#if FF_USE_TRIM
		if(FF_CAN_TRIM){
			lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1;	/* Inform storage device that the volume area may be erased */
			disk_ioctl(pdrv, CTRL_TRIM, lba);
		}
#endif

sdmmc/sdmmc_cmd.c

added:

int FF_CAN_TRIM = 0;

esp_err_t sdmmc_can_trim(sdmmc_card_t* card), at lines 630 to 636 changed from:

esp_err_t sdmmc_can_trim(sdmmc_card_t* card)
{
    if ((card->is_mmc) && (card->ext_csd.sec_feature & EXT_CSD_SEC_GB_CL_EN)) {
        return ESP_OK;
    }
    return ESP_FAIL;
}

to:

esp_err_t sdmmc_can_trim(sdmmc_card_t* card)
{
    if ((card->is_mmc) && (card->ext_csd.sec_feature & EXT_CSD_SEC_GB_CL_EN)) {
        FF_CAN_TRIM = 1;
        return ESP_OK;
    }
    FF_CAN_TRIM = 0;
    return ESP_FAIL;
}

sdmmc/include/sdmmc_cmd.h

added:

extern int FF_CAN_TRIM;
@SybexX SybexX added the Type: Bug bugs in IDF label Jan 2, 2024
@espressif-bot espressif-bot added the Status: Opened Issue is new label Jan 2, 2024
@github-actions github-actions bot changed the title my SD/MMC no longer works from version 5.0 my SD/MMC no longer works from version 5.0 (IDFGH-11818) Jan 2, 2024
@haberturdeur
Copy link
Collaborator

haberturdeur commented Jan 5, 2024

Hi, thank you for the report.
Could you please try using the original code, with only this change (adding the can_trim check):

DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff)
{
    sdmmc_card_t* card = s_cards[pdrv];
    assert(card);
    switch(cmd) {
        case CTRL_SYNC:
            return RES_OK;
        case GET_SECTOR_COUNT:
            *((DWORD*) buff) = card->csd.capacity;
            return RES_OK;
        case GET_SECTOR_SIZE:
            *((WORD*) buff) = card->csd.sector_size;
            return RES_OK;
        case GET_BLOCK_SIZE:
            return RES_ERROR;
#if FF_USE_TRIM
        case CTRL_TRIM:
            if (sdmmc_can_trim(card) != ESP_OK) {
                return RES_PARERR;
            }
            return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
                    (*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
#endif //FF_USE_TRIM
    }
    return RES_ERROR;
}

@espressif-bot espressif-bot added Status: In Progress Work is in progress and removed Status: Opened Issue is new labels Jan 5, 2024
@SybexX
Copy link
Author

SybexX commented Jan 5, 2024

After a few tests everything (initialization, reading, writing and deleting) works as it should, see picture

SD-ATA-TRIM

@espressif-bot espressif-bot added Status: Reviewing Issue is being reviewed and removed Status: In Progress Work is in progress labels Jan 5, 2024
@SybexX
Copy link
Author

SybexX commented Jan 5, 2024

I've now tested it with two SDs that didn't work before and both work now.

@SybexX
Copy link
Author

SybexX commented Jan 5, 2024

I've now tested with a second ESP32-CAM and a third SD that didn't work before and now it works as expected ^^

@haberturdeur
Copy link
Collaborator

Thank you!

@pacucha42
Copy link
Collaborator

@SybexX - closing the task. Feel free to reopen if the issue appears again. Thank you

@pacucha42 pacucha42 reopened this Jan 8, 2024
@pacucha42
Copy link
Collaborator

Reopening, there is a code fix ready for merging so this task cannot yet be closed. My bad, sorry

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Reviewing Issue is being reviewed labels Jan 8, 2024
DarkZeros pushed a commit to DarkZeros/esp-idf that referenced this issue May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

4 participants