dev_spi_msd.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-04-17 Bernard first version.
  9. */
  10. #ifndef __DEV_SPI_MSD_H_INCLUDED__
  11. #define __DEV_SPI_MSD_H_INCLUDED__
  12. #include <stdint.h>
  13. #include <rtdevice.h>
  14. #include "drivers/dev_spi.h"
  15. /* SD command (SPI mode) */
  16. #define GO_IDLE_STATE 0 /* CMD0 R1 */
  17. #define SEND_OP_COND 1 /* CMD1 R1 */
  18. #define SWITCH_FUNC 6 /* CMD6 R1 */
  19. #define SEND_IF_COND 8 /* CMD8 R7 */
  20. #define SEND_CSD 9 /* CMD9 R1 */
  21. #define SEND_CID 10 /* CMD10 R1 */
  22. #define STOP_TRANSMISSION 12 /* CMD12 R1B */
  23. #define SEND_STATUS 13 /* CMD13 R2 */
  24. #define SET_BLOCKLEN 16 /* CMD16 R1 */
  25. #define READ_SINGLE_BLOCK 17 /* CMD17 R1 */
  26. #define READ_MULTIPLE_BLOCK 18 /* CMD18 R1 */
  27. #define WRITE_BLOCK 24 /* CMD24 R1 */
  28. #define WRITE_MULTIPLE_BLOCK 25 /* CMD25 R1 */
  29. #define PROGRAM_CSD 27 /* CMD27 R1 */
  30. #define SET_WRITE_PROT 28 /* CMD28 R1B */
  31. #define CLR_WRITE_PROT 29 /* CMD29 R1B */
  32. #define SEND_WRITE_PROT 30 /* CMD30 R1 */
  33. #define ERASE_WR_BLK_START_ADDR 32 /* CMD32 R1 */
  34. #define ERASE_WR_BLK_END_ADDR 33 /* CMD33 R1 */
  35. #define ERASE 38 /* CMD38 R1B */
  36. #define LOCK_UNLOCK 42 /* CMD42 R1 */
  37. #define APP_CMD 55 /* CMD55 R1 */
  38. #define GEN_CMD 56 /* CMD56 R1 */
  39. #define READ_OCR 58 /* CMD58 R3 */
  40. #define CRC_ON_OFF 59 /* CMD59 R1 */
  41. /* Application-Specific Command */
  42. #define SD_STATUS 13 /* ACMD13 R2 */
  43. #define SEND_NUM_WR_BLOCKS 22 /* ACMD22 R1 */
  44. #define SET_WR_BLK_ERASE_COUNT 23 /* ACMD23 R1 */
  45. #define SD_SEND_OP_COND 41 /* ACMD41 R1 */
  46. #define SET_CLR_CARD_DETECT 42 /* ACMD42 R1 */
  47. #define SEND_SCR 51 /* ACMD51 R1 */
  48. /* Start Data tokens */
  49. /* Tokens (necessary because at nop/idle (and CS active) only 0xff is on the data/command line) */
  50. #define MSD_TOKEN_READ_START 0xFE /* Data token start byte, Start Single Block Read */
  51. #define MSD_TOKEN_WRITE_SINGLE_START 0xFE /* Data token start byte, Start Single Block Write */
  52. #define MSD_TOKEN_WRITE_MULTIPLE_START 0xFC /* Data token start byte, Start Multiple Block Write */
  53. #define MSD_TOKEN_WRITE_MULTIPLE_STOP 0xFD /* Data toke stop byte, Stop Multiple Block Write */
  54. /* MSD reponses and error flags */
  55. #define MSD_RESPONSE_NO_ERROR 0x00
  56. #define MSD_IN_IDLE_STATE 0x01
  57. #define MSD_ERASE_RESET 0x02
  58. #define MSD_ILLEGAL_COMMAND 0x04
  59. #define MSD_COM_CRC_ERROR 0x08
  60. #define MSD_ERASE_SEQUENCE_ERROR 0x10
  61. #define MSD_ADDRESS_ERROR 0x20
  62. #define MSD_PARAMETER_ERROR 0x40
  63. #define MSD_RESPONSE_FAILURE 0xFF
  64. /* Data response error */
  65. #define MSD_DATA_OK 0x05
  66. #define MSD_DATA_CRC_ERROR 0x0B
  67. #define MSD_DATA_WRITE_ERROR 0x0D
  68. #define MSD_DATA_OTHER_ERROR 0xFF
  69. #define MSD_DATA_RESPONSE_MASK 0x1F
  70. #define MSD_GET_DATA_RESPONSE(res) (res & MSD_DATA_RESPONSE_MASK)
  71. #define MSD_CMD_LEN 6 /**< command, arg and crc. */
  72. #define MSD_RESPONSE_MAX_LEN 5 /**< response max len */
  73. #define MSD_CSD_LEN 16 /**< SD crad CSD register len */
  74. #define SECTOR_SIZE 512 /**< sector size, default 512byte */
  75. /* card try timeout, unit: ms */
  76. #define CARD_TRY_TIMES 3000
  77. #define CARD_TRY_TIMES_ACMD41 800
  78. #define CARD_WAIT_TOKEN_TIMES 800
  79. #define MSD_USE_PRE_ERASED /**< id define MSD_USE_PRE_ERASED, before CMD25, send ACMD23 */
  80. /**
  81. * SD/MMC card type
  82. */
  83. typedef enum
  84. {
  85. MSD_CARD_TYPE_UNKNOWN = 0, /**< unknown */
  86. MSD_CARD_TYPE_MMC, /**< MultiMedia Card */
  87. MSD_CARD_TYPE_SD_V1_X, /**< Ver 1.X Standard Capacity SD Memory Card */
  88. MSD_CARD_TYPE_SD_V2_X, /**< Ver 2.00 or later Standard Capacity SD Memory Card */
  89. MSD_CARD_TYPE_SD_SDHC, /**< High Capacity SD Memory Card */
  90. MSD_CARD_TYPE_SD_SDXC, /**< later Extended Capacity SD Memory Card */
  91. }msd_card_type;
  92. typedef enum
  93. {
  94. response_type_unknown = 0,
  95. response_r1,
  96. response_r1b,
  97. response_r2,
  98. response_r3,
  99. response_r4,
  100. response_r5,
  101. response_r7,
  102. }response_type;
  103. struct msd_device
  104. {
  105. struct rt_device parent; /**< RT-Thread device struct */
  106. struct rt_device_blk_geometry geometry; /**< sector size, sector count */
  107. struct rt_spi_device * spi_device; /**< SPI interface */
  108. msd_card_type card_type; /**< card type: MMC SD1.x SD2.0 SDHC SDXC */
  109. uint32_t max_clock; /**< MAX SPI clock */
  110. };
  111. extern rt_err_t msd_init(const char * sd_device_name, const char * spi_device_name);
  112. #endif // __DEV_SPI_MSD_H_INCLUDED