From 517428bd015b23d375f19d10a8bfd362ab6a52a8 Mon Sep 17 00:00:00 2001 From: DiSlord Live Date: Wed, 30 Nov 2022 20:43:38 +0300 Subject: [PATCH] Allow read/write more then one sector (512 bytes) in one time --- ili9341.c | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/ili9341.c b/ili9341.c index c20a882..3bf12a7 100644 --- a/ili9341.c +++ b/ili9341.c @@ -1351,7 +1351,7 @@ static bool SD_RxDataBlock(uint8_t *buff, uint16_t len, uint8_t token) { } // Transmit data block to SD -static bool SD_TxDataBlock(const uint8_t *buff, uint8_t token) { +static bool SD_TxDataBlock(const uint8_t *buff, uint16_t len, uint8_t token) { uint8_t resp; // Transmit token spi_TxByte(token); @@ -1361,13 +1361,13 @@ static bool SD_TxDataBlock(const uint8_t *buff, uint8_t token) { #endif #ifdef __USE_SDCARD_DMA__ - spi_DMATxBuffer((uint8_t*)buff, SD_SECTOR_SIZE, true); + spi_DMATxBuffer((uint8_t*)buff, len, true); #else - spi_TxBuffer((uint8_t*)buff, SD_SECTOR_SIZE); + spi_TxBuffer((uint8_t*)buff, len); #endif // Send CRC #ifdef SD_USE_DATA_CRC - uint16_t bcrc = crc16(buff, SD_SECTOR_SIZE); + uint16_t bcrc = crc16(buff, len); spi_TxWord(bcrc); #else spi_TxWord(0xFFFF); @@ -1590,14 +1590,18 @@ DRESULT disk_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) { #endif SD_Select_SPI(); - // READ_SINGLE_BLOCK - uint8_t cnt = SD_READ_WRITE_REPEAT; // read repeat count - do{ - if ((SD_SendCmd(CMD17, sector) == 0) && SD_RxDataBlock(buff, SD_SECTOR_SIZE, SD_TOKEN_START_BLOCK)){ - count = 0; - break; - } - }while (--cnt); + uint8_t cmd = count == 1 ? CMD17 : CMD18; + // convert to byte address + if (!(CardType & CT_BLOCK)) sector*= SD_SECTOR_SIZE; + // Read single / multiple block + if (SD_SendCmd(cmd, sector) == 0) { + do { + if (SD_RxDataBlock(buff, SD_SECTOR_SIZE, SD_TOKEN_START_BLOCK)) + buff+= SD_SECTOR_SIZE; + else break; + } while(--count); + } + if (cmd == CMD18) SD_SendCmd(CMD12, 0); // Finish multiple block transfer SD_Unselect_SPI(); #if DEBUG == 1 @@ -1625,9 +1629,6 @@ DRESULT disk_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) { if (pdrv != 0 || count != 1 || (Stat & STA_NOINIT)) return RES_NOTRDY; // Write protection if (Stat & STA_PROTECT) return RES_WRPRT; - // Convert to byte address if no Block mode - if (!(CardType & CT_BLOCK)) sector*= SD_SECTOR_SIZE; - #if DEBUG == 1 #if 0 DEBUG_PRINT("Sector write 0x%08X, %d\r\n", sector, count); @@ -1642,14 +1643,14 @@ DRESULT disk_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) { #endif SD_Select_SPI(); - // WRITE_SINGLE_BLOCK - uint8_t cnt = SD_READ_WRITE_REPEAT; // write repeat count - do{ - if ((SD_SendCmd(CMD24, sector) == 0) && SD_TxDataBlock(buff, SD_TOKEN_START_BLOCK)){ - count = 0; - break; - } - } while (--cnt); + do { + // WRITE_SINGLE_BLOCK * count + uint32_t sect = (CardType & CT_BLOCK) ? sector : sector * SD_SECTOR_SIZE; + if ((SD_SendCmd(CMD24, sect) == 0) && SD_TxDataBlock(buff, SD_SECTOR_SIZE, SD_TOKEN_START_BLOCK)) { + sector++; + buff+= SD_SECTOR_SIZE; + } else break; + } while (--count); SD_Unselect_SPI(); #if DEBUG == 1