📄 newflash.c
字号:
#include "stdio.h"
#include "file.h"
/* dspvc33 some register*/
#define Dsp_Rst 0x809fc0 ;/*reset signal input*/
#define Dsp_Int0 0x809fc1 ;/*serial*/
#define Dsp_Int1 0x809fc2 ;/*0x1000*/
#define Dsp_Int2 0x809fc3 ;/*0x400000*/
#define Dsp_Int3 0x809fc4 ;/*0xfff000*/
#define Dsp_Xint0 0x809fc5 ;
#define Dsp_Rint0 0x809fc6 ;
#define Dsp_Tint0 0x809fc9 ; /*timer */
#define Dsp_Tint1 0x809fca ;
/* flash constan and variable */
/*unlock cycle*/
#define First_Addr 0xaaa
#define Second_Addr 0x555
#define Third_Addr 0xaaa
#define Fourth_Addr 0xaaa
#define Fifth_Addr 0x555
#define Sixth_Addr 0xaaa
#define First_Data 0xaa
#define Second_Data 0x55
/* command*/
#define Reset_Command 0xf0
#define Autoselect_Command 0x90
#define Program_Command 0xa0
#define Erase_Setup_Command 0x80
#define Chip_Erase_Command 0x10
/*polling bit*/
#define Data_Polling_Bit 0x80 /* d7 = 1 */
#define Toggle_Bit 0x40 /* d6 = 1 */
#define Hangup_Bit 0x20 /* d5 = 1 */
#define Blank_Data 0xffff /* no data */
/* Flag Table*/
#define Pass_Flag 0
#define Fail_Flag 1
#define Error_Flag 2
#define OverMem 3
/*Function */
int Wf_Reset0_Function(unsigned char*);
int Wf_Chip_Erase_Function(unsigned char *);/*dddd */
int Wf_Program_Function(unsigned char*, unsigned char*);/*ccc*/
int Wf_Fast_Program_Function(unsigned char *,unsigned char *);
int Reset0_Function(void);
int Wf_Program();
/*polling check*/
int Data_Polling_Check(unsigned char*,unsigned char*);
int Toggle_Bit_Check(unsigned char*);
/* define the flash start of adresss and the flash size*/
#define FLASH_MEM 0x400000
#define Flash_Memory_Size 0x200000
/*define the part address and data about the fast mode */
#define Fast_Mode_Third_Addr 0xaaa
#define Fast_Mode_Third_Data 0x20
unsigned char *CommandDest;
/*unsigned char tablelist[96]={0x08,0x00,0x00,0x00,0xf9,0x10,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x01,0x80,0x00,
0x66,0x00,0x78,0x08,0xff,0x7f,0x7b,0x08,0x08,0x01,0x80,0x64,0x00,0x00,0x80,0x0c,
0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,
0x00,0x00,0x80,0x0c,0x22,0x00,0x78,0x08,0xff,0x7f,0x7b,0x08,0x11,0x01,0x80,0x64,
0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,
0x00,0x00,0x80,0x0c,0x00,0x00,0x80,0x0c,0xed,0xff,0x00,0x6a,0x00,0x00,0x00,0x00};
*/
/* temporary the string ,size is 1k bytes*/
unsigned char tablelist[1024];
/* main function*/
char filename[128];
long int lengthtemp;
unsigned char *processaddr;
unsigned char *Sourceaddr;
int Erase_Block_Yes_No;/* if by block erase or not*/
int Fast_Yes_No; /* if Fast_Yes_No =1,normal operation, */
long int datalength;
long codelength; /* Fast_Yes_No=0 fast mode*/
main()
{
long int datalength1;
long codelength1;
FILE *stream;
int numread,i,Conditionback;
int *testSram;
int testdata;
int flag;
Conditionback=1;
CommandDest = (unsigned char *)FLASH_MEM;
Erase_Block_Yes_No=0;
Fast_Yes_No = 0;
puts("Please Input your file\n");
scanf("%s",filename);
/* scanf("%h",codelength);*/
/*****************************/
/* first unlock block*/
/* second erase block by each block*/
if(Erase_Block_Yes_No == 0)
{
processaddr = (unsigned char *) FLASH_MEM;
flag=Wf_Chip_Erase_Function(processaddr);
puts(" erasing the Flash\n");
}
else if(Erase_Block_Yes_No == 1)
{
processaddr = (unsigned char *) FLASH_MEM;
/*the next program is used to check whether the flash can be written by each block*/
/*first 16k*/
Wf_Chip_Erase_Function(processaddr);
/*second 8k */
processaddr = (unsigned char*)FLASH_MEM+0x4000;
Wf_Chip_Erase_Function(processaddr);
/*third 8k */
processaddr = processaddr+0x2000;
Wf_Chip_Erase_Function(processaddr);
}
/* third program block*/
/* the section program is about how to write the data to the flash
memory by the normal process*/
/* utilize the fopen () function ,ftell()function,calculate the code length,
and open the related file*/
/*stream=fopen("e:\\wftemp\\xf0.bin","rb");*/
stream = fopen(filename,"rb");
fseek(stream,0,SEEK_END); /* the end address of the file */
codelength1 = (long)ftell(stream);
/*stream = fopen(filename,"rb");*/
fseek(stream,0,SEEK_SET); /* the start address of the file */
datalength1 = (long)ftell(stream);
codelength = codelength1-datalength1;/* calculate the length of the file */
/*printf("the codelength is %Xh",codelength); */
if(stream != NULL)
{
processaddr = (unsigned char *)FLASH_MEM; /* configure the flash address */
while(Conditionback)/* when Conditionback = 0 ,return from the while cycle*/
{
/* write the binary code into flash ,the size of the code is less than 1k each time*/
if(codelength>0x400)
{
datalength = 0x400;
codelength -= 0x400;
lengthtemp = 0x400;
}
else
{
datalength = codelength;
lengthtemp = codelength;
Conditionback = 0;
}
numread = fread(tablelist,sizeof(unsigned char),datalength,stream);
/* move the binary tablelist into the internal RAM 0x804000*/
Sourceaddr = (unsigned char *)0x804000;
for(i = 0; i<datalength; i++)
{
*Sourceaddr = tablelist[i];
Sourceaddr++;
}
/* start to write the code into the flash */
Wf_Program();
}
fclose(stream);
*processaddr = Autoselect_Command;
*processaddr = Reset_Command;
puts(" End of writting the Flash\n");
puts(" Now Please reset your system\n");
return Pass_Flag;
}
else
{
puts("the file is empty\n");
return Fail_Flag;
}
}
/* program function */
int Wf_Program()
{
datalength=lengthtemp;/* datalength= 1999*/
if(Fast_Yes_No == 0)
{
/*processaddr = (unsigned char *)FLASH_MEM; */
Sourceaddr = (unsigned char *)0x804000;
/* to judge whether the datalenth over the flash memory size */
if(datalength < Flash_Memory_Size)
{
while(datalength--)
{
/* write the data into the flash memory */
Wf_Program_Function(processaddr,Sourceaddr);
processaddr++;
Sourceaddr++;
}
}
else
return (OverMem);
}
else
{
/****************************/
/* the next program is runned in the fast mode */
/* this process is following */
/* first set fast mode */
/*processaddr = (unsigned char*)FLASH_MEM;*/
Sourceaddr = (unsigned char*)0x804000;
*(CommandDest+First_Addr) = First_Data;
*(CommandDest+Second_Addr) = Second_Data;
*(CommandDest+Fast_Mode_Third_Addr) = Fast_Mode_Third_Data;
/* process fast mode */
if(datalength < Flash_Memory_Size)
{
while(datalength--)
{
Wf_Fast_Program_Function(processaddr,Sourceaddr);
processaddr++;
Sourceaddr++;
}
}
}
}
/* reset fast mode */
/**processaddr = Autoselect_Command;
*rocessaddr = Reset_Command;*/
/* fourth reset flash
Wf_Reset0_Function(processaddr); */
/* function content */
/* use read/reset command when to change device status from autoselect to
read mode or to select device from hang up.but this command cannot abort
programming and erasing when device is executing programming and/or erase.*/
int Wf_Reset0_Function(unsigned char *addr)
{
unsigned char * flash_address;
flash_address = addr;
*flash_address = Reset_Command;
return (Pass_Flag);
}
int Wf_Chip_Erase_Function(unsigned char *Flash_Addr )
{
unsigned char *flash_Address;
flash_Address = Flash_Addr;
/* command write */
*(flash_Address+First_Addr) = First_Data;
*(flash_Address+Second_Addr) = Second_Data;
*(flash_Address+Third_Addr) = Erase_Setup_Command;
/* data polling check */
/* return (Data_Polling_Check(flash_address,Blank_Data);
/* Toggle bit Check */
return (Toggle_Bit_Check(flash_Address));
}
int Wf_Program_Function(unsigned char *addr,unsigned char *data)
{
unsigned char *flash_address;
flash_address = addr;
/* command write */
*(CommandDest+First_Addr) = First_Data;
*(CommandDest+Second_Addr) = Second_Data;
*(CommandDest+Third_Addr) = Program_Command;
*addr = *data;
/* data polling check */
/*return (Data_Polling_Check(CommandDest,data));*/
return (Toggle_Bit_Check(CommandDest));
/* toggle bit check */
/* return (Toggle_Bit_Check(addr));*/
}
int Wf_Fast_Program_Function(unsigned char *addr,unsigned char *data)
{
*CommandDest = Program_Command;
*addr = *data;
/* data polling check */
return (Data_Polling_Check(CommandDest,data));
/* toggle bit check */
/* return(Toggle_Bit_Check(addr)); */
}
int Data_Polling_Check(unsigned char *addr,unsigned char *data)
{
unsigned char polling_data,read_data;
polling_data = *data&Data_Polling_Bit;
do{
read_data = *addr;
if((read_data&Data_Polling_Bit) == polling_data)
break;
}while((read_data&Hangup_Bit) != Hangup_Bit);
read_data = *addr;
if((read_data&Data_Polling_Bit) != polling_data)
return (Fail_Flag);
return(Pass_Flag);
}
int Toggle_Bit_Check(unsigned char *addr)
{
unsigned char toggle_data,read_data;
toggle_data = *addr&Toggle_Bit;
do{
if(((read_data = *addr) &Toggle_Bit) == toggle_data)
break;
toggle_data = read_data & Toggle_Bit;
}while((read_data&Hangup_Bit) != Hangup_Bit);
toggle_data = *addr & Toggle_Bit;
if(((read_data = *addr)&Toggle_Bit) != toggle_data)
return (Fail_Flag);
return (Pass_Flag);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -