?? flash.c
字號:
fs_index = fs_index % 256;
}
read_block = (fs_temp[fs_index] & 0x3FFF);//最高兩位沒用到
state = read_flash_block(read_temp, read_block);
if(state != 1)
{
return (0);
}
if(len >= (512 - start_addr))
{
memcpy(write_pos,(read_temp + start_addr), (512 - start_addr));
len -= (512 - start_addr);
write_pos += (512 - start_addr);
}
else
{
memcpy(write_pos,(read_temp + start_addr),len);
len = 0;
write_pos += len;
}
start_addr = 0;
fs_index++;
}
return (1);
}
/*******************************************************************************/
//功能: 寫flash
//成功返回 1 ,否則返回0
/*******************************************************************************/
int flash_write(void *buf, int len, unsigned int BaseAddr)
{
unsigned int fs_block;
unsigned int write_block; //當前要寫的扁區
unsigned int start_addr; //要寫的數據所在扇區的起始地址
unsigned int state;
unsigned int fs_index;
unsigned char * write_pos;
unsigned char write_temp[528];
unsigned short fs_temp[264];
if((BaseAddr + len) >= (7680 * 512))
{
return (0);
}
fs_block = (BaseAddr / 512) /256; //要寫的扁區的索引項在文件索引表中的扇區號
fs_index = (BaseAddr / 512) % 256; //要寫的扇區的索引項在文件索引表中的扇區號的對應的位置
start_addr = BaseAddr % 512; //要寫的數據所在扇區的位置
state = read_fs_index((unsigned char *)fs_temp, fs_block);//Closed by ChengDong Lu at 04/20/2006
// state = read_fs_index((unsigned char *)fs_temp, fs_block - 8128);
if(state != 1)
{
return (0);
}
write_pos = (unsigned char *)buf;//Added by ChengDong Lu at 04/19/2006
while(len > 0)
{
if(fs_index >=256)
{
fs_block++;
state = read_fs_index((unsigned char *)fs_temp, fs_block);//Closed by ChengDong Lu at 04/20/2006
//state = read_fs_index((unsigned char *)fs_temp, fs_block - 8128);
if(state != 1)
{
return (0);
}
fs_index = fs_index % 256;
}
write_block = (fs_temp[fs_index] & 0x3FFF);//最高兩位沒用到
if(state != 1)
{
return (0);
}
if(len >= (512 - start_addr))
{
if(start_addr > 0 || len < 512)
{
state = read_flash_block(write_temp, write_block);
if(state != 1)
{
return (0);
}
}
// memcpy((write_temp + start_addr),(write_pos + start_addr), (512 - start_addr));
memcpy((write_temp + start_addr),write_pos, (512 - start_addr));
len -= (512 - start_addr);
write_pos += (512 - start_addr);
state = write_flash_block(write_temp, write_block);
if(state != 1)
{
/* //如果還不成功,則找一個沒用過的扇區來替換上次要寫的扇區,再往里面寫
write_block = find_empty_block();
if(write_block == 0)
{
return (0);
}
else
{
fs_temp[fs_index] = write_block;
//在寫之前還得重新算一下cs
state = write_fs_index((unsigned char *)fs_temp, fs_block);//修改文件索引表
if(state != 1)
{
return (0);
}
}
state = write_flash_block_ext(write_temp, write_block, start_addr, (512 - start_addr));
if(state != 1)//如果沒寫成功 ,再寫一次
{
}
*/
return (0);
}
}
else
{
state = read_flash_block(write_temp, write_block);
if(state != 1)
{
return (0);
}
memcpy((write_temp + start_addr), write_pos, len);
write_flash_block(write_temp, write_block);
if(state != 1)
{
//如果還不成功,則找一個沒用過的扇區來替換上次要寫的扇區,再往里面寫
/* write_block = find_empty_block();
if(write_block == 0)
{
return (0);
}
else
{
fs_temp[fs_index] = write_block;
//在寫之前還得重新算一下cs
state = write_fs_index((unsigned char *)fs_temp, fs_block);//修改文件索引表
if(state != 1)
{
return (0);
}
}
state = write_flash_block_ext(write_temp, write_block, start_addr, len);
if(state != 1)//如果沒寫成功 ,再寫一次
{
return (0);
}
*/
return (0);
}
len = 0;
write_pos += len;
}
start_addr = 0;
fs_index++;
}
return (1);
}
void test(void)
{
unsigned int opr_block;
// unsigned char write_temp[512];
unsigned char read_temp[512];
unsigned char write_buf[512 * 4];
unsigned char read_buf[512 * 4];
unsigned int state;
unsigned int i,j;
unsigned int error = 8;
for(i = 0; i < 512; i++)
{
// write_temp[i] = 'E';
write_buf[i] = 0x35;
write_buf[512 + i] = 0x36;
write_buf[512 * 2 + i] = 0x37;
write_buf[512 * 3 + i] = 0x38;
read_temp[i] = 0x0;
read_buf[i] = 0x0;
read_buf[512 + i] = 0x0;
read_buf[512 * 2 + i] = 0x0;
read_buf[512 * 3 + i] = 0x0;
}
//當 j=0xeb i = 457; 出錯(文件索引表的第2個扇區的被覆蓋,)
//當 j=0x8c1 i = 457; 出錯(文件索引表的第19個扇區的被覆蓋)
//當 j=0x90b i = 457; 出錯(文件索引表的第26個扇區的被覆蓋)
//當 j=0xc71 i = 457; 出錯(文件索引表的第個扇區的被覆蓋)
for(j = 0x0; j < 3538; j++)
{
for(j = 0; j < 1111; j++)
{
// write_buf[j] = i;//colsed by ChengDong Lu at 04/20/2006
write_buf[j] = j;
read_buf[j] = 0x0;
}
state = flash_write(write_buf, 1111, 1111*j);
if(state != 1)
{
// return ;
error = 0;
break;
}
state = flash_read(read_buf, 1111 , 1111*j);
if(state != 1)
{
//return ;
error = 2;
break;
}
for(i = 0; i < 1111; i++)
{
if( write_buf[i] != read_buf[i])
{
//return;
error = 3;
break;
}
}
}
error = error;
state = flash_read(read_buf, 512 * 4, 255*512 + 7);
if(state != 1)
{
return ;
}
opr_block =255;
while(1)
{
state = read_flash_block(read_temp,opr_block );
if(state != 1)
{
return ;
}
opr_block++;
}
}
/*
void init_string(unsigned char *mystring, unsigned int len)
{
unsigned int i;
for(i = 0; i < len; i++)
{
mystring[i] = 0x0;
}
}
void write_string(unsigned char *mystring, unsigned int len, unsigned char letter)
{
unsigned int i;
for(i = 0; i < len; i++)
{
mystring[i] = letter;
}
}
void test2(void)
{
unsigned int i;
unsigned int state;
unsigned char test8128_buf[528];
unsigned char test8160_buf[528];
init_string(test8128_buf, 528);
init_string(test8160_buf, 528);
for(i = 0; i < 32; i++)
{
state = read_flash_block(test8128_buf, 8128 + i );
if(state != 1)
{
return ;
}
state = read_flash_block(test8160_buf, 8160 + i);
if(state != 1)
{
return ;
}
init_string(test8128_buf, 528);
init_string(test8160_buf, 528);
}
}
*/
void test_write(void)
{
unsigned int i,j;
unsigned char write_temp[528];
unsigned char write_buf[1111];
unsigned char read_buf[1111];
unsigned int error = 0;
unsigned int state;
write_temp[0]= 0;
for(i = 0; i < 3538; i++)
{
for(j = 0; j < 1111; j++)
{
//write_buf[j] = i;//colsed by ChengDong Lu at 04/20/2006
write_buf[j] = j;
read_buf[j] = 0x0;
}
state = flash_write(write_buf, 1111, 1111*i);
if(state != 1)
{
//return ;
error = 1;
break;
}
}
error = error;
read_buf[0] = read_buf[0];
write_temp[0] = write_temp[0];
}
void test_read(void)
{
unsigned int i,j;
// unsigned int write_number;
unsigned char write_buf[1111];
unsigned char read_buf[1111];
unsigned char write_temp[528];
unsigned int error = 0;
unsigned int state;
state = read_flash_block(write_temp, 2);
for(i = 0; i < 3538; i++)
{
for(j = 0; j < 1111; j++)
{
// write_buf[j] = i;//closed by ChengDong Lu at 04/20/2006
write_buf[j] = j;
}
state = flash_read(read_buf, 1111, 1111*i);
if(state != 1)
{
//return ;
error = 1;
break;
}
for(j = 0; j < 1111; j++)
{
if(write_buf[j] != read_buf[j])
{
error = 2;
break;
}
}
}
error = error;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -