forked from LonelyWolf/stm32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdcard.h
146 lines (124 loc) · 8 KB
/
sdcard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Define to prevent recursive inclusion -------------------------------------
#ifndef __SDCARD_H
#define __SDCARD_H
// SDCARD SPI peripheral
#define SDCARD_SPI_PORT SPI1
// SDCARD GPIO peripherals
#define SDCARD_PORT_PERIPH RCC_AHBPeriph_GPIOB
// SDCARD CS (Chip Select) pin
#define SDCARD_CS_PORT GPIOB
#define SDCARD_CS_PIN GPIO_Pin_6 // PB6
// SDCARD CS pin macros
#define SDCARD_CS_L() SDCARD_CS_PORT->BSRRH = SDCARD_CS_PIN
#define SDCARD_CS_H() SDCARD_CS_PORT->BSRRL = SDCARD_CS_PIN
// SD commands index
#define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
#define SD_CMD_SEND_OP_COND ((uint8_t)1)
#define SD_CMD_ALL_SEND_CID ((uint8_t)2) // Not supported in SPI mode
#define SD_CMD_SEND_REL_ADDR ((uint8_t)3) // Not supported in SPI mode
#define SD_CMD_SET_DSR ((uint8_t)4) // Not supported in SPI mode
#define SD_CMD_SD_CMD_SEN_OP_COND ((uint8_t)5)
#define SD_CMD_HS_SWITCH ((uint8_t)6)
#define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) // Not supported in SPI mode
#define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8)
#define SD_CMD_SEND_CSD ((uint8_t)9)
#define SD_CMD_SEND_CID ((uint8_t)10)
#define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) // Not supported in SPI mode
#define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
#define SD_CMD_SEND_STATUS ((uint8_t)13)
#define SD_CMD_HS_BUSTEST_READ ((uint8_t)14)
#define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) // Not supported in SPI mode
#define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
#define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
#define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
#define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19)
#define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) // Not supported in SPI mode
#define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
#define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
#define SD_CMD_PROG_CSD ((uint8_t)27)
#define SD_CMD_SET_WRITE_PROT ((uint8_t)28) // Not supported in SPI mode
#define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) // Not supported in SPI mode
#define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) // Not supported in SPI mode
#define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) // To set the address of the first write
// block to be erased. (For SD card only)
#define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) // To set the address of the last write block of the
// continuous range to be erased. (For SD card only)
#define SD_CMD_ERASE_GRP_START ((uint8_t)35) // To set the address of the first write block to be erased.
// (For MMC card only spec 3.31)
#define SD_CMD_ERASE_GRP_END ((uint8_t)36) // To set the address of the last write block of the
// continuous range to be erased. (For MMC card only spec 3.31)
#define SD_CMD_ERASE ((uint8_t)38)
#define SD_CMD_FAST_IO ((uint8_t)39) // Not supported in SPI mode
#define SD_CMD_GO_IRQ_STATE ((uint8_t)40) // Not supported in SPI mode
#define SD_CMD_LOCK_UNLOCK ((uint8_t)42)
#define SD_CMD_APP_CMD ((uint8_t)55)
#define SD_CMD_GEN_CMD ((uint8_t)56)
#define SD_CMD_READ_OCR ((uint8_t)58) // Read OCR register
#define SD_CMD_CRC_ON_OFF ((uint8_t)59) // On/Off CRC check by SD Card (in SPI mode)
// Following commands are SD Card Specific commands.
// SD_CMD_APP_CMD should be sent before sending these commands.
#define SD_CMD_SD_APP_OP_COND ((uint8_t)41) // For SD Card only
// Mask for R6 response
#define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
// Control tokens
#define SD_TOKEN_START_BLOCK ((uint8_t)0xfe) // Start block
#define SD_TOKEN_DATA_ACCEPTED ((uint8_t)0x05) // Data accepted
#define SD_TOKEN_WRITE_CRC_ERROR ((uint8_t)0x0b) // Data rejected due to a CRC error
#define SD_TOKEN_WRITE_ERROR ((uint8_t)0x0d) // Data rejected due to a write error
#define SD_TOKEN_READ_ERROR ((uint8_t)0x01) // Data read error
#define SD_TOKEN_READ_CC_ERROR ((uint8_t)0x02) // Internal card controller error
#define SD_TOKEN_READ_ECC_ERROR ((uint8_t)0x04) // Card ECC failed
#define SD_TOKEN_READ_RANGE_ERROR ((uint8_t)0x08) // Read address out of range
// Masks for R1 response
#define SD_R1_IDLE ((uint8_t)0x01) // The card is in idle state
#define SD_R1_ILLEGAL_CMD ((uint8_t)0x04) // Illegal command
#define SD_R1_CRC_ERROR ((uint8_t)0x08) // The CRC check of the last command failed
#define SD_R1_ADDR_ERROR ((uint8_t)0x20) // Incorrect address specified
#define SD_R1_PARAM_ERROR ((uint8_t)0x40) // Parameter error
// SD card response type
typedef enum {
SD_R1 = 0x01, // R1
SD_R1b = 0x02, // R1b
SD_R2 = 0x03, // R2
SD_R3 = 0x04, // R3
SD_R7 = 0x05 // R7
} SDCmdResp_TypeDef;
typedef enum {
SDCT_UNKNOWN = 0x00,
SDCT_SDSC_V1 = 0x01, // Standard capacity SD card v1.0
SDCT_SDSC_V2 = 0x02, // Standard capacity SD card v2.0
SDCT_MMC = 0x03, // MMC
SDCT_SDHC = 0x04 // High capacity SD card (SDHC or SDXC)
} SDCardType_TypeDef;
typedef enum {
SDR_Success = 0x00,
SDR_Timeout = 0x01,
SDR_CRCError = 0x02, // Computed CRC not equal to received from SD card
SDR_ReadError = 0x03, // Read block error (response for CMD17)
SDR_WriteError = 0x04, // Write block error (response for CMD24)
SDR_WriteErrorInternal = 0x05, // Write block error due to internal card error
SDR_Unsupported = 0x06, // Unsupported card found
SDR_BadResponse = 0x07,
SDR_SetBlockSizeFailed = 0x08, // Set block size command failed (response for CMD16)
SDR_UnknownCard = 0x09,
SDR_NoResponse = 0x0a,
SDR_AddrError = 0x0b, // Address error (misaligned or out of bounds)
SDR_WriteCRCError = 0x0c // Data write rejected due to a CRC error
} SDResult_TypeDef;
typedef struct {
SDCardType_TypeDef CardType; // Card type (detected by SD_Init())
uint32_t CardCapacity; // Card capacity (MBytes for SDHC/SDXC, bytes otherwise)
uint32_t CardBlockSize; // SD card block size (bytes), determined in SD_ReadCSD()
uint32_t CardMaxBusClkFreq; // Maximum card bus frequency (MHz)
uint8_t CSD[16]; // SD card CSD register (card structure data)
uint8_t CID[16]; // SD card CID register (card indentification number)
uint8_t CardCSDVer; // SD card CSD register version
} SDCard_TypeDef;
extern SDCard_TypeDef SDCard; // SD card parameters
uint16_t CRC16_buf(const uint8_t * pBuf, uint16_t len);
SDResult_TypeDef SD_Init(void);
SDResult_TypeDef SD_ReadCSD(void);
SDResult_TypeDef SD_ReadCID(void);
SDResult_TypeDef SD_ReadBlock(uint32_t addr, uint8_t *pBuf, uint32_t len);
SDResult_TypeDef SD_WriteBlock(uint32_t addr, uint8_t *pBuf, uint32_t len);
#endif // __SDCARD_H