⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardmng.c

📁 好记星的控件,包括button,list,对文件操作
💻 C
📖 第 1 页 / 共 5 页
字号:
    
    if(r1 != 0x00){
        DisableCard();                   //SET CS = 'H'
        if(CardInitial() == CARD_ERR_NO){
            goto reSend_w;
        }
        return CARD_ERR_CMDILLEGAL;     // command is illegal
    }
    
    for(i = 0; i < MIN_NWR; i++){       //wait NWR(min)
        SendBytes(DUMMY, 1);
    }

    SendBytes(DATA_TOKEN, 1);           // send data token
    SendBytes(buf, len);                // send data
    SendBytes(DUMMY, 1);                // 2 bytes CRC
    SendBytes(DUMMY, 1);

    //check the data response
    for(i = 0; i < 8; i++){             //should be at once
        ReceiveBytes(&r1, 1);

        r1 &= 0x1f;        
        if(r1 == 0x05){
            break;                      // data accepted
        }
        if(r1 == 0x0b || r1 == 0x0d){
            DisableCard();              //SET CS = 'H'
            return CARD_ERR_WRITE;      // data rejected due to CRC error or Write error
        }
    }
    if(i == 8){		
        DisableCard();                  //SET CS = 'H'
        return CARD_ERR_TIMEOUT;        // time out
    }

    // waiting for ready
    while(1){
        ReceiveBytes(&r1, 1);
        if(r1 == 0xff){
            break;
        }
    }

    DisableCard();                       //SET CS = 'H'
    
    return CARD_ERR_NO;
}

/****************************************************************************/
/* FUNCTION:   CardPowerOff                                                */
/* DESCRIPTION:turn of card                                                */
/* INPUTS:                                                                 */
/* OUTPUTS:                                                                */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   ===========================*/
/*   Victor     2004-07-30      create                                      */
/****************************************************************************/
void CardPowerOff(void)
{
    MMC_CS_ENABLE

    *((volatile UINT8 *)0x3003ad) &= ~0xfc;
    *((volatile UINT8 *)0x30038d) |= 0xe0;
    *((volatile UINT8 *)0x30038c) &= ~0xe0;

    MMC_PWR_DISABLE
}

void CardPowerOn(void)
{
    UINT i;

    CardPowerOff();				//wsl----121的是打开的
    
    // MMC power down for 100ms [note.]must be conformed by WAI
    for(i = 0; i < 0xcccc; i++){
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
    }

    for(i = 0; i < 0xcccc; i++){
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
    }

    // p16 is cs of the MMC in SPI model. set p16 high for dis-select MMC
    MMC_CS_DISABLE
    MMC_PWR_ENABLE

    // MMC power up for 100ms [note.]must be conformed by WAI
    for(i = 0; i < 0xcccc; i++){
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
    }

    for(i = 0; i < 0xcccc; i++){
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
    }

    // EFP31[1:0]=EFP32[1:0]=EFP33[1:0]=01(D[7:2]), set p31 to SDO. p32 to SPICLK, p33 to SDI
    *((volatile UINT8 *)0x3003ad) &= ~0xfc;
    *((volatile UINT8 *)0x3003ad) |= 0x54;
}

/****************************************************************************/
/* FUNCTION:   CardInitial                                                 */
/* DESCRIPTION:Initialize card                                             */
/* INPUTS                                                                   */
/* OUTPUTS:    CARD_ERR_NO         Initial OK                              */
/*              CARD_ERR_INEXISTENT card is not existent                    */
/*              CARD_ERR_FORMAT     file format is not supported            */
/*              output from ResetCard                                       */
/*              output from ConfigCard                                      */
/*              output from ReadBlock                                       */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   ==========================*/
/*   Victor     2004-07-30      create                                      */
/****************************************************************************/
const UINT8 password[8] = {'v','i','c','t','o','r','l','i'};
BOOL IsCardPasswordIn(void)
{
    UINT16          statusReg;
    // unlock card
    if(GetStatusReg(&statusReg) == CARD_ERR_NO){
        if((statusReg & 0x0100) == 0x0000){    // unlocked
            if(SetBlockLen(8+2) == CARD_ERR_NO){
                if(LockOperate((UINT8 *)password, 8, LOCK_LOCK_PASSWORD) == CARD_ERR_NO){
                    if(GetStatusReg(&statusReg) == CARD_ERR_NO){
                        if((statusReg & 0x0100) == 0x0100){    // locked
                            LockOperate((UINT8 *)password, 8, LOCK_UNLOCK_PASSWORD);
                            SetBlockLen(512);
                            return TRUE;
                        }else{
                            SetBlockLen(512);
                            return FALSE;
                        }
                    }else{
                        SetBlockLen(512);
                        return FALSE;
                    }
                }else{
                    SetBlockLen(512);
                    return FALSE;
                }
            }else{
                return FALSE;
            }
        }else{
            return TRUE;
        }
    }else{
        return FALSE;
    }
}
void UnlockCard(void)
{
    UINT16          statusReg;
    // unlock card
    if(GetStatusReg(&statusReg) == CARD_ERR_NO){
        if((statusReg & 0x0100) == 0x0100){    // locked
            if(SetBlockLen(8+2) == CARD_ERR_NO){
                LockOperate((UINT8 *)password, 8, LOCK_UNLOCK_PASSWORD);
                SetBlockLen(512);
            }
        }
    }
}
void LockCard(void)
{
    UINT16          statusReg;
    // unlock card
    if(GetStatusReg(&statusReg) == CARD_ERR_NO){
        if((statusReg & 0x0100) == 0x0000){    // unlocked
            if(SetBlockLen(8+2) == CARD_ERR_NO){
                LockOperate((UINT8 *)password, 8, LOCK_LOCK_PASSWORD);
                SetBlockLen(512);
            }
        }
    }
}
INT CardInitial(void)
{
    UINT            i, j;
    UINT16          statusReg;
    volatile INT    retVal;
    Delay10US(3000*2);
    for(i = 0; i < 8; i++){
        CardPowerOn();
        
        SetDefaultTransferCondition();
    
        /* Dummy clock 80 [clk] */
        for(j = 0; j < 10; j++){
            SendBytes(DUMMY, 1);
        }
    
        if((retVal = ResetCard()) == CARD_ERR_NO){
            if((retVal = ConfigCard()) == CARD_ERR_NO){
                UnlockCard();
/*
                // set password
                if(SetBlockLen(8+2) != CARD_ERR_NO){
                    asm("nop");
                }
                if(LockOperate((UINT8 *)password, 8, LOCK_SET_PASSWORD) != CARD_ERR_NO){
                    asm("nop");
                }
                if(GetStatusReg(&statusReg) != CARD_ERR_NO){
                    asm("nop");
                }
                if((statusReg & 0xff00) != 0){
                    asm("nop");
                }
*/
                // get card file system partition information
                if((retVal = ReadBlock(0x0, 512, block)) == CARD_ERR_NO){
                    if((block[0x1fe] != 0x55) || (block[0x1ff] != 0xaa)){
                        retVal = CARD_ERR_FORMAT;         // not fat format
                    }else{
                        if((block[0x36] == 'F') && (block[0x37] == 'A') && (block[0x38] == 'T') && (block[0x39] == '1') && (block[0x3a] == '6')){
		                        cardPartition.firstSector = 0;
		                        cardPartition.totalSector = *((UINT32 *)(&block[0x20]));
                            return CARD_ERR_NO;
                        }else{
		                        cardPartition.state = block[0x1be];
		                        cardPartition.beginHead = block[0x1bf];
		                        cardPartition.beginSector = block[0x1c0] & 0x3f;
		                        cardPartition.beginCylinder = (UINT16)block[0x1c1] + (((UINT16)(block[0x1c0] & 0xc0)) << 2);
		                        cardPartition.type = block[0x1c2];              // must be 4 or 6 means FAT16
		                        cardPartition.endHead = block[0x1c3];
		                        cardPartition.endSector = block[0x1c4] & 0x3f;
		                        cardPartition.endCylinder =  (UINT16)block[0x1c5] + (((UINT16)(block[0x1c4] & 0xc0)) << 2);;
		                        cardPartition.firstSector = (UINT32)block[0x1c6] + ((UINT32)block[0x1c7] << 8) + ((UINT32)block[0x1c8] << 16) + ((UINT32)block[0x1c9] << 24);
		                        cardPartition.totalSector = (UINT32)block[0x1ca] + ((UINT32)block[0x1cb] << 8) + ((UINT32)block[0x1cc] << 16) + ((UINT32)block[0x1cd] << 24);
		                        if((cardPartition.type != 0x04) && (cardPartition.type != 0x06)){
		                            retVal = CARD_ERR_FORMAT;       // not fat-16 format
		                        }else{
		                            return CARD_ERR_NO;
		                        }
		                    }
                    }
                }
            }
        }
    }

    return retVal;
}

/****************************************************************************/
/* FUNCTION:   CardIoCtrl                                                  */
/* DESCRIPTION:do nothing                                                  */
/* INPUTS:                                                                 */
/* OUTPUTS:                                                                */
/* RETURN:                                                                 */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   =======================*/
/*   Victor     2004-07-30      create                                      */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -