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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include "ao_sdcard.h"
23 extern uint8_t ao_radio_mutex;
24 #define get_radio() ao_mutex_get(&ao_radio_mutex)
25 #define put_radio() ao_mutex_put(&ao_radio_mutex)
31 #define ao_sdcard_get_slow() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz); } while (0)
32 #define ao_sdcard_get() do { get_radio(); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST); } while (0)
33 #define ao_sdcard_put() do { ao_spi_put(AO_SDCARD_SPI_BUS); put_radio(); } while (0)
34 #define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS)
35 #define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS)
36 #define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS)
37 #define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,0)
38 #define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,1)
40 /* Include SD card commands */
42 #define SDCARD_DEBUG 0
47 #define SDCARD_TRACE 0
50 /* Emit error and warning messages */
55 static uint8_t initialized;
56 static uint8_t present;
58 static enum ao_sdtype sdtype;
60 #define ao_sdcard_lock() ao_mutex_get(&mutex)
61 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
64 #define DBG(...) printf(__VA_ARGS__)
66 #define DBG(...) (void) 0
70 #define WARN(...) printf(__VA_ARGS__)
72 #define WARN(...) (void) 0
75 #define later(x,y) ((int16_t) ((x) - (y)) >= 0)
78 * Wait while the card is busy. The card will return a stream of 0xff
79 * when it is ready to accept a command
83 ao_sdcard_wait_busy(void)
85 uint16_t timeout = ao_time() + SDCARD_BUSY_TIMEOUT;
88 ao_sdcard_recv(&reply, 1);
89 DBG("\t\twait busy %02x\n", reply);
92 if (later(ao_time(), timeout)) {
93 WARN("wait busy timeout\n");
102 * Send an SD command and await the status reply
106 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
112 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
114 /* Wait for the card to not be busy */
115 if (cmd != SDCARD_GO_IDLE_STATE) {
116 if (!ao_sdcard_wait_busy())
117 return SDCARD_STATUS_TIMEOUT;
120 data[0] = (cmd & 0x3f) | 0x40;
125 if (cmd == SDCARD_GO_IDLE_STATE)
126 data[5] = 0x95; /* Valid for 0 arg */
127 else if (cmd == SDCARD_SEND_IF_COND)
128 data[5] = 0x87; /* Valid for 0x1aa arg */
130 data[5] = 0xff; /* no CRC */
131 ao_sdcard_send(data, 6);
133 /* The first reply byte will be the status,
134 * which must have the high bit clear
136 timeout = ao_time() + SDCARD_CMD_TIMEOUT;
138 ao_sdcard_recv(&reply, 1);
139 DBG ("\t\tgot byte %02x\n", reply);
140 if ((reply & 0x80) == 0)
142 if (later(ao_time(), timeout)) {
143 WARN("send_cmd %02x timeout\n", cmd);
144 return SDCARD_STATUS_TIMEOUT;
148 if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
149 WARN("send_cmd %d failed %02x\n", cmd, reply);
155 * Retrieve any reply, discarding the trailing CRC byte
158 ao_sdcard_recv_reply(uint8_t *reply, int len)
163 ao_sdcard_recv(reply, len);
165 ao_sdcard_recv(&discard, 1);
169 * Switch to 'idle' state. This is used to get the card into SPI mode
172 ao_sdcard_go_idle_state(void)
176 DBG ("go_idle_state\n");
178 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
179 ao_sdcard_recv_reply(NULL, 0);
180 ao_sdcard_deselect();
181 DBG ("\tgo_idle_state status %02x\n", ret);
186 ao_sdcard_send_op_cond(void)
190 DBG ("send_op_cond\n");
192 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
193 ao_sdcard_recv_reply(NULL, 0);
194 ao_sdcard_deselect();
195 DBG ("\tsend_op_cond %02x\n", ret);
200 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
204 DBG ("send_if_cond\n");
206 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
207 if (ret != SDCARD_STATUS_IDLE_STATE) {
208 DBG ("\tsend_if_cond failed %02x\n", ret);
211 ao_sdcard_recv_reply(send_if_cond_response, 4);
212 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
214 send_if_cond_response[0],
215 send_if_cond_response[1],
216 send_if_cond_response[2],
217 send_if_cond_response[3]);
218 ao_sdcard_deselect();
223 * _ao_sdcard_send_status
225 * Get the 2-byte status value.
227 * Called from other functions with CS held low already,
228 * hence prefixing the name with '_'
231 _ao_sdcard_send_status(void)
236 DBG ("send_status\n");
237 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
238 ao_sdcard_recv_reply(&extra, 1);
239 if (ret != SDCARD_STATUS_READY_STATE)
240 DBG ("\tsend_if_cond failed %02x\n", ret);
241 return ret | (extra << 8);
245 * ao_sdcard_set_blocklen
247 * Set the block length for future read and write commands
250 ao_sdcard_set_blocklen(uint32_t blocklen)
254 DBG ("set_blocklen %d\n", blocklen);
256 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
257 ao_sdcard_recv_reply(NULL, 0);
258 ao_sdcard_deselect();
259 if (ret != SDCARD_STATUS_READY_STATE)
260 DBG ("\tsend_if_cond failed %02x\n", ret);
267 * Send the app command prefix
269 * Called with the CS held low, hence
273 _ao_sdcard_app_cmd(void)
278 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
279 ao_sdcard_recv_reply(NULL, 0);
280 DBG ("\tapp_cmd status %02x\n");
285 ao_sdcard_app_send_op_cond(uint32_t arg)
289 DBG("send_op_comd\n");
291 ret = _ao_sdcard_app_cmd();
292 if (ret != SDCARD_STATUS_IDLE_STATE)
294 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
295 ao_sdcard_recv_reply(NULL, 0);
297 ao_sdcard_deselect();
298 DBG ("\tapp_send_op_cond status %02x\n", ret);
303 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
309 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
310 if (ret != SDCARD_STATUS_READY_STATE)
311 DBG ("\tread_ocr failed %02x\n", ret);
313 ao_sdcard_recv_reply(read_ocr_response, 4);
314 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
315 read_ocr_response[0], read_ocr_response[1],
316 read_ocr_response[2], read_ocr_response[3]);
318 ao_sdcard_deselect();
323 * Follow the flow-chart defined by the SD group to
324 * initialize the card and figure out what kind it is
327 ao_sdcard_setup(void)
331 uint8_t response[10];
333 DBG ("Testing sdcard\n");
335 ao_sdcard_get_slow();
337 * min 74 clocks with CS high
339 ao_sdcard_send_fixed(0xff, 10);
341 /* Reset the card and get it into SPI mode */
342 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
343 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
346 if (i == SDCARD_IDLE_RETRY)
349 /* Figure out what kind of card we have */
350 sdtype = ao_sdtype_unknown;
352 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
356 /* Check for SD version 2 */
357 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
362 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
363 ao_delay(AO_MS_TO_TICKS(10));
364 ret = ao_sdcard_app_send_op_cond(arg);
365 if (ret != SDCARD_STATUS_IDLE_STATE)
368 if (ret != SDCARD_STATUS_READY_STATE) {
370 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
371 ao_delay(AO_MS_TO_TICKS(10));
372 ret = ao_sdcard_send_op_cond();
373 if (ret != SDCARD_STATUS_IDLE_STATE)
376 if (ret != SDCARD_STATUS_READY_STATE)
378 sdtype = ao_sdtype_mmc3;
382 ret = ao_sdcard_read_ocr(response);
383 if (ret != SDCARD_STATUS_READY_STATE)
385 if ((response[0] & 0xc0) == 0xc0)
386 sdtype = ao_sdtype_sd2block;
388 sdtype = ao_sdtype_sd2byte;
390 sdtype = ao_sdtype_sd1;
394 /* For everything but SDHC cards, set the block length */
395 if (sdtype != ao_sdtype_sd2block) {
396 ret = ao_sdcard_set_blocklen(512);
397 if (ret != SDCARD_STATUS_READY_STATE)
398 DBG ("set_blocklen failed, ignoring\n");
402 DBG ("SD card detected, type %d\n", sdtype);
408 _ao_sdcard_reset(void)
412 uint8_t response[10];
414 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
415 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
418 if (i == SDCARD_IDLE_RETRY) {
423 /* Follow the setup path to get the card out of idle state and
424 * up and running again
426 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
428 // uint8_t sdver2 = 0;
430 /* Check for SD version 2 */
431 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
436 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
437 ret = ao_sdcard_app_send_op_cond(arg);
438 if (ret != SDCARD_STATUS_IDLE_STATE)
442 if (ret != SDCARD_STATUS_READY_STATE) {
444 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
445 ret = ao_sdcard_send_op_cond();
446 if (ret != SDCARD_STATUS_IDLE_STATE)
449 if (ret != SDCARD_STATUS_READY_STATE)
453 /* For everything but SDHC cards, set the block length */
454 if (sdtype != ao_sdtype_sd2block) {
455 ret = ao_sdcard_set_blocklen(512);
456 if (ret != SDCARD_STATUS_READY_STATE)
457 DBG ("set_blocklen failed, ignoring\n");
465 * The card will send 0xff until it is ready to send
466 * the data block at which point it will send the START_BLOCK
467 * marker followed by the data. This function waits while
468 * the card is sending 0xff
471 ao_sdcard_wait_block_start(void)
474 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
476 DBG ("\twait_block_start\n");
478 ao_sdcard_recv(&v, 1);
479 DBG("\t\trecv %02x\n", v);
482 if (later(ao_time(), timeout)) {
483 printf ("wait block start timeout\n");
491 * Read a block of 512 bytes from the card
494 ao_sdcard_read_block(uint32_t block, uint8_t *data)
505 if (sdtype != ao_sdtype_unknown)
512 DBG("read block %d\n", block);
513 if (sdtype != ao_sdtype_sd2block)
517 for (tries = 0; tries < 10; tries++) {
520 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
521 ao_sdcard_recv_reply(NULL, 0);
522 if (ret != SDCARD_STATUS_READY_STATE) {
524 WARN ("read block command failed %d status %02x\n", block, ret);
525 status = _ao_sdcard_send_status();
526 WARN ("\tstatus now %04x\n", status);
531 ao_sdcard_send_fixed(0xff, 1);
533 /* Wait for the data start block marker */
534 start_block = ao_sdcard_wait_block_start();
535 if (start_block != SDCARD_DATA_START_BLOCK) {
536 WARN ("wait block start failed %02x\n", start_block);
541 ao_sdcard_recv(data, 512);
542 ao_sdcard_recv(crc, 2);
544 ao_sdcard_deselect();
545 if (ret == SDCARD_STATUS_READY_STATE)
547 if (ret == SDCARD_STATUS_IDLE_STATE) {
548 ret = _ao_sdcard_reset();
549 if (ret != SDCARD_STATUS_READY_STATE)
557 if (ret != SDCARD_STATUS_READY_STATE)
558 WARN("read failed\n");
560 WARN("took %d tries to read %d\n", tries + 1, block);
563 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
564 return ret == SDCARD_STATUS_READY_STATE;
568 * Write a block of 512 bytes to the card
571 ao_sdcard_write_block(uint32_t block, uint8_t *data)
575 uint8_t start_block[8];
583 if (sdtype != ao_sdtype_unknown)
590 DBG("write block %d\n", block);
591 if (sdtype != ao_sdtype_sd2block)
596 for (tries = 0; tries < 10; tries++) {
599 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
600 ao_sdcard_recv_reply(NULL, 0);
601 if (ret != SDCARD_STATUS_READY_STATE)
604 /* Write a pad byte followed by the data start block marker */
605 start_block[0] = 0xff;
606 start_block[1] = SDCARD_DATA_START_BLOCK;
607 ao_sdcard_send(start_block, 2);
610 ao_sdcard_send(data, 512);
613 ao_sdcard_send_fixed(0xff, 2);
615 /* See if the card liked the data */
616 ao_sdcard_recv(response, sizeof (response));
617 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
619 WARN("Data not accepted, response");
620 for (i = 0; i < (int) sizeof (response); i++)
621 WARN(" %02x", response[i]);
627 /* Wait for the bus to go idle (should be done with an interrupt?) */
628 if (!ao_sdcard_wait_busy()) {
633 /* Check the current status after the write completes */
634 status = _ao_sdcard_send_status();
635 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
636 WARN ("send status after write %04x\n", status);
641 ao_sdcard_deselect();
642 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
643 if (ret == SDCARD_STATUS_READY_STATE)
649 WARN("took %d tries to write %d\n", tries + 1, block);
651 return ret == SDCARD_STATUS_READY_STATE;
655 static uint8_t test_data[512];
658 ao_sdcard_test_read(void)
663 if (ao_cmd_status != ao_cmd_success)
666 for (i = 0; i < 100; i++) {
667 printf ("."); flush();
668 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
669 printf ("read error %d\n", i);
674 for (i = 0; i < 18; i++)
675 printf (" %02x", test_data[i]);
680 ao_sdcard_test_write(void)
684 for (i = 0; i < 16; i++) {
686 printf (" %02x", test_data[i]);
689 if (!ao_sdcard_write_block(1, test_data)) {
690 printf ("write error\n");
695 static const struct ao_cmds ao_sdcard_cmds[] = {
696 { ao_sdcard_test_read, "x\0Test read" },
697 { ao_sdcard_test_write, "y\0Test read" },
705 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP);
706 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP);
707 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP);
708 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
710 ao_cmd_register(&ao_sdcard_cmds[0]);