From f82d019db73133acc10754dae8afb44225f9d673 Mon Sep 17 00:00:00 2001 From: Leo C Date: Sat, 22 Nov 2014 19:40:59 +0100 Subject: [PATCH] Multidrive support for mmc.c (1. step) --- avr/mmc.c | 173 ++++++++++++++++++++++++------------------------------ 1 file changed, 77 insertions(+), 96 deletions(-) diff --git a/avr/mmc.c b/avr/mmc.c index eb10ea6..2592c45 100644 --- a/avr/mmc.c +++ b/avr/mmc.c @@ -5,16 +5,38 @@ /* are platform dependent. */ /*-----------------------------------------------------------------------*/ -#include +#include "common.h" #include #include "timer.h" #include "spi.h" #include "diskio.h" -#include "debug.h" -#include "print-utils.h" +//#include "debug.h" -/* Definitions for MMC/SDC command */ +/* Port Controls (Platform dependent) */ +/* SD card socket connections */ + +#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_CS_1 SBIT(PORTG,4) /* Chip select pin */ +#define SD_CS_1_IN SBIT(PING,4) +#define SD_CS_1_DDR SBIT(DDRG,4) + + +#define FCLK_SLOW() SPISetMMCInitClock() /* Set slow clock (100k-400k) */ +#define FCLK_FAST() SPISetFastClock() /* Set fast clock (depends on the CSD) */ + + +/*-------------------------------------------------------------------------- + Definitions for MMC/SDC command + ---------------------------------------------------------------------------*/ + #define CMD0 (0) /* GO_IDLE_STATE */ #define CMD1 (1) /* SEND_OP_COND (MMC) */ #define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */ @@ -34,64 +56,23 @@ #define CMD58 (58) /* READ_OCR */ -/* SD card socket connections */ -#if 0 -//#define MMC_PORT PORTG /* Socket contact port */ -//#define MMC_INPORT PING -//#define SD_WP_PIN 5 /* Write protect switch */ -#endif - -/* SD card SPI access */ -#define SD_CD_PORT PORTG -#define SD_CD_DDR DDRG -#define SD_CD_INPORT PING -#define SD_CD_PIN 3 /* Card detect switch */ - -#define SD_WP_PORT PORTG -#define SD_WP_DDR DDRG -#define SD_WP_INPORT PING -//#define SD_WP_PIN 5 /* Write protect switch */ - -#define SD_CS_PORT PORTG -#define SD_CS_DDR DDRG -#define SD_CS_PIN 4 /* Chip select pin */ - - -/* Port Controls (Platform dependent) */ - -#ifdef SD_CD_PIN -static inline -bool sd_cd(void) -{ - return (SD_CD_INPORT & _BV(SD_CD_PIN)) == 0; -} -#endif - -#ifdef SD_WP_PIN -static inline -bool sd_wp(void) -{ - return (SD_WP_INPORT & _BV(SD_WP_PIN)) == 0; -} -#endif - -#define CS_LOW() SD_CS_PORT &= ~(1< 1 ? CMD18 : CMD17; /* READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */ if (send_cmd(cmd, sector) == 0) { @@ -465,10 +446,10 @@ DRESULT disk_write ( ) { if (drv || !count) return RES_PARERR; - if (disk_stat & STA_NOINIT) return RES_NOTRDY; - if (disk_stat & STA_PROTECT) return RES_WRPRT; + if (socket[drv].stat & STA_NOINIT) return RES_NOTRDY; + if (socket[drv].stat & STA_PROTECT) return RES_WRPRT; - if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */ + if (!(socket[drv].CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */ if (count == 1) { /* Single block write */ if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */ @@ -476,7 +457,7 @@ DRESULT disk_write ( count = 0; } else { /* Multiple block write */ - if (CardType & CT_SDC) send_cmd(ACMD23, count); + if (socket[drv].CardType & CT_SDC) send_cmd(ACMD23, count); if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ do { if (!xmit_datablock(buff, 0xFC)) break; @@ -512,7 +493,7 @@ DRESULT disk_ioctl ( res = RES_ERROR; - if (disk_stat & STA_NOINIT) return RES_NOTRDY; + if (socket[drv].stat & STA_NOINIT) return RES_NOTRDY; switch (cmd) { case CTRL_SYNC : /* Make sure that no pending write process. Do not remove this or written sector might not left updated. */ @@ -535,7 +516,7 @@ DRESULT disk_ioctl ( break; case GET_BLOCK_SIZE: /* Get erase block size in unit of sector (DWORD) */ - if (CardType & CT_SD2) { /* SDv2? */ + if (socket[drv].CardType & CT_SD2) { /* SDv2? */ if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */ spi_rcvr(); if (rcvr_datablock(csd, 16)) { /* Read partial block */ @@ -547,7 +528,7 @@ DRESULT disk_ioctl ( } } else { /* SDv1 or MMCv3 */ if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */ - if (CardType & CT_SD1) { /* SDv1 */ + if (socket[drv].CardType & CT_SD1) { /* SDv1 */ *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); } else { /* MMCv3 */ *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); @@ -560,7 +541,7 @@ DRESULT disk_ioctl ( /* Following commands are never used by FatFs module */ case MMC_GET_TYPE: /* Get card type flags (1 byte) */ - *ptr = CardType; + *ptr = socket[drv].CardType; res = RES_OK; break; @@ -594,7 +575,7 @@ DRESULT disk_ioctl ( case CTRL_POWER_OFF : /* Power off */ power_off(); - disk_stat |= STA_NOINIT; + socket[drv].stat |= STA_NOINIT; res = RES_OK; break; @@ -617,21 +598,21 @@ void disk_timerproc (void) { BYTE s; - s = disk_stat; + s = socket[0].stat; -#ifdef SD_WP_PIN - if (sd_wp()) /* Write protected */ +#ifdef SD_WP_1 + if (SD_WP_1_IN == 0) /* Write protected */ s |= STA_PROTECT; else /* Write enabled */ s &= ~STA_PROTECT; #endif -#ifdef SD_CD_PIN - if (sd_cd()) /* Card inserted */ +#ifdef SD_CD_1 + if (SD_CD_1_IN == 0) /* Card inserted */ s &= ~STA_NODISK; else /* Socket empty */ s |= (STA_NODISK | STA_NOINIT); #endif - disk_stat = s; /* Update MMC status */ + socket[0].stat = s; /* Update MMC status */ } -- 2.39.2