00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00047 #include <cfg/clock.h>
00048 #include <cfg/mmci.h>
00049
00050 #include <dev/mmcard.h>
00051 #include <dev/spibus.h>
00052 #include <dev/gpio.h>
00053
00054 #ifdef MMC_CD_PORT
00055 #if (MMC_CD_PORT==NUTGPIO_PORTA)
00056 #define MMC_CD_SIGNAL sig_GPIO1
00057 #elif (MMC_CD_PORT==NUTGPIO_PORTB)
00058 #define MMC_CD_SIGNAL sig_GPIO2
00059 #elif (MMC_CD_PORT==NUTGPIO_PORTC)
00060 #define MMC_CD_SIGNAL sig_GPIO3
00061 #endif
00062 #endif
00063
00064 #if !defined(SPI_MMC_CLK)
00065 #define SPI_MMC_CLK 33000000
00066 #endif
00067
00068 #if !defined(SPI_MMC_CS)
00069 #define SPI_MMC_CS 1
00070 #endif
00071
00072
00078
00079 static int SpiMmcGpioReset(NUTDEVICE * dev);
00080 static void SpiMmcGpioLed(int mode);
00081 static int SpiMmcGpioPower(int mode);
00082
00086 static MEMCARDSUPP mcsSpiMmcGpio = {
00087 0,
00088 0,
00089 SpiMmcGpioReset,
00090 SpiMmcGpioLed,
00091 SpiMmcGpioPower,
00092 };
00093
00094 #if defined(MMC_CD_PORT) && defined(MMC_CD_PIN)
00095
00100 static void SpiMmcGpioIrq(void *arg)
00101 {
00102 MEMCARDSUPP *mcs = (MEMCARDSUPP *) arg;
00103
00104 if (GpioPinGet(MMC_CD_PORT, MMC_CD_PIN) == 0) {
00105
00106
00107
00108 mcs->mcs_cf = 1;
00109
00110 mcs->mcs_sf = NUTMC_SF_CD;
00111
00112 #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
00113 if (GpioPinGet(MMC_WP_PORT, MMC_WP_PIN) == 0) {
00114 mcs->mcs_sf |= NUTMC_SF_WP
00115 }
00116 #endif
00117 } else {
00118
00119
00120
00121 mcs->mcs_cf = 1;
00122
00123 mcs->mcs_sf = 0;
00124 }
00125 }
00126 #endif
00127
00135 static void SpiMmcGpioLed(int mode)
00136 {
00137 if (mode == NUTMC_IND_OFF) {
00138
00139 } else {
00140
00141 }
00142 }
00143
00154 static int SpiMmcGpioPower(int mode)
00155 {
00156 return -1;
00157 }
00158
00164 static int SpiMmcGpioReset(NUTDEVICE * dev)
00165 {
00166 NUTSPINODE * node;
00167 NUTSPIBUS * bus;
00168
00169 int rc;
00170
00171 node = (NUTSPINODE *) dev->dev_icb;
00172 bus = (NUTSPIBUS *) node->node_bus;
00173
00174
00175
00176
00177
00178 node->node_mode |= SPI_MODE_CSHIGH;
00179 rc = (bus->bus_alloc) (node, 1000);
00180 if (rc == 0) {
00181 uint8_t txb[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
00182 (bus->bus_transfer) (node, &txb, NULL, 10);
00183 }
00184 node->node_mode &= ~SPI_MODE_CSHIGH;
00185 (bus->bus_release) (node);
00186
00187
00188 SpiMmcGpioLed(NUTMC_IND_OFF);
00189
00190 return 0;
00191 }
00192
00201 static int SpiMmcGpioInit(NUTDEVICE * dev)
00202 {
00203 int rc;
00204
00205 SpiMmcGpioReset(dev);
00206
00207 #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
00208
00209 GpioPinConfigSet(MMC_WP_PORT, MMC_WP_PIN, GPIO_CFG_PULLUP);
00210 #endif
00211
00212 #if defined(MMC_CD_PORT) && defined(MMC_CD_PIN)
00213
00214 GpioPinConfigSet(MMC_CD_PORT, MMC_CD_PIN, GPIO_CFG_PULLUP);
00215
00216
00217
00218 rc = GpioRegisterIrqHandler(&MMC_CD_SIGNAL, MMC_CD_PIN, SpiMmcGpioIrq, dev->dev_dcb);
00219 if (rc == 0) {
00220 GpioIrqEnable(&MMC_CD_SIGNAL, MMC_CD_PIN);
00221
00222 rc = SpiMmcInit(dev);
00223 }
00224
00225 SpiMmcGpioIrq(dev->dev_dcb);
00226 #else
00227
00228 rc = SpiMmcInit(dev);
00229
00230
00231 ((MEMCARDSUPP *)dev->dev_dcb)->mcs_cf = 1;
00232
00233 ((MEMCARDSUPP *)dev->dev_dcb)->mcs_sf = NUTMC_SF_CD;
00234
00235 #if defined(MMC_WP_PORT) && defined(MMC_WP_PIN)
00236 if (GpioPinGet(MMC_WP_PORT, MMC_WP_PIN) == 0) {
00237 ((MEMCARDSUPP *)dev->dev_dcb)->mcs_sf |= NUTMC_SF_WP
00238 }
00239 #endif
00240
00241 #endif
00242 return rc;
00243 }
00244
00248 NUTSPINODE nodeSpiMmcGpio = {
00249 NULL,
00250 NULL,
00251 SPI_MMC_CLK,
00252 SPI_MODE_0,
00253 8,
00254 SPI_MMC_CS
00255 };
00256
00269 NUTDEVICE devSpiMmcGpio = {
00270 0,
00271 {'M', 'M', 'C', '0', 0, 0, 0, 0, 0}
00272 ,
00273 0,
00274 0,
00275 0,
00276 &nodeSpiMmcGpio,
00277 &mcsSpiMmcGpio,
00278 SpiMmcGpioInit,
00279 SpiMmcIOCtl,
00280 SpiMmcBlockRead,
00281 SpiMmcBlockWrite,
00282 #ifdef __HARVARD_ARCH__
00283 SpiMmcBlockWrite_P,
00284 #endif
00285 SpiMmcMount,
00286 SpiMmcUnmount,
00287 0
00288 };
00289