sdspi_transaction.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_err.h"
  8. #include "esp_log.h"
  9. #include "sys/lock.h"
  10. #include "driver/sdmmc_types.h"
  11. #include "driver/sdmmc_defs.h"
  12. #include "driver/sdmmc_types.h"
  13. #include "sdspi_private.h"
  14. #include "sdspi_crc.h"
  15. static const char* TAG = "sdspi_transaction";
  16. static _lock_t s_lock;
  17. static bool s_app_cmd;
  18. static uint8_t sdspi_msg_crc7(sdspi_hw_cmd_t* hw_cmd)
  19. {
  20. const size_t bytes_to_crc = offsetof(sdspi_hw_cmd_t, arguments) +
  21. sizeof(hw_cmd->arguments); /* can't take address of bit fields */
  22. return sdspi_crc7((const uint8_t *)hw_cmd, bytes_to_crc);
  23. }
  24. void make_hw_cmd(uint32_t opcode, uint32_t arg, int timeout_ms, sdspi_hw_cmd_t *hw_cmd)
  25. {
  26. hw_cmd->start_bit = 0;
  27. hw_cmd->transmission_bit = 1;
  28. hw_cmd->cmd_index = opcode;
  29. hw_cmd->stop_bit = 1;
  30. hw_cmd->r1 = 0xff;
  31. memset(hw_cmd->response, 0xff, sizeof(hw_cmd->response));
  32. hw_cmd->ncr = 0xff;
  33. uint32_t arg_s = __builtin_bswap32(arg);
  34. memcpy(hw_cmd->arguments, &arg_s, sizeof(arg_s));
  35. hw_cmd->crc7 = sdspi_msg_crc7(hw_cmd);
  36. hw_cmd->timeout_ms = timeout_ms;
  37. }
  38. static void r1_response_to_err(uint8_t r1, int cmd, esp_err_t *out_err)
  39. {
  40. if (r1 & SD_SPI_R1_NO_RESPONSE) {
  41. ESP_LOGD(TAG, "cmd=%d, R1 response not found", cmd);
  42. *out_err = ESP_ERR_TIMEOUT;
  43. } else if (r1 & SD_SPI_R1_CMD_CRC_ERR) {
  44. ESP_LOGD(TAG, "cmd=%d, R1 response: command CRC error", cmd);
  45. *out_err = ESP_ERR_INVALID_CRC;
  46. } else if (r1 & SD_SPI_R1_ILLEGAL_CMD) {
  47. ESP_LOGD(TAG, "cmd=%d, R1 response: command not supported", cmd);
  48. *out_err = ESP_ERR_NOT_SUPPORTED;
  49. } else if (r1 & SD_SPI_R1_ADDR_ERR) {
  50. ESP_LOGD(TAG, "cmd=%d, R1 response: alignment error", cmd);
  51. *out_err = ESP_ERR_INVALID_ARG;
  52. } else if (r1 & SD_SPI_R1_PARAM_ERR) {
  53. ESP_LOGD(TAG, "cmd=%d, R1 response: size error", cmd);
  54. *out_err = ESP_ERR_INVALID_SIZE;
  55. } else if ((r1 & SD_SPI_R1_ERASE_RST) ||
  56. (r1 & SD_SPI_R1_ERASE_SEQ_ERR)) {
  57. *out_err = ESP_ERR_INVALID_STATE;
  58. } else if (r1 & SD_SPI_R1_IDLE_STATE) {
  59. // Idle state is handled at command layer
  60. } else if (r1 != 0) {
  61. ESP_LOGD(TAG, "cmd=%d, R1 response: unexpected value 0x%02x", cmd, r1);
  62. *out_err = ESP_ERR_INVALID_RESPONSE;
  63. }
  64. }
  65. static void r1_sdio_response_to_err(uint8_t r1, int cmd, esp_err_t *out_err)
  66. {
  67. if (r1 & SD_SPI_R1_NO_RESPONSE) {
  68. ESP_LOGI(TAG, "cmd=%d, R1 response not found", cmd);
  69. *out_err = ESP_ERR_TIMEOUT;
  70. } else if (r1 & SD_SPI_R1_CMD_CRC_ERR) {
  71. ESP_LOGI(TAG, "cmd=%d, R1 response: command CRC error", cmd);
  72. *out_err = ESP_ERR_INVALID_CRC;
  73. } else if (r1 & SD_SPI_R1_ILLEGAL_CMD) {
  74. ESP_LOGI(TAG, "cmd=%d, R1 response: command not supported", cmd);
  75. *out_err = ESP_ERR_NOT_SUPPORTED;
  76. } else if (r1 & SD_SPI_R1_PARAM_ERR) {
  77. ESP_LOGI(TAG, "cmd=%d, R1 response: size error", cmd);
  78. *out_err = ESP_ERR_INVALID_SIZE;
  79. } else if (r1 & SDIO_R1_FUNC_NUM_ERR) {
  80. ESP_LOGI(TAG, "cmd=%d, R1 response: function number error", cmd);
  81. *out_err = ESP_ERR_INVALID_ARG;
  82. } else if (r1 & SD_SPI_R1_IDLE_STATE) {
  83. // Idle state is handled at command layer
  84. } else if (r1 != 0) {
  85. ESP_LOGI(TAG, "cmd=%d, R1 response: unexpected value 0x%02x", cmd, r1);
  86. *out_err = ESP_ERR_INVALID_RESPONSE;
  87. }
  88. }
  89. esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo)
  90. {
  91. _lock_acquire(&s_lock);
  92. // Convert the command to wire format
  93. WORD_ALIGNED_ATTR sdspi_hw_cmd_t hw_cmd;
  94. make_hw_cmd(cmdinfo->opcode, cmdinfo->arg, cmdinfo->timeout_ms, &hw_cmd);
  95. // Flags indicate which of the transfer types should be used
  96. int flags = 0;
  97. if (SCF_CMD(cmdinfo->flags) == SCF_CMD_ADTC) {
  98. flags = SDSPI_CMD_FLAG_DATA | SDSPI_CMD_FLAG_WRITE;
  99. } else if (SCF_CMD(cmdinfo->flags) == (SCF_CMD_ADTC | SCF_CMD_READ)) {
  100. flags = SDSPI_CMD_FLAG_DATA;
  101. }
  102. // The block size is 512, when larger than 512, the data must send in multi blocks
  103. if (cmdinfo->datalen > SDSPI_MAX_DATA_LEN) {
  104. flags |= SDSPI_CMD_FLAG_MULTI_BLK;
  105. }
  106. // In SD host, response format is encoded using SCF_RSP_* flags which come
  107. // as part of sdmmc_command_t from the upper layer (sdmmc_cmd.c).
  108. // SPI mode uses different command formats. In fact, most of the commands
  109. // use R1 response. Therefore, instead of adding another parallel set of
  110. // response flags for the SPI mode, response format is determined here:
  111. if (!s_app_cmd && cmdinfo->opcode == SD_SEND_IF_COND) {
  112. flags |= SDSPI_CMD_FLAG_RSP_R7;
  113. } else if (!s_app_cmd && cmdinfo->opcode == MMC_SEND_STATUS) {
  114. flags |= SDSPI_CMD_FLAG_RSP_R2;
  115. } else if (!s_app_cmd && cmdinfo->opcode == SD_READ_OCR) {
  116. flags |= SDSPI_CMD_FLAG_RSP_R3;
  117. } else if (s_app_cmd && cmdinfo->opcode == SD_APP_SD_STATUS) {
  118. flags |= SDSPI_CMD_FLAG_RSP_R2;
  119. } else if (!s_app_cmd && cmdinfo->opcode == MMC_GO_IDLE_STATE &&
  120. !(cmdinfo->flags & SCF_RSP_R1)) {
  121. /* used to send CMD0 without expecting a response */
  122. flags |= SDSPI_CMD_FLAG_NORSP;
  123. } else if (!s_app_cmd && cmdinfo->opcode == SD_IO_SEND_OP_COND) {
  124. flags |= SDSPI_CMD_FLAG_RSP_R4;
  125. } else if (!s_app_cmd && cmdinfo->opcode == SD_IO_RW_DIRECT) {
  126. flags |= SDSPI_CMD_FLAG_RSP_R5;
  127. } else if (!s_app_cmd && cmdinfo->opcode == SD_IO_RW_EXTENDED) {
  128. flags |= SDSPI_CMD_FLAG_RSP_R5 | SDSPI_CMD_FLAG_DATA;
  129. if (cmdinfo->arg & SD_ARG_CMD53_WRITE) flags |= SDSPI_CMD_FLAG_WRITE;
  130. // The CMD53 can assign block mode in the arg when the length is exactly 512 bytes
  131. if (cmdinfo->arg & SD_ARG_CMD53_BLOCK_MODE) flags |= SDSPI_CMD_FLAG_MULTI_BLK;
  132. } else {
  133. flags |= SDSPI_CMD_FLAG_RSP_R1;
  134. }
  135. // Send the command and get the response.
  136. esp_err_t ret = sdspi_host_start_command(slot, &hw_cmd,
  137. cmdinfo->data, cmdinfo->datalen, flags);
  138. // Extract response bytes and store them into cmdinfo structure
  139. if (ret == ESP_OK) {
  140. ESP_LOGV(TAG, "r1 = 0x%02x hw_cmd.r[0]=0x%08x", hw_cmd.r1, hw_cmd.response[0]);
  141. // Some errors should be reported using return code
  142. if (flags & SDSPI_CMD_FLAG_RSP_R1) {
  143. cmdinfo->response[0] = hw_cmd.r1;
  144. r1_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  145. } else if (flags & SDSPI_CMD_FLAG_RSP_R2) {
  146. cmdinfo->response[0] = (((uint32_t)hw_cmd.r1) << 8) | (hw_cmd.response[0] >> 24);
  147. } else if (flags & (SDSPI_CMD_FLAG_RSP_R3 | SDSPI_CMD_FLAG_RSP_R7)) {
  148. r1_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  149. cmdinfo->response[0] = __builtin_bswap32(hw_cmd.response[0]);
  150. } else if (flags & SDSPI_CMD_FLAG_RSP_R4) {
  151. r1_sdio_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  152. cmdinfo->response[0] = __builtin_bswap32(hw_cmd.response[0]);
  153. } else if (flags & SDSPI_CMD_FLAG_RSP_R5) {
  154. r1_sdio_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  155. cmdinfo->response[0] = hw_cmd.response[0];
  156. }
  157. }
  158. // Save a flag whether the next command is expected to be an app command
  159. if (ret == ESP_OK) {
  160. s_app_cmd = (cmdinfo->opcode == MMC_APP_CMD);
  161. } else {
  162. s_app_cmd = false;
  163. }
  164. _lock_release(&s_lock);
  165. return ret;
  166. }