embkernel
 All Classes Functions Variables Typedefs Groups Pages
FsDriveSdSpi.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_SPI_HPP_
7 #define FS_DRIVE_SPI_HPP_
8 
9 #include "FsDrive.hpp"
10 #include "RtosInclude.hpp"
11 #include "DrvSpi.hpp"
12 
13 class FsDriveSdSpi: public FsDrive {
14  friend class RtosInterrupt;
15 
16 private:
17 
18  typedef struct {
19  uint8_t cmdId :6;
20  uint8_t cmdRsp :1;
21  uint8_t start :1;
22  uint8_t args[4];
23  uint8_t stop :1; //lsb
24  uint8_t crc :7;
25  }__attribute__((packed)) CMD_RESP;
26 
27  typedef union {
28  struct {
29  uint8_t idle :1; //lsb
30  uint8_t eraseReset :1;
31  uint8_t illeagalCmd :1;
32  uint8_t crcError :1;
33  uint8_t eraseSeqError :1;
34  uint8_t addressError :1;
35  uint8_t parameterError :1;
36  uint8_t zero :1;
37  } bits;
38  uint8_t value;
39  }__attribute__((packed)) RESP_R1;
40 
41  typedef struct {
42  uint8_t res1 :4;
43  uint8_t cmdVersion :4;
44  uint8_t res2;
45  uint8_t voltageAccepted :4;
46  uint8_t res3 :4;
47  uint8_t checkPattern;
48  }__attribute__((packed)) RESP_R7;
49 
50  typedef struct {
51  uint8_t switchTo1_8Accepted;
52  }__attribute__((packed)) OCR;
53 
54  typedef enum
55  :uint8_t {
56  START_SINGLE_BLOCK_WRITE = 0xFE,
57  START_MULTIPLE_BLOCK_WRITE = 0xFC,
58  STOP_TRANSMISSION = 0xFD,
59  } CTRL_TOKEN;
60 
61  const static uint8_t RESP_R1_IDLE_VALUE = 0x01;
62 
63 public:
64  FsDriveSdSpi(DrvSpi* spi);
65  ~FsDriveSdSpi();
66 
67 private:
68  static FsDriveSdSpi* sInstances[];
69 
70  DrvSpi* mSpi;
71  RESP_R1 sendCmd(uint8_t cmd, uint32_t arg);
72  bool waitReady();
73 
74  bool readDataBlock(uint8_t* buffer, unsigned int size);
75  bool writeDataBlock(const uint8_t* buffer, unsigned int size, bool isMultipleBlock);
76  bool internalInit();
77  bool internalReadBlocks(FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
78  bool internalWriteBlocks(const FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
79 protected:
80  virtual bool init();
81  virtual bool readBlocks(FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
82  virtual bool writeBlocks(const FsDefs::SECTOR* buffer, unsigned int blockNumber, unsigned int blockCount);
83 
84 private:
85  const static uint8_t CMD0_GO_IDLE_STATE = 0; //Resp R1
86  const static uint8_t CMD1_SEND_OP_COND = 1; //Resp R1
87  const static uint8_t CMD6_SWITCH_FUNC = 6; //Resp R1
88  const static uint8_t CMD8_SEND_IF_COND = 8; //Resp R7
89  const static uint8_t CMD9_SEND_CSD = 9; //Resp R1
90  const static uint8_t CMD10_SEND_CID = 10; //Resp R1
91  const static uint8_t CMD12_STOP_TRANSMISSION = 12; //Resp R1b
92  const static uint8_t CMD13_SEND_STATUS = 13; //Resp R2
93  const static uint8_t CMD16_SET_BLOCKLEN = 16; //Resp R1
94  const static uint8_t CMD17_READ_SINGLE_BLOCK = 17; //Resp R1
95  const static uint8_t CMD18_READ_MULTIPLE_BLOCK = 18; //Resp R1
96  const static uint8_t CMD24_WRITE_BLOCK = 24; //Resp R1
97  const static uint8_t CMD25_WRITE_MULTIPLE_BLOCK = 25; //Resp R1
98  const static uint8_t CMD27_PROGRAM_CSD = 27; //Resp R1
99  const static uint8_t CMD28_SET_WRITE_PROT = 28; //Resp R1b
100  const static uint8_t CMD29_CLR_WRITE_PROT = 29; //Resp R1b
101  const static uint8_t CMD30_SEND_WRITE_PROT = 30; //Resp R1
102  const static uint8_t CMD32_ERASE_WR_BLK_START_ADDR = 32; //Resp R1
103  const static uint8_t CMD33_ERASE_WR_BLK_END_ADDR = 33; //Resp R1
104  const static uint8_t CMD38_ERASE = 38; //Resp R1b
105  const static uint8_t CMD42_LOCK_UNLOCK = 42; //Resp R1
106  const static uint8_t CMD55_APP_CMD = 55; //Resp R1
107  const static uint8_t CMD56_GEN_CMD = 56; //Resp R1
108  const static uint8_t CMD58_READ_OCR = 58; //Resp R3
109  const static uint8_t CMD59_CRC_ON_OFF = 59; //Resp R1
110  const static uint8_t ACMD13_SD_STATUS = 13; //Resp R2
111  const static uint8_t ACMD22_SEND_NUM_WR_BLOCKS = 22; //Resp R1
112  const static uint8_t ACMD23_SET_WR_BLK_ERASE_COUNT = 23; //Resp R1
113  const static uint8_t ACMD41_SD_SEND_OP_COND = 41; //Resp R1
114  const static uint8_t ACMD42_SET_CLR_CARD_DETECT = 42; //Resp R1
115  const static uint8_t ACMD51_SEND_SCR = 51; //Resp R1
116 };
117 
118 #endif /* FS_DRIVE_SPI_HPP_ */