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__)
63 #define WARN(...) printf(__VA_ARGS__)
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)
106 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
108 /* Wait for the card to not be busy */
109 if (cmd != SDCARD_GO_IDLE_STATE) {
110 if (!ao_sdcard_wait_busy())
111 return SDCARD_STATUS_TIMEOUT;
114 data[0] = cmd & 0x3f | 0x40;
119 if (cmd == SDCARD_GO_IDLE_STATE)
120 data[5] = 0x95; /* Valid for 0 arg */
121 else if (cmd == SDCARD_SEND_IF_COND)
122 data[5] = 0x87; /* Valid for 0x1aa arg */
124 data[5] = 0xff; /* no CRC */
125 ao_sdcard_send(data, 6);
127 /* The first reply byte will be the status,
128 * which must have the high bit clear
130 timeout = ao_time() + SDCARD_CMD_TIMEOUT;
132 ao_sdcard_recv(&reply, 1);
133 DBG ("\t\tgot byte %02x\n", reply);
134 if ((reply & 0x80) == 0)
136 if (later(ao_time(), timeout)) {
137 WARN("send_cmd %02x timeout\n", cmd);
138 return SDCARD_STATUS_TIMEOUT;
142 if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
143 WARN("send_cmd %d failed %02x\n", cmd, reply);
149 * Retrieve any reply, discarding the trailing CRC byte
152 ao_sdcard_recv_reply(uint8_t *reply, int len)
157 ao_sdcard_recv(reply, len);
159 ao_sdcard_recv(&discard, 1);
163 * Switch to 'idle' state. This is used to get the card into SPI mode
166 ao_sdcard_go_idle_state(void)
170 DBG ("go_idle_state\n");
172 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
173 ao_sdcard_recv_reply(NULL, 0);
174 ao_sdcard_deselect();
175 DBG ("\tgo_idle_state status %02x\n", ret);
180 ao_sdcard_send_op_cond(void)
184 DBG ("send_op_cond\n");
186 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
187 ao_sdcard_recv_reply(NULL, 0);
188 ao_sdcard_deselect();
189 DBG ("\tsend_op_cond %02x\n", ret);
194 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
198 DBG ("send_if_cond\n");
200 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
201 if (ret != SDCARD_STATUS_IDLE_STATE) {
202 DBG ("\tsend_if_cond failed %02x\n", ret);
205 ao_sdcard_recv_reply(send_if_cond_response, 4);
206 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
208 send_if_cond_response[0],
209 send_if_cond_response[1],
210 send_if_cond_response[2],
211 send_if_cond_response[3]);
212 ao_sdcard_deselect();
217 * _ao_sdcard_send_status
219 * Get the 2-byte status value.
221 * Called from other functions with CS held low already,
222 * hence prefixing the name with '_'
225 _ao_sdcard_send_status(void)
230 DBG ("send_status\n");
231 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
232 ao_sdcard_recv_reply(&extra, 1);
233 if (ret != SDCARD_STATUS_READY_STATE)
234 DBG ("\tsend_if_cond failed %02x\n", ret);
235 return ret | (extra << 8);
239 * ao_sdcard_set_blocklen
241 * Set the block length for future read and write commands
244 ao_sdcard_set_blocklen(uint32_t blocklen)
248 DBG ("set_blocklen %d\n", blocklen);
250 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
251 ao_sdcard_recv_reply(NULL, 0);
252 ao_sdcard_deselect();
253 if (ret != SDCARD_STATUS_READY_STATE)
254 DBG ("\tsend_if_cond failed %02x\n", ret);
261 * Send the app command prefix
263 * Called with the CS held low, hence
267 _ao_sdcard_app_cmd(void)
272 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
273 ao_sdcard_recv_reply(NULL, 0);
274 DBG ("\tapp_cmd status %02x\n");
279 ao_sdcard_app_send_op_cond(uint32_t arg)
283 DBG("send_op_comd\n");
285 ret = _ao_sdcard_app_cmd();
286 if (ret != SDCARD_STATUS_IDLE_STATE)
288 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
289 ao_sdcard_recv_reply(NULL, 0);
291 ao_sdcard_deselect();
292 DBG ("\tapp_send_op_cond status %02x\n", ret);
297 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
303 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
304 if (ret != SDCARD_STATUS_READY_STATE)
305 DBG ("\tread_ocr failed %02x\n", ret);
307 ao_sdcard_recv_reply(read_ocr_response, 4);
308 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
309 read_ocr_response[0], read_ocr_response[1],
310 read_ocr_response[2], read_ocr_response[3]);
312 ao_sdcard_deselect();
317 * Follow the flow-chart defined by the SD group to
318 * initialize the card and figure out what kind it is
321 ao_sdcard_setup(void)
325 uint8_t response[10];
327 DBG ("Testing sdcard\n");
329 ao_sdcard_get_slow();
331 * min 74 clocks with CS high
333 ao_sdcard_send_fixed(0xff, 10);
335 /* Reset the card and get it into SPI mode */
336 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
337 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
340 if (i == SDCARD_IDLE_RETRY)
343 /* Figure out what kind of card we have */
344 sdtype = ao_sdtype_unknown;
346 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
350 /* Check for SD version 2 */
351 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
356 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
357 ao_delay(AO_MS_TO_TICKS(10));
358 ret = ao_sdcard_app_send_op_cond(arg);
359 if (ret != SDCARD_STATUS_IDLE_STATE)
362 if (ret != SDCARD_STATUS_READY_STATE) {
364 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
365 ao_delay(AO_MS_TO_TICKS(10));
366 ret = ao_sdcard_send_op_cond();
367 if (ret != SDCARD_STATUS_IDLE_STATE)
370 if (ret != SDCARD_STATUS_READY_STATE)
372 sdtype = ao_sdtype_mmc3;
376 ret = ao_sdcard_read_ocr(response);
377 if (ret != SDCARD_STATUS_READY_STATE)
379 if ((response[0] & 0xc0) == 0xc0)
380 sdtype = ao_sdtype_sd2block;
382 sdtype = ao_sdtype_sd2byte;
384 sdtype = ao_sdtype_sd1;
388 /* For everything but SDHC cards, set the block length */
389 if (sdtype != ao_sdtype_sd2block) {
390 ret = ao_sdcard_set_blocklen(512);
391 if (ret != SDCARD_STATUS_READY_STATE)
392 DBG ("set_blocklen failed, ignoring\n");
396 DBG ("SD card detected, type %d\n", sdtype);
402 _ao_sdcard_reset(void)
406 uint8_t response[10];
408 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
409 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
412 if (i == SDCARD_IDLE_RETRY) {
417 /* Follow the setup path to get the card out of idle state and
418 * up and running again
420 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
424 /* Check for SD version 2 */
425 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
430 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
431 ret = ao_sdcard_app_send_op_cond(arg);
432 if (ret != SDCARD_STATUS_IDLE_STATE)
436 if (ret != SDCARD_STATUS_READY_STATE) {
438 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
439 ret = ao_sdcard_send_op_cond();
440 if (ret != SDCARD_STATUS_IDLE_STATE)
443 if (ret != SDCARD_STATUS_READY_STATE)
447 /* For everything but SDHC cards, set the block length */
448 if (sdtype != ao_sdtype_sd2block) {
449 ret = ao_sdcard_set_blocklen(512);
450 if (ret != SDCARD_STATUS_READY_STATE)
451 DBG ("set_blocklen failed, ignoring\n");
459 * The card will send 0xff until it is ready to send
460 * the data block at which point it will send the START_BLOCK
461 * marker followed by the data. This function waits while
462 * the card is sending 0xff
465 ao_sdcard_wait_block_start(void)
468 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
470 DBG ("\twait_block_start\n");
472 ao_sdcard_recv(&v, 1);
473 DBG("\t\trecv %02x\n", v);
476 if (later(ao_time(), timeout)) {
477 printf ("wait block start timeout\n");
485 * Read a block of 512 bytes from the card
488 ao_sdcard_read_block(uint32_t block, uint8_t *data)
499 if (sdtype != ao_sdtype_unknown)
506 DBG("read block %d\n", block);
507 if (sdtype != ao_sdtype_sd2block)
511 for (tries = 0; tries < 10; tries++) {
514 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
515 ao_sdcard_recv_reply(NULL, 0);
516 if (ret != SDCARD_STATUS_READY_STATE) {
518 WARN ("read block command failed %d status %02x\n", block, ret);
519 status = _ao_sdcard_send_status();
520 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];
570 static uint8_t check_data[512];
578 if (sdtype != ao_sdtype_unknown)
585 DBG("write block %d\n", block);
586 if (sdtype != ao_sdtype_sd2block)
591 for (tries = 0; tries < 10; tries++) {
594 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
595 ao_sdcard_recv_reply(NULL, 0);
596 if (ret != SDCARD_STATUS_READY_STATE)
599 /* Write a pad byte followed by the data start block marker */
600 start_block[0] = 0xff;
601 start_block[1] = SDCARD_DATA_START_BLOCK;
602 ao_sdcard_send(start_block, 2);
605 ao_sdcard_send(data, 512);
608 ao_sdcard_send_fixed(0xff, 2);
610 /* See if the card liked the data */
611 ao_sdcard_recv(response, sizeof (response));
612 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
614 WARN("Data not accepted, response");
615 for (i = 0; i < sizeof (response); i++)
616 WARN(" %02x", response[i]);
622 /* Wait for the bus to go idle (should be done with an interrupt?) */
623 if (!ao_sdcard_wait_busy()) {
628 /* Check the current status after the write completes */
629 status = _ao_sdcard_send_status();
630 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
631 WARN ("send status after write %04x\n", status);
636 ao_sdcard_deselect();
637 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
638 if (ret == SDCARD_STATUS_READY_STATE)
644 WARN("took %d tries to write %d\n", tries + 1, block);
646 return ret == SDCARD_STATUS_READY_STATE;
650 static uint8_t test_data[512];
653 ao_sdcard_test_read(void)
658 if (ao_cmd_status != ao_cmd_success)
661 for (i = 0; i < 100; i++) {
662 printf ("."); flush();
663 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
664 printf ("read error %d\n", i);
669 for (i = 0; i < 18; i++)
670 printf (" %02x", test_data[i]);
675 ao_sdcard_test_write(void)
679 for (i = 0; i < 16; i++) {
681 printf (" %02x", test_data[i]);
684 if (!ao_sdcard_write_block(1, test_data)) {
685 printf ("write error\n");
690 static const struct ao_cmds ao_sdcard_cmds[] = {
691 { ao_sdcard_test_read, "x\0Test read" },
692 { ao_sdcard_test_write, "y\0Test read" },
700 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP);
701 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP);
702 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP);
703 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
705 ao_cmd_register(&ao_sdcard_cmds[0]);