📄 flashpgmfunc.~pas
字号:
Unit FlashPGMFunc;
Interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
Const
VERSION = '1.0';
function blank_check( addr, size:longword ):integer;
procedure load_binfile(fname:TFileName;pos:longword);
procedure save_binfile(file_name:TFileName;pos,read_size:longword);
function flash_write( src:pchar; addr,size:longword ):integer;
function flash_read( dst:pchar; addr,size:longword ):integer;
function flash_verify( src:pchar; addr,size:longword ):integer;
procedure erase_sectors(sector_addr, sector_count:longword);
procedure erase_blocks(block_addr, block_count:longword);
function connect_device:integer;
Implementation
Uses Unit1,Unit2,S3C4510Func,SST39VF160Func,Define;
{ ----------------------------------------------------------------
* brief : print the main menu
* author: guojian
* param: none
* retval: none
* modify history:
2003-12-3 Guo Jian first version
----------------------------------------------------------------- }
procedure show_menu;
begin
{printf( " s3c4510 flash tools\n" );
printf( "1. press 1 to load bin file\n" );
printf( "2. press 2 to erase the whole flash\n" );
printf( "3. press 3 to erase sectors\n");
printf( "4. press 4 to erase blocks\n" );
printf( "5. press 5 to read the flash to file\n" );
printf( "press x to exit the tools\n" ); }
end;
{ ----------------------------------------------------------------
* brief : check if the flash is empty in the give size
* author: guojian
* param: addr-----the first check address(0x0~0x1fffff)
size-----check size
* retval: 1--------not empty
0---------empty( all 0xff )
-1---------read error
* modify history:
if you change the function please give the discription
----------------------------------------------------------------- }
function blank_check( addr, size:longword ):integer;
var
tmp:word;
break_point:integer;
percent:integer;
i:integer;
begin
percent:=trunc(size/100);
//check first byte if addr is odd
if( addr mod 2 <>0) then
begin
tmp := sst_read_word( addr - 1 );
if( ( tmp shr 8 ) <> $ff ) then
begin
Result:=1;
Exit;
end;
//the first byte is empty
size:=size-1;
addr:=addr+1;
end;
//if read size is odd
if( size mod 2 <>0 ) then
break_point := 1
else
break_point := 0;
//very time we check 2 byte
i := 0;
while( size <> break_point ) do
begin
tmp := sst_read_word( addr );
if( tmp <> $ffff ) then
begin
Result:=1;
Exit;
end;
//prepare for next check
size := size - 2;
addr := addr + 2;
//count the blank check percent
if( size < percent * ( 100 - i ) ) then
begin
if(i=0) then
Form1.Memo1.Lines.Add('finished blank check '+inttostr(i)+'%' )
else
Form1.Memo1.Lines[Form1.Memo1.Lines.Count-1]:='finished blank check '+inttostr(i)+'%';
i:=i+1;
end;
end;
if( size = 0 )then
begin
Result:=0;
Exit;
end;
//check the last byte
tmp := sst_read_word( addr );
if( ( tmp and $00ff ) <> $ff ) then
begin
Result:=1;
Exit;
end;
Result:=0;
end;
{ ----------------------------------------------------------------
* brief : load a bin file to the flash at addr 0
* author: guojian
* param: none
* retval: none
* modify history:
2003-12-3 Guo Jian first version
----------------------------------------------------------------- }
procedure load_binfile(fname:TFileName;pos:longword);
var
read_size:integer;
cur_rsize:integer;
file_size:longword;
loadfile_fd:integer;
count :integer;
buf:array[0..10000]of byte;
i:integer;
start_pos:longword;
begin
//open the file
loadfile_fd:=FileOpen(fname,fmOpenRead);//Write or fmShareDenyNone);
if( loadfile_fd <0 ) then
begin
Form1.Memo1.Lines.Add( 'can not open the bin file make sure the input file exist' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
//get the program position
if(pos> FLASH_SIZE ) then
begin
Form1.Memo1.Lines.Add( 'bad program position ' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
//check the size
FileSeek(loadfile_fd,0,0);
file_size := FileSeek(loadfile_fd,0,2);
if( file_size > (FLASH_SIZE - pos) ) then
begin
Form1.Memo1.Lines.Add('file is big than the flash size' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
Form1.Memo1.Lines.Add('bin file size is '+inttostr(file_size)+' bytes' );
//blank check
Form1.Memo1.Lines.Add('now blank check......' );
if( blank_check( pos, file_size ) <> 0 ) then
begin
Form1.Memo1.Lines.Add('you should erase the flash first' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
Form1.Memo1.Lines.Add('blank check ok' );
//load the data from file
//write to flash
start_pos:=pos;
Form1.Memo1.Lines.Add('now programing......' );
FileSeek(loadfile_fd,0,0);
repeat
read_size:=FileRead(loadfile_fd,buf[0],10000);
if(read_size<=0) then
begin
Form1.Memo1.Lines.Add('bin file read error ' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
if( flash_write( pchar(@buf[0]), pos, read_size ) <> 0 ) then
begin
Form1.Memo1.Lines.Add('load_binfile:write error' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
pos:=pos+read_size;
until read_size<10000;
//verify the flash
pos:=start_pos;
Form1.Memo1.Lines.Add('now verify......' );
FileSeek(loadfile_fd,0,0);
repeat
read_size:=FileRead(loadfile_fd,buf[0],10000);
if(read_size<=0) then
begin
Form1.Memo1.Lines.Add('bin file read error ' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
if( flash_verify( pchar(@buf[0]), pos, read_size ) <> 0 ) then
begin
Form1.Memo1.Lines.Add('verify error');
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
pos:=pos+read_size;
until read_size<10000;
Form1.Memo1.Lines.Add('program OK');
Form1.Memo1.Lines.Add( '----------------------------------' );
end;
{ ----------------------------------------------------------------
* brief : read the flash and save the data to the file
* author: guojian
* param: none
* retval: none
* modify history:
2003-12-3 Guo Jian first version
----------------------------------------------------------------- }
procedure save_binfile(file_name:TFileName;pos,read_size:longword);
var
savefile_fd:integer;
bin_ptr:pchar;
write_size:integer;
count:integer;
write_ptr:pchar;
cur_wsize:integer;
begin
//open the file
savefile_fd:=FileCreate(file_name,fmCreate);
FileClose(savefile_fd);
savefile_fd:=FileOpen(file_name,fmOpenReadWrite or fmShareDenyNone);
if( savefile_fd <0 ) then
begin
Form1.Memo1.Lines.Add( 'can not create the bin file ' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
//get the program position
//printf( "please input the read address on the flash\n" );
if( pos > FLASH_SIZE ) then
begin
Form1.Memo1.Lines.Add( 'bad read address: '+inttostr(pos)+' is larger than FLASH size!' );
Form1.Memo1.Lines.Add( '----------------------------------' );
Exit;
end;
//get the read size and check
if( ( pos + read_size ) > FLASH_SIZE )then
begin
Form1.Memo1.Lines.Add( 'read size is too big ' );
read_size := FLASH_SIZE - pos;
end;
//read the data from flash
try
GetMem(bin_ptr,read_size);
except
on EOutOfMemory do
begin
Form1.Memo1.Lines.Add('can not alloc memory ' );
Form1.Memo1.Lines.Add( '----------------------------------' );
FreeMem( bin_ptr );
Exit;
end;
end;
if( flash_read( bin_ptr, pos, read_size ) = -1 ) then
begin
Form1.Memo1.Lines.Add( 'error at read flash' );
Form1.Memo1.Lines.Add( '----------------------------------' );
FreeMem( bin_ptr );
Exit;
end;
//write to the bin file
write_ptr := bin_ptr;
count := read_size;
while( count <> 0 ) do
begin
if(count>10000) then
cur_wsize:=10000
else
cur_wsize:=count;
write_size := FileWrite( savefile_fd, write_ptr[0],cur_wsize);// count );
if( write_size = -1 )then
begin
Form1.Memo1.Lines.Add( 'error at write file <'+file_name+'>' );
Form1.Memo1.Lines.Add( '----------------------------------' );
FreeMem( bin_ptr );
FileClose(savefile_fd);
Exit;
end;
count :=count-write_size;
write_ptr :=write_ptr + write_size;
end;
Form1.Memo1.Lines.Add( 'read OK' );
Form1.Memo1.Lines.Add( '----------------------------------' );
FileClose( savefile_fd );
FreeMem( bin_ptr );
end;
{ ----------------------------------------------------------------
* brief : write the bin file to flash
* author: guojian
* param: src------point to the program data
addr-----the first write address(0x0~0x1fffff)
size-----write size
* retval: 0----------succeed
-1---------error
* modify history:
if you change the function please give the discription
----------------------------------------------------------------- }
function flash_write( src:pchar; addr,size:longword ):integer;
var
break_point:integer;
tmp:word;
tmp_ptr:pchar;
ch:char;
i,j:integer;
percent:integer;
saved_size_str:string;
begin
i:=0;
saved_size_str:='% of '+inttostr(size)+' bytes';
percent := trunc(size/100);
//write first byte if addr is odd
if( addr mod 2 <>0 ) then
begin
tmp := sst_read_word( addr - 1 );
ch := src^;
src:=src+1;
tmp := tmp and $ff;
tmp := tmp + ( ord(ch) shl 8 );
//write the first byte
sst_write_word( addr - 1, tmp );
size:=size-1;
addr:=addr+1;
end;
//if read size is odd
if( size mod 2<>0 ) then
break_point := 1
else
break_point := 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -