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

📄 cardmng.c

📁 好记星的控件,包括button,list,对文件操作
💻 C
📖 第 1 页 / 共 5 页
字号:
/****************************************************************************/
/*                                                                          */
/*              Copyright (C) 2005 SHENZHEN MEIJIN CO.LTD                   */
/*                                                                          */
/* FILE NAME:   Main.c                                                      */
/* MODULE NAME: 初始化模块                                                  */
/* DESCRIPTION: 本文件硬件初始化后的主函数                                  */
/*                                                                          */
/* FUNCTIONS:                                                               */
/*                                                                          */
/****************************************************************************/
/*    NAME         DATE               REMARKS                               */
/* ==========  ============   ==============================================*/
/*   谢永良     2005-04-13          创建                                    */
/****************************************************************************/
#include "Kernel.h"

//#define __PCB_VER_10__

#define REG_SPI_RXD     	(0x301700)
#define REG_SPI_TXD     	(0x301704)
#define REG_SPI_CTL1    	(0x301708)
#define REG_SPI_CTL2    	(0x30170C)

#define REG_SPI_WAIT    	(0x301710)
#define REG_SPI_STAT    	(0x301714)
#define REG_SPI_INT     	(0x301718)
#define REG_SPI_RXMK    	(0x30171C)

#define REG_SPI_PMON    	(0x301720)

#ifdef __PCB_VER_10__
#define MMC_CS_ENABLE       *((volatile UINT8 *)0x300c01) &= ~0x08;     // PA3
#define MMC_CS_DISABLE      *((volatile UINT8 *)0x300c01) |= 0x08;      // PA3
#define MMC_PWR_ENABLE      *((volatile UINT8 *)0x30038c) &= ~0x01;     // P60
#define MMC_PWR_DISABLE     *((volatile UINT8 *)0x30038c) |= 0x01;      // P60
#define MMC_IS_WP           ( (*((volatile UINT8 *)0x30038c) & 0x10) == 0 )     // P64
#define MMC_IS_INSERT       ( (*((volatile UINT8 *)0x30038a) & 0x01) == 0 )     // P50
#else
#define MMC_CS_ENABLE       *((volatile UINT8 *)0x300c01) &= ~0x08;     // PA3
#define MMC_CS_DISABLE      *((volatile UINT8 *)0x300c01) |= 0x08;      // PA3
#define MMC_PWR_ENABLE      *((volatile UINT8 *)0x300c01) &= ~0x04;     // PA2
#define MMC_PWR_DISABLE     *((volatile UINT8 *)0x300c01) |= 0x04;      // PA2
#define MMC_IS_WP           ( (*((volatile UINT8 *)0x30038e) & 0x04) == 0 )     // P72
#define MMC_IS_INSERT       ( (*((volatile UINT8 *)0x30038a) & 0x04) == 0 )     // P52
#endif

//#define DUMMY       0xff
const UINT8 data_dummy = 0xff;	//121
#define DUMMY       ((UINT8 *)(&data_dummy))
//#define DATA_TOKEN  SINGLE_WRITE_START
#define DATA_TOKEN  ((UINT8 *)(&data_token))
#define PAGE_SIZE   512

UINT32          regOCR;
REG_CID         regCID;
REG_CSD         regCSD;
UINT8           _regCSD[18];         // Size of CSD is 16 bytes, CRC is 2 bytes
CARD_PARTITION  cardPartition;
UINT8           block[512];
UINT            maxNAC;


const UINT8 ucpCmd0[]  = {0x40, 0x00, 0x00, 0x00, 0x00, 0x95};   //CMD0  resets the multimedia card
const UINT8 ucpCmd1[]  = {0x41, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD1  activates MMC card's initialization process
const UINT8 ucpCmd9[]  = {0x49, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD9  asks the card to send its CSD
const UINT8 ucpCmd10[] = {0x4a, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD10 asks the card to send its CID
const UINT8 ucpCmd13[] = {0x4d, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD13 asks the card to send its status registor
const UINT8 ucpCmd17[] = {0x51, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD17 read a single block
const UINT8 ucpCmd55[] = {0x77, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD55 followed by application command(ACMD)
const UINT8 ucpCmd58[] = {0x7a, 0x00, 0x00, 0x00, 0x00, 0xff};   //CMD58 reads OCR
const UINT8 ucpACmd41[]= {0x69, 0x00, 0x00, 0x00, 0x00, 0xff};   //ACMD41 activates SD card's initialization process
const UINT8 ucpACmd42[]= {0x6a, 0x00, 0x00, 0x00, 0x00, 0xff};   //ACMD42 lock/unlock

const UINT32 timeUnit[] = {1, 1, 1, 1, 10, 100, 1000, 10000};
const UINT32 timeValue[] = {0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8};
//=======以下是121移植来的

#define EV_CARD                 0x0020      // type
#define CARD_INSERT             0x0         // xParam
#define CARD_REMOVE             0x1         // xParam

#define STACK_SIZE 	2048
typedef void (* ISRFUNC)(INT Param);

NU_HISR         CardHISRControl;
UINT            maxNAC;

const UINT8 cucCRC7tbl[] = {0x00,0x09,0x12,0x1B,0x24,0x2D,0x36,0x3F,0x48,0x41,0x5A,0x53,0x6C,0x65,0x7E,0x77};

const UINT8 data_token = SINGLE_WRITE_START;


static NU_TIMER g_mCardTimer;
static BOOL g_bCardConnect = FALSE;
VOID __CardTimerFunc(UNSIGNED Param);

/****************************************************************************/
/* FUNCTION:   CRC7                                                        */
/* DESCRIPTION:caculate crc-7                                              */
/* INPUTS:     buf                   string will be caculated crc-7        */
/*              len                   length of the string                  */
/* OUTPUTS:                                                                */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   =================*/
/*   Victor     2004-09-25      create                                      */
/****************************************************************************/
UINT8 CRC7(UINT8 *buf, UINT len)
{
    UINT    i;
    UINT8   crc7;

    //crc7 = 0x00;
    crc7 = 0xff;
	
    for (i = 0; i < len; i++){
        crc7 = cucCRC7tbl[crc7 >> 4] ^ ((crc7 << 4) | (buf[i] >> 4));
        crc7 = cucCRC7tbl[crc7 >> 4] ^ ((crc7 << 4) | (buf[i] & 0x0f));
        //crc7 ^= cucCRC7tbl[(buf[i] >> 4)];
        //crc7 ^= cucCRC7tbl[(buf[i] & 0x0f)];
	}
	
	return (crc7 << 1) + 1;
}

BOOL CheckCrc7(unsigned char *p, unsigned int count)
{
    unsigned int crc;
    unsigned char ch;
    unsigned int i;
    signed int j;
    
    crc = 0;

    for(j = 0; j < count - 1; j++) {
        ch = p[j];
        for(i = 0; i < 8; i ++){
            crc = (crc << 1) + (unsigned int)(ch >> 7);
            ch = ch << 1;
            if(crc >= 0x89){
                crc -= 0x89;
            }
        }
    }

    for(i = 0; i < 7; i ++){
        crc = crc << 1;
        if(crc >= 0x89){
            crc -= 0x89;
        }
    }

    crc = (crc << 1) + 1;
    
    if(crc == p[count-1]){
        return TRUE;
    }else{
        return FALSE;
    }
}

/****************************************************************************/
/* FUNCTION:   InitialBCU                                                  */
/* DESCRIPTION:initial BCU for card                                        */
/* INPUTS:                                                                 */
/* OUTPUTS:                                                                */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   ==============================================*/
/*   Victor     2004-07-29      create                                      */
/****************************************************************************/
void InitialBCU(void)		//wsll--原来是屏蔽的
{
    /*
    // CEFUNC = 01
    *((volatile UINT16 *)0x48130) &= ~0x0600;
    *((volatile UINT16 *)0x48130) |= 0x0200;

    // maskrom wait cycle(48MHz x2 model internal=1.8V external=3.3V)
    *((volatile UINT16 *)0x48124) = 0x0012;         //area 11(high 4M for MaskRom) 
    *((volatile UINT16 *)0x48126) = 0x7012;         //area 10(low 4M for MaskRom)
    
    *((volatile UINT16 *)0x48132) |= 0x0200;        // A6IO(D9) = 1 A6 internal access
    *((volatile UINT16 *)0x48132) &= ~0x0002;       // A6EC(D1) = 0 little endian format
    *((volatile UINT16 *)0x4812A) &= ~0x0700;       // clear A6WT[2:0](D[A:8]) in 48MHz x2 model
    */
}

/****************************************************************************/
/* FUNCTION:   InitialIO                                                   */
/* DESCRIPTION:initial I/O port for card                                   */
/* INPUTS:                                                                 */
/* OUTPUTS:                                                                */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   ==============================================*/
/*   Victor     2004-07-29      create                                      */
/****************************************************************************/
void InitialIO(void)
{
#ifdef __PCB_VER_10__
    // set PA3 as #MMCCS
    *((volatile UINT8 *)0x300c20) &= ~0xC0;          // PA3 as port
    *((volatile UINT8 *)0x300c00) |= 0x08;           // set PA3 as output
    MMC_CS_DISABLE

    // set P60 as #MMCPWR
    *((volatile UINT8 *)0x3003ac) &= ~0x03;          // P60 as port
    *((volatile UINT8 *)0x30038d) |= 0x01;          // set P60 as output
    MMC_PWR_DISABLE

    // set P64 as #MMCWP
    *((volatile UINT8 *)0x3003ad) &= ~0x03;          //  P64 as port
    *((volatile UINT8 *)0x30038d) &= ~0x10;          // set P64 as input

    //set P50 as #MMCCD
    *((volatile UINT8 *)0x3003aa) &= ~0x03;         //selcet P50
    *((volatile UINT8 *)0x30038b) &= ~0x01;         //set P50 is input

    //set P65-P67 as MMCSDO,MMCCLK,MMCSDI
    *((volatile UINT8 *)0x3003ad) &= ~0xFC;
    *((volatile UINT8 *)0x3003ad) |= 0x54;
#else
    // set PA3 as #MMCCS
    *((volatile UINT8 *)0x300c20) &= ~0xC0;          // PA3 as port
    *((volatile UINT8 *)0x300c00) |= 0x08;           // set PA3 as output
    MMC_CS_DISABLE

    // set PA2 as #MMCPWR
    *((volatile UINT8 *)0x300c20) &= ~0x30;          // PA2 as port
    *((volatile UINT8 *)0x300c00) |= 0x04;          // set PA2 as output
    MMC_PWR_DISABLE

    // set P72 as #MMCWP
    *((volatile UINT8 *)0x3003ae) &= ~0x30;          //  P72 as port,this port only has input function.

    //set P52 as #MMCCD
    *((volatile UINT8 *)0x3003aa) &= ~0x30;         //selcet P52
    *((volatile UINT8 *)0x30038b) &= ~0x04;         //set P52 is input

    //set P65-P67 as MMCSDO,MMCCLK,MMCSDI
    *((volatile UINT8 *)0x3003ad) &= ~0xFC;
    *((volatile UINT8 *)0x3003ad) |= 0x54;
#endif
}

/****************************************************************************/
/* FUNCTION:   SetDefaultTransferCondition                                 */
/* DESCRIPTION:set default transfer condition for card in initial period   */
/* INPUTS:                                                                 */
/* OUTPUTS:                                                                */
/****************************************************************************/
/*    NAME        DATE                REMARKS                               */
/* ==========  ============   ==============================================*/
/*   Victor     2004-07-29      create                                      */
/****************************************************************************/
void SetDefaultTransferCondition(void)
{
    *((volatile UINT32 *)REG_SPI_CTL1) &= ~0x0001;       // set ENA(D0) to 0(SPI circuit is off)

    // Set MODE(D1)=Master
    *((volatile UINT32 *)REG_SPI_CTL1) |= 0x0002;

    // Set transfer data bit length: BPT[3:0](D[D:A]) = 7, data bit length = 7+1 = 8 
    *((volatile UINT32 *)REG_SPI_CTL1) &= ~0x3c00;
    *((volatile UINT32 *)REG_SPI_CTL1) |= 0x1c00;
    
    // Set transfer clock(fOD < 400KHz): MCBR[2:0](D[6:4]) = 7, clock = 48MHz/512 = 187500Hz/2
    *((volatile UINT32 *)REG_SPI_CTL1) &= ~0x0070;
    *((volatile UINT32 *)REG_SPI_CTL1) |= 0x0070;
    
    // Set transfer timing: CPOL(D8)=0, CPHA(D9)=0      ??? I can't understand
    *((volatile UINT32 *)REG_SPI_CTL1) &= ~0x0300;
    //*((volatile UINT32 *)REG_SPI_CTL1) |= 0x0200;
    
//    // Set DMA: TxDMA enable,RxDMA enable
//    *((volatile UINT32 *)REG_SPI_CTL1) |= 0x0c;
    // Set DMA: TxDMA disable,RxDMA disable
    *((volatile UINT32 *)REG_SPI_CTL1) &= ~0x0c;

    // Set wait cycles between data transfers: SPIW[F:0]=0, wait cycles = 0+1 = 1
    *((volatile UINT32 *)REG_SPI_WAIT) = 0x0000;
    
    // Disable receive data mask
    *((volatile UINT32 *)REG_SPI_RXMK) &= ~0x02;
}

/****************************************************************************/
/* FUNCTION:   SendBytes                                                   */
/* DESCRIPTION:Send bytes to card                                          */
/* INPUTS:     str                   string will be send to card           */
/*              len                   length of the string                  */
/* OUTPUTS:                                                                */
/****************************************************************************/

⌨️ 快捷键说明

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