📄 flashdemo.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include "flash.h"
int main(int argc,char* argv[])
{
FILE* srcfile=0;
char buffer[255];
unsigned int *address=0;
unsigned int *nextaddress;
unsigned int *erasebase;
int erasesize;
int bytesread, filesize;
if (argc == 3)
{
srcfile=fopen(argv[2], "r");
address=(unsigned int*)strtol(argv[1],0,0);
}
if (srcfile==0)
{
printf("Enter source filename:");
scanf("%s",buffer);
srcfile=fopen(buffer, "r");
}
if (srcfile==0)
{
printf("Could not open file. Exiting.\n");
exit(1);
}
if (address==0)
{
printf("Enter destination address:");
scanf("%x",&address);
}
if (address==0)
{
printf("Invalid Address. Exiting.\n");
exit(1);
}
printf("Initialising Flash...\n");
Flash_Write_Enable (); // Target Specific Code
Flash_Init(); // Flash Specific Code
printf("Calculating erase area...\n");
bytesread=1;
filesize=0;
while(bytesread!=0)
{
bytesread=fread(buffer, sizeof(char), 256, srcfile);
filesize+=bytesread;
}
rewind(srcfile);
erasebase=address;
erasesize=filesize;
Flash_Calc_Blocks(&erasebase, &erasesize);
printf("In order to program %d bytes, starting at address 0x%x,\n",filesize, (int)address);
printf("%d bytes from address 0x%x will be erased.\n",erasesize, (int)erasebase);
printf("Erasing Blocks...\n");
Flash_Erase_Blocks(erasebase, erasesize);
printf("Programing Flash...\n");
nextaddress=address;
bytesread=fread(buffer, sizeof(char), 256, srcfile);
while(bytesread!=0)
{
Flash_Write_Area(nextaddress, (int*)buffer, bytesread);
nextaddress+=bytesread/4;
bytesread=fread(buffer, sizeof(char), 256, srcfile);
}
printf("Verifying Flash...\n");
fseek(srcfile, 0, SEEK_SET);
nextaddress=address;
bytesread=fread(buffer, sizeof(int), 1, srcfile);
while(bytesread!=0)
{
if(*buffer!=*(char *)nextaddress++)
{
printf("Verify failed. Exiting.\n");
exit(1);
}
bytesread=fread(buffer, sizeof(int), 1, srcfile);
}
printf("Flash programmed successfully with %d bytes.\n", (nextaddress-address)*4);
Flash_Close(); // Flash Specific Code
Flash_Write_Disable (); // Target Specific Code
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -