📄 flashpgm.c
字号:
// The ARM ANYWHERE II JTAG based flash programer// any problem,contact me:surper_bat@citiz.net#define VERSION "1.0"#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include "s3c4510.h"#include "SST39VF160.h"//globe variblesint lpt_address; //the first addr of the parallel//function declarevoid show_menu( void );int blank_check( unsigned int addr, unsigned int size );void erase_blocks( void );void erase_sectors( void );int flash_read( unsigned char *dst, unsigned int addr, unsigned int size );int flash_verify( unsigned char *src, unsigned int addr, unsigned int size );int flash_write( unsigned char *src, unsigned int addr, unsigned int size );void load_binfile( void );void save_binfile( void );/*---------------------------------------------------------------- * brief : print the main menu * author: guojian * param: none * retval: none * modify history: 2003-12-3 Guo Jian first version-----------------------------------------------------------------*/void show_menu( void ){ 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" ); return;}/*---------------------------------------------------------------- * 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-----------------------------------------------------------------*/int blank_check( unsigned int addr, unsigned int size ){ unsigned short tmp; int break_point; int percent=size/10; int i; //check first byte if addr is odd if( addr % 2 ) { tmp = sst_read_word( addr - 1 ); if( ( tmp >> 8 ) != 0xff ) return 1; //the first byte is empty size--; addr++; } //if read size is odd if( size % 2 ) break_point = 1; else break_point = 0; //very time we check 2 byte i = 0; while( size != break_point ) { tmp = sst_read_word( addr ); if( tmp != 0xffff ) return 1; //prepare for next check size = size - 2; addr = addr + 2; //count the blank check percent if( size < percent * ( 10 - i ) ) { printf( "finished blank check %d %%\n ", i*10 ); i++; } } printf( "\n" ); if( size == 0 ) return 0; //check the last byte tmp = sst_read_word( addr ); if( ( tmp & 0x00ff ) != 0xff ) return 1; return 0;}/*---------------------------------------------------------------- * 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-----------------------------------------------------------------*/void load_binfile( void ){ int ret; int loadfile_fd; struct stat file_stat; unsigned int file_size; unsigned int pos; unsigned short tmp; char file_name[ 255 ]; unsigned char *bin_ptr = NULL; int read_size; int count = 0; unsigned char *read_ptr = NULL; printf( "please input the bin file name\n" ); scanf( "%s", file_name ); //open the file if( ( loadfile_fd = open( file_name, O_RDONLY ) ) == -1 ) { printf( "can not open the bin file make sure the input file exit\n" ); return; } //get the program position printf( "please input the program position\n" ); scanf( "%d", &pos ); if( pos > FLASH_SIZE ) { printf( "bad program position \n" ); return; } //check the size if( stat( file_name, &file_stat ) < 0 ) { printf( "error at stat\n" ); return;; } file_size = file_stat.st_size; if( file_size > FLASH_SIZE - pos ) { printf( "file is big than the flash size\n" ); return; } printf( "bin file size is %d\n", file_size ); //blank check printf( "now blank check......\n" ); if( blank_check( pos, file_size ) != 0 ) { printf( "you should erase the flash first\n" ); return; } printf( "blank check ok\n" ); //load the data from file bin_ptr = malloc( file_size ); if( bin_ptr == NULL ) { printf( "can not alloc memery \n" ); return; } count = file_size; read_ptr = bin_ptr; while( count != 0 ) { read_size = read( loadfile_fd, read_ptr, count ); if( read_size == -1 ) { printf( "bin file read error \n" ); return; } count -= read_size; read_ptr += read_size; } //write to flash printf( "now programing......\n" ); if( flash_write( bin_ptr, pos, file_size ) != 0 ) { printf( "load_binfile:write error\n" ); return; } //verify the flash printf( "now verify......\n" ); if( flash_verify( bin_ptr, pos, file_size ) != 0 ) { printf( "verify error\n" ); return; } printf( "program OK\n" ); close( loadfile_fd ); free( bin_ptr ); bin_ptr = NULL; return;}/*---------------------------------------------------------------- * 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-----------------------------------------------------------------*/void save_binfile( void ){ int savefile_fd; unsigned int read_size; unsigned int addr; unsigned short tmp; char file_name[ 255 ]; unsigned char *bin_ptr = NULL; int write_size; int count = 0; unsigned char *write_ptr = NULL; printf( "please input the bin file name\n" ); scanf( "%s", file_name ); //open the file if( ( savefile_fd = open( file_name, O_CREAT | O_TRUNC | O_WRONLY ) ) == -1 ) { printf( "can not create the bin file \n" ); return; } //get the program position printf( "please input the read address on the flash\n" ); scanf( "%d", &addr ); if( addr > FLASH_SIZE ) { printf( "bad read address \n" ); return; } //get the read size and check printf( "please input the read byte\n" ); scanf( "%d", &read_size ); if( ( addr + read_size ) > FLASH_SIZE ) { printf( "read size is too big \n" ); read_size = FLASH_SIZE - addr; } //read the data from flash bin_ptr = malloc( read_size ); if( bin_ptr == NULL ) { printf( "can not alloc memery \n" ); return; } if( flash_read( bin_ptr, addr, read_size ) == -1 ) { printf( "error at read flash\n" ); return; } //write to the bin file write_ptr = bin_ptr; count = read_size; while( count != 0 ) { write_size = write( savefile_fd, write_ptr, count ); if( write_size == -1 ) { printf( "error at write file\n" ); return; } count -= write_size; write_ptr += write_size; } printf( "read OK\n" ); close( savefile_fd ); free( bin_ptr ); bin_ptr = NULL; return;}/*---------------------------------------------------------------- * 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-----------------------------------------------------------------*/int flash_write( unsigned char *src, unsigned int addr, unsigned int size ){ int break_point; unsigned short tmp; unsigned short *tmp_ptr; unsigned char ch; int i=0; int percent = size/10; //write first byte if addr is odd if( addr % 2 ) { tmp = sst_read_word( addr - 1 ); ch = *src; src++; tmp = tmp & 0xff; tmp = tmp + ( ch << 8 ); //write the first byte sst_write_word( addr - 1, tmp ); size--; addr++; } //if read size is odd if( size % 2 ) break_point = 1; else break_point = 0; //very time we write 2 byte tmp_ptr = (unsigned short *)src; while( size != break_point ) { //read from bin file tmp = *tmp_ptr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -