//
// file: mcu2flash.c
//
// AUTHOR: stephen lg
//
// E_mail: yohu@sst.com
//
// ABSTRACT: control the flash via the mcu and serial with debug soft
//
// MODIFICATION HISTORY:
// MODIFICATION HISTORY:
//***********************************************************************//
#include <REG51.H>
#include <string.h>
#include <cmd.h>
#include <flash_cmd.h>
#define uchar unsigned char // 8bit3
#define uint unsigned int // 16bit
#define ulong unsigned long // 32bit
#define EBYTE ((unsigned char volatile xdata *) 0)
#define cmd_len 3
#define tol_len 2
#define data_len 17
uchar idata cmd_buff[cmd_len]; //store the cmd data
uchar idata len_buff[tol_len]; //store the data total length
uchar idata data_buff[data_len]; // sotre the data
uchar idata count1 ; // count1 for low byte
uchar idata count2 ; // count2 for high byte
uint idata n; // write the data to external ram; for counter
uint idata k; // read/verify the data for external ram;
uchar idata count; // counter
bit send_flag; // finished received, set this flag
bit pass_error; // imply the pc, the received data right or error
bit rec_cmd; // receive cmd flag
bit rec_len; // receive len flag
bit rec_data; // receive data flag
sbit P36=P3^6; // write
sbit P37=P3^7; // read
sbit P13=P1^3; // chip enable
sbit P21=P2^1; // A9 for erase
void init_serialcom(void)
{
SCON|=0x50; // serial: model 2
TMOD|=0x20; // time1: model 2
PCON|=0x00; // SMOD : 0
TH1|=0x0f3; // 24M 4800 bps
TR1=1; // start the time1
}
void delay_100(void) // delay 20ms
{
uchar idata i=0;
uchar idata j=0;
for(i=0;i <=200;i=i+1) // 20 ms
{
while(j <=200) // (1/24) * 12 = 0.5 us * 200 = 100 us
{j=j+1;}
}
}
// send one byte
void send_byte(uchar in_byte)
{
SBUF=in_byte;
while (TI==0);
TI=0;
}
// send the string in read/verify
void send_string(uchar *str,uchar strlen)
{
uchar k=0;
do
{
send_byte(*(str+k));
k=k+1;
}while(k <strlen);
}
//*************************************************************//
void serial_int() interrupt 4 using 1
{ EA=0;
ES=0;
if(RI){
uchar idata m; // write counter for each time
uchar idata rec; // receive data for temp
if(rec_cmd) // receive command
{
RI=0;
rec=SBUF;
P1=SBUF;
cmd_buff[count]=rec;
count=count+1;
if(count==cmd_len)
{
count=0;
rec_cmd=0;
send_flag=1;
}
else
{
rec_cmd=1;
send_flag=0;
}
}
else if(rec_len) // receive data length
{
RI=0;
rec=SBUF;
P1=SBUF;
len_buff[count]=rec;
count=count+1;
if(count==tol_len)
{
count=0;
rec_len=0;
rec_data=1;
send_byte(ACK);
}
else
{
rec_len=1;
}
}
else if(rec_data) // receive data flow
{
RI=0;
rec=SBUF; // count1 low
P1=SBUF; // count2 high
data_buff[count]=rec;
if(count==0);
else
count1=count1+1;
count=count+1;
if((count2==len_buff[1])&&(count1==len_buff[0]))
{
if(data_buff[0]==count-1)
{
send_byte(PASS);
// **************************** //
// add the flash operat: write
// **************************** //
P13=0;P37=1;P36=0;
for(m=1;m <count;m=m+1)
{
EBYTE[n]=data_buff[m];
n=n+1;
k=n;
}
P13=1;P37=1;P36=0;
n=0;
//****************** end *********************//
rec_cmd=1;
rec_len=0;
rec_data=0;
send_flag=0;
count=0;
count1=0;
count2=0;
}
else
{
send_byte(ERROR);
rec_data=0;
rec_cmd=1;
rec_len=0;
send_flag=0;
count=0;
count1=0;
count2=0;
}
}
else if (count1==0xff)
{
count2=count2+1;
}
else if (count==data_len)
{
// **************************** //
// add the flash operat: write
// **************************** //
P13=0;P37=1;P36=0;
for(m=1;m <count;m=m+1)
{
EBYTE[n]=data_buff[m];
n=n+1;
k=n;
}
P13=1;P37=1;P36=0;
// ****************** end ********************* //
send_byte(ACK);
rec_data=1;
rec_cmd=0;
rec_len=0;
send_flag=0;
count=0;
}
else
{
rec_data=1;
rec_cmd=0;
rec_len=0;
send_flag=0;
}
}
}
EA=1;
ES=1;
}
main()
{
EA=1;
ES=1;
P36=1;
P37=1;
send_flag=0;
count=0;
count1=0;
count2=0;
n=0;
k=0;
rec_cmd=1;
rec_len=0;
rec_data=0;
init_serialcom();
while(1)
{
if(send_flag)
{
//********************************************************************************//
//***************************** sst flash 512K bit ***************************//
//********************************************************************************//
if((cmd_buff[0]==SST)&&(cmd_buff[1]==SST29VE512_ID)&&(cmd_buff[2]==chip_write))
{
send_byte(ACK);
send_flag=0;
count=0;
rec_len=1;
}
else if ((cmd_buff[0]==SST)&&(cmd_buff[1]==SST29VE512_ID)&&(cmd_buff[2]==chip_erase))
{ // ************************************** //
// add the operate code : erase
// ************************************** //
P21=1;P37=1;
P13=0;
P36=0;
delay_100();
delay_100();
delay_100();
delay_100();
delay_100(); //100 ms
delay_100(); //20 ms
P36=1;
P13=1;
//******** end ***********************//
send_byte(ACK);
send_flag=0;
count=0;
rec_cmd=1;
}
else if ((cmd_buff[0]==SST)&&(cmd_buff[1]==SST29VE512_ID)&&(cmd_buff[2]==chip_verify))
{ // ************************************** //
// add the flash operat: write
// ************************************** //
P13=0;P37=0;P36=1;
send_string(EBYTE,k);
P13=1;P37=0;P36=1;
k=0;
//****************** end *********************//
send_flag=0;
count=0;
rec_cmd=1;
}
else if ((cmd_buff[0]==SST)&&(cmd_buff[1]==SST29VE512_ID)&&(cmd_buff[2]==chip_read))
{ // ************************************** //
// add the flash operat: write
// ************************************** //
P13=0;P37=0;P36=1;
send_string(EBYTE,k);
P13=1;P37=0;P36=1;
k=0;
//****************** end *********************//
send_flag=0;
count=0;
rec_cmd=1;
}
//********************************************************************************//
//********************************* end **********************************//
//********************************************************************************//
else
{
send_byte(NACK);
send_flag=0;
count=0;
rec_cmd=1;
}
}
}
}
对发flash 的00~ff 地址读写是好的。
但超过ff地址,就不行了
n,k 我设置了 unsigned int(16 bit),可以对0000~ffff地址操作吗,不知道这么做,对吗?
大虾求救
yohu@sst.com