Skip to content

Commit

Permalink
RP2040: Add support for 2GB SD cards
Browse files Browse the repository at this point in the history
Standard capacity (up to 2GB cards) use byte-addressing in read/write
commands.
  • Loading branch information
PetteriAimonen authored and erichelgeson committed May 26, 2023
1 parent 65a4566 commit 66c1e47
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/BlueSCSI_platform_RP2040/sd_card_sdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ bool SdioCard::writeSector(uint32_t sector, const uint8_t* src)
// If possible, report transfer status to application through callback.
sd_callback_t callback = get_stream_callback(src, 512, "writeSector", sector);

// Cards up to 2GB use byte addressing, SDHC cards use sector addressing
uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);

uint32_t reply;
if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
!checkReturnOk(rp2040_sdio_command_R1(CMD24, sector, &reply)) || // WRITE_BLOCK
!checkReturnOk(rp2040_sdio_command_R1(CMD24, address, &reply)) || // WRITE_BLOCK
!checkReturnOk(rp2040_sdio_tx_start(src, 1))) // Start transmission
{
return false;
Expand Down Expand Up @@ -366,11 +369,14 @@ bool SdioCard::writeSectors(uint32_t sector, const uint8_t* src, size_t n)

sd_callback_t callback = get_stream_callback(src, n * 512, "writeSectors", sector);

// Cards up to 2GB use byte addressing, SDHC cards use sector addressing
uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);

uint32_t reply;
if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
!checkReturnOk(rp2040_sdio_command_R1(CMD55, g_sdio_rca, &reply)) || // APP_CMD
!checkReturnOk(rp2040_sdio_command_R1(ACMD23, n, &reply)) || // SET_WR_CLK_ERASE_COUNT
!checkReturnOk(rp2040_sdio_command_R1(CMD25, sector, &reply)) || // WRITE_MULTIPLE_BLOCK
!checkReturnOk(rp2040_sdio_command_R1(CMD25, address, &reply)) || // WRITE_MULTIPLE_BLOCK
!checkReturnOk(rp2040_sdio_tx_start(src, n))) // Start transmission
{
return false;
Expand Down Expand Up @@ -409,10 +415,13 @@ bool SdioCard::readSector(uint32_t sector, uint8_t* dst)

sd_callback_t callback = get_stream_callback(dst, 512, "readSector", sector);

// Cards up to 2GB use byte addressing, SDHC cards use sector addressing
uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);

uint32_t reply;
if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
!checkReturnOk(rp2040_sdio_rx_start(dst, 1)) || // Prepare for reception
!checkReturnOk(rp2040_sdio_command_R1(CMD17, sector, &reply))) // READ_SINGLE_BLOCK
!checkReturnOk(rp2040_sdio_command_R1(CMD17, address, &reply))) // READ_SINGLE_BLOCK
{
return false;
}
Expand Down Expand Up @@ -457,10 +466,13 @@ bool SdioCard::readSectors(uint32_t sector, uint8_t* dst, size_t n)

sd_callback_t callback = get_stream_callback(dst, n * 512, "readSectors", sector);

// Cards up to 2GB use byte addressing, SDHC cards use sector addressing
uint32_t address = (type() == SD_CARD_TYPE_SDHC) ? sector : (sector * 512);

uint32_t reply;
if (!checkReturnOk(rp2040_sdio_command_R1(16, 512, &reply)) || // SET_BLOCKLEN
!checkReturnOk(rp2040_sdio_rx_start(dst, n)) || // Prepare for reception
!checkReturnOk(rp2040_sdio_command_R1(CMD18, sector, &reply))) // READ_MULTIPLE_BLOCK
!checkReturnOk(rp2040_sdio_command_R1(CMD18, address, &reply))) // READ_MULTIPLE_BLOCK
{
return false;
}
Expand Down

0 comments on commit 66c1e47

Please sign in to comment.