RX

Dependencies:   mbed BufferedSerial SX1276GenericLib X_NUCLEO_IKS01A2

Committer:
TMRL123
Date:
Wed Jun 05 00:25:53 2019 +0000
Revision:
0:fb7bf6d81e5f
RX

Who changed what in which revision?

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