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