RX
Dependencies: mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2
SDCard_Y.cpp@0:674f1e460248, 2019-06-05 (annotated)
- Committer:
- TMRL123
- Date:
- Wed Jun 05 00:23:37 2019 +0000
- Revision:
- 0:674f1e460248
Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TMRL123 | 0:674f1e460248 | 1 | #include "SDCard_Y.hh" |
TMRL123 | 0:674f1e460248 | 2 | |
TMRL123 | 0:674f1e460248 | 3 | |
TMRL123 | 0:674f1e460248 | 4 | |
TMRL123 | 0:674f1e460248 | 5 | #define SD_COMMAND_TIMEOUT 5000 |
TMRL123 | 0:674f1e460248 | 6 | |
TMRL123 | 0:674f1e460248 | 7 | #define R1_IDLE_STATE (1 << 0) |
TMRL123 | 0:674f1e460248 | 8 | #define R1_ERASE_RESET (1 << 1) |
TMRL123 | 0:674f1e460248 | 9 | #define R1_ILLEGAL_COMMAND (1 << 2) |
TMRL123 | 0:674f1e460248 | 10 | #define R1_COM_CRC_ERROR (1 << 3) |
TMRL123 | 0:674f1e460248 | 11 | #define R1_ERASE_SEQUENCE_ERROR (1 << 4) |
TMRL123 | 0:674f1e460248 | 12 | #define R1_ADDRESS_ERROR (1 << 5) |
TMRL123 | 0:674f1e460248 | 13 | #define R1_PARAMETER_ERROR (1 << 6) |
TMRL123 | 0:674f1e460248 | 14 | |
TMRL123 | 0:674f1e460248 | 15 | |
TMRL123 | 0:674f1e460248 | 16 | |
TMRL123 | 0:674f1e460248 | 17 | |
TMRL123 | 0:674f1e460248 | 18 | SDCard::SDCard(SPI *t_spi, PinName t_cs) : cs(t_cs) |
TMRL123 | 0:674f1e460248 | 19 | { |
TMRL123 | 0:674f1e460248 | 20 | spi = t_spi; |
TMRL123 | 0:674f1e460248 | 21 | cs = 1; |
TMRL123 | 0:674f1e460248 | 22 | } |
TMRL123 | 0:674f1e460248 | 23 | |
TMRL123 | 0:674f1e460248 | 24 | SDCard::~SDCard() |
TMRL123 | 0:674f1e460248 | 25 | { |
TMRL123 | 0:674f1e460248 | 26 | delete spi; |
TMRL123 | 0:674f1e460248 | 27 | } |
TMRL123 | 0:674f1e460248 | 28 | |
TMRL123 | 0:674f1e460248 | 29 | int SDCard::init(void) |
TMRL123 | 0:674f1e460248 | 30 | { |
TMRL123 | 0:674f1e460248 | 31 | spi->frequency(100000); |
TMRL123 | 0:674f1e460248 | 32 | cs = 1; |
TMRL123 | 0:674f1e460248 | 33 | |
TMRL123 | 0:674f1e460248 | 34 | for( int i=0 ; i<16 ; i++) |
TMRL123 | 0:674f1e460248 | 35 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 36 | |
TMRL123 | 0:674f1e460248 | 37 | if(cmd(0,0) != R1_IDLE_STATE) |
TMRL123 | 0:674f1e460248 | 38 | return -1; |
TMRL123 | 0:674f1e460248 | 39 | |
TMRL123 | 0:674f1e460248 | 40 | int r = cmd8(); |
TMRL123 | 0:674f1e460248 | 41 | |
TMRL123 | 0:674f1e460248 | 42 | if(r == R1_IDLE_STATE){ |
TMRL123 | 0:674f1e460248 | 43 | return init_card_v2(); |
TMRL123 | 0:674f1e460248 | 44 | } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { |
TMRL123 | 0:674f1e460248 | 45 | return init_card_v1(); |
TMRL123 | 0:674f1e460248 | 46 | } else { |
TMRL123 | 0:674f1e460248 | 47 | return -1; |
TMRL123 | 0:674f1e460248 | 48 | } |
TMRL123 | 0:674f1e460248 | 49 | |
TMRL123 | 0:674f1e460248 | 50 | return 0; |
TMRL123 | 0:674f1e460248 | 51 | } |
TMRL123 | 0:674f1e460248 | 52 | |
TMRL123 | 0:674f1e460248 | 53 | int SDCard::init_card_v1() |
TMRL123 | 0:674f1e460248 | 54 | { |
TMRL123 | 0:674f1e460248 | 55 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) |
TMRL123 | 0:674f1e460248 | 56 | { |
TMRL123 | 0:674f1e460248 | 57 | cmd(55, 0); |
TMRL123 | 0:674f1e460248 | 58 | if(cmd(41, 0) == 0) |
TMRL123 | 0:674f1e460248 | 59 | { |
TMRL123 | 0:674f1e460248 | 60 | cdv = 512; |
TMRL123 | 0:674f1e460248 | 61 | return 1; |
TMRL123 | 0:674f1e460248 | 62 | } |
TMRL123 | 0:674f1e460248 | 63 | } |
TMRL123 | 0:674f1e460248 | 64 | |
TMRL123 | 0:674f1e460248 | 65 | return -1; |
TMRL123 | 0:674f1e460248 | 66 | } |
TMRL123 | 0:674f1e460248 | 67 | |
TMRL123 | 0:674f1e460248 | 68 | |
TMRL123 | 0:674f1e460248 | 69 | int SDCard::init_card_v2() |
TMRL123 | 0:674f1e460248 | 70 | { |
TMRL123 | 0:674f1e460248 | 71 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) |
TMRL123 | 0:674f1e460248 | 72 | { |
TMRL123 | 0:674f1e460248 | 73 | wait_ms(50); |
TMRL123 | 0:674f1e460248 | 74 | cmd58(); |
TMRL123 | 0:674f1e460248 | 75 | cmd(55, 0); |
TMRL123 | 0:674f1e460248 | 76 | if (cmd(41, 0x40000000) == 0) |
TMRL123 | 0:674f1e460248 | 77 | { |
TMRL123 | 0:674f1e460248 | 78 | cmd58(); |
TMRL123 | 0:674f1e460248 | 79 | cdv = 1; |
TMRL123 | 0:674f1e460248 | 80 | return 2; |
TMRL123 | 0:674f1e460248 | 81 | } |
TMRL123 | 0:674f1e460248 | 82 | } |
TMRL123 | 0:674f1e460248 | 83 | |
TMRL123 | 0:674f1e460248 | 84 | return -1; |
TMRL123 | 0:674f1e460248 | 85 | } |
TMRL123 | 0:674f1e460248 | 86 | |
TMRL123 | 0:674f1e460248 | 87 | |
TMRL123 | 0:674f1e460248 | 88 | int SDCard::cmd(int cmd, int arg) |
TMRL123 | 0:674f1e460248 | 89 | { |
TMRL123 | 0:674f1e460248 | 90 | cs = 0; |
TMRL123 | 0:674f1e460248 | 91 | spi->write(0x40 | cmd); |
TMRL123 | 0:674f1e460248 | 92 | spi->write(arg >> 24); |
TMRL123 | 0:674f1e460248 | 93 | spi->write(arg >> 16); |
TMRL123 | 0:674f1e460248 | 94 | spi->write(arg >> 8); |
TMRL123 | 0:674f1e460248 | 95 | spi->write(arg >> 0); |
TMRL123 | 0:674f1e460248 | 96 | spi->write(0x95); |
TMRL123 | 0:674f1e460248 | 97 | |
TMRL123 | 0:674f1e460248 | 98 | for( int i=0 ; i<SD_COMMAND_TIMEOUT ; i++) |
TMRL123 | 0:674f1e460248 | 99 | { |
TMRL123 | 0:674f1e460248 | 100 | int respuesta = spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 101 | if( !(respuesta & 0x80) ) |
TMRL123 | 0:674f1e460248 | 102 | { |
TMRL123 | 0:674f1e460248 | 103 | cs = 1; |
TMRL123 | 0:674f1e460248 | 104 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 105 | return respuesta; |
TMRL123 | 0:674f1e460248 | 106 | } |
TMRL123 | 0:674f1e460248 | 107 | } |
TMRL123 | 0:674f1e460248 | 108 | |
TMRL123 | 0:674f1e460248 | 109 | cs = 1; |
TMRL123 | 0:674f1e460248 | 110 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 111 | return -1; |
TMRL123 | 0:674f1e460248 | 112 | |
TMRL123 | 0:674f1e460248 | 113 | } |
TMRL123 | 0:674f1e460248 | 114 | |
TMRL123 | 0:674f1e460248 | 115 | |
TMRL123 | 0:674f1e460248 | 116 | int SDCard::cmd8() |
TMRL123 | 0:674f1e460248 | 117 | { |
TMRL123 | 0:674f1e460248 | 118 | cs = 0; |
TMRL123 | 0:674f1e460248 | 119 | |
TMRL123 | 0:674f1e460248 | 120 | spi->write(0x40 | 8); // CMD8 |
TMRL123 | 0:674f1e460248 | 121 | spi->write(0x00); // reserved |
TMRL123 | 0:674f1e460248 | 122 | spi->write(0x00); // reserved |
TMRL123 | 0:674f1e460248 | 123 | spi->write(0x01); // 3.3v |
TMRL123 | 0:674f1e460248 | 124 | spi->write(0xAA); // check pattern |
TMRL123 | 0:674f1e460248 | 125 | spi->write(0x87); // crc |
TMRL123 | 0:674f1e460248 | 126 | |
TMRL123 | 0:674f1e460248 | 127 | for( int i=0 ; i<SD_COMMAND_TIMEOUT * 1000 ; i++) |
TMRL123 | 0:674f1e460248 | 128 | { |
TMRL123 | 0:674f1e460248 | 129 | char respuesta[5]; |
TMRL123 | 0:674f1e460248 | 130 | respuesta[0] = spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 131 | |
TMRL123 | 0:674f1e460248 | 132 | if( !(respuesta[0] & 0x80)) |
TMRL123 | 0:674f1e460248 | 133 | { |
TMRL123 | 0:674f1e460248 | 134 | for( int j=1; j<5 ; j++) |
TMRL123 | 0:674f1e460248 | 135 | respuesta[i] = spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 136 | |
TMRL123 | 0:674f1e460248 | 137 | cs = 1; |
TMRL123 | 0:674f1e460248 | 138 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 139 | return respuesta[0]; |
TMRL123 | 0:674f1e460248 | 140 | } |
TMRL123 | 0:674f1e460248 | 141 | } |
TMRL123 | 0:674f1e460248 | 142 | |
TMRL123 | 0:674f1e460248 | 143 | cs = 1; |
TMRL123 | 0:674f1e460248 | 144 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 145 | return -1; |
TMRL123 | 0:674f1e460248 | 146 | |
TMRL123 | 0:674f1e460248 | 147 | } |
TMRL123 | 0:674f1e460248 | 148 | |
TMRL123 | 0:674f1e460248 | 149 | |
TMRL123 | 0:674f1e460248 | 150 | |
TMRL123 | 0:674f1e460248 | 151 | int SDCard::cmd58() |
TMRL123 | 0:674f1e460248 | 152 | { |
TMRL123 | 0:674f1e460248 | 153 | cs = 0; |
TMRL123 | 0:674f1e460248 | 154 | |
TMRL123 | 0:674f1e460248 | 155 | int arg = 0; |
TMRL123 | 0:674f1e460248 | 156 | |
TMRL123 | 0:674f1e460248 | 157 | spi->write(0x40 | 58); |
TMRL123 | 0:674f1e460248 | 158 | spi->write(arg >> 24); |
TMRL123 | 0:674f1e460248 | 159 | spi->write(arg >> 16); |
TMRL123 | 0:674f1e460248 | 160 | spi->write(arg >> 8); |
TMRL123 | 0:674f1e460248 | 161 | spi->write(arg >> 0); |
TMRL123 | 0:674f1e460248 | 162 | spi->write(0x95); |
TMRL123 | 0:674f1e460248 | 163 | |
TMRL123 | 0:674f1e460248 | 164 | // wait for the repsonse (response[7] == 0) |
TMRL123 | 0:674f1e460248 | 165 | for(int i = 0; i < SD_COMMAND_TIMEOUT; i++) |
TMRL123 | 0:674f1e460248 | 166 | { |
TMRL123 | 0:674f1e460248 | 167 | int respuesta = spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 168 | |
TMRL123 | 0:674f1e460248 | 169 | if( !(respuesta & 0x80) ) |
TMRL123 | 0:674f1e460248 | 170 | { |
TMRL123 | 0:674f1e460248 | 171 | int ocr = spi->write(0xFF) << 24; |
TMRL123 | 0:674f1e460248 | 172 | ocr |= spi->write(0xFF) << 16; |
TMRL123 | 0:674f1e460248 | 173 | ocr |= spi->write(0xFF) << 8; |
TMRL123 | 0:674f1e460248 | 174 | ocr |= spi->write(0xFF) << 0; |
TMRL123 | 0:674f1e460248 | 175 | cs = 1; |
TMRL123 | 0:674f1e460248 | 176 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 177 | return respuesta; |
TMRL123 | 0:674f1e460248 | 178 | } |
TMRL123 | 0:674f1e460248 | 179 | } |
TMRL123 | 0:674f1e460248 | 180 | cs = 1; |
TMRL123 | 0:674f1e460248 | 181 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 182 | |
TMRL123 | 0:674f1e460248 | 183 | return -1; // timeout |
TMRL123 | 0:674f1e460248 | 184 | } |
TMRL123 | 0:674f1e460248 | 185 | |
TMRL123 | 0:674f1e460248 | 186 | |
TMRL123 | 0:674f1e460248 | 187 | |
TMRL123 | 0:674f1e460248 | 188 | bool SDCard::read(uint8_t *vect, int post) |
TMRL123 | 0:674f1e460248 | 189 | { |
TMRL123 | 0:674f1e460248 | 190 | while( cmd(17,post*512) == -1); |
TMRL123 | 0:674f1e460248 | 191 | |
TMRL123 | 0:674f1e460248 | 192 | cs = 0; |
TMRL123 | 0:674f1e460248 | 193 | |
TMRL123 | 0:674f1e460248 | 194 | while (spi->write(0xFF) != 0xFE); |
TMRL123 | 0:674f1e460248 | 195 | |
TMRL123 | 0:674f1e460248 | 196 | for (uint32_t i = 0; i < 512; i++) |
TMRL123 | 0:674f1e460248 | 197 | *(vect+i) = spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 198 | |
TMRL123 | 0:674f1e460248 | 199 | spi->write(0xFF); // checksum |
TMRL123 | 0:674f1e460248 | 200 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 201 | |
TMRL123 | 0:674f1e460248 | 202 | cs = 1; |
TMRL123 | 0:674f1e460248 | 203 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 204 | |
TMRL123 | 0:674f1e460248 | 205 | while(cmd(12,512) == -1); |
TMRL123 | 0:674f1e460248 | 206 | |
TMRL123 | 0:674f1e460248 | 207 | return 1; |
TMRL123 | 0:674f1e460248 | 208 | } |
TMRL123 | 0:674f1e460248 | 209 | |
TMRL123 | 0:674f1e460248 | 210 | |
TMRL123 | 0:674f1e460248 | 211 | |
TMRL123 | 0:674f1e460248 | 212 | bool SDCard::write(uint8_t *vect, int post) |
TMRL123 | 0:674f1e460248 | 213 | { |
TMRL123 | 0:674f1e460248 | 214 | |
TMRL123 | 0:674f1e460248 | 215 | while( cmd(24,post*512) == -1 ); |
TMRL123 | 0:674f1e460248 | 216 | |
TMRL123 | 0:674f1e460248 | 217 | cs = 0; |
TMRL123 | 0:674f1e460248 | 218 | |
TMRL123 | 0:674f1e460248 | 219 | spi->write(0xFE); |
TMRL123 | 0:674f1e460248 | 220 | |
TMRL123 | 0:674f1e460248 | 221 | for (uint32_t i = 0; i < 512; i++) |
TMRL123 | 0:674f1e460248 | 222 | spi->write(*(vect+i)); |
TMRL123 | 0:674f1e460248 | 223 | |
TMRL123 | 0:674f1e460248 | 224 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 225 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 226 | |
TMRL123 | 0:674f1e460248 | 227 | if( (spi->write(0xFF) & 0x1F) != 0x05 ) |
TMRL123 | 0:674f1e460248 | 228 | { |
TMRL123 | 0:674f1e460248 | 229 | cs = 1; |
TMRL123 | 0:674f1e460248 | 230 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 231 | return 0; |
TMRL123 | 0:674f1e460248 | 232 | } |
TMRL123 | 0:674f1e460248 | 233 | |
TMRL123 | 0:674f1e460248 | 234 | while(spi->write(0xFF) == 0); |
TMRL123 | 0:674f1e460248 | 235 | |
TMRL123 | 0:674f1e460248 | 236 | cs = 1; |
TMRL123 | 0:674f1e460248 | 237 | spi->write(0xFF); |
TMRL123 | 0:674f1e460248 | 238 | |
TMRL123 | 0:674f1e460248 | 239 | while(cmd(12,512) == -1); |
TMRL123 | 0:674f1e460248 | 240 | |
TMRL123 | 0:674f1e460248 | 241 | return 1; |
TMRL123 | 0:674f1e460248 | 242 | |
TMRL123 | 0:674f1e460248 | 243 | } |
TMRL123 | 0:674f1e460248 | 244 |