sdmmc: read/write/erase return early on sector count == 0

Closes https://github.com/espressif/esp-idf/issues/10593
This commit is contained in:
Adam Múdry 2023-02-20 15:56:55 +01:00
parent af805df3cb
commit 415e785135
3 changed files with 38 additions and 3 deletions

View File

@ -58,7 +58,7 @@ esp_err_t sdmmc_get_status(sdmmc_card_t* card);
* @param start_sector sector where to start writing
* @param sector_count number of sectors to write
* @return
* - ESP_OK on success
* - ESP_OK on success or sector_count equal to 0
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
@ -74,7 +74,7 @@ esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
* @param start_sector sector where to start reading
* @param sector_count number of sectors to read
* @return
* - ESP_OK on success
* - ESP_OK on success or sector_count equal to 0
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
@ -92,7 +92,7 @@ esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
* @param sector_count number of sectors to erase
* @param arg erase command (CMD38) argument
* @return
* - ESP_OK on success
* - ESP_OK on success or sector_count equal to 0
* - One of the error codes from SDMMC host controller
*/
esp_err_t sdmmc_erase_sectors(sdmmc_card_t* card, size_t start_sector,

View File

@ -359,6 +359,10 @@ esp_err_t sdmmc_send_cmd_send_status(sdmmc_card_t* card, uint32_t* out_status)
esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
size_t start_block, size_t block_count)
{
if (block_count == 0) {
return ESP_OK;
}
esp_err_t err = ESP_OK;
size_t block_size = card->csd.sector_size;
if (esp_ptr_dma_capable(src) && (intptr_t)src % 4 == 0) {
@ -456,6 +460,10 @@ esp_err_t sdmmc_write_sectors_dma(sdmmc_card_t* card, const void* src,
esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
size_t start_block, size_t block_count)
{
if (block_count == 0) {
return ESP_OK;
}
esp_err_t err = ESP_OK;
size_t block_size = card->csd.sector_size;
if (esp_ptr_dma_capable(dst) && (intptr_t)dst % 4 == 0) {
@ -530,6 +538,10 @@ esp_err_t sdmmc_read_sectors_dma(sdmmc_card_t* card, void* dst,
esp_err_t sdmmc_erase_sectors(sdmmc_card_t* card, size_t start_sector,
size_t sector_count, sdmmc_erase_arg_t arg)
{
if (sector_count == 0) {
return ESP_OK;
}
if (start_sector + sector_count > card->csd.capacity) {
return ESP_ERR_INVALID_SIZE;
}

View File

@ -1035,6 +1035,29 @@ TEST_CASE("SDMMC discard test (SD slot 1, 4 line)", "[sd][test_env=UT_T1_SDMODE]
}
#endif //WITH_SD_TEST
#if WITH_SD_TEST
TEST_CASE("sdmmc read/write/erase sector shoud return ESP_OK with sector count == 0", "[sd][test_env=UT_T1_SDMODE]")
{
sd_test_board_power_on();
sdmmc_host_t config = SDMMC_HOST_DEFAULT();
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
TEST_ESP_OK(sdmmc_host_init());
TEST_ESP_OK(sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config));
sdmmc_card_t* card = malloc(sizeof(sdmmc_card_t));
TEST_ASSERT_NOT_NULL(card);
TEST_ESP_OK(sdmmc_card_init(&config, card));
TEST_ESP_OK(sdmmc_write_sectors(card, NULL, 0, 0));
TEST_ESP_OK(sdmmc_read_sectors(card, NULL, 0, 0));
TEST_ESP_OK(sdmmc_erase_sectors(card, 0, 0, SDMMC_ERASE_ARG));
free(card);
TEST_ESP_OK(sdmmc_host_deinit());
sd_test_board_power_off();
}
#endif //WITH_SD_TEST
#if WITH_EMMC_TEST
static void test_mmc_sanitize_blocks(sdmmc_card_t* card)
{