sdspi_private.h 4.3 KB

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