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_IDLE_RETRY; i++) {
350 ret = ao_sdcard_app_send_op_cond(arg);
351 if (ret != SDCARD_STATUS_IDLE_STATE)
354 if (ret != SDCARD_STATUS_READY_STATE) {
356 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
357 ret = ao_sdcard_send_op_cond();
358 if (ret != SDCARD_STATUS_IDLE_STATE)
361 if (ret != SDCARD_STATUS_READY_STATE)
363 sdtype = ao_sdtype_mmc3;
367 ret = ao_sdcard_read_ocr(response);
368 if (ret != SDCARD_STATUS_READY_STATE)
370 if ((response[0] & 0xc0) == 0xc0)
371 sdtype = ao_sdtype_sd2block;
373 sdtype = ao_sdtype_sd2byte;
375 sdtype = ao_sdtype_sd1;
379 /* For everything but SDHC cards, set the block length */
380 if (sdtype != ao_sdtype_sd2block) {
381 ret = ao_sdcard_set_blocklen(512);
382 if (ret != SDCARD_STATUS_READY_STATE)
383 DBG ("set_blocklen failed, ignoring\n");
387 DBG ("SD card detected, type %d\n", sdtype);
393 _ao_sdcard_reset(void)
397 uint8_t response[10];
399 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
400 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
403 if (i == SDCARD_IDLE_RETRY) {
408 /* Follow the setup path to get the card out of idle state and
409 * up and running again
411 if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
415 /* Check for SD version 2 */
416 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
421 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
422 ret = ao_sdcard_app_send_op_cond(arg);
423 if (ret != SDCARD_STATUS_IDLE_STATE)
427 if (ret != SDCARD_STATUS_READY_STATE) {
429 for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
430 ret = ao_sdcard_send_op_cond();
431 if (ret != SDCARD_STATUS_IDLE_STATE)
434 if (ret != SDCARD_STATUS_READY_STATE)
438 /* For everything but SDHC cards, set the block length */
439 if (sdtype != ao_sdtype_sd2block) {
440 ret = ao_sdcard_set_blocklen(512);
441 if (ret != SDCARD_STATUS_READY_STATE)
442 DBG ("set_blocklen failed, ignoring\n");
450 * The card will send 0xff until it is ready to send
451 * the data block at which point it will send the START_BLOCK
452 * marker followed by the data. This function waits while
453 * the card is sending 0xff
456 ao_sdcard_wait_block_start(void)
459 uint16_t timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
461 DBG ("\twait_block_start\n");
463 ao_sdcard_recv(&v, 1);
464 DBG("\t\trecv %02x\n", v);
467 if (later(ao_time(), timeout)) {
468 printf ("wait block start timeout\n");
476 * Read a block of 512 bytes from the card
479 ao_sdcard_read_block(uint32_t block, uint8_t *data)
490 if (sdtype != ao_sdtype_unknown)
497 DBG("read block %d\n", block);
498 if (sdtype != ao_sdtype_sd2block)
502 for (tries = 0; tries < 10; tries++) {
505 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
506 ao_sdcard_recv_reply(NULL, 0);
507 if (ret != SDCARD_STATUS_READY_STATE) {
509 WARN ("read block command failed %d status %02x\n", block, ret);
510 status = _ao_sdcard_send_status();
511 WARN ("\tstatus now %04x\n", status);
515 ao_sdcard_send_fixed(0xff, 1);
517 /* Wait for the data start block marker */
518 start_block = ao_sdcard_wait_block_start();
519 if (start_block != SDCARD_DATA_START_BLOCK) {
520 WARN ("wait block start failed %02x\n", start_block);
525 ao_sdcard_recv(data, 512);
526 ao_sdcard_recv(crc, 2);
528 ao_sdcard_deselect();
529 if (ret == SDCARD_STATUS_READY_STATE)
531 if (ret == SDCARD_STATUS_IDLE_STATE) {
532 ret = _ao_sdcard_reset();
533 if (ret != SDCARD_STATUS_READY_STATE)
541 if (ret != SDCARD_STATUS_READY_STATE)
542 WARN("read failed\n");
544 WARN("took %d tries to read %d\n", tries + 1, block);
547 DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
548 return ret == SDCARD_STATUS_READY_STATE;
552 * Write a block of 512 bytes to the card
555 ao_sdcard_write_block(uint32_t block, uint8_t *data)
559 uint8_t start_block[8];
561 static uint8_t check_data[512];
569 if (sdtype != ao_sdtype_unknown)
576 DBG("write block %d\n", block);
577 if (sdtype != ao_sdtype_sd2block)
582 for (tries = 0; tries < 10; tries++) {
585 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
586 ao_sdcard_recv_reply(NULL, 0);
587 if (ret != SDCARD_STATUS_READY_STATE)
590 /* Write a pad byte followed by the data start block marker */
591 start_block[0] = 0xff;
592 start_block[1] = SDCARD_DATA_START_BLOCK;
593 ao_sdcard_send(start_block, 2);
596 ao_sdcard_send(data, 512);
599 ao_sdcard_send_fixed(0xff, 2);
601 /* See if the card liked the data */
602 ao_sdcard_recv(response, sizeof (response));
603 if ((response[0] & SDCARD_DATA_RES_MASK) != SDCARD_DATA_RES_ACCEPTED) {
605 WARN("Data not accepted, response");
606 for (i = 0; i < sizeof (response); i++)
607 WARN(" %02x", response[i]);
613 /* Wait for the bus to go idle (should be done with an interrupt?) */
614 if (!ao_sdcard_wait_busy()) {
619 /* Check the current status after the write completes */
620 status = _ao_sdcard_send_status();
621 if ((status & 0xff) != SDCARD_STATUS_READY_STATE) {
622 WARN ("send status after write %04x\n", status);
627 ao_sdcard_deselect();
628 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
629 if (ret == SDCARD_STATUS_READY_STATE)
635 WARN("took %d tries to write %d\n", tries + 1, block);
637 return ret == SDCARD_STATUS_READY_STATE;
641 static uint8_t test_data[512];
644 ao_sdcard_test_read(void)
649 if (ao_cmd_status != ao_cmd_success)
652 for (i = 0; i < 100; i++) {
653 printf ("."); flush();
654 if (!ao_sdcard_read_block(ao_cmd_lex_u32+i, test_data)) {
655 printf ("read error %d\n", i);
660 for (i = 0; i < 18; i++)
661 printf (" %02x", test_data[i]);
666 ao_sdcard_test_write(void)
670 for (i = 0; i < 16; i++) {
672 printf (" %02x", test_data[i]);
675 if (!ao_sdcard_write_block(1, test_data)) {
676 printf ("write error\n");
681 static const struct ao_cmds ao_sdcard_cmds[] = {
682 { ao_sdcard_test_read, "x\0Test read" },
683 { ao_sdcard_test_write, "y\0Test read" },
691 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_SCK_PIN, STM_PUPDR_PULL_UP);
692 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MISO_PIN, STM_PUPDR_PULL_UP);
693 stm_pupdr_set(AO_SDCARD_SPI_PORT, AO_SDCARD_SPI_MOSI_PIN, STM_PUPDR_PULL_UP);
694 ao_spi_init_cs(AO_SDCARD_SPI_CS_PORT, (1 << AO_SDCARD_SPI_CS_PIN));
696 ao_cmd_register(&ao_sdcard_cmds[0]);