59ac94008f84aafb6974f903923caa649e0ad570
[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    1
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_WAIT; i++) {
330                 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
331                         break;
332         }
333         if (i == SDCARD_IDLE_WAIT)
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_IDLE_WAIT; i++) {
350                         ret = ao_sdcard_app_send_op_cond(arg);
351                         if (ret != SDCARD_STATUS_IDLE_STATE)
352                                 break;
353                 }
354                 if (ret != SDCARD_STATUS_READY_STATE) {
355                         /* MMC */
356                         for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
357                                 ret = ao_sdcard_send_op_cond();
358                                 if (ret != SDCARD_STATUS_IDLE_STATE)
359                                         break;
360                         }
361                         if (ret != SDCARD_STATUS_READY_STATE)
362                                 goto bail;
363                         sdtype = ao_sdtype_mmc3;
364                 } else {
365                         /* SD */
366                         if (sdver2 != 0) {
367                                 ret = ao_sdcard_read_ocr(response);
368                                 if (ret != SDCARD_STATUS_READY_STATE)
369                                         goto bail;
370                                 if ((response[0] & 0xc0) == 0xc0)
371                                         sdtype = ao_sdtype_sd2block;
372                                 else
373                                         sdtype = ao_sdtype_sd2byte;
374                         } else {
375                                 sdtype = ao_sdtype_sd1;
376                         }
377                 }
378
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");
384                 }
385         }
386
387         DBG ("SD card detected, type %d\n", sdtype);
388 bail:
389         ao_sdcard_put();
390 }
391
392 static uint8_t
393 _ao_sdcard_reset(void)
394 {
395         int i;
396         uint8_t ret;
397         uint8_t response[10];
398
399         for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
400                 if (ao_sdcard_go_idle_state() == SDCARD_STATUS_IDLE_STATE)
401                         break;
402         }
403         if (i == SDCARD_IDLE_WAIT) {
404                 ret = 0x3f;
405                 goto bail;
406         }
407
408         /* Follow the setup path to get the card out of idle state and
409          * up and running again
410          */
411         if (ao_sdcard_send_if_cond(0x1aa, response) == SDCARD_STATUS_IDLE_STATE) {
412                 uint32_t        arg = 0;
413                 uint8_t         sdver2 = 0;
414
415                 /* Check for SD version 2 */
416                 if ((response[2] & 0xf) == 1 && response[3] == 0xaa) {
417                         arg = 0x40000000;
418                         sdver2 = 1;
419                 }
420
421                 for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
422                         ret = ao_sdcard_app_send_op_cond(arg);
423                         if (ret != SDCARD_STATUS_IDLE_STATE)
424                                 break;
425                 }
426
427                 if (ret != SDCARD_STATUS_READY_STATE) {
428                         /* MMC */
429                         for (i = 0; i < SDCARD_IDLE_WAIT; i++) {
430                                 ret = ao_sdcard_send_op_cond();
431                                 if (ret != SDCARD_STATUS_IDLE_STATE)
432                                         break;
433                         }
434                         if (ret != SDCARD_STATUS_READY_STATE)
435                                 goto bail;
436                 }
437
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");
443                 }
444         }
445 bail:
446         return ret;
447 }
448
449 /*
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
454  */
455 static uint8_t
456 ao_sdcard_wait_block_start(void)
457 {
458         uint8_t         v;
459         uint16_t        timeout = ao_time() + SDCARD_BLOCK_TIMEOUT;
460
461         DBG ("\twait_block_start\n");
462         for (;;) {
463                 ao_sdcard_recv(&v, 1);
464                 DBG("\t\trecv %02x\n", v);
465                 if (v != 0xff)
466                         break;
467                 if (later(ao_time(), timeout)) {
468                         printf ("wait block start timeout\n");
469                         return 0xff;
470                 }
471         }
472         return v;
473 }
474
475 /*
476  * Read a block of 512 bytes from the card
477  */
478 uint8_t
479 ao_sdcard_read_block(uint32_t block, uint8_t *data)
480 {
481         uint8_t ret;
482         uint8_t start_block;
483         uint8_t crc[2];
484         int tries;
485
486         ao_sdcard_lock();
487         if (!initialized) {
488                 ao_sdcard_setup();
489                 initialized = 1;
490                 if (sdtype != ao_sdtype_unknown)
491                         present = 1;
492         }
493         if (!present) {
494                 ao_sdcard_unlock();
495                 return 0;
496         }
497         DBG("read block %d\n", block);
498         if (sdtype != ao_sdtype_sd2block)
499                 block <<= 9;
500
501         ao_sdcard_get();
502         for (tries = 0; tries < 10; tries++) {
503                 ao_sdcard_select();
504
505                 ret = ao_sdcard_send_cmd(SDCARD_READ_BLOCK, block);
506                 ao_sdcard_recv_reply(NULL, 0);
507                 if (ret != SDCARD_STATUS_READY_STATE) {
508                         uint16_t        status;
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);
512                         goto bail;
513                 }
514
515                 ao_sdcard_send_fixed(0xff, 1);
516
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);
521                         ret = 0x3f;
522                         goto bail;
523                 }
524
525                 ao_sdcard_recv(data, 512);
526                 ao_sdcard_recv(crc, 2);
527         bail:
528                 ao_sdcard_deselect();
529                 if (ret == SDCARD_STATUS_READY_STATE)
530                         break;
531                 if (ret == SDCARD_STATUS_IDLE_STATE) {
532                         ret = _ao_sdcard_reset();
533                         if (ret != SDCARD_STATUS_READY_STATE)
534                                 break;
535                 }
536         }
537         ao_sdcard_put();
538         ao_sdcard_unlock();
539
540 #if SDCARD_WARN
541         if (ret != SDCARD_STATUS_READY_STATE)
542                 WARN("read failed\n");
543         else if (tries)
544                 WARN("took %d tries to read %d\n", tries + 1, block);
545 #endif
546
547         DBG("read %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
548         return ret == SDCARD_STATUS_READY_STATE;
549 }
550
551 /*
552  * Write a block of 512 bytes to the card
553  */
554 uint8_t
555 ao_sdcard_write_block(uint32_t block, uint8_t *data)
556 {
557         uint8_t ret;
558         uint8_t response[1];
559         uint8_t start_block[8];
560         uint16_t status;
561         static uint8_t  check_data[512];
562         int     i;
563         int     tries;
564
565         ao_sdcard_lock();
566         if (!initialized) {
567                 ao_sdcard_setup();
568                 initialized = 1;
569                 if (sdtype != ao_sdtype_unknown)
570                         present = 1;
571         }
572         if (!present) {
573                 ao_sdcard_unlock();
574                 return 0;
575         }
576         DBG("write block %d\n", block);
577         if (sdtype != ao_sdtype_sd2block)
578                 block <<= 9;
579
580         ao_sdcard_get();
581
582         for (tries = 0; tries < 10; tries++) {
583                 ao_sdcard_select();
584
585                 ret = ao_sdcard_send_cmd(SDCARD_WRITE_BLOCK, block);
586                 ao_sdcard_recv_reply(NULL, 0);
587                 if (ret != SDCARD_STATUS_READY_STATE)
588                         goto bail;
589
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);
594
595                 /* Send the data */
596                 ao_sdcard_send(data, 512);
597
598                 /* Fake the CRC */
599                 ao_sdcard_send_fixed(0xff, 2);
600
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) {
604                         int i;
605                         WARN("Data not accepted, response");
606                         for (i = 0; i < sizeof (response); i++)
607                                 WARN(" %02x", response[i]);
608                         WARN("\n");
609                         ret = 0x3f;
610                         goto bail;
611                 }
612                 
613                 /* Wait for the bus to go idle (should be done with an interrupt?) */
614                 if (!ao_sdcard_wait_busy()) {
615                         ret = 0x3f;
616                         goto bail;
617                 }
618
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);
623                         ret = status & 0xff;
624                         goto bail;
625                 }
626         bail:
627                 ao_sdcard_deselect();
628                 DBG("write %s\n", ret == SDCARD_STATUS_READY_STATE ? "success" : "failure");
629                 if (ret == SDCARD_STATUS_READY_STATE)
630                         break;
631         }
632         ao_sdcard_put();
633         ao_sdcard_unlock();
634         if (tries)
635                 WARN("took %d tries to write %d\n", tries + 1, block);
636
637         return ret == SDCARD_STATUS_READY_STATE;
638 }
639
640 #if SDCARD_DEBUG
641 static uint8_t  test_data[512];
642
643 static void
644 ao_sdcard_test_read(void)
645 {
646         int i;
647
648         ao_cmd_decimal();
649         if (ao_cmd_status != ao_cmd_success)
650                 return;
651         
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);
656                         return;
657                 }
658         }
659         printf ("data:");
660         for (i = 0; i < 18; i++)
661                 printf (" %02x", test_data[i]);
662         printf ("\n");
663 }
664
665 static void
666 ao_sdcard_test_write(void)
667 {
668         int     i;
669         printf ("data:");
670         for (i = 0; i < 16; i++) {
671                 test_data[i]++;
672                 printf (" %02x", test_data[i]);
673         }
674         printf ("\n");
675         if (!ao_sdcard_write_block(1, test_data)) {
676                 printf ("write error\n");
677                 return;
678         }
679 }
680
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" },
684         { 0, NULL },
685 };
686 #endif
687
688 void
689 ao_sdcard_init(void)
690 {
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));
695 #if SDCARD_DEBUG
696         ao_cmd_register(&ao_sdcard_cmds[0]);
697 #endif
698 }