]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/mmc.c
SD slot 0: Enable card detection
[z180-stamp.git] / avr / mmc.c
index afb1473627bcfab089788b47cd735456bbddec0c..80152f83500483b6f442cef4d235195f11e14571 100644 (file)
--- a/avr/mmc.c
+++ b/avr/mmc.c
 /* Port Controls  (Platform dependent) */
 /* SD card socket connections */
 
-//#define SD_CD_0      SBIT(PORT,)             /* Card detect switch */
+/* TODO: config.h cofig macros */
+
+//#define SD_CD_0              SBIT(PORT,)             /* Card detect switch */
 //#define SD_CD_0_IN   SBIT(PIN,)
 //#define SD_CD_0_DDR  SBIT(DDR,)
 
-//#define SD_WP_0      SBIT(PORT,)             /* Write protect switch */
+//#define SD_WP_0              SBIT(PORT,)             /* Write protect switch */
 //#define SD_WP_0_IN   SBIT(PIN,)
 //#define SD_WP_0_DDR  SBIT(DDR,)
 
-#define SD_CS_0                SBIT(PORTB,0)           /* Chip select pin */
+#define SD_CS_0                SBIT(PORTB,0)   /* Chip select pin */
 #define SD_CS_0_IN     SBIT(PINB,0)
 #define SD_CS_0_DDR    SBIT(DDRB,0)
 
-//#define SD_CD_1              SBIT(PORTG,3)           /* Card detect switch */
+
+//#define SD_CD_1              SBIT(PORTG,3)   /* Card detect switch */
 //#define SD_CD_1_IN   SBIT(PING,3)
 //#define SD_CD_1_DDR  SBIT(DDRG,3)
 
-//#define SD_WP_1      SBIT(PORTG,5)           /* Write protect switch */
-#define SD_WP_1_IN     SBIT(PING,5)
-#define SD_WP_1_DDR    SBIT(DDRG,5)
+//#define SD_WP_1              SBIT(PORTG,5)   /* Write protect switch */
+//#define SD_WP_1_IN   SBIT(PING,5)
+//#define SD_WP_1_DDR  SBIT(DDRG,5)
 
 #define SD_CS_1                SBIT(PORTG,4)           /* Chip select pin */
 #define SD_CS_1_IN     SBIT(PING,4)
@@ -43,7 +46,7 @@
 
 
 #define        SPI_CLK_SLOW()  SPISetMMCInitClock()    /* Set slow clock (100k-400k) */
-#define        SPI_CLK_FAST()  SPISetFastClock()       /* Set fast clock (depends on the CSD) */
+#define        SPI_CLK_FAST()  SPISetFastClock()               /* Set fast clock (depends on the CSD) */
 
 /*--------------------------------------------------------------------------
  Definitions for MMC/SDC command
@@ -128,11 +131,12 @@ void deselect(BYTE drv)
        SPI_OFF();
 
        if (drv == 0) {
-#ifndef SD_CD_0
-               // SD_CS_0 = 1;
+#ifdef SD_CS_0_IN
+               SD_CS_0_DDR = 0;
+               SD_CS_0 = 0;
 #endif
        } else {
-#ifndef SD_CD_1
+#ifdef SD_CS_1_IN
                SD_CS_1_DDR = 0;
                SD_CS_1 = 0;
 #endif
@@ -148,10 +152,14 @@ static
 int select(BYTE drv)   /* 1:Successful, 0:Timeout */
 {
        //debug("*** enter select()\n");
-       if (drv == 0)
+       if (drv == 0) {
+#ifdef SD_CS_0_IN
+               SD_CS_0 = 1;
+               SD_CS_0_DDR = 1;
+#endif
                SD_CS_0 = 0;
-       else {
-#ifndef SD_CD_1
+       else {
+#ifdef SD_CS_1_IN
                SD_CS_1 = 1;
                SD_CS_1_DDR = 1;
 #endif
@@ -189,50 +197,18 @@ void power_on(BYTE drv)
 
        if (drv == 0) {
 #ifdef SD_PWR_0
-               SD_PWR_0_DDR = 1;       /* Turns on PWR pin as output */
                SD_PWR_0 = 0;           /* Drives PWR pin high */
-
-               for (uint32_t to = get_timer(0); get_timer(to) < 30;)
-                       ; /* Wait for 30ms */
 #endif
 
-#ifdef SD_WP_0
-               SD_WP_0_DDR = 0;
-               SD_WP_0 = 1;
-#endif
-#ifdef SD_CD_0
-               /* Card detect, input with pullup */
-               SD_CD_0_DDR = 0;
-               SD_CD_0 = 1;
-#endif
-               SD_CS_0 = 1;
-               SD_CS_0_DDR = 1;
        } else {
 #ifdef SD_PWR_1
-               SD_PWR_1_DDR = 1;       /* Turns on PWR pin as output */
                SD_PWR_1 = 0;           /* Drives PWR pin high */
-
+#endif
+       }
+#if defined SD_PWR_0 || defined SD_PWR_1
                for (uint32_t to = get_timer(0); get_timer(to) < 30;)
                        ; /* Wait for 30ms */
 #endif
-
-#ifdef SD_WP_1
-               SD_WP_1_DDR = 0;
-               SD_WP_1 = 1;
-#endif
-#ifdef SD_CD_1
-               /* Card detect, input with pullup */
-               SD_CD_1_DDR = 0;
-               SD_CD_1 = 1;
-               SD_CS_1 = 1;
-               SD_CS_1_DDR = 1;
-#else
-               /* Input, no pullup */
-               SD_CS_1 = 0;
-               SD_CS_1_DDR = 0;
-#endif
-       }
-
        //debug("***  exit power_on()\n");
 }
 
@@ -410,6 +386,55 @@ BYTE send_cmd (            /* Returns R1 resp (bit7==1:Send failed) */
 
  ---------------------------------------------------------------------------*/
 
+void setup_mmc(void)
+{
+#ifdef SD_PWR_0
+               SD_PWR_1 = 1;           /* Drives PWR pin low */
+               SD_PWR_0_DDR = 1;       /* Turns on PWR pin as output */
+#endif
+#ifdef SD_WP_0
+               SD_WP_0_DDR = 0;
+               SD_WP_0 = 1;            /* Pullup */
+#endif
+
+#ifdef SD_PWR_1
+               SD_PWR_1 = 1;           /* Drives PWR pin low */
+               SD_PWR_1_DDR = 1;       /* Turns on PWR pin as output */
+#endif
+#ifdef SD_WP_1
+               SD_WP_1_DDR = 0;
+               SD_WP_1 = 1;            /* Pullup */
+#endif
+
+       /* SPI as master */
+       PRR0 &= ~_BV(PRSPI);
+       SPI_DDR = (SPI_DDR & ~(_BV(SPI_MISO) | _BV(SPI_SS)))
+                       | _BV(SPI_MOSI) | _BV(SPI_SCK);
+       SPI_PORT = SPI_PORT & ~(_BV(SPI_MOSI) | _BV(SPI_SCK));
+
+#if defined SD_CD_0
+       SD_CD_0_DDR = 0;
+       SD_CD_0 = 1;
+#elif defined SD_CS_0_IN
+       SD_CS_0_DDR = 0;
+       SD_CS_0 = 0;
+#else
+       SD_CS_0_DDR = 1;
+       SD_CS_0 = 1;
+#endif
+
+#if defined SD_CD_1
+       SD_CD_1_DDR = 0;
+       SD_CD_1 = 1;
+#elif defined SD_CS_1_IN
+       SD_CS_1_DDR = 0;
+       SD_CS_1 = 0;
+#else
+       SD_CS_1_DDR = 1;
+       SD_CS_1 = 1;
+#endif
+}
+
 /*-----------------------------------------------------------------------*/
 /* Initialize Disk Drive                                                 */
 /*-----------------------------------------------------------------------*/
@@ -715,34 +740,43 @@ void disk_timerproc (void)
 #ifdef SD_WP_0
        if (SD_WP_0_IN == 0)                    /* Write protected */
                s |= STA_PROTECT;
-       else                                    /* Write enabled */
+       else                                                    /* Write enabled */
                s &= ~STA_PROTECT;
 #endif
-#ifdef SD_CD_0
+
+#if defined SD_CD_0
        if (SD_CD_0_IN == 0)                    /* Card inserted */
                s &= ~STA_NODISK;
-       else                                    /* Socket empty */
+       else                                                    /* Socket empty */
                s |= (STA_NODISK | STA_NOINIT);
+#elif defined SD_CS_0_IN
+       if (SD_CS_0_DDR == 0) {
+               if (SD_CS_0_IN == 1)            /* Card inserted */
+                       s &= ~STA_NODISK;
+               else                                            /* Socket empty */
+                       s |= (STA_NODISK | STA_NOINIT);
+       }
 #endif
-       socket[0].stat = s;                     /* Update MMC status */
+       socket[0].stat = s;                             /* Update MMC status */
 
        s = socket[1].stat;
 #ifdef SD_WP_1
        if (SD_WP_1_IN == 0)                    /* Write protected */
                s |= STA_PROTECT;
-       else                                    /* Write enabled */
+       else                                                    /* Write enabled */
                s &= ~STA_PROTECT;
 #endif
-#ifdef SD_CD_1
+
+#if defined SD_CD_1
        if (SD_CD_1_IN == 0)                    /* Card inserted */
                s &= ~STA_NODISK;
-       else                                    /* Socket empty */
+       else                                                    /* Socket empty */
                s |= (STA_NODISK | STA_NOINIT);
-#else
+#elif defined SD_CS_1_IN
        if (SD_CS_1_DDR == 0) {
-               if (SD_CS_1_IN == 1)                    /* Card inserted */
+               if (SD_CS_1_IN == 1)            /* Card inserted */
                        s &= ~STA_NODISK;
-               else                                    /* Socket empty */
+               else                                            /* Socket empty */
                        s |= (STA_NODISK | STA_NOINIT);
        }
 #endif