2 * Copyright © 2013 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include "ao_sdcard.h"
22 extern uint8_t ao_radio_mutex;
23 #define get_radio() ao_mutex_get(&ao_radio_mutex)
24 #define put_radio() ao_mutex_put(&ao_radio_mutex)
30 #define ao_sdcard_get_slow() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz); } while (0)
31 #define ao_sdcard_get() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST); } while (0)
32 #define ao_sdcard_put() do { ao_spi_put(AO_SDCARD_SPI_BUS); put_radio(); } while (0)
33 #define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS)
34 #define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS)
35 #define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS)
36 #define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,0)
37 #define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,1)
39 /* Include SD card commands */
40 #define SDCARD_DEBUG 0
43 #define SDCARD_TRACE 0
45 /* Emit error and warning messages */
48 static uint8_t initialized;
49 static uint8_t present;
51 static enum ao_sdtype sdtype;
53 #define ao_sdcard_lock() ao_mutex_get(&mutex)
54 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
57 #define DBG(...) printf(__VA_ARGS__)
59 #define DBG(...) (void) 0
63 #define WARN(...) printf(__VA_ARGS__)
65 #define WARN(...) (void) 0
68 #define later(x,y) ((int16_t) ((x) - (y)) >= 0)
71 * Wait while the card is busy. The card will return a stream of 0xff
72 * when it is ready to accept a command
76 ao_sdcard_wait_busy(void)
78 uint16_t timeout = ao_time() + SDCARD_BUSY_TIMEOUT;
81 ao_sdcard_recv(&reply, 1);
82 DBG("\t\twait busy %02x\n", reply);
85 if (later(ao_time(), timeout)) {
86 WARN("wait busy timeout\n");
95 * Send an SD command and await the status reply
99 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
105 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
107 /* Wait for the card to not be busy */
108 if (cmd != SDCARD_GO_IDLE_STATE) {
109 if (!ao_sdcard_wait_busy())
110 return SDCARD_STATUS_TIMEOUT;
113 data[0] = (cmd & 0x3f) | 0x40;
118 if (cmd == SDCARD_GO_IDLE_STATE)
119 data[5] = 0x95; /* Valid for 0 arg */
120 else if (cmd == SDCARD_SEND_IF_COND)
121 data[5] = 0x87; /* Valid for 0x1aa arg */
123 data[5] = 0xff; /* no CRC */
124 ao_sdcard_send(data, 6);
126 /* The first reply byte will be the status,
127 * which must have the high bit clear
129 timeout = ao_time() + SDCARD_CMD_TIMEOUT;
131 ao_sdcard_recv(&reply, 1);
132 DBG ("\t\tgot byte %02x\n", reply);
133 if ((reply & 0x80) == 0)
135 if (later(ao_time(), timeout)) {
136 WARN("send_cmd %02x timeout\n", cmd);
137 return SDCARD_STATUS_TIMEOUT;
141 if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
142 WARN("send_cmd %d failed %02x\n", cmd, reply);
148 * Retrieve any reply, discarding the trailing CRC byte
151 ao_sdcard_recv_reply(uint8_t *reply, int len)
156 ao_sdcard_recv(reply, len);
158 ao_sdcard_recv(&discard, 1);
162 * Switch to 'idle' state. This is used to get the card into SPI mode
165 ao_sdcard_go_idle_state(void)
169 DBG ("go_idle_state\n");
171 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
172 ao_sdcard_recv_reply(NULL, 0);
173 ao_sdcard_deselect();
174 DBG ("\tgo_idle_state status %02x\n", ret);
179 ao_sdcard_send_op_cond(void)
183 DBG ("send_op_cond\n");
185 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
186 ao_sdcard_recv_reply(NULL, 0);
187 ao_sdcard_deselect();
188 DBG ("\tsend_op_cond %02x\n", ret);
193 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
197 DBG ("send_if_cond\n");
199 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
200 if (ret != SDCARD_STATUS_IDLE_STATE) {
201 DBG ("\tsend_if_cond failed %02x\n", ret);
204 ao_sdcard_recv_reply(send_if_cond_response, 4);
205 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
207 send_if_cond_response[0],
208 send_if_cond_response[1],
209 send_if_cond_response[2],
210 send_if_cond_response[3]);
211 ao_sdcard_deselect();
216 * _ao_sdcard_send_status
218 * Get the 2-byte status value.
220 * Called from other functions with CS held low already,
221 * hence prefixing the name with '_'
224 _ao_sdcard_send_status(void)
229 DBG ("send_status\n");
230 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
231 ao_sdcard_recv_reply(&extra, 1);
232 if (ret != SDCARD_STATUS_READY_STATE)
233 DBG ("\tsend_if_cond failed %02x\n", ret);
234 return ret | (extra << 8);
238 * ao_sdcard_set_blocklen
240 * Set the block length for future read and write commands
243 ao_sdcard_set_blocklen(uint32_t blocklen)
247 DBG ("set_blocklen %d\n", blocklen);
249 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
250 ao_sdcard_recv_reply(NULL, 0);
251 ao_sdcard_deselect();
252 if (ret != SDCARD_STATUS_READY_STATE)
253 DBG ("\tsend_if_cond failed %02x\n", ret);
260 * Send the app command prefix
262 * Called with the CS held low, hence
266 _ao_sdcard_app_cmd(void)
271 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
272 ao_sdcard_recv_reply(NULL, 0);
273 DBG ("\tapp_cmd status %02x\n");
278 ao_sdcard_app_send_op_cond(uint32_t arg)
282 DBG("send_op_comd\n");
284 ret = _ao_sdcard_app_cmd();
285 if (ret != SDCARD_STATUS_IDLE_STATE)
287 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
288 ao_sdcard_recv_reply(NULL, 0);
290 ao_sdcard_deselect();
291 DBG ("\tapp_send_op_cond status %02x\n", ret);
296 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
302 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
303 if (ret != SDCARD_STATUS_READY_STATE)
304 DBG ("\tread_ocr failed %02x\n", ret);
306 ao_sdcard_recv_reply(read_ocr_response, 4);
307 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
308 read_ocr_response[0], read_ocr_response[1],
309 read_ocr_response[2], read_ocr_response[3]);
311 ao_sdcard_deselect();
316 * Follow the flow-chart defined by the SD group to
317 * initialize the card and figure out what kind it is
320 ao_sdcard_setup(void)
324 uint8_t response[10];
326 DBG ("Testing sdcard\n");
328 ao_sdcard_get_slow();
330 * min 74 clocks with CS high
332 ao_sdcard_send_fixed(0xff, 10);
334 /* Reset the card and get it into SPI mode */
335 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
336 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
339 if (i == SDCARD_IDLE_RETRY)
342 /* Figure out what kind of card we have */
343 sdtype = ao_sdtype_unknown;
345 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
349 /* Check for SD version 2 */
350 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
355 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
356 ao_delay(AO_MS_TO_TICKS(10));
357 ret = ao_sdcard_app_send_op_cond(arg);
358 if (ret != SDCARD_STATUS_IDLE_STATE)
361 if (ret != SDCARD_STATUS_READY_STATE) {
363 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
364 ao_delay(AO_MS_TO_TICKS(10));
365 ret = ao_sdcard_send_op_cond();
366 if (ret != SDCARD_STATUS_IDLE_STATE)
369 if (ret != SDCARD_STATUS_READY_STATE)
371 sdtype = ao_sdtype_mmc3;
375 ret = ao_sdcard_read_ocr(response);
376 if (ret != SDCARD_STATUS_READY_STATE)
378 if ((response[0] & 0xc0) == 0xc0)
379 sdtype = ao_sdtype_sd2block;
381 sdtype = ao_sdtype_sd2byte;
383 sdtype = ao_sdtype_sd1;
387 /* For everything but SDHC cards, set the block length */
388 if (sdtype != ao_sdtype_sd2block) {
389 ret = ao_sdcard_set_blocklen(512);
390 if (ret != SDCARD_STATUS_READY_STATE)
391 DBG ("set_blocklen failed, ignoring\n");
395 DBG ("SD card detected, type %d\n", sdtype);
401 _ao_sdcard_reset(void)
405 uint8_t response[10];
407 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
408 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
411 if (i == SDCARD_IDLE_RETRY) {
416 /* Follow the setup path to get the card out of idle state and
417 * up and running again
419 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
421 // uint8_t sdver2 = 0;
423 /* Check for SD version 2 */
424 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
429 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
430 ret = ao_sdcard_app_send_op_cond(arg);
431 if (ret != SDCARD_STATUS_IDLE_STATE)
435 if (ret != SDCARD_STATUS_READY_STATE) {
437 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
438 ret = ao_sdcard_send_op_cond();
439 if (ret != SDCARD_STATUS_IDLE_STATE)
442 if (ret != SDCARD_STATUS_READY_STATE)
446 /* For everything but SDHC cards, set the block length */
447 if (sdtype != ao_sdtype_sd2block) {
448 ret = ao_sdcard_set_blocklen(512);
449 if (ret != SDCARD_STATUS_READY_STATE)
450 DBG ("set_blocklen failed, ignoring\n");
458 * The card will send 0xff until it is ready to send
459 * the data block at which point it will send the START_BLOCK
460 * marker followed by the data. This function waits while
461 * the card is sending 0xff
464 ao_sdcard_wait_block_start(void)
467 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
469 DBG ("\twait_block_start\n");
471 ao_sdcard_recv(&v, 1);
472 DBG("\t\trecv %02x\n", v);
475 if (later(ao_time(), timeout)) {
476 printf ("wait block start timeout\n");
484 * Read a block of 512 bytes from the card
487 ao_sdcard_read_block(uint32_t block, uint8_t *data)
498 if (sdtype != ao_sdtype_unknown)
505 DBG("read block %d\n", block);
506 if (sdtype != ao_sdtype_sd2block)
510 for (tries = 0; tries < 10; tries++) {
513 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
514 ao_sdcard_recv_reply(NULL, 0);
515 if (ret != SDCARD_STATUS_READY_STATE) {
517 WARN ("read block command failed %d status %02x\n", block, ret);
518 status = _ao_sdcard_send_status();
519 WARN ("\tstatus now %04x\n", status);
524 ao_sdcard_send_fixed(0xff, 1);
526 /* Wait for the data start block marker */
527 start_block = ao_sdcard_wait_block_start();
528 if (start_block != SDCARD_DATA_START_BLOCK) {
529 WARN ("wait block start failed %02x\n", start_block);
534 ao_sdcard_recv(data, 512);
535 ao_sdcard_recv(crc, 2);
537 ao_sdcard_deselect();
538 if (ret == SDCARD_STATUS_READY_STATE)
540 if (ret == SDCARD_STATUS_IDLE_STATE) {
541 ret = _ao_sdcard_reset();
542 if (ret != SDCARD_STATUS_READY_STATE)
550 if (ret != SDCARD_STATUS_READY_STATE)
551 WARN("read failed\n");
553 WARN("took %d tries to read %d\n", tries + 1, block);
556 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
557 return ret == SDCARD_STATUS_READY_STATE;
561 * Write a block of 512 bytes to the card
564 ao_sdcard_write_block(uint32_t block, uint8_t *data)
568 uint8_t start_block[8];
576 if (sdtype != ao_sdtype_unknown)
583 DBG("write block %d\n", block);
584 if (sdtype != ao_sdtype_sd2block)
589 for (tries = 0; tries < 10; tries++) {
592 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
593 ao_sdcard_recv_reply(NULL, 0);
594 if (ret != SDCARD_STATUS_READY_STATE)
597 /* Write a pad byte followed by the data start block marker */
598 start_block[0] = 0xff;
599 start_block[1] = SDCARD_DATA_START_BLOCK;
600 ao_sdcard_send(start_block, 2);
603 ao_sdcard_send(data, 512);
606 ao_sdcard_send_fixed(0xff, 2);
608 /* See if the card liked the data */
609 ao_sdcard_recv(response, sizeof (response));
610 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
612 WARN("Data not accepted, response");
613 for (i = 0; i < (int) sizeof (response); i++)
614 WARN(" %02x", response[i]);
620 /* Wait for the bus to go idle (should be done with an interrupt?) */
621 if (!ao_sdcard_wait_busy()) {
626 /* Check the current status after the write completes */
627 status = _ao_sdcard_send_status();
628 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
629 WARN ("send status after write %04x\n", status);
634 ao_sdcard_deselect();
635 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
636 if (ret == SDCARD_STATUS_READY_STATE)
642 WARN("took %d tries to write %d\n", tries + 1, block);
644 return ret == SDCARD_STATUS_READY_STATE;
648 static uint8_t test_data[512];
651 ao_sdcard_test_read(void)
656 if (ao_cmd_status != ao_cmd_success)
659 for (i = 0; i < 100; i++) {
660 printf ("."); flush();
661 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
662 printf ("read error %d\n", i);
667 for (i = 0; i < 18; i++)
668 printf (" %02x", test_data[i]);
673 ao_sdcard_test_write(void)
677 for (i = 0; i < 16; i++) {
679 printf (" %02x", test_data[i]);
682 if (!ao_sdcard_write_block(1, test_data)) {
683 printf ("write error\n");
688 static const struct ao_cmds ao_sdcard_cmds[] = {
689 { ao_sdcard_test_read, "x\0Test read" },
690 { ao_sdcard_test_write, "y\0Test read" },
698 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP);
699 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP);
700 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP);
701 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
703 ao_cmd_register(&ao_sdcard_cmds[0]);