sdspi_private.h 4.7 KB

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