Root/tornado/fw/mmc-hw.c

1/*
2 * fw/mmc-hw.c - MMC hardware interface
3 *
4 * Written 2012 by Werner Almesberger
5 * Copyright 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include "io.h"
17#include "delay.h"
18#include "mmc-hw.h"
19
20
21/*
22 * Pin assignment from
23 * http://alumni.cs.ucr.edu/~amitra/sdcard/ProdManualSDCardv1.9.pdf
24 *
25 * Pin SD mode SPI mode
26 * 1 DAT3 Chip Select (active low)
27 * 2 CMD MOSI
28 * 3 VSS VSS
29 * 4 VDD VDD
30 * 5 CLK Clock
31 * 6 VSS VSS
32 * 7 DAT0 MISO
33 */
34
35
36#define MMC_nCS CARD_DAT3
37#define MMC_MOSI CARD_CMD
38#define MMC_CLK CARD_CLK
39#define MMC_MISO CARD_DAT0
40
41
42void mmc_select(void)
43{
44    CLR(MMC_nCS);
45}
46
47void mmc_deselect(void)
48{
49    SET(MMC_nCS);
50}
51
52
53uint8_t mmc_recv(void)
54{
55    uint8_t i, v = 0;
56
57    SET(MMC_MOSI);
58    for (i = 0; i != 8; i++) {
59        SET(MMC_CLK);
60        v = (v << 1) | PIN(MMC_MISO);
61        CLR(MMC_CLK);
62    }
63    return v;
64}
65
66
67void mmc_send(uint8_t v)
68{
69    uint8_t i;
70
71    for (i = 0; i != 8; i++) {
72        if (v & 0x80)
73            SET(MMC_MOSI);
74        else
75            CLR(MMC_MOSI);
76        SET(MMC_CLK);
77        CLR(MMC_CLK);
78        v <<= 1;
79    }
80}
81
82
83void mmc_activate(void)
84{
85    SET(MMC_nCS);
86    CLR(MMC_CLK);
87    OUT(MMC_nCS);
88    SET(MMC_MOSI);
89    SET(MMC_CLK); /* for pre-charging */
90    OUT(MMC_MOSI);
91    OUT(MMC_CLK);
92    IN(MMC_MISO);
93
94    _delay_ms(100); /* allow card to pre-charge, to limit inrush current */
95
96    CLR(CARD_nPWR);
97
98    _delay_ms(10);
99}
100
101
102void mmc_deactivate(void)
103{
104    CLR(MMC_CLK);
105    CLR(MMC_nCS);
106    CLR(MMC_MOSI);
107    CLR(MMC_MISO);
108
109    SET(CARD_nPWR);
110    _delay_ms(10);
111
112    OUT(MMC_MISO);
113}
114

Archive Download this file

Branches:
master
tornado-v1



interactive