49 #define PRINTF(...) printf(__VA_ARGS__)
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
61 #define GO_IDLE_STATE 0
62 #define SEND_OP_COND 1
64 #define SEND_IF_COND 8
67 #define STOP_TRANSMISSION 12
68 #define SEND_STATUS 13
69 #define READ_SINGLE_BLOCK 17
70 #define WRITE_BLOCK 24
79 #define START_BLOCK_TOKEN 0xfe
82 #define DATA_ACCEPTED 2
83 #define DATA_CRC_ERROR 5
84 #define DATA_WRITE_ERROR 6
86 static uint16_t rw_block_size;
87 static uint16_t block_size;
89 static int read_register(
int register_cmd,
char *buf,
int register_size);
92 send_command(uint8_t cmd, uint32_t argument)
97 req[1] = argument >> 24;
98 req[2] = argument >> 16;
99 req[3] = argument >> 8;
105 sd_arch_spi_write(SPI_IDLE);
106 sd_arch_spi_write_block(req,
sizeof(req));
107 sd_arch_spi_write(SPI_IDLE);
113 get_response(
int length)
117 static uint8_t r[R7];
119 for(i = 0; i < SD_READ_RESPONSE_ATTEMPTS; i++) {
120 x = sd_arch_spi_read();
121 if((x & 0x80) == 0) {
128 if(i == SD_READ_RESPONSE_ATTEMPTS) {
132 for(i = 1; i < length; i++) {
133 r[i] = sd_arch_spi_read();
139 static unsigned char *
140 transaction(
int command,
unsigned long argument,
141 int response_type,
unsigned attempts)
148 for(i = 0; i < attempts; i++) {
150 send_command(command, argument);
151 r = get_response(response_type);
165 unsigned char reg[16];
167 uint8_t *r, read_bl_len;
169 if(sd_arch_init() < 0) {
170 return SD_INIT_ERROR_ARCH;
173 if(SD_CONNECTED() < 0) {
174 return SD_INIT_ERROR_NO_CARD;
177 r = transaction(GO_IDLE_STATE, 0, R1, SD_TRANSACTION_ATTEMPTS);
179 PRINTF(
"Go-idle result: %d\n", r[0]);
181 PRINTF(
"Failed to get go-idle response\n");
184 r = transaction(SEND_IF_COND, 0, R7, SD_TRANSACTION_ATTEMPTS);
186 PRINTF(
"IF cond: %d %d %d %d %d\n", r[0], r[1], r[2], r[3], r[4]);
188 PRINTF(
"failed to get IF cond\n");
189 return SD_INIT_ERROR_NO_IF_COND;
194 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
196 send_command(SEND_OP_COND, 0);
197 r = get_response(R1);
199 if(r !=
NULL && !(r[0] & 1)) {
207 PRINTF(
"OP cond: %d (%d iterations)\n", r[0], i);
209 PRINTF(
"Failed to get OP cond get_response\n");
214 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
216 send_command(READ_OCR, 0);
217 r = get_response(R3);
227 PRINTF(
"OCR: %d %d %d %d %d\n", r[0], r[1], r[2], r[3], r[4]);
231 if(read_register(SEND_CSD, reg,
sizeof(reg)) < 0) {
232 PRINTF(
"Failed to get block size of SD card\n");
233 return SD_INIT_ERROR_NO_BLOCK_SIZE;
236 read_bl_len = reg[5] & 0x0f;
237 block_size = 1 << read_bl_len;
238 rw_block_size = (block_size > SD_DEFAULT_BLOCK_SIZE) ?
239 SD_DEFAULT_BLOCK_SIZE : block_size;
240 PRINTF(
"Found block size %d\n", block_size);
249 sd_write_block(sd_offset_t offset,
char *buf)
254 unsigned char data_response;
255 unsigned char status_code;
259 retval = SD_WRITE_ERROR_NO_CMD_RESPONSE;
260 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
262 send_command(WRITE_BLOCK, offset);
263 r = get_response(R1);
270 if(r !=
NULL && r[0] == 0) {
273 sd_arch_spi_write(START_BLOCK_TOKEN);
276 sd_arch_spi_write_block(buf, rw_block_size);
279 retval = SD_WRITE_ERROR_NO_BLOCK_RESPONSE;
280 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
281 data_response = sd_arch_spi_read();
282 if((data_response & 0x11) == 1) {
284 status_code = (data_response >> 1) & 0x7;
285 if(status_code == DATA_ACCEPTED) {
286 retval = rw_block_size;
288 retval = SD_WRITE_ERROR_PROGRAMMING;
302 read_block(
unsigned read_cmd, sd_offset_t offset,
char *buf,
int len)
312 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
314 send_command(read_cmd, offset);
315 r = get_response(R1);
322 if(r !=
NULL && r[0] == 0) {
325 for(i = 0; i < SD_TRANSACTION_ATTEMPTS; i++) {
326 token = sd_arch_spi_read();
327 if(token == START_BLOCK_TOKEN || (token > 0 && token <= 8)) {
332 if(token == START_BLOCK_TOKEN) {
334 for(i = 0; i < len; i++) {
335 buf[i] = sd_arch_spi_read();
343 }
else if(token > 0 && token <= 8) {
345 retval = SD_READ_ERROR_TOKEN;
348 retval = SD_READ_ERROR_NO_TOKEN;
360 PRINTF(
"status during read: %d\n", r[0]);
363 return SD_READ_ERROR_NO_CMD_RESPONSE;
367 sd_read_block(sd_offset_t offset,
char *buf)
369 return read_block(READ_SINGLE_BLOCK, offset, buf, rw_block_size);
373 read_register(
int register_cmd,
char *buf,
int register_size)
375 return read_block(register_cmd, 0, buf, register_size);
379 sd_get_capacity(
void)
381 unsigned char reg[16];
385 sd_offset_t block_nr;
388 r = read_register(SEND_CSD, reg,
sizeof(reg));
393 c_size = ((reg[6] & 3) << 10) + (reg[7] << 2) + ((reg[8] >> 6) & 3);
394 c_size_mult = ((reg[9] & 3) << 1) + ((reg[10] & 0x80) >> 7);
395 mult = 1 << (c_size_mult + 2);
396 block_nr = (c_size + 1) * mult;
398 return block_nr * block_size;
402 sd_error_string(
int error_code)
407 return "operation succeeded";
408 case SD_INIT_ERROR_NO_CARD:
409 return "no card was found";
410 case SD_INIT_ERROR_ARCH:
411 return "architecture-dependent initialization failed";
412 case SD_INIT_ERROR_NO_IF_COND:
413 return "unable to obtain the interface condition";
414 case SD_INIT_ERROR_NO_BLOCK_SIZE:
415 return "unable to obtain the block size";
416 case SD_WRITE_ERROR_NO_CMD_RESPONSE:
417 return "no response from the card after submitting a write request";
418 case SD_WRITE_ERROR_NO_BLOCK_RESPONSE:
419 return "no response from the card after sending a data block";
420 case SD_WRITE_ERROR_PROGRAMMING:
421 return "the write request failed because of a card error";
422 case SD_WRITE_ERROR_TOKEN:
423 return "the card is not ready to grant a write request";
424 case SD_READ_ERROR_NO_TOKEN:
425 case SD_WRITE_ERROR_NO_TOKEN:
426 return "did not receive a start block token";
427 case SD_READ_ERROR_INVALID_SIZE:
428 return "invalid read block size";
429 case SD_READ_ERROR_TOKEN:
430 return "the card is not ready to read a data block";
431 case SD_READ_ERROR_NO_CMD_RESPONSE:
432 return "no response from the card after submitting a read request";
437 return "unspecified error";
441 sd_write(sd_offset_t offset,
char *buf,
size_t size)
444 uint16_t offset_in_block;
448 char sd_buf[rw_block_size];
451 memset(sd_buf, 0,
sizeof(sd_buf));
453 offset_in_block = offset & (rw_block_size - 1);
456 to_write = MIN(size - written, rw_block_size - offset_in_block);
457 address = (offset + written) & ~(rw_block_size - 1);
459 for(i = 0; i < SD_READ_BLOCK_ATTEMPTS; i++) {
460 r = sd_read_block(address, sd_buf);
461 if(r ==
sizeof(sd_buf)) {
465 if(r !=
sizeof(sd_buf)) {
469 memcpy(&sd_buf[offset_in_block], &buf[written], to_write);
470 r = sd_write_block(address, sd_buf);
471 if(r !=
sizeof(sd_buf)) {
476 }
while(written < size);
482 sd_read(sd_offset_t offset,
char *buf,
size_t size)
486 char sd_buf[rw_block_size];
487 uint16_t offset_in_block;
492 offset_in_block = offset & (rw_block_size - 1);
495 to_read = MIN(size - total_read, rw_block_size - offset_in_block);
496 for(i = 0; i < SD_READ_BLOCK_ATTEMPTS; i++) {
497 r = sd_read_block((offset + total_read) & ~(rw_block_size - 1), sd_buf);
498 if(r ==
sizeof(sd_buf)) {
502 if(r !=
sizeof(sd_buf)) {
506 memcpy(&buf[total_read], &sd_buf[offset_in_block], to_read);
507 total_read += to_read;
509 }
while(total_read < size);