?? cflib.c
字號:
#include "44b.h"
#include "44blib.h"
#include "rtc.h"
#define CF_CONTROL_ADDR 0x0a080000
#define CF_CONTROL_MASK_POWER (1<<2)
#define CF_CONTROL_MASK_RESET (1<<3)
#define CF_CONTROL_MASK_IOIS (1<<4)
#define CF_MEMORY_ATTR_BASE 0x02080000
#define CF_MEMORY_COMMON_BASE 0x02080800
#define CF_IO_BASE 0x020c0000
int cf_ctrl_value = 0x2;
/********************************************************************
// Function name : cf_pwr_ctrl
// Description : CF卡電源控制
// Return type : void
// Argument : int bpwron : 1打開電源,0關閉電源
*********************************************************************/
void cf_pwr_ctrl(int bpwron)
{
if(bpwron)
{
cf_ctrl_value |= CF_CONTROL_MASK_POWER;
}else
{
cf_ctrl_value &= ~CF_CONTROL_MASK_POWER;
}
*(unsigned char *)CF_CONTROL_ADDR = cf_ctrl_value;
}
/********************************************************************
// Function name : cf_rst_ctrl
// Description : CF卡復位操作
// Return type : void
// Argument : int brst
*********************************************************************/
void cf_rst_ctrl(int brst)
{
if(brst)
{
cf_ctrl_value |= CF_CONTROL_MASK_RESET;
}else
{
cf_ctrl_value &= ~CF_CONTROL_MASK_RESET;
}
*(unsigned char *)CF_CONTROL_ADDR = cf_ctrl_value;
}
/********************************************************************
// Function name : cf_iois_ctrl
// Description : CF卡IOIS信號控制
// Return type : void
// Argument : int bmemorywrite
*********************************************************************/
void cf_iois_ctrl(int bmemorywrite)
{
if(bmemorywrite)
{
cf_ctrl_value |= CF_CONTROL_MASK_IOIS;
}else
{
cf_ctrl_value &= ~CF_CONTROL_MASK_IOIS;
}
*(unsigned char *)CF_CONTROL_ADDR = cf_ctrl_value;
}
void cf_init()
{
/* CF卡片選信號總線寬度設置 */
rBWSCON &= (~(0xf<<16));
rBWSCON |= (0x00<<16);
rBANKCON5 = 0x7ffc;
Delay(10);
/* 打開CF卡電源 */
cf_pwr_ctrl(0);
Delay(500);
/* CF卡復位 */
cf_rst_ctrl(0);
Delay(3);
cf_rst_ctrl(1);
Delay(3);
cf_rst_ctrl(0);
Delay(500);
}
/********************************************************************
// Function name : cf_read_cis
// Description : 讀取CF卡卡信息
// Return type : void
// Argument : unsigned char *cisstring
*********************************************************************/
void cf_read_cis(unsigned char *cisstring)
{
unsigned char CISdata[180];
unsigned char data;
int i;
for (i=0;i<180;i++)
{
if(i % 0x8 == 0)
Uart_Printf("\n0x%08x : ", i*2);
//copy CIS to CISdata[]
data =*(unsigned char *) (CF_MEMORY_ATTR_BASE+i*2);// CIS can be access in even address only
CISdata[i] = (data & 0xff);
// Uart_Printf("0x%02x(%c) ", CISdata[i], CISdata[i]);
Uart_Printf("0x%02x ", CISdata[i]);
}
cisstring[0] = 0;
for (i=25; i<45 ; i++)
{
if (CISdata[i]!='\0')
sprintf(cisstring, "%s%c", cisstring, CISdata[i]);
else
sprintf(cisstring, "%s%c", cisstring, '\n');
}
}
/********************************************************************
// Function name : cf_memory_write_block
// Description : 寫數據到CF卡,每個BLOCK 512字節
// Return type : void
// Argument : unsigned char *data:待寫入數據
// Argument : int sectnum:BLOCK號
*********************************************************************/
void cf_memory_write_block(unsigned char *data, int sectnum)
{
unsigned char tempU8;
unsigned short i;
//***Set sector count***
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000002) = 0x1;//sector count =1
//***Set the LBA address of memory block to be written***
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000003) = sectnum + 32; //LBA [7:0] =1
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000004) = 0x0; //LBA [15:8:] =0
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000005) = 0x0; //LBA [23:16:] =0
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000006) = 0xE0; //LBA [27:24]=0 (lower 4 bit of register)
//Issue write command
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007) = 0x30;//issue 30H command for sector write
//Poll for busy bit
tempU8 = *(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);
while ( tempU8&0x80)
{
//poll for busy bit (bit 7 of register), quit loop when busy bit =0
tempU8 = *(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);
}
//Write data to data buffer until DRQ is clear
for (i=0;(tempU8&0x08)==0x08;i++)
{
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000008)=data[i];// write 2 byte of data to data buffer
tempU8=*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);// poll for DRQ (bit 3 of register)
}
Uart_Printf("Finish Writing\n");
}
/********************************************************************
// Function name : cf_memory_read_block
// Description : 從CF卡中讀取數據
// Return type : void
// Argument : unsigned char *data:保存數據的緩沖區
// Argument : int sectnum:BLOCK號
*********************************************************************/
void cf_memory_read_block(unsigned char *data, int sectnum)
{
unsigned char tempU8;
int i;
//Set sector count
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000002) = 0x1;//sector count =1
//Set the LBA address of memory block to be read
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000003) = sectnum + 32; //LBA [7:0] =1
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000004) = 0x0; //LBA [15:8] =0
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000005) = 0x0; //LBA [23:16] =0
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000006) = 0xE0; //LBA [27:24] =0 (lower 4 bit of register)
//Issue read command
*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007) = 0x20;//issue 20H command for sector read
//Poll for busy bit
tempU8 = *(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);
while ( tempU8&0x80)
{
//poll for busy bit (bit 7 of register), quit loop when busy bit =0
tempU8 = *(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);
}
//Read data from buffer into U16data[] until DRQ is clear
for (i=0;(tempU8&0x08)==0x08;i++)
{
data[i]=*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000008);// read 2 byte of data from buffer
tempU8=*(unsigned char *) (CF_MEMORY_COMMON_BASE+0x000007);// poll for DRQ (bit 3 of register)
}
Uart_Printf("Finish Reading\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -