sdspi_transaction.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "sys/lock.h"
  18. #include "soc/sdmmc_reg.h"
  19. #include "soc/sdmmc_periph.h"
  20. #include "driver/sdmmc_types.h"
  21. #include "driver/sdmmc_defs.h"
  22. #include "driver/sdmmc_host.h"
  23. #include "sdspi_private.h"
  24. #include "sdspi_crc.h"
  25. static const char* TAG = "sdspi_transaction";
  26. static _lock_t s_lock;
  27. static bool s_app_cmd;
  28. static uint8_t sdspi_msg_crc7(sdspi_hw_cmd_t* hw_cmd)
  29. {
  30. const size_t bytes_to_crc = offsetof(sdspi_hw_cmd_t, arguments) +
  31. sizeof(hw_cmd->arguments); /* can't take address of bit fields */
  32. return sdspi_crc7((const uint8_t *)hw_cmd, bytes_to_crc);
  33. }
  34. void make_hw_cmd(uint32_t opcode, uint32_t arg, int timeout_ms, sdspi_hw_cmd_t *hw_cmd)
  35. {
  36. hw_cmd->start_bit = 0;
  37. hw_cmd->transmission_bit = 1;
  38. hw_cmd->cmd_index = opcode;
  39. hw_cmd->stop_bit = 1;
  40. hw_cmd->r1 = 0xff;
  41. memset(hw_cmd->response, 0xff, sizeof(hw_cmd->response));
  42. hw_cmd->ncr = 0xff;
  43. uint32_t arg_s = __builtin_bswap32(arg);
  44. memcpy(hw_cmd->arguments, &arg_s, sizeof(arg_s));
  45. hw_cmd->crc7 = sdspi_msg_crc7(hw_cmd);
  46. hw_cmd->timeout_ms = timeout_ms;
  47. }
  48. static void r1_response_to_err(uint8_t r1, int cmd, esp_err_t *out_err)
  49. {
  50. if (r1 & SD_SPI_R1_NO_RESPONSE) {
  51. ESP_LOGD(TAG, "cmd=%d, R1 response not found", cmd);
  52. *out_err = ESP_ERR_TIMEOUT;
  53. } else if (r1 & SD_SPI_R1_CMD_CRC_ERR) {
  54. ESP_LOGD(TAG, "cmd=%d, R1 response: command CRC error", cmd);
  55. *out_err = ESP_ERR_INVALID_CRC;
  56. } else if (r1 & SD_SPI_R1_ILLEGAL_CMD) {
  57. ESP_LOGD(TAG, "cmd=%d, R1 response: command not supported", cmd);
  58. *out_err = ESP_ERR_NOT_SUPPORTED;
  59. } else if (r1 & SD_SPI_R1_ADDR_ERR) {
  60. ESP_LOGD(TAG, "cmd=%d, R1 response: alignment error", cmd);
  61. *out_err = ESP_ERR_INVALID_ARG;
  62. } else if (r1 & SD_SPI_R1_PARAM_ERR) {
  63. ESP_LOGD(TAG, "cmd=%d, R1 response: size error", cmd);
  64. *out_err = ESP_ERR_INVALID_SIZE;
  65. } else if ((r1 & SD_SPI_R1_ERASE_RST) ||
  66. (r1 & SD_SPI_R1_ERASE_SEQ_ERR)) {
  67. *out_err = ESP_ERR_INVALID_STATE;
  68. } else if (r1 & SD_SPI_R1_IDLE_STATE) {
  69. // Idle state is handled at command layer
  70. } else if (r1 != 0) {
  71. ESP_LOGD(TAG, "cmd=%d, R1 response: unexpected value 0x%02x", cmd, r1);
  72. *out_err = ESP_ERR_INVALID_RESPONSE;
  73. }
  74. }
  75. esp_err_t sdspi_host_do_transaction(int slot, sdmmc_command_t *cmdinfo)
  76. {
  77. _lock_acquire(&s_lock);
  78. // Convert the command to wire format
  79. sdspi_hw_cmd_t hw_cmd;
  80. make_hw_cmd(cmdinfo->opcode, cmdinfo->arg, cmdinfo->timeout_ms, &hw_cmd);
  81. // Flags indicate which of the transfer types should be used
  82. int flags = 0;
  83. if (SCF_CMD(cmdinfo->flags) == SCF_CMD_ADTC) {
  84. flags = SDSPI_CMD_FLAG_DATA | SDSPI_CMD_FLAG_WRITE;
  85. } else if (SCF_CMD(cmdinfo->flags) == (SCF_CMD_ADTC | SCF_CMD_READ)) {
  86. flags = SDSPI_CMD_FLAG_DATA;
  87. }
  88. // In SD host, response format is encoded using SCF_RSP_* flags which come
  89. // as part of sdmmc_command_t from the upper layer (sdmmc_cmd.c).
  90. // SPI mode uses different command formats. In fact, most of the commands
  91. // use R1 response. Therefore, instead of adding another parallel set of
  92. // response flags for the SPI mode, response format is determined here:
  93. if (!s_app_cmd && cmdinfo->opcode == SD_SEND_IF_COND) {
  94. flags |= SDSPI_CMD_FLAG_RSP_R7;
  95. } else if (!s_app_cmd && cmdinfo->opcode == MMC_SEND_STATUS) {
  96. flags |= SDSPI_CMD_FLAG_RSP_R2;
  97. } else if (!s_app_cmd && cmdinfo->opcode == SD_READ_OCR) {
  98. flags |= SDSPI_CMD_FLAG_RSP_R3;
  99. } else if (s_app_cmd && cmdinfo->opcode == SD_APP_SD_STATUS) {
  100. flags |= SDSPI_CMD_FLAG_RSP_R2;
  101. } else if (!s_app_cmd && cmdinfo->opcode == MMC_GO_IDLE_STATE &&
  102. !(cmdinfo->flags & SCF_RSP_R1)) {
  103. /* used to send CMD0 without expecting a response */
  104. flags |= SDSPI_CMD_FLAG_NORSP;
  105. } else {
  106. flags |= SDSPI_CMD_FLAG_RSP_R1;
  107. }
  108. // Send the command and get the response.
  109. esp_err_t ret = sdspi_host_start_command(slot, &hw_cmd,
  110. cmdinfo->data, cmdinfo->datalen, flags);
  111. // Extract response bytes and store them into cmdinfo structure
  112. if (ret == ESP_OK) {
  113. ESP_LOGV(TAG, "r1 = 0x%02x hw_cmd.r[0]=0x%08x", hw_cmd.r1, hw_cmd.response[0]);
  114. // Some errors should be reported using return code
  115. if (flags & SDSPI_CMD_FLAG_RSP_R1) {
  116. cmdinfo->response[0] = hw_cmd.r1;
  117. r1_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  118. } else if (flags & SDSPI_CMD_FLAG_RSP_R2) {
  119. cmdinfo->response[0] = (((uint32_t)hw_cmd.r1) << 8) | (hw_cmd.response[0] >> 24);
  120. } else if (flags & (SDSPI_CMD_FLAG_RSP_R3 | SDSPI_CMD_FLAG_RSP_R7)) {
  121. r1_response_to_err(hw_cmd.r1, cmdinfo->opcode, &ret);
  122. cmdinfo->response[0] = __builtin_bswap32(hw_cmd.response[0]);
  123. }
  124. }
  125. // Save a flag whether the next command is expected to be an app command
  126. if (ret == ESP_OK) {
  127. s_app_cmd = (cmdinfo->opcode == MMC_APP_CMD);
  128. } else {
  129. s_app_cmd = false;
  130. }
  131. _lock_release(&s_lock);
  132. return ret;
  133. }