Skip to content

Commit 229f67b

Browse files
committed
sdspi: handle delayed R1 responses for data read commands
1 parent bbbdec2 commit 229f67b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

components/driver/sdspi_host.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,9 @@ static esp_err_t start_command_read_blocks(int slot, sdspi_hw_cmd_t *cmd,
575575
{
576576
bool need_stop_command = rx_length > SDSPI_MAX_DATA_LEN;
577577
spi_transaction_t* t_command = get_transaction(slot);
578+
const int cmd_extra_bytes = 8;
578579
*t_command = (spi_transaction_t) {
579-
.length = (SDSPI_CMD_R1_SIZE + 8) * 8,
580+
.length = (SDSPI_CMD_R1_SIZE + cmd_extra_bytes) * 8,
580581
.tx_buffer = cmd,
581582
.rx_buffer = cmd,
582583
};
@@ -587,9 +588,21 @@ static esp_err_t start_command_read_blocks(int slot, sdspi_hw_cmd_t *cmd,
587588
release_transaction(slot);
588589

589590
uint8_t* cmd_u8 = (uint8_t*) cmd;
590-
size_t pre_scan_data_size = 8;
591+
size_t pre_scan_data_size = cmd_extra_bytes;
591592
uint8_t* pre_scan_data_ptr = cmd_u8 + SDSPI_CMD_R1_SIZE;
592593

594+
/* R1 response is delayed by 1-8 bytes from the request.
595+
* This loop searches for the response and writes it to cmd->r1.
596+
*/
597+
while ((cmd->r1 & SD_SPI_R1_NO_RESPONSE) != 0 && pre_scan_data_size > 0) {
598+
cmd->r1 = *pre_scan_data_ptr;
599+
++pre_scan_data_ptr;
600+
--pre_scan_data_size;
601+
}
602+
if (cmd->r1 & SD_SPI_R1_NO_RESPONSE) {
603+
ESP_LOGD(TAG, "no response token found");
604+
return ESP_ERR_TIMEOUT;
605+
}
593606

594607
while (rx_length > 0) {
595608
size_t extra_data_size = 0;

0 commit comments

Comments
 (0)