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
34
35
36
39
40
43
44
47
48
51
57
58
59
68
69
70
71
76
77
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
141
142
143
144
145
146
147
148
149
150
151
154
155
158
159
162
163
164
167
168
169
170
171
172
175
176
177
180
181
184
185
188
189
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
217
218
221
222
225
226
229
/* ... */
#ifndef __STM32_ADAFRUIT_SD_H
#define __STM32_ADAFRUIT_SD_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* ... */
#define __IO volatile
/* ... */
/* ... */
/* ... */
/* ... */
enum {
BSP_SD_OK = 0x00,
MSD_OK = 0x00,
BSP_SD_ERROR = 0x01,
BSP_SD_TIMEOUT
...};
typedef struct
{
uint8_t Reserved1:2;
uint16_t DeviceSize:12;
uint8_t MaxRdCurrentVDDMin:3;
uint8_t MaxRdCurrentVDDMax:3;
uint8_t MaxWrCurrentVDDMin:3;
uint8_t MaxWrCurrentVDDMax:3;
uint8_t DeviceSizeMul:3;
...} struct_v1;
typedef struct
{
uint8_t Reserved1:6;
uint32_t DeviceSize:22;
uint8_t Reserved2:1;
...} struct_v2;
/* ... */
typedef struct
{
uint8_t CSDStruct:2;
uint8_t Reserved1:6;
uint8_t TAAC:8;
uint8_t NSAC:8;
uint8_t MaxBusClkFrec:8;
uint16_t CardComdClasses:12;
uint8_t RdBlockLen:4;
uint8_t PartBlockRead:1;
uint8_t WrBlockMisalign:1;
uint8_t RdBlockMisalign:1;
uint8_t DSRImpl:1;
union csd_version {
struct_v1 v1;
struct_v2 v2;
...} version;
uint8_t EraseSingleBlockEnable:1;
uint8_t EraseSectorSize:7;
uint8_t WrProtectGrSize:7;
uint8_t WrProtectGrEnable:1;
uint8_t Reserved2:2;
uint8_t WrSpeedFact:3;
uint8_t MaxWrBlockLen:4;
uint8_t WriteBlockPartial:1;
uint8_t Reserved3:5;
uint8_t FileFormatGrouop:1;
uint8_t CopyFlag:1;
uint8_t PermWrProtect:1;
uint8_t TempWrProtect:1;
uint8_t FileFormat:2;
uint8_t Reserved4:2;
uint8_t crc:7;
uint8_t Reserved5:1;
...} SD_CSD;
/* ... */
typedef struct
{
uint8_t ManufacturerID;
uint16_t OEM_AppliID;
uint32_t ProdName1;
uint8_t ProdName2;
uint8_t ProdRev;
uint32_t ProdSN;
uint8_t Reserved1;
uint16_t ManufactDate;
uint8_t CID_CRC;
uint8_t Reserved2;
...} SD_CID;
/* ... */
typedef struct
{
SD_CSD Csd;
SD_CID Cid;
uint32_t CardCapacity;
uint32_t CardBlockSize;
uint32_t LogBlockNbr;
uint32_t LogBlockSize;
...} SD_CardInfo;
/* ... */
/* ... */
/* ... */
#define SD_BLOCK_SIZE 0x200
/* ... */
#define SD_PRESENT ((uint8_t)0x01)
#define SD_NOT_PRESENT ((uint8_t)0x00)
#define SD_DATATIMEOUT ((uint32_t)100000000)
/* ... */
#define BSP_SD_CardInfo SD_CardInfo
5 defines
/* ... */
/* ... */
/* ... */
/* ... */
uint8_t BSP_SD_Init(void);
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);
uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr);
uint8_t BSP_SD_GetCardState(void);
uint8_t BSP_SD_GetCardInfo(SD_CardInfo *pCardInfo);
void SD_IO_Init(void);
void SD_IO_CSState(uint8_t state);
void SD_IO_WriteReadData(const uint8_t *DataIn, uint8_t *DataOut, uint16_t DataLength);
uint8_t SD_IO_WriteByte(uint8_t Data);
void HAL_Delay(__IO uint32_t Delay);
#ifdef __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif
/* ... */
/* ... */
/* ... */
/* ... */