sdspi_private.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #pragma once
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include "esp_err.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/queue.h"
  20. /// Control tokens used to frame data transfers
  21. /// (see section 7.3.3 of SD simplified spec)
  22. /// Token sent before single/multi block reads and single block writes
  23. #define TOKEN_BLOCK_START 0b11111110
  24. /// Token sent before multi block writes
  25. #define TOKEN_BLOCK_START_WRITE_MULTI 0b11111100
  26. /// Token used to stop multi block write (for reads, CMD12 is used instead)
  27. #define TOKEN_BLOCK_STOP_WRITE_MULTI 0b11111101
  28. /// Data response tokens
  29. /// Mask (high 3 bits are undefined for data response tokens)
  30. #define TOKEN_RSP_MASK 0b11111
  31. /// Data accepted
  32. #define TOKEN_RSP_OK 0b00101
  33. /// Data rejected due to CRC error
  34. #define TOKEN_RSP_CRC_ERR 0b01011
  35. /// Data rejected due to write error
  36. #define TOKEN_RSP_WRITE_ERR 0b01101
  37. /// Data error tokens have format 0b0000xyzw where xyzw are signle bit flags.
  38. /// MASK and VAL are used to check if a token is an error token
  39. #define TOKEN_ERR_MASK 0b11110000
  40. #define TOKEN_ERR_VAL 0b00000000
  41. /// Argument is out of range
  42. #define TOKEN_ERR_RANGE BIT(3)
  43. /// Card internal ECC error
  44. #define TOKEN_ERR_CARD_ECC BIT(2)
  45. /// Card controller error
  46. #define TOKEN_ERR_INTERNAL BIT(1)
  47. /// Card is locked
  48. #define TOKEN_ERR_LOCKED BIT(0)
  49. /// Transfer format in SPI mode. See section 7.3.1.1 of SD simplified spec.
  50. typedef struct {
  51. // These fields form the command sent from host to the card (6 bytes)
  52. uint8_t cmd_index : 6;
  53. uint8_t transmission_bit : 1;
  54. uint8_t start_bit : 1;
  55. uint8_t arguments[4];
  56. uint8_t stop_bit : 1;
  57. uint8_t crc7 : 7;
  58. /// Ncr is the dead time between command and response; should be 0xff
  59. uint8_t ncr;
  60. /// Response data, should be set by host to 0xff for read operations
  61. uint8_t r1;
  62. /// Up to 16 bytes of response. Luckily, this is aligned on 4 byte boundary.
  63. uint32_t response[4];
  64. /// response timeout, in milliseconds
  65. int timeout_ms;
  66. } sdspi_hw_cmd_t;
  67. #define SDSPI_CMD_NORESP_SIZE 6 //!< Size of the command without any response
  68. #define SDSPI_CMD_R1_SIZE 8 //!< Size of the command with R1 response
  69. #define SDSPI_CMD_R2_SIZE 9 //!< Size of the command with R1b response
  70. #define SDSPI_CMD_R3_SIZE 12 //!< Size of the command with R3 response
  71. #define SDSPI_CMD_R7_SIZE 12 //!< Size of the command with R7 response
  72. #define SDSPI_CMD_FLAG_DATA BIT(0) //!< Command has data transfer
  73. #define SDSPI_CMD_FLAG_WRITE BIT(1) //!< Data is written to the card
  74. #define SDSPI_CMD_FLAG_RSP_R1 BIT(2) //!< Response format R1 (1 byte)
  75. #define SDSPI_CMD_FLAG_RSP_R2 BIT(3) //!< Response format R2 (2 bytes)
  76. #define SDSPI_CMD_FLAG_RSP_R3 BIT(4) //!< Response format R3 (5 bytes)
  77. #define SDSPI_CMD_FLAG_RSP_R7 BIT(5) //!< Response format R7 (5 bytes)
  78. #define SDSPI_CMD_FLAG_NORSP BIT(6) //!< Don't expect response (used when sending CMD0 first time).
  79. #define SDSPI_MAX_DATA_LEN 512 //!< Max size of single block transfer
  80. void make_hw_cmd(uint32_t opcode, uint32_t arg, int timeout_ms, sdspi_hw_cmd_t *hw_cmd);
  81. esp_err_t sdspi_host_start_command(int slot, sdspi_hw_cmd_t *cmd,
  82. void *data, uint32_t data_size, int flags);