embkernel
 All Classes Functions Variables Typedefs Groups Pages
FsDriveSdio.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef FS_DRIVE_SDIO_HPP_
7 #define FS_DRIVE_SDIO_HPP_
8 
9 #include "FsDrive.hpp"
10 #include "RtosInclude.hpp"
11 
12 class FsDriveSdio: public FsDrive {
13  friend class RtosInterrupt;
14 
15 private:
16  //Structures as defined in: "Physical Layer Simplified Specification Version 3.01"
17  typedef enum {
18  RESP_NONE,
19  RESP_1,
20  RESP_2,
21  RESP_3,
22  RESP_4,
23  RESP_5,
24  RESP_6,
25  RESP_7
26  } RESPONSE;
27 
28  typedef enum{
29  SD_STATE_IDLE=0,
30  SD_STATE_READY=1,
31  SD_STATE_IDENT=2,
32  SD_STATE_STBY=3,
33  SD_STATE_TRAN=4,
34  SD_STATE_DATA=5,
35  SD_STATE_RCV=6,
36  SD_STATE_PRG=7,
37  SD_STATE_DIS=8,
38  }SD_STATE; //4.10 page 75
39 
40  typedef union {
41  struct {
42  uint32_t res1 :3;
43  uint32_t akeSeqError :1;
44  uint32_t res2 :1;
45  uint32_t appCmd :1;
46  uint32_t res3 :2;
47 
48  uint32_t readyForData :1;
49  SD_STATE currentState :4;
50  uint32_t eraseReset :1;
51  uint32_t cardEccDisabled :1;
52  uint32_t wpEraseSkip :1;
53 
54  uint32_t csdOverwrite :1;
55  uint32_t res4 :2;
56  uint32_t error :1;
57  uint32_t ccError :1;
58  uint32_t cardEccFailed :1;
59  uint32_t illegalCommand :1;
60  uint32_t comCrcError :1;
61 
62  uint32_t lockUnlockFailed :1;
63  uint32_t cardIsLocked :1;
64  uint32_t wpViolation :1;
65  uint32_t eraseParam :1;
66  uint32_t eraseSeqError :1;
67  uint32_t blockLenError :1;
68  uint32_t addressError :1;
69  uint32_t outOfRange :1;
70  }bits;
71  uint32_t value;
72  }RESP_STATUS; //4.10 page 75
73 
74  const static uint32_t RESP_STATUS_ERROR_BITS = 0xFDFFE008;
75 
76  typedef struct {
77  union {
78  struct {
79  uint8_t unused :1;
80  uint8_t crc :7;
81 
82  uint8_t manufacturingDate[2];
83  uint8_t productSerial[4];
84  uint8_t productRevision;
85  uint8_t productName[5];
86  uint8_t oemId[2];
87  uint8_t manufacturerId;
88  }bits;
89  uint32_t values[4];
90  };
91  }__attribute__((packed)) REG_CID;
92 
93  REG_CID mCid;
94 
95  typedef union {
96  struct {
97  struct {
98  uint8_t res1 :3;
99  uint8_t akeSeqError :1;
100  uint8_t res2 :1;
101  uint8_t appCmd :1;
102  uint8_t res3 :2;
103 
104  uint8_t readyForData :1;
105  uint8_t currentState :4;
106  uint8_t error :1;
107  uint8_t illegalCommand :1;
108  uint8_t comCrcError :1;
109  }status;
110  uint16_t newRca;
111  }bits;
112  uint32_t value;
113  }RESP_RCA;
114 
115  RESP_RCA mRca;
116 
117  //uint16_t mRca;
118 
119  typedef struct {
120  union {
121  struct {
122  uint32_t unused :1;
123  uint32_t crc :7;
124  uint32_t res1 :2;
125  uint32_t fileFormat :2;
126  uint32_t tmpWriteProtect :1;
127  uint32_t permWriteProtect :1;
128  uint32_t copy :1;
129  uint32_t fileFormatGrp :1;
130  uint32_t res2 :5;
131  uint32_t writeBlPartial :1;
132  uint32_t writeBlLen :4;
133  uint32_t r2wFactor :3;
134  uint32_t res3 :2;
135  uint32_t wpGrpEnable :1;
136 
137  uint32_t wpGrpSize :7;
138  uint32_t sectorSize :7;
139  uint32_t eraseBlkEnable :1;
140  uint32_t cSizeMult :3;
141  uint32_t vddWrCurrMax :3;
142  uint32_t vddWrCurrMin :3;
143  uint32_t vddRdCurrMax :3;
144  uint32_t vddRdCurrMin :3;
145  uint32_t cSizeLo :2;
146 
147  uint32_t cSizeHi :10;
148  uint32_t res4 :2;
149  uint32_t dsrImp :1;
150  uint32_t readBlkMisalign :1;
151  uint32_t writeBlkMissalign :1;
152  uint32_t readBlPartial :1;
153  uint32_t readBlLen :4;
154  uint32_t ccc :12;
155 
156  uint32_t tranSpeed :8;
157  uint32_t nsac :8;
158  uint32_t taac :8;
159  uint32_t res5 :6;
160  uint32_t csdStruct :2;
161  }bitsV1;
162  struct {
163  uint32_t unused :1;
164  uint32_t crc :7;
165  uint32_t res1 :2;
166  uint32_t fileFormat :2;
167  uint32_t tmpWriteProtect :1;
168  uint32_t permWriteProtect :1;
169  uint32_t copy :1;
170  uint32_t fileFormatGrp :1;
171  uint32_t res2 :5;
172  uint32_t writeBlPartial :1;
173  uint32_t writeBlLen :4;
174  uint32_t r2wFactor :3;
175  uint32_t res3 :2;
176  uint32_t wpGrpEnable :1;
177 
178  uint32_t wpGrpSize :7;
179  uint32_t sectorSize :7;
180  uint32_t eraseBlkEnable :1;
181  uint32_t res4 :1;
182  uint32_t cSizeLo :16;
183 
184  uint32_t cSizeHi :6;
185  uint32_t res5 :6;
186  uint32_t dsrImp :1;
187  uint32_t readBlkMisalign :1;
188  uint32_t writeBlkMissalign :1;
189  uint32_t readBlPartial :1;
190  uint32_t readBlLen :4;
191  uint32_t ccc :12;
192 
193  uint32_t tranSpeed :8;
194  uint32_t nsac :8;
195  uint32_t taac :8;
196  uint32_t res6 :6;
197  uint32_t csdStruct :2;
198  }bitsV2;
199  uint32_t values[4];
200  };
201  }__attribute__((packed)) REG_CSD;
202 
203  REG_CSD mCsd;
204 
205  typedef union {
206  struct {
207  uint32_t commandSupportBits :2;
208  uint32_t res2 :9;
209  uint32_t extendedSecuritySupport :4;
210  uint32_t sdSpec3 :1;
211  uint32_t datBusWidthSupport :4;
212  uint32_t cprmSecuritySuppport :3;
213  uint32_t dataStatusAfterErases :1;
214  uint32_t sdSpecVersion :4;
215  uint32_t scrStructure :4;
216 
217  uint32_t res1;
218  }bits;
219  uint32_t value[2];
220  }__attribute__((packed)) RESP_SCR;
221 
222  RESP_SCR mScr;
223 
224  const static uint32_t RESP_RCA_STATUS_ERROR_BITS = 0x0000E000;
225 
226  struct {
227  union {
228  uint32_t isSdhc :1;
229  uint32_t spare :31;
230  }bits;
231  uint32_t value;
232  }mFlags;
233 
234 public:
235  FsDriveSdio();
236  ~FsDriveSdio();
237 
238 private:
239  static FsDriveSdio* sInstances[];
240 
241  RtosSemaphore mSemaInt;
242 
243  void onIntSdio();
244 
245  bool waitInterrupt(uint32_t* status);
246  bool sendCmd(uint32_t arg, uint32_t flags, RESPONSE response = RESP_NONE);
247  bool getState(SD_STATE* state);
248  void dataTransferConfig(uint32_t timer, uint32_t datalen, uint32_t ctrlFlags);
249 
250 protected:
251  virtual bool init();
252  virtual bool readBlocks(FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
253  virtual bool writeBlocks(const FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
254 
255 private:
256  const static uint32_t SDIO_STATIC_FLAGS = ((uint32_t) 0x000005FF);
257  //CMD
258  const static uint8_t CMD_GO_IDLE_STATE = ((uint8_t) 0);//
259  const static uint8_t CMD_SEND_OP_COND = ((uint8_t) 1);//
260  const static uint8_t CMD_ALL_SEND_CID = ((uint8_t) 2);//
261  const static uint8_t CMD_SET_REL_ADDR = ((uint8_t) 3);// SDIO_SEND_REL_ADDR for SD Card
262  const static uint8_t CMD_SET_DSR = ((uint8_t) 4);//
263  const static uint8_t CMD_SDIO_SEN_OP_COND = ((uint8_t) 5);//
264  const static uint8_t CMD_HS_SWITCH = ((uint8_t) 6);//
265  const static uint8_t CMD_SEL_DESEL_CARD = ((uint8_t) 7);//
266  const static uint8_t CMD_HS_SEND_EXT_CSD = ((uint8_t) 8);//
267  const static uint8_t CMD_SEND_CSD = ((uint8_t) 9);//
268  const static uint8_t CMD_SEND_CID = ((uint8_t) 10);//
269  const static uint8_t CMD_READ_DAT_UNTIL_STOP = ((uint8_t) 11);// SD Card doesn't support it
270  const static uint8_t CMD_STOP_TRANSMISSION = ((uint8_t) 12);//
271  const static uint8_t CMD_SEND_STATUS = ((uint8_t) 13);//
272  const static uint8_t CMD_HS_BUSTEST_READ = ((uint8_t) 14);//
273  const static uint8_t CMD_GO_INACTIVE_STATE = ((uint8_t) 15);//
274  const static uint8_t CMD_SET_BLOCKLEN = ((uint8_t) 16);//
275  const static uint8_t CMD_READ_SINGLE_BLOCK = ((uint8_t) 17);//
276  const static uint8_t CMD_READ_MULT_BLOCK = ((uint8_t) 18);//
277  const static uint8_t CMD_HS_BUSTEST_WRITE = ((uint8_t) 19);//
278  const static uint8_t CMD_WRITE_DAT_UNTIL_STOP = ((uint8_t) 20);// SD Card doesn't support it
279  const static uint8_t CMD_SET_BLOCK_COUNT = ((uint8_t) 23);// SD Card doesn't support it
280  const static uint8_t CMD_WRITE_SINGLE_BLOCK = ((uint8_t) 24);//
281  const static uint8_t CMD_WRITE_MULT_BLOCK = ((uint8_t) 25);//
282  const static uint8_t CMD_PROG_CID = ((uint8_t) 26);// reserved for manufacturers
283  const static uint8_t CMD_PROG_CSD = ((uint8_t) 27);//
284  const static uint8_t CMD_SET_WRITE_PROT = ((uint8_t) 28);//
285  const static uint8_t CMD_CLR_WRITE_PROT = ((uint8_t) 29);//
286  const static uint8_t CMD_SEND_WRITE_PROT = ((uint8_t) 30);//
287  const static uint8_t CMD_SD_ERASE_GRP_START = ((uint8_t) 32);// To set the address of the first write block to be erased. (For SD card only)
288  const static uint8_t 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)
289  const static uint8_t 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)
290  const static uint8_t 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)
291  const static uint8_t CMD_ERASE = ((uint8_t) 38);//
292  const static uint8_t CMD_FAST_IO = ((uint8_t) 39);// SD Card doesn't support it
293  const static uint8_t CMD_GO_IRQ_STATE = ((uint8_t) 40);// SD Card doesn't support it
294  const static uint8_t CMD_LOCK_UNLOCK = ((uint8_t) 42);//
295  const static uint8_t CMD_APP_CMD = ((uint8_t) 55);//
296  const static uint8_t CMD_GEN_CMD = ((uint8_t) 56);//
297  const static uint8_t CMD_NO_CMD = ((uint8_t) 64);//
298  const static uint8_t CMD_APP_SD_SET_BUSWIDTH = ((uint8_t) 6);// For SD Card only
299  const static uint8_t CMD_SD_APP_STAUS = ((uint8_t) 13);// For SD Card only
300  const static uint8_t CMD_SD_APP_SEND_NUM_WRITE_BLOCKS = ((uint8_t) 22);// For SD Card only
301  const static uint8_t CMD_SD_APP_OP_COND = ((uint8_t) 41);// For SD Card only
302  const static uint8_t CMD_SD_APP_SET_CLR_CARD_DETECT = ((uint8_t) 42);// For SD Card only
303  const static uint8_t CMD_SD_APP_SEND_SCR = ((uint8_t) 51);// For SD Card only
304  const static uint8_t CMD_SDIO_RW_DIRECT = ((uint8_t) 52);// For SD I/O Card only
305  const static uint8_t CMD_SDIO_RW_EXTENDED = ((uint8_t) 53);// For SD I/O Card only
306  const static uint8_t CMDA_SD_APP_GET_MKB = ((uint8_t) 43);// For SD Card only
307  const static uint8_t CMD_SD_APP_GET_MID = ((uint8_t) 44);// For SD Card only
308  const static uint8_t CMD_SD_APP_SET_CER_RN1 = ((uint8_t) 45);// For SD Card only
309  const static uint8_t CMD_SD_APP_GET_CER_RN2 = ((uint8_t) 46);// For SD Card only
310  const static uint8_t CMD_SD_APP_SET_CER_RES2 = ((uint8_t) 47);// For SD Card only
311  const static uint8_t CMD_SD_APP_GET_CER_RES1 = ((uint8_t) 48);// For SD Card only
312  const static uint8_t CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK = ((uint8_t) 18);// For SD Card only
313  const static uint8_t CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK = ((uint8_t) 25);// For SD Card only
314  const static uint8_t CMD_SD_APP_SECURE_ERASE = ((uint8_t) 38);// For SD Card only
315  const static uint8_t CMD_SD_APP_CHANGE_SECURE_AREA = ((uint8_t) 49);// For SD Card only
316  const static uint8_t CMD_SD_APP_SECURE_WRITE_MKB = ((uint8_t) 48);// For SD Card only
317 
318  const static uint32_t SD_R6_GENERAL_UNKNOWN_ERROR = ((uint32_t) 0x00002000);
319  const static uint32_t SD_R6_ILLEGAL_CMD = ((uint32_t) 0x00004000);
320  const static uint32_t SD_R6_COM_CRC_FAILED = ((uint32_t) 0x00008000);
321 
322  const static uint32_t SD_VOLTAGE_WINDOW_SD = ((uint32_t) 0x80100000);
323  const static uint32_t SD_HIGH_CAPACITY = ((uint32_t) 0x40000000);
324  const static uint32_t SD_STD_CAPACITY = ((uint32_t) 0x00000000);
325  const static uint32_t SD_CHECK_PATTERN = ((uint32_t) 0x000001AA);
326 
327  const static uint32_t SD_MAX_VOLT_TRIAL = ((uint32_t) 0x0000FFFF);
328  const static uint32_t SD_ALLZERO = ((uint32_t) 0x00000000);
329 
330  const static uint32_t SD_WIDE_BUS_SUPPORT = ((uint32_t) 0x00040000);
331  const static uint32_t SD_SINGLE_BUS_SUPPORT = ((uint32_t) 0x00010000);
332  const static uint32_t SD_CARD_LOCKED = ((uint32_t) 0x02000000);
333 
334  const static uint32_t SD_DATATIMEOUT = ((uint32_t) 0xFFFFFFFF);
335  const static uint32_t SD_0TO7BITS = ((uint32_t) 0x000000FF);
336  const static uint32_t SD_8TO15BITS = ((uint32_t) 0x0000FF00);
337  const static uint32_t SD_16TO23BITS = ((uint32_t) 0x00FF0000);
338  const static uint32_t SD_24TO31BITS = ((uint32_t) 0xFF000000);
339  const static uint32_t SD_MAX_DATA_LENGTH = ((uint32_t) 0x01FFFFFF);
340 
341  const static uint32_t SD_HALFFIFO = ((uint32_t) 0x00000008);
342  const static uint32_t SD_HALFFIFOBYTES = ((uint32_t) 0x00000020);
343 
344  const static uint32_t SD_CCCC_LOCK_UNLOCK = ((uint32_t) 0x00000080);
345  const static uint32_t SD_CCCC_WRITE_PROT = ((uint32_t) 0x00000040);
346  const static uint32_t SD_CCCC_ERASE = ((uint32_t) 0x00000020);
347 
348  const static uint32_t SDIO_SEND_IF_COND = ((uint32_t) 0x00000008);
349 
350  const static uint32_t SDIO_IT_CCRCFAIL = 0x00000001;
351  const static uint32_t SDIO_IT_DCRCFAIL = 0x00000002;
352  const static uint32_t SDIO_IT_CTIMEOUT = 0x00000004;
353  const static uint32_t SDIO_IT_DTIMEOUT = 0x00000008;
354  const static uint32_t SDIO_IT_TXUNDERR = 0x00000010;
355  const static uint32_t SDIO_IT_RXOVERR = 0x00000020;
356  const static uint32_t SDIO_IT_CMDREND = 0x00000040;
357  const static uint32_t SDIO_IT_CMDSENT = 0x00000080;
358  const static uint32_t SDIO_IT_DATAEND = 0x00000100;
359  const static uint32_t SDIO_IT_STBITERR = 0x00000200;
360  const static uint32_t SDIO_IT_DBCKEND = 0x00000400;
361  const static uint32_t SDIO_IT_CMDACT = 0x00000800;
362  const static uint32_t SDIO_IT_TXACT = 0x00001000;
363  const static uint32_t SDIO_IT_RXACT = 0x00002000;
364  const static uint32_t SDIO_IT_TXFIFOHE = 0x00004000;
365  const static uint32_t SDIO_IT_RXFIFOHF = 0x00008000;
366  const static uint32_t SDIO_IT_TXFIFOF = 0x00010000;
367  const static uint32_t SDIO_IT_RXFIFOF = 0x00020000;
368  const static uint32_t SDIO_IT_TXFIFOE = 0x00040000;
369  const static uint32_t SDIO_IT_RXFIFOE = 0x00080000;
370  const static uint32_t SDIO_IT_TXDAVL = 0x00100000;
371  const static uint32_t SDIO_IT_RXDAVL = 0x00200000;
372  const static uint32_t SDIO_IT_SDIOIT = 0x00400000;
373  const static uint32_t SDIO_IT_CEATAEND = 0x00800000;
374 };
375 
376 #endif /* FS_DRIVE_SDIO_HPP_ */