|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | #include <inttypes.h> |
| 8 | +#include <sys/param.h> // for MIN/MAX |
8 | 9 | #include "esp_private/sdmmc_common.h" |
9 | 10 |
|
| 11 | +// the maximum size in blocks of the chunks a SDMMC write/read will be split into |
| 12 | +#define MAX_NUM_BLOCKS_PER_MULTI_BLOCK_RW (16u) |
| 13 | + |
10 | 14 | static const char* TAG = "sdmmc_cmd"; |
11 | 15 |
|
12 | 16 |
|
@@ -462,31 +466,47 @@ esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src, |
462 | 466 | err = sdmmc_write_sectors_dma(card, src, start_block, block_count, block_size * block_count); |
463 | 467 | } else { |
464 | 468 | // SDMMC peripheral needs DMA-capable buffers. Split the write into |
465 | | - // separate single block writes, if needed, and allocate a temporary |
| 469 | + // separate (multi) block writes, if needed, and allocate a temporary |
466 | 470 | // DMA-capable buffer. |
467 | | - void *tmp_buf = NULL; |
468 | | - size_t actual_size = 0; |
469 | | - // We don't want to force the allocation into SPIRAM, the allocator |
470 | | - // will decide based on the buffer size and memory availability. |
471 | | - tmp_buf = heap_caps_malloc(block_size, MALLOC_CAP_DMA); |
472 | | - if (!tmp_buf) { |
473 | | - ESP_LOGE(TAG, "%s: not enough mem, err=0x%x", __func__, ESP_ERR_NO_MEM); |
474 | | - return ESP_ERR_NO_MEM; |
| 471 | + size_t blocks_per_write = MIN(CONFIG_SD_UNALIGNED_MULTI_BLOCK_RW_MAX_CHUNK_SIZE, block_count); |
| 472 | + |
| 473 | + // prefer using DMA aligned buffer if available over allocating local temporary buffer |
| 474 | + bool use_dma_aligned_buffer = (card->host.dma_aligned_buffer != NULL); |
| 475 | + void* buf = use_dma_aligned_buffer ? card->host.dma_aligned_buffer : NULL; |
| 476 | + |
| 477 | + // only allocate temporary buffer if we can't use the dma_aligned buffer |
| 478 | + if (!use_dma_aligned_buffer) { |
| 479 | + // We don't want to force the allocation into SPIRAM, the allocator |
| 480 | + // will decide based on the buffer size and memory availability. |
| 481 | + buf = heap_caps_malloc(block_size * blocks_per_write, MALLOC_CAP_DMA); |
| 482 | + if (!buf) { |
| 483 | + ESP_LOGE(TAG, "%s: not enough mem, err=0x%x", __func__, ESP_ERR_NO_MEM); |
| 484 | + return ESP_ERR_NO_MEM; |
| 485 | + } |
| 486 | + } |
| 487 | + size_t actual_size = heap_caps_get_allocated_size(buf); |
| 488 | + blocks_per_write = actual_size / card->csd.sector_size; |
| 489 | + if (blocks_per_write == 0) { |
| 490 | + ESP_LOGE(TAG, "%s: buffer smaller than sector size: buf=%d, sector=%d", actual_size, card->csd.sector_size); |
| 491 | + return ESP_ERR_INVALID_SIZE; |
475 | 492 | } |
476 | | - actual_size = heap_caps_get_allocated_size(tmp_buf); |
477 | 493 |
|
478 | 494 | const uint8_t* cur_src = (const uint8_t*) src; |
479 | | - for (size_t i = 0; i < block_count; ++i) { |
480 | | - memcpy(tmp_buf, cur_src, block_size); |
481 | | - cur_src += block_size; |
482 | | - err = sdmmc_write_sectors_dma(card, tmp_buf, start_block + i, 1, actual_size); |
| 495 | + for (size_t i = 0; i < block_count; i += blocks_per_write) { |
| 496 | + // make sure not to write more than the remaining blocks, i.e. block_count - i |
| 497 | + blocks_per_write = MIN(blocks_per_write, (block_count - i)); |
| 498 | + memcpy(buf, cur_src, block_size * blocks_per_write); |
| 499 | + cur_src += block_size * blocks_per_write; |
| 500 | + err = sdmmc_write_sectors_dma(card, buf, start_block + i, blocks_per_write, actual_size); |
483 | 501 | if (err != ESP_OK) { |
484 | | - ESP_LOGD(TAG, "%s: error 0x%x writing block %d+%d", |
485 | | - __func__, err, start_block, i); |
| 502 | + ESP_LOGD(TAG, "%s: error 0x%x writing blocks %d+[%d..%d]", |
| 503 | + __func__, err, start_block, i, i + blocks_per_write - 1); |
486 | 504 | break; |
487 | 505 | } |
488 | 506 | } |
489 | | - free(tmp_buf); |
| 507 | + if (!use_dma_aligned_buffer) { |
| 508 | + free(buf); |
| 509 | + } |
490 | 510 | } |
491 | 511 | return err; |
492 | 512 | } |
@@ -600,33 +620,50 @@ esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst, |
600 | 620 | err = sdmmc_read_sectors_dma(card, dst, start_block, block_count, block_size * block_count); |
601 | 621 | } else { |
602 | 622 | // SDMMC peripheral needs DMA-capable buffers. Split the read into |
603 | | - // separate single block reads, if needed, and allocate a temporary |
| 623 | + // separate (multi) block reads, if needed, and allocate a temporary |
604 | 624 | // DMA-capable buffer. |
605 | | - void *tmp_buf = NULL; |
606 | | - size_t actual_size = 0; |
607 | | - tmp_buf = heap_caps_malloc(block_size, MALLOC_CAP_DMA); |
608 | | - if (!tmp_buf) { |
609 | | - ESP_LOGE(TAG, "%s: not enough mem, err=0x%x", __func__, ESP_ERR_NO_MEM); |
610 | | - return ESP_ERR_NO_MEM; |
| 625 | + size_t blocks_per_read = MIN(CONFIG_SD_UNALIGNED_MULTI_BLOCK_RW_MAX_CHUNK_SIZE, block_count); |
| 626 | + |
| 627 | + // prefer using DMA aligned buffer if available over allocating local temporary buffer |
| 628 | + bool use_dma_aligned_buffer = (card->host.dma_aligned_buffer != NULL); |
| 629 | + void* buf = use_dma_aligned_buffer ? card->host.dma_aligned_buffer : NULL; |
| 630 | + |
| 631 | + // only allocate temporary buffer if we can't use the dma_aligned buffer |
| 632 | + if (!use_dma_aligned_buffer) { |
| 633 | + // We don't want to force the allocation into SPIRAM, the allocator |
| 634 | + // will decide based on the buffer size and memory availability. |
| 635 | + buf = heap_caps_malloc(block_size * blocks_per_read, MALLOC_CAP_DMA); |
| 636 | + if (!buf) { |
| 637 | + ESP_LOGE(TAG, "%s: not enough mem, err=0x%x", __func__, ESP_ERR_NO_MEM); |
| 638 | + return ESP_ERR_NO_MEM; |
| 639 | + } |
| 640 | + } |
| 641 | + size_t actual_size = heap_caps_get_allocated_size(buf); |
| 642 | + blocks_per_read = actual_size / card->csd.sector_size; |
| 643 | + if (blocks_per_read == 0) { |
| 644 | + ESP_LOGE(TAG, "%s: buffer smaller than sector size: buf=%d, sector=%d", actual_size, card->csd.sector_size); |
| 645 | + return ESP_ERR_INVALID_SIZE; |
611 | 646 | } |
612 | | - actual_size = heap_caps_get_allocated_size(tmp_buf); |
613 | 647 |
|
614 | 648 | uint8_t* cur_dst = (uint8_t*) dst; |
615 | | - for (size_t i = 0; i < block_count; ++i) { |
616 | | - err = sdmmc_read_sectors_dma(card, tmp_buf, start_block + i, 1, actual_size); |
| 649 | + for (size_t i = 0; i < block_count; i += blocks_per_read) { |
| 650 | + // make sure not to read more than the remaining blocks, i.e. block_count - i |
| 651 | + blocks_per_read = MIN(blocks_per_read, (block_count - i)); |
| 652 | + err = sdmmc_read_sectors_dma(card, buf, start_block + i, blocks_per_read, actual_size); |
617 | 653 | if (err != ESP_OK) { |
618 | | - ESP_LOGD(TAG, "%s: error 0x%x writing block %d+%d", |
619 | | - __func__, err, start_block, i); |
| 654 | + ESP_LOGD(TAG, "%s: error 0x%x reading blocks %d+[%d..%d]", |
| 655 | + __func__, err, start_block, i, i + blocks_per_read - 1); |
620 | 656 | break; |
621 | 657 | } |
622 | | - memcpy(cur_dst, tmp_buf, block_size); |
623 | | - cur_dst += block_size; |
| 658 | + memcpy(cur_dst, buf, block_size * blocks_per_read); |
| 659 | + cur_dst += block_size * blocks_per_read; |
| 660 | + } |
| 661 | + if (!use_dma_aligned_buffer) { |
| 662 | + free(buf); |
624 | 663 | } |
625 | | - free(tmp_buf); |
626 | 664 | } |
627 | 665 | return err; |
628 | 666 | } |
629 | | - |
630 | 667 | esp_err_t sdmmc_read_sectors_dma(sdmmc_card_t* card, void* dst, |
631 | 668 | size_t start_block, size_t block_count, size_t buffer_len) |
632 | 669 | { |
|
0 commit comments