altos: Delay while waking up SD card a bit
[fw/altos] / src / drivers / ao_sdcard.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include "ao_sdcard.h"
20
21 extern uint8_t ao_radio_mutex;
22
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)
31
32 /* Include SD card commands */
33 #define SDCARD_DEBUG    0
34
35 /* Spew SD tracing */
36 #define SDCARD_TRACE    0
37
38 /* Emit error and warning messages */
39 #define SDCARD_WARN     0
40
41 static uint8_t  initialized;
42 static uint8_t  present;
43 static uint8_t  mutex;
44 static enum ao_sdtype sdtype;
45
46 #define ao_sdcard_lock()        ao_mutex_get(&mutex)
47 #define ao_sdcard_unlock()      ao_mutex_put(&mutex)
48
49 #if SDCARD_TRACE
50 #define DBG(...) printf(__VA_ARGS__)
51 #else
52 #define DBG(...)
53 #endif
54
55 #if SDCARD_WARN
56 #define WARN(...) printf(__VA_ARGS__)
57 #else
58 #define WARN(...)
59 #endif
60
61 #define later(x,y)      ((int16_t) ((x) - (y)) >= 0)
62
63 /*
64  * Wait while the card is busy. The card will return a stream of 0xff
65  * when it is ready to accept a command
66  */
67
68 static uint8_t
69 ao_sdcard_wait_busy(void)
70 {
71         uint16_t        timeout = ao_time() + SDCARD_BUSY_TIMEOUT;
72         uint8_t         reply;
73         for (;;) {
74                 ao_sdcard_recv(&reply, 1);
75                 DBG("\t\twait busy %02x\n", reply);
76                 if (reply == 0xff)
77                         break;
78                 if (later(ao_time(), timeout)) {
79                         WARN("wait busy timeout\n");
80                         return 0;
81                 }
82         }
83         return 1;
84 }
85
86
87 /*
88  * Send an SD command and await the status reply
89  */
90
91 static uint8_t
92 ao_sdcard_send_cmd(uint8_t cmd, uint32_t arg)
93 {
94         uint8_t data[6];
95         uint8_t reply;
96         int i;
97         uint16_t timeout;
98
99         DBG ("\tsend_cmd %d arg %08x\n", cmd, arg);
100
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;
105         }
106         
107         data[0] = cmd & 0x3f | 0x40;
108         data[1] = arg >> 24;
109         data[2] = arg >> 16;
110         data[3] = arg >> 8;
111         data[4] = arg;
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 */
116         else
117                 data[5] = 0xff; /* no CRC */
118         ao_sdcard_send(data, 6);
119
120         /* The first reply byte will be the status,
121          * which must have the high bit clear
122          */
123         timeout = ao_time() + SDCARD_CMD_TIMEOUT;
124         for (;;) {
125                 ao_sdcard_recv(&reply, 1);
126                 DBG ("\t\tgot byte %02x\n", reply);
127                 if ((reply & 0x80) == 0)
128                         break;
129                 if (later(ao_time(), timeout)) {
130                         WARN("send_cmd %02x timeout\n", cmd);
131                         return SDCARD_STATUS_TIMEOUT;
132                 }
133         }
134 #if SDCARD_WARN
135         if (reply != SDCARD_STATUS_READY_STATE && reply != SDCARD_STATUS_IDLE_STATE)
136                 WARN("send_cmd %d failed %02x\n", cmd, reply);
137 #endif
138         return reply;
139 }
140
141 /*
142  * Retrieve any reply, discarding the trailing CRC byte
143  */
144 static void
145 ao_sdcard_recv_reply(uint8_t *reply, int len)
146 {
147         uint8_t discard;
148
149         if (len)
150                 ao_sdcard_recv(reply, len);
151         /* trailing byte */
152         ao_sdcard_recv(&discard, 1);
153 }
154
155 /*
156  * Switch to 'idle' state. This is used to get the card into SPI mode
157  */
158 static uint8_t
159 ao_sdcard_go_idle_state(void)
160 {
161         uint8_t ret;
162
163         DBG ("go_idle_state\n");
164         ao_sdcard_select();
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);
169         return ret;
170 }
171
172 static uint8_t
173 ao_sdcard_send_op_cond(void)
174 {
175         uint8_t ret;
176
177         DBG ("send_op_cond\n");
178         ao_sdcard_select();
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);
183         return ret;
184 }
185
186 static uint8_t
187 ao_sdcard_send_if_cond(uint32_t arg, uint8_t send_if_cond_response[4])
188 {
189         uint8_t ret;
190
191         DBG ("send_if_cond\n");
192         ao_sdcard_select();
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);
196                 return ret;
197         }
198         ao_sdcard_recv_reply(send_if_cond_response, 4);
199         DBG ("send_if_cond status %02x response %02x %02x %02x %02x\n",
200                 ret,
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();
206         return ret;
207 }
208
209 /*
210  * _ao_sdcard_send_status
211  *
212  * Get the 2-byte status value.
213  *
214  * Called from other functions with CS held low already,
215  * hence prefixing the name with '_'
216  */
217 static uint16_t
218 _ao_sdcard_send_status(void)
219 {
220         uint8_t ret;
221         uint8_t extra;
222
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);
229 }
230
231 /*
232  * ao_sdcard_set_blocklen
233  *
234  * Set the block length for future read and write commands
235  */
236 static uint8_t
237 ao_sdcard_set_blocklen(uint32_t blocklen)
238 {
239         uint8_t ret;
240
241         DBG ("set_blocklen %d\n", blocklen);
242         ao_sdcard_select();
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);
248         return ret;
249 }
250
251 /*
252  * _ao_sdcard_app_cmd
253  *
254  * Send the app command prefix
255  *
256  * Called with the CS held low, hence
257  * the '_' prefix
258  */
259 static uint8_t
260 _ao_sdcard_app_cmd(void)
261 {
262         uint8_t ret;
263
264         DBG ("app_cmd\n");
265         ret = ao_sdcard_send_cmd(SDCARD_APP_CMD, 0);
266         ao_sdcard_recv_reply(NULL, 0);
267         DBG ("\tapp_cmd status %02x\n");
268         return ret;
269 }
270
271 static uint8_t
272 ao_sdcard_app_send_op_cond(uint32_t arg)
273 {
274         uint8_t ret;
275
276         DBG("send_op_comd\n");
277         ao_sdcard_select();
278         ret = _ao_sdcard_app_cmd();
279         if (ret != SDCARD_STATUS_IDLE_STATE)
280                 goto bail;
281         ret = ao_sdcard_send_cmd(SDCARD_APP_SEND_OP_COMD, arg);
282         ao_sdcard_recv_reply(NULL, 0);
283 bail:
284         ao_sdcard_deselect();
285         DBG ("\tapp_send_op_cond status %02x\n", ret);
286         return ret;
287 }
288
289 static uint8_t
290 ao_sdcard_read_ocr(uint8_t read_ocr_response[4])
291 {
292         uint8_t ret;
293
294         DBG ("read_ocr\n");
295         ao_sdcard_select();
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);
299         else {
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]);
304         }
305         ao_sdcard_deselect();
306         return ret;
307 }
308
309 /*
310  * Follow the flow-chart defined by the SD group to
311  * initialize the card and figure out what kind it is
312  */
313 static void
314 ao_sdcard_setup(void)
315 {
316         int     i;
317         uint8_t ret;
318         uint8_t response[10];
319
320         DBG ("Testing sdcard\n");
321
322         ao_sdcard_get_slow();
323         /*
324          * min 74 clocks with CS high
325          */
326         ao_sdcard_send_fixed(0xff, 10);
327
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)
331                         break;
332         }
333         if (i == SDCARD_IDLE_RETRY)
334                 goto bail;
335
336         /* Figure out what kind of card we have */
337         sdtype = ao_sdtype_unknown;
338
339         if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
340                 uint32_t        arg = 0;
341                 uint8_t         sdver2 = 0;
342
343                 /* Check for SD version 2 */
344                 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
345                         arg = 0x40000000;
346                         sdver2 = 1;
347                 }
348
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)
353                                 break;
354                 }
355                 if (ret != SDCARD_STATUS_READY_STATE) {
356                         /* MMC */
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)
361                                         break;
362                         }
363                         if (ret != SDCARD_STATUS_READY_STATE)
364                                 goto bail;
365                         sdtype = ao_sdtype_mmc3;
366                 } else {
367                         /* SD */
368                         if (sdver2 != 0) {
369                                 ret = ao_sdcard_read_ocr(response);
370                                 if (ret != SDCARD_STATUS_READY_STATE)
371                                         goto bail;
372                                 if ((response[0] & 0xc0) == 0xc0)
373                                         sdtype = ao_sdtype_sd2block;
374                                 else
375                                         sdtype = ao_sdtype_sd2byte;
376                         } else {
377                                 sdtype = ao_sdtype_sd1;
378                         }
379                 }
380
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");
386                 }
387         }
388
389         DBG ("SD card detected, type %d\n", sdtype);
390 bail:
391         ao_sdcard_put();
392 }
393
394 static uint8_t
395 _ao_sdcard_reset(void)
396 {
397         int i;
398         uint8_t ret;
399         uint8_t response[10];
400
401         for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
402                 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
403                         break;
404         }
405         if (i == SDCARD_IDLE_RETRY) {
406                 ret = 0x3f;
407                 goto bail;
408         }
409
410         /* Follow the setup path to get the card out of idle state and
411          * up and running again
412          */
413         if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
414                 uint32_t        arg = 0;
415                 uint8_t         sdver2 = 0;
416
417                 /* Check for SD version 2 */
418                 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
419                         arg = 0x40000000;
420                         sdver2 = 1;
421                 }
422
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)
426                                 break;
427                 }
428
429                 if (ret != SDCARD_STATUS_READY_STATE) {
430                         /* MMC */
431                         for (i = 0; i < SDCARD_IDLE_RETRY; i++) {
432                                 ret = ao_sdcard_send_op_cond();
433                                 if (ret != SDCARD_STATUS_IDLE_STATE)
434                                         break;
435                         }
436                         if (ret != SDCARD_STATUS_READY_STATE)
437                                 goto bail;
438                 }
439
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");
445                 }
446         }
447 bail:
448         return ret;
449 }
450
451 /*
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
456  */
457 static uint8_t
458 ao_sdcard_wait_block_start(void)
459 {
460         uint8_t         v;
461         uint16_t        timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
462
463         DBG ("\twait_block_start\n");
464         for (;;) {
465                 ao_sdcard_recv(&v, 1);
466                 DBG("\t\trecv %02x\n", v);
467                 if (v != 0xff)
468                         break;
469                 if (later(ao_time(), timeout)) {
470                         printf ("wait block start timeout\n");
471                         return 0xff;
472                 }
473         }
474         return v;
475 }
476
477 /*
478  * Read a block of 512 bytes from the card
479  */
480 uint8_t
481 ao_sdcard_read_block(uint32_t block, uint8_t *data)
482 {
483         uint8_t ret;
484         uint8_t start_block;
485         uint8_t crc[2];
486         int tries;
487
488         ao_sdcard_lock();
489         if (!initialized) {
490                 ao_sdcard_setup();
491                 initialized = 1;
492                 if (sdtype != ao_sdtype_unknown)
493                         present = 1;
494         }
495         if (!present) {
496                 ao_sdcard_unlock();
497                 return 0;
498         }
499         DBG("read block %d\n", block);
500         if (sdtype != ao_sdtype_sd2block)
501                 block <<= 9;
502
503         ao_sdcard_get();
504         for (tries = 0; tries < 10; tries++) {
505                 ao_sdcard_select();
506
507                 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
508                 ao_sdcard_recv_reply(NULL, 0);
509                 if (ret != SDCARD_STATUS_READY_STATE) {
510                         uint16_t        status;
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);
514                         goto bail;
515                 }
516
517                 ao_sdcard_send_fixed(0xff, 1);
518
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);
523                         ret = 0x3f;
524                         goto bail;
525                 }
526
527                 ao_sdcard_recv(data, 512);
528                 ao_sdcard_recv(crc, 2);
529         bail:
530                 ao_sdcard_deselect();
531                 if (ret == SDCARD_STATUS_READY_STATE)
532                         break;
533                 if (ret == SDCARD_STATUS_IDLE_STATE) {
534                         ret = _ao_sdcard_reset();
535                         if (ret != SDCARD_STATUS_READY_STATE)
536                                 break;
537                 }
538         }
539         ao_sdcard_put();
540         ao_sdcard_unlock();
541
542 #if SDCARD_WARN
543         if (ret != SDCARD_STATUS_READY_STATE)
544                 WARN("read failed\n");
545         else if (tries)
546                 WARN("took %d tries to read %d\n", tries + 1, block);
547 #endif
548
549         DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
550         return ret == SDCARD_STATUS_READY_STATE;
551 }
552
553 /*
554  * Write a block of 512 bytes to the card
555  */
556 uint8_t
557 ao_sdcard_write_block(uint32_t block, uint8_t *data)
558 {
559         uint8_t ret;
560         uint8_t response[1];
561         uint8_t start_block[8];
562         uint16_t status;
563         static uint8_t  check_data[512];
564         int     i;
565         int     tries;
566
567         ao_sdcard_lock();
568         if (!initialized) {
569                 ao_sdcard_setup();
570                 initialized = 1;
571                 if (sdtype != ao_sdtype_unknown)
572                         present = 1;
573         }
574         if (!present) {
575                 ao_sdcard_unlock();
576                 return 0;
577         }
578         DBG("write block %d\n", block);
579         if (sdtype != ao_sdtype_sd2block)
580                 block <<= 9;
581
582         ao_sdcard_get();
583
584         for (tries = 0; tries < 10; tries++) {
585                 ao_sdcard_select();
586
587                 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
588                 ao_sdcard_recv_reply(NULL, 0);
589                 if (ret != SDCARD_STATUS_READY_STATE)
590                         goto bail;
591
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);
596
597                 /* Send the data */
598                 ao_sdcard_send(data, 512);
599
600                 /* Fake the CRC */
601                 ao_sdcard_send_fixed(0xff, 2);
602
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) {
606                         int i;
607                         WARN("Data not accepted, response");
608                         for (i = 0; i < sizeof (response); i++)
609                                 WARN(" %02x", response[i]);
610                         WARN("\n");
611                         ret = 0x3f;
612                         goto bail;
613                 }
614                 
615                 /* Wait for the bus to go idle (should be done with an interrupt?) */
616                 if (!ao_sdcard_wait_busy()) {
617                         ret = 0x3f;
618                         goto bail;
619                 }
620
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);
625                         ret = status & 0xff;
626                         goto bail;
627                 }
628         bail:
629                 ao_sdcard_deselect();
630                 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
631                 if (ret == SDCARD_STATUS_READY_STATE)
632                         break;
633         }
634         ao_sdcard_put();
635         ao_sdcard_unlock();
636         if (tries)
637                 WARN("took %d tries to write %d\n", tries + 1, block);
638
639         return ret == SDCARD_STATUS_READY_STATE;
640 }
641
642 #if SDCARD_DEBUG
643 static uint8_t  test_data[512];
644
645 static void
646 ao_sdcard_test_read(void)
647 {
648         int i;
649
650         ao_cmd_decimal();
651         if (ao_cmd_status != ao_cmd_success)
652                 return;
653         
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);
658                         return;
659                 }
660         }
661         printf ("data:");
662         for (i = 0; i < 18; i++)
663                 printf (" %02x", test_data[i]);
664         printf ("\n");
665 }
666
667 static void
668 ao_sdcard_test_write(void)
669 {
670         int     i;
671         printf ("data:");
672         for (i = 0; i < 16; i++) {
673                 test_data[i]++;
674                 printf (" %02x", test_data[i]);
675         }
676         printf ("\n");
677         if (!ao_sdcard_write_block(1, test_data)) {
678                 printf ("write error\n");
679                 return;
680         }
681 }
682
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" },
686         { 0, NULL },
687 };
688 #endif
689
690 void
691 ao_sdcard_init(void)
692 {
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));
697 #if SDCARD_DEBUG
698         ao_cmd_register(&ao_sdcard_cmds[0]);
699 #endif
700 }