]> cloudbase.mooo.com Git - z180-stamp.git/commitdiff
Card detect over cs pin: clean initialisation/power up
authorLeo C <erbl259-lmu@yahoo.de>
Thu, 4 Dec 2014 23:09:29 +0000 (00:09 +0100)
committerLeo C <erbl259-lmu@yahoo.de>
Thu, 4 Dec 2014 23:09:29 +0000 (00:09 +0100)
avr/cmd_sd.c
avr/gpio.c
avr/main.c
avr/mmc.c

index f43725af7ec590c437a267faaeaf1294810c2ef1..46dbcfdc3ff30619f80f1593338e9a2a5a59a839 100644 (file)
@@ -330,18 +330,18 @@ CMD_TBL_ITEM(
 ),
 CMD_TBL_ITEM(
        dump,   CONFIG_SYS_MAXARGS,     1,      do_dump,
-       "Dump sector",
-       ""
+       "Dump sector(s)",
+       "<drive> [sector [count ]]"
 ),
 CMD_TBL_ITEM(
        read,   2,                      1,      do_read,
        "Read disk sector(s) into meomory",
-       "drive [sector [count [memaddr]]]"
+       "<drive> [sector [count [memaddr]]]"
 ),
 CMD_TBL_ITEM(
        write,  2,                      1,      do_write,
        "Write sector(s) from meomory to disk",
-       "drive [sector [count [memaddr]]]"
+       "<drive> [sector [count [memaddr]]]"
 ),
 CMD_TBL_ITEM(
        sync,   2,                      1,      do_ioctl_sync,
index 3c47247ef64baf90ff66b18e198713cec3c97cf6..31dbc21f46393c1adfd58fe7ac8719b307d60e79 100644 (file)
@@ -7,36 +7,35 @@
 
 /*
 
-Pin    Name    Port    Timer   Mode    max div         max div         min f [Hz]
-----------------------------------------------------------------------------------
-0              PG5     OC0B    PWM     (2**8)*1024       262144        70.31
-1              PG4
-2      CLK2    PB4     OC2A    Toggle  (2**8)*1024*2     524288        35.16
-3      ZCLK    PB5     OC1A    PWM     (2**16)*1024    67108864        0.2746
-4              PB6     OC1B    PWM     (2**16)*1024    67108864        0.2746
-5              PB7     OC0A    Toggle  (2**8)*1024*2     524288        35.16
-6              PG3
-7              PG2
-8              PG1
-9              PG0
-10     CLKO    PE7
-
-
-pre    Timer0          Timer1          Timer2
+Pin Name    Port    Timer   Mode    max div         max div     min f [Hz]
+--------------------------------------------------------------------------------
+0           PG5     OC0B    PWM     (2**8)*1024     262144      70.31
+1           PG4
+2   CLK2    PB4     OC2A    Toggle  (2**8)*1024*2   524288      35.16
+3   ZCLK    PB5     OC1A    PWM     (2**16)*1024    67108864    0.2746
+4           PB6     OC1B    PWM     (2**16)*1024    67108864    0.2746
+5           PB7     OC0A    Toggle  (2**8)*1024*2   524288      35.16
+6           PG3
+7           PG2
+8           PG1
+9           PG0
+10  CLKO    PE7
+--------------------------------------------------------------------------------
+
+
+pre Timer0      Timer1      Timer2
 --------------------------------------------------
-0      0               0               0
-1      1               1               1
-2      8       x8      8       x8      8       x8
-3      64      x8      64      x8      32      x4
-4      256     x4      256     x4      64      x2
-5      1024    x4      1024    x4      128     x2
-6                                      256     x2
-7                                      1024    x4
+0   0           0           0
+1   1           1           1
+2   8    x8     8    x8     8    x8
+3   64   x8     64   x8     32   x4
+4   256  x4     256  x4     64   x2
+5   1024 x4     1024 x4     128  x2
+6                           256  x2
+7                           1024 x4
 --------------------------------------------------
-
 */
 
-
 #define PWMTOGGLE      0b01
 #define PWMPOS         0b10
 #define PWMNEG         0b11
@@ -312,7 +311,7 @@ int gpio_clockdiv_set(int pin, unsigned long divider)
 
        top = ltop - 1;
 
-       PING |= _BV(0);         /* Debug */
+//     PING |= _BV(0);         /* Debug */
 
        switch (timertype & TIMER) {
        case TIMER0:
@@ -363,7 +362,7 @@ int gpio_clockdiv_set(int pin, unsigned long divider)
                break;
        }
 
-       PING |= _BV(0);         /* Debug */
+//     PING |= _BV(0);         /* Debug */
 
        gpio_config(pin, OUTPUT_TIMER);
 
index 8acf265b5b430f319780724655899aacb9ba2b64..f601736922eec785633f8201729dd45118968bac 100644 (file)
@@ -18,7 +18,6 @@
 #include "cli.h"
 #include "env.h"
 #include "z180-serv.h"
-#include "spi.h"
 #include "gpio.h"
 #include "time.h"
 #include "rtc.h"
@@ -112,13 +111,6 @@ void setup_avr(void)
        TCCR3B = (0b01<<WGM32)|(0b001<<CS30); /* CTC Mode, Prescaler 1 */
        TIMSK3 = _BV(OCIE3A);           /* Enable TC2.oca interrupt */
 
-       /* SPI as master */
-       PRR0 &= ~_BV(PRSPI);
-       SPI_DDR = (SPI_DDR & ~_BV(SPI_MISO))
-                       | _BV(SPI_MOSI) | _BV(SPI_SCK) | _BV(SPI_SS);
-       SPI_PORT = (SPI_PORT & ~(_BV(SPI_MOSI) | _BV(SPI_SCK)))
-                       | _BV(SPI_SS);
-
        /* INT5, INT6: falling edge */
        EICRB = (EICRB & ~((0b11 << ISC50) | (0b11 << ISC60))) |
                (0b10 << ISC50) | (0b10 << ISC60);
@@ -240,10 +232,13 @@ void main_loop(void)
 
 int main(void)
 {
-       setup_avr();
-       z80_setup_bus();
+       extern void setup_mmc(void);
+
        for (int i = 0; i < GPIO_MAX; i++)
                gpio_config(i, INPUT_PULLUP);
+       setup_avr();
+       setup_mmc();
+       z80_setup_bus();
        env_init();
 
        if (reset_reason_is_power_on())
index afb1473627bcfab089788b47cd735456bbddec0c..12f58f3020e47f5186f678595ef6601fc571ab7e 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
@@ -189,50 +192,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 +381,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 +735,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