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"
21 extern uint8_t ao_radio_mutex;
23 #define ao_sdcard_get_slow() do { ao_mutex_get(&ao_radio_mutex); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_250kHz); } while (0)
24 #define ao_sdcard_get() do { ao_mutex_get(&ao_radio_mutex); ao_spi_get(AO_SDCARD_SPI_BUS, AO_SPI_SPEED_FAST); } while (0)
25 #define ao_sdcard_put() do { ao_spi_put(AO_SDCARD_SPI_BUS); ao_mutex_put(&ao_radio_mutex); } while (0)
26 #define ao_sdcard_send_fixed(d,l) ao_spi_send_fixed((d), (l), AO_SDCARD_SPI_BUS)
27 #define ao_sdcard_send(d,l) ao_spi_send((d), (l), AO_SDCARD_SPI_BUS)
28 #define ao_sdcard_recv(d,l) ao_spi_recv((d), (l), AO_SDCARD_SPI_BUS)
29 #define ao_sdcard_select() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,0)
30 #define ao_sdcard_deselect() ao_gpio_set(AO_SDCARD_SPI_CS_PORT,AO_SDCARD_SPI_CS_PIN,AO_SDCARD_SPI_CS,1)
32 /* Include SD card commands */
33 #define SDCARD_DEBUG 0
36 #define SDCARD_TRACE 0
38 /* Emit error and warning messages */
41 static uint8_t initialized;
42 static uint8_t present;
44 static enum ao_sdtype sdtype;
46 #define ao_sdcard_lock() ao_mutex_get(&mutex)
47 #define ao_sdcard_unlock() ao_mutex_put(&mutex)
50 #define DBG(...) printf(__VA_ARGS__)
56 #define WARN(...) printf(__VA_ARGS__)
61 #define later(x,y) ((int16_t) ((x) - (y)) >= 0)
64 * Wait while the card is busy. The card will return a stream of 0xff
65 * when it is ready to accept a command
69 ao_sdcard_wait_busy(void)
71 uint16_t timeout = ao_time() + SDCARD_BUSY_TIMEOUT;
74 ao_sdcard_recv(&reply, 1);
75 DBG("\t\twait busy %02x\n", reply);
78 if (later(ao_time(), timeout)) {
79 WARN("wait busy timeout\n");
88 * Send an SD command and await the status reply
92 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
99 DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
101 /* Wait for the card to not be busy */
102 if (cmd != SDCARD_GO_IDLE_STATE) {
103 if (!ao_sdcard_wait_busy())
104 return SDCARD_STATUS_TIMEOUT;
107 data[0] = cmd & 0x3f | 0x40;
112 if (cmd == SDCARD_GO_IDLE_STATE)
113 data[5] = 0x95; /* Valid for 0 arg */
114 else if (cmd == SDCARD_SEND_IF_COND)
115 data[5] = 0x87; /* Valid for 0x1aa arg */
117 data[5] = 0xff; /* no CRC */
118 ao_sdcard_send(data, 6);
120 /* The first reply byte will be the status,
121 * which must have the high bit clear
123 timeout = ao_time() + SDCARD_CMD_TIMEOUT;
125 ao_sdcard_recv(&reply, 1);
126 DBG ("\t\tgot byte %02x\n", reply);
127 if ((reply & 0x80) == 0)
129 if (later(ao_time(), timeout)) {
130 WARN("send_cmd %02x timeout\n", cmd);
131 return SDCARD_STATUS_TIMEOUT;
135 if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
136 WARN("send_cmd %d failed %02x\n", cmd, reply);
142 * Retrieve any reply, discarding the trailing CRC byte
145 ao_sdcard_recv_reply(uint8_t *reply, int len)
150 ao_sdcard_recv(reply, len);
152 ao_sdcard_recv(&discard, 1);
156 * Switch to 'idle' state. This is used to get the card into SPI mode
159 ao_sdcard_go_idle_state(void)
163 DBG ("go_idle_state\n");
165 ret = ao_sdcard_send_cmd(SDCARD_GO_IDLE_STATE, 0);
166 ao_sdcard_recv_reply(NULL, 0);
167 ao_sdcard_deselect();
168 DBG ("\tgo_idle_state status %02x\n", ret);
173 ao_sdcard_send_op_cond(void)
177 DBG ("send_op_cond\n");
179 ret = ao_sdcard_send_cmd(SDCARD_SEND_OP_COND, 0);
180 ao_sdcard_recv_reply(NULL, 0);
181 ao_sdcard_deselect();
182 DBG ("\tsend_op_cond %02x\n", ret);
187 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
191 DBG ("send_if_cond\n");
193 ret = ao_sdcard_send_cmd(SDCARD_SEND_IF_COND, arg);
194 if (ret != SDCARD_STATUS_IDLE_STATE) {
195 DBG ("\tsend_if_cond failed %02x\n", ret);
198 ao_sdcard_recv_reply(send_if_cond_response, 4);
199 DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
201 send_if_cond_response[0],
202 send_if_cond_response[1],
203 send_if_cond_response[2],
204 send_if_cond_response[3]);
205 ao_sdcard_deselect();
210 * _ao_sdcard_send_status
212 * Get the 2-byte status value.
214 * Called from other functions with CS held low already,
215 * hence prefixing the name with '_'
218 _ao_sdcard_send_status(void)
223 DBG ("send_status\n");
224 ret = ao_sdcard_send_cmd(SDCARD_SEND_STATUS, 0);
225 ao_sdcard_recv_reply(&extra, 1);
226 if (ret != SDCARD_STATUS_READY_STATE)
227 DBG ("\tsend_if_cond failed %02x\n", ret);
228 return ret | (extra << 8);
232 * ao_sdcard_set_blocklen
234 * Set the block length for future read and write commands
237 ao_sdcard_set_blocklen(uint32_t blocklen)
241 DBG ("set_blocklen %d\n", blocklen);
243 ret = ao_sdcard_send_cmd(SDCARD_SET_BLOCKLEN, blocklen);
244 ao_sdcard_recv_reply(NULL, 0);
245 ao_sdcard_deselect();
246 if (ret != SDCARD_STATUS_READY_STATE)
247 DBG ("\tsend_if_cond failed %02x\n", ret);
254 * Send the app command prefix
256 * Called with the CS held low, hence
260 _ao_sdcard_app_cmd(void)
265 ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
266 ao_sdcard_recv_reply(NULL, 0);
267 DBG ("\tapp_cmd status %02x\n");
272 ao_sdcard_app_send_op_cond(uint32_t arg)
276 DBG("send_op_comd\n");
278 ret = _ao_sdcard_app_cmd();
279 if (ret != SDCARD_STATUS_IDLE_STATE)
281 ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
282 ao_sdcard_recv_reply(NULL, 0);
284 ao_sdcard_deselect();
285 DBG ("\tapp_send_op_cond status %02x\n", ret);
290 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
296 ret = ao_sdcard_send_cmd(SDCARD_READ_OCR, 0);
297 if (ret != SDCARD_STATUS_READY_STATE)
298 DBG ("\tread_ocr failed %02x\n", ret);
300 ao_sdcard_recv_reply(read_ocr_response, 4);
301 DBG ("\tread_ocr status %02x response %02x %02x %02x %02x\n", ret,
302 read_ocr_response[0], read_ocr_response[1],
303 read_ocr_response[2], read_ocr_response[3]);
305 ao_sdcard_deselect();
310 * Follow the flow-chart defined by the SD group to
311 * initialize the card and figure out what kind it is
314 ao_sdcard_setup(void)
318 uint8_t response[10];
320 DBG ("Testing sdcard\n");
322 ao_sdcard_get_slow();
324 * min 74 clocks with CS high
326 ao_sdcard_send_fixed(0xff, 10);
328 /* Reset the card and get it into SPI mode */
329 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
330 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
333 if (i == SDCARD_IDLE_RETRY)
336 /* Figure out what kind of card we have */
337 sdtype = ao_sdtype_unknown;
339 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
343 /* Check for SD version 2 */
344 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
349 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
350 ao_delay(AO_MS_TO_TICKS(10));
351 ret = ao_sdcard_app_send_op_cond(arg);
352 if (ret != SDCARD_STATUS_IDLE_STATE)
355 if (ret != SDCARD_STATUS_READY_STATE) {
357 for (i = 0; i < SDCARD_OP_COND_RETRY; i++) {
358 ao_delay(AO_MS_TO_TICKS(10));
359 ret = ao_sdcard_send_op_cond();
360 if (ret != SDCARD_STATUS_IDLE_STATE)
363 if (ret != SDCARD_STATUS_READY_STATE)
365 sdtype = ao_sdtype_mmc3;
369 ret = ao_sdcard_read_ocr(response);
370 if (ret != SDCARD_STATUS_READY_STATE)
372 if ((response[0] & 0xc0) == 0xc0)
373 sdtype = ao_sdtype_sd2block;
375 sdtype = ao_sdtype_sd2byte;
377 sdtype = ao_sdtype_sd1;
381 /* For everything but SDHC cards, set the block length */
382 if (sdtype != ao_sdtype_sd2block) {
383 ret = ao_sdcard_set_blocklen(512);
384 if (ret != SDCARD_STATUS_READY_STATE)
385 DBG ("set_blocklen failed, ignoring\n");
389 DBG ("SD card detected, type %d\n", sdtype);
395 _ao_sdcard_reset(void)
399 uint8_t response[10];
401 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
402 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
405 if (i == SDCARD_IDLE_RETRY) {
410 /* Follow the setup path to get the card out of idle state and
411 * up and running again
413 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
417 /* Check for SD version 2 */
418 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
423 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
424 ret = ao_sdcard_app_send_op_cond(arg);
425 if (ret != SDCARD_STATUS_IDLE_STATE)
429 if (ret != SDCARD_STATUS_READY_STATE) {
431 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
432 ret = ao_sdcard_send_op_cond();
433 if (ret != SDCARD_STATUS_IDLE_STATE)
436 if (ret != SDCARD_STATUS_READY_STATE)
440 /* For everything but SDHC cards, set the block length */
441 if (sdtype != ao_sdtype_sd2block) {
442 ret = ao_sdcard_set_blocklen(512);
443 if (ret != SDCARD_STATUS_READY_STATE)
444 DBG ("set_blocklen failed, ignoring\n");
452 * The card will send 0xff until it is ready to send
453 * the data block at which point it will send the START_BLOCK
454 * marker followed by the data. This function waits while
455 * the card is sending 0xff
458 ao_sdcard_wait_block_start(void)
461 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
463 DBG ("\twait_block_start\n");
465 ao_sdcard_recv(&v, 1);
466 DBG("\t\trecv %02x\n", v);
469 if (later(ao_time(), timeout)) {
470 printf ("wait block start timeout\n");
478 * Read a block of 512 bytes from the card
481 ao_sdcard_read_block(uint32_t block, uint8_t *data)
492 if (sdtype != ao_sdtype_unknown)
499 DBG("read block %d\n", block);
500 if (sdtype != ao_sdtype_sd2block)
504 for (tries = 0; tries < 10; tries++) {
507 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
508 ao_sdcard_recv_reply(NULL, 0);
509 if (ret != SDCARD_STATUS_READY_STATE) {
511 WARN ("read block command failed %d status %02x\n", block, ret);
512 status = _ao_sdcard_send_status();
513 WARN ("\tstatus now %04x\n", status);
517 ao_sdcard_send_fixed(0xff, 1);
519 /* Wait for the data start block marker */
520 start_block = ao_sdcard_wait_block_start();
521 if (start_block != SDCARD_DATA_START_BLOCK) {
522 WARN ("wait block start failed %02x\n", start_block);
527 ao_sdcard_recv(data, 512);
528 ao_sdcard_recv(crc, 2);
530 ao_sdcard_deselect();
531 if (ret == SDCARD_STATUS_READY_STATE)
533 if (ret == SDCARD_STATUS_IDLE_STATE) {
534 ret = _ao_sdcard_reset();
535 if (ret != SDCARD_STATUS_READY_STATE)
543 if (ret != SDCARD_STATUS_READY_STATE)
544 WARN("read failed\n");
546 WARN("took %d tries to read %d\n", tries + 1, block);
549 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
550 return ret == SDCARD_STATUS_READY_STATE;
554 * Write a block of 512 bytes to the card
557 ao_sdcard_write_block(uint32_t block, uint8_t *data)
561 uint8_t start_block[8];
563 static uint8_t check_data[512];
571 if (sdtype != ao_sdtype_unknown)
578 DBG("write block %d\n", block);
579 if (sdtype != ao_sdtype_sd2block)
584 for (tries = 0; tries < 10; tries++) {
587 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
588 ao_sdcard_recv_reply(NULL, 0);
589 if (ret != SDCARD_STATUS_READY_STATE)
592 /* Write a pad byte followed by the data start block marker */
593 start_block[0] = 0xff;
594 start_block[1] = SDCARD_DATA_START_BLOCK;
595 ao_sdcard_send(start_block, 2);
598 ao_sdcard_send(data, 512);
601 ao_sdcard_send_fixed(0xff, 2);
603 /* See if the card liked the data */
604 ao_sdcard_recv(response, sizeof (response));
605 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
607 WARN("Data not accepted, response");
608 for (i = 0; i < sizeof (response); i++)
609 WARN(" %02x", response[i]);
615 /* Wait for the bus to go idle (should be done with an interrupt?) */
616 if (!ao_sdcard_wait_busy()) {
621 /* Check the current status after the write completes */
622 status = _ao_sdcard_send_status();
623 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
624 WARN ("send status after write %04x\n", status);
629 ao_sdcard_deselect();
630 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
631 if (ret == SDCARD_STATUS_READY_STATE)
637 WARN("took %d tries to write %d\n", tries + 1, block);
639 return ret == SDCARD_STATUS_READY_STATE;
643 static uint8_t test_data[512];
646 ao_sdcard_test_read(void)
651 if (ao_cmd_status != ao_cmd_success)
654 for (i = 0; i < 100; i++) {
655 printf ("."); flush();
656 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
657 printf ("read error %d\n", i);
662 for (i = 0; i < 18; i++)
663 printf (" %02x", test_data[i]);
668 ao_sdcard_test_write(void)
672 for (i = 0; i < 16; i++) {
674 printf (" %02x", test_data[i]);
677 if (!ao_sdcard_write_block(1, test_data)) {
678 printf ("write error\n");
683 static const struct ao_cmds ao_sdcard_cmds[] = {
684 { ao_sdcard_test_read, "x\0Test read" },
685 { ao_sdcard_test_write, "y\0Test read" },
693 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP);
694 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP);
695 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP);
696 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
698 ao_cmd_register(&ao_sdcard_cmds[0]);