Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: Работа с MMC в Codevision
Форум разработчиков электроники ELECTRONIX.ru > Микроконтроллеры (MCs) > AVR
Krik99
Вот начал переделывать эту либу http://www.captain.at/electronic-atmega-mmc.php под CodeVision и вроде всё ОК, только как сделать в их функции чтения чтобы можно было выбирать сектор для чтения? Подскажите плиз, может кто уже делал с этой либой, а то хочется чтобы всё правельно работал. За ранние спасибо!
dimka76
Цитата(Krik99 @ Jun 19 2009, 21:14) *
как сделать в их функции чтения чтобы можно было выбирать сектор для чтения?


например, так как это сделано у мистера ChaN в его FatFs
Код
/* Definitions for MMC/SDC command */
#define CMD0    (0x40+0)    /* GO_IDLE_STATE */
#define CMD1    (0x40+1)    /* SEND_OP_COND (MMC) */
#define    ACMD41    (0xC0+41)    /* SEND_OP_COND (SDC) */
#define CMD8    (0x40+8)    /* SEND_IF_COND */
#define CMD9    (0x40+9)    /* SEND_CSD */
#define CMD10    (0x40+10)    /* SEND_CID */
#define CMD12    (0x40+12)    /* STOP_TRANSMISSION */
#define ACMD13    (0xC0+13)    /* SD_STATUS (SDC) */
#define CMD16    (0x40+16)    /* SET_BLOCKLEN */
#define CMD17    (0x40+17)    /* READ_SINGLE_BLOCK */
#define CMD18    (0x40+18)    /* READ_MULTIPLE_BLOCK */
#define CMD23    (0x40+23)    /* SET_BLOCK_COUNT (MMC) */
#define    ACMD23    (0xC0+23)    /* SET_WR_BLK_ERASE_COUNT (SDC) */
#define CMD24    (0x40+24)    /* WRITE_BLOCK */
#define CMD25    (0x40+25)    /* WRITE_MULTIPLE_BLOCK */
#define CMD55    (0x40+55)    /* APP_CMD */
#define CMD58    (0x40+58)    /* READ_OCR */


BYTE CardType;            /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */


/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
    BYTE drv,            /* Physical drive nmuber (0) */
    BYTE *buff,            /* Pointer to the data buffer to store read data */
    DWORD sector,        /* Start sector number (LBA) */
    BYTE count            /* Sector count (1..255) */
)
{
//    if (drv || !count) return RES_PARERR;
//    if (Stat & STA_NOINIT) return RES_NOTRDY;

    if (!(CardType & 8)) sector *= 512;    /* Convert to byte address if needed */

    if (count == 1) {    /* Single block read */
        if ((send_cmd(CMD17, sector) == 0)    /* READ_SINGLE_BLOCK */
            && rcvr_datablock(buff, 512))
            count = 0;
    }
    else {                /* Multiple block read */
        if (send_cmd(CMD18, sector) == 0) {    /* READ_MULTIPLE_BLOCK */
            do {
                if (!rcvr_datablock(buff, 512)) break;
                buff += 512;
            } while (--count);
            send_cmd(CMD12, 0);                /* STOP_TRANSMISSION */
        }
    }
    release_spi();

    return count ? RES_ERROR : RES_OK;
}
Krik99
Код
....
char Command(unsigned char befF, unsigned int AdrH, unsigned int AdrL, unsigned char befH ){    // sends a command to the MMC
    SPI(0xFF);
    SPI(befF);
    SPI((unsigned char)(AdrH >> 8));
    SPI((unsigned char)AdrH);
    SPI((unsigned char)(AdrL >> 8));
    SPI((unsigned char)AdrL);
    SPI(befH);
    SPI(0xFF);
    return SPI(0xFF);    // return the last received character
}  

int MMC_Init(void) { // init SPI
    unsigned char i;                
    PORTB.4=1; // disable MMC
    // start MMC in SPI mode
    for(i=0; i < 10; i++) SPI(0xFF); // send 10*8=80 clock pulses
    PORTB.4=0; // enable MMC

    if (Command(0x40,0,0,0x95) !=1) goto mmcerror; // reset MMC

st: // if there is no MMC, prg. loops here    
    if (Command(0x41,0,0,0xFF) !=0) goto st;
    return 1;
mmcerror:      
    return 0;
}  
.....
int sendmmc(void) { // send 512 bytes from the MMC via the serial port
    unsigned int i;  
    usart_tx_text("MMC read \r\n");
    // 512 byte-read-mode
    if (Command(0x51,0,512,0xFF)!= 0){usart_tx_text("MMC: read error 1 \r\n"); return 1; }
    // wait for 0xFE - start of any transmission
    // ATT: typecast (char)0xFE is a must!
    while(SPI(0xFF)!=(char)0xFE);
        
    for(i=0; i < 512; i++) {
        while(!(UCSRA & (1 << 5))); // wait for serial port
        UDR = SPI(0xFF);  // send character
    }    
        usart_tx_text("\r\n");
    // at the end, send 2 dummy bytes
    SPI(0xFF); // actually this returns the CRC/checksum byte
    SPI(0xFF);
    return 0;
}
......

Да ну переписывать весь код, вот мой подскажите плиз что надо изменить.
dimka76
Цитата(Krik99 @ Jun 20 2009, 09:33) *
Код
....

int sendmmc(void) { // send 512 bytes from the MMC via the serial port
    .....
    // 512 byte-read-mode
    if (Command(0x51,0,512,0xFF)!= 0){usart_tx_text("MMC: read error 1 \r\n"); return 1; }
    // wait for 0xFE - start of any transmission
    // ATT: typecast (char)0xFE is a must!
    while(SPI(0xFF)!=(char)0xFE);
        
    for(i=0; i < 512; i++) {
        while(!(UCSRA & (1 << 5))); // wait for serial port
        UDR = SPI(0xFF);  // send character
    }    
    .......
    return 0;
}
......

Да ну переписывать весь код, вот мой подскажите плиз что надо изменить.



в вызове функции
Код
Command(0x51,0,512,0xFF);


второй и третий параметр это адрес, вот и меняйте его, точнее достаточно, наверное, только второй параметр менять, а остальное оставить как есть.
или переписать функцию
Код
int sendmmc(void)

вот так
Код
int sendmmc(unsigned int sector)
{
....
}

а функцию
Код
Command(0x51,0,512,0xFF);
вызывать вот так
Код
Command(0x51,sector,512,0xFF);


короче
Код
int sendmmc(unsigned int sector)
{
....
    
   Command(0x51,sector,512,0xFF);

.....
}
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.