@@ -51,6 +51,34 @@ void make_hw_cmd(uint32_t opcode, uint32_t arg, int timeout_ms, sdspi_hw_cmd_t *
5151 hw_cmd -> timeout_ms = timeout_ms ;
5252}
5353
54+ static void r1_response_to_err (uint8_t r1 , esp_err_t * out_err )
55+ {
56+ if (r1 & SD_SPI_R1_NO_RESPONSE ) {
57+ ESP_LOGD (TAG , "R1 response not found" );
58+ * out_err = ESP_ERR_TIMEOUT ;
59+ } else if (r1 & SD_SPI_R1_CMD_CRC_ERR ) {
60+ ESP_LOGD (TAG , "R1 response: command CRC error" );
61+ * out_err = ESP_ERR_INVALID_CRC ;
62+ } else if (r1 & SD_SPI_R1_ILLEGAL_CMD ) {
63+ ESP_LOGD (TAG , "R1 response: command not supported" );
64+ * out_err = ESP_ERR_NOT_SUPPORTED ;
65+ } else if (r1 & SD_SPI_R1_ADDR_ERR ) {
66+ ESP_LOGD (TAG , "R1 response: alignment error" );
67+ * out_err = ESP_ERR_INVALID_ARG ;
68+ } else if (r1 & SD_SPI_R1_PARAM_ERR ) {
69+ ESP_LOGD (TAG , "R1 response: size error" );
70+ * out_err = ESP_ERR_INVALID_SIZE ;
71+ } else if ((r1 & SD_SPI_R1_ERASE_RST ) ||
72+ (r1 & SD_SPI_R1_ERASE_SEQ_ERR )) {
73+ * out_err = ESP_ERR_INVALID_STATE ;
74+ } else if (r1 & SD_SPI_R1_IDLE_STATE ) {
75+ // Idle state is handled at command layer
76+ } else if (r1 != 0 ) {
77+ ESP_LOGD (TAG , "R1 response: unexpected value 0x%02x" , r1 );
78+ * out_err = ESP_ERR_INVALID_RESPONSE ;
79+ }
80+ }
81+
5482esp_err_t sdspi_host_do_transaction (int slot , sdmmc_command_t * cmdinfo )
5583{
5684 _lock_acquire (& s_lock );
@@ -93,21 +121,11 @@ esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo)
93121 // Some errors should be reported using return code
94122 if (flags & SDSPI_CMD_FLAG_RSP_R1 ) {
95123 cmdinfo -> response [0 ] = hw_cmd .r1 ;
96- if (hw_cmd .r1 == 0xff ) {
97- // No response received at all
98- } else if (hw_cmd .r1 & SD_SPI_R1_CMD_CRC_ERR ) {
99- ret = ESP_ERR_INVALID_CRC ;
100- } else if (hw_cmd .r1 & SD_SPI_R1_IDLE_STATE ) {
101- // Idle state is handled at command layer
102- } else if (hw_cmd .r1 != 0 ) {
103- ESP_LOGD (TAG , "Unexpected R1 response: 0x%02x" , hw_cmd .r1 );
104- }
124+ r1_response_to_err (hw_cmd .r1 , & ret );
105125 } else if (flags & SDSPI_CMD_FLAG_RSP_R2 ) {
106126 cmdinfo -> response [0 ] = (((uint32_t )hw_cmd .r1 ) << 8 ) | (hw_cmd .response [0 ] >> 24 );
107127 } else if (flags & (SDSPI_CMD_FLAG_RSP_R3 | SDSPI_CMD_FLAG_RSP_R7 )) {
108- // Drop r1 response, only copy the other 4 bytes of data
109- // TODO: can we somehow preserve r1 response and keep upper layer
110- // same as in SD mode?
128+ r1_response_to_err (hw_cmd .r1 , & ret );
111129 cmdinfo -> response [0 ] = __builtin_bswap32 (hw_cmd .response [0 ]);
112130 }
113131 }
0 commit comments