📄 flash.c
字号:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define cs1_s PTS|=0x80 //set cs
#define cs1_c PTS&=~0x80 //clr cs
volatile uchar buf[528]; //sent or read buff
volatile uint bufc; //buffer sent or read couter
volatile uchar comm[10]; //commom buffer
volatile uchar comc; //commom couter
volatile uchar ty; //op type: 0: no, 1:read, 2: write
void ini_at45(void) //inilazation
{
uchar temp;
bufc=0;
comc=0;
PTS=0x80;
DDRS=0x80;
SPI0BR=0x01; //波特率为8MHz
temp=SPI0SR;
SPI0CR1=0xdc; //SPI enable, interrupt enable, master, mode 3
SPI0CR2=0x00;
cs1_s;
}
//
void sad(uint page)
{
uchar tmp;
tmp=(page>>7)&0x1f; //get the high 5 bits address
comm[2]=tmp;
tmp=(page<<1)&0x00fe; //get the low 7 bits address drop
comm[1]=tmp;
comm[0]=0x00;
comc=3;
ty=0;
cs1_c; //start
}
void sent(uchar bs) //write the buf into AT45DB041, bs: buffer select
{
uchar i;
for(i=0;i<5;i++)
comm[i]=0; //3 don't care bytes
comc=3;
ty=2;
bufc=(bs==2?520:256);
cs1_c; //chip sellect
i=SPI0SR;
SPI0DR=(bs==2?0x87:0x84); //if bs=2, then to buffer2 else to buffer1
}
void load(uchar bs, uint page) //load the buffer data to flash bs: buffer chose page: page address
{
uchar tmp;
sad(page); //set address
tmp=SPI0SR;
SPI0DR=(bs==2?0x86:0x83); //if bs=2, then to buffer2 else to buffer1
} //
void out(uchar bs,uint page) //data out to buffer, bs: buffer sellect, page page adress
{
uchar tmp;
sad(page); //set address
tmp=SPI0SR;
SPI0DR=(bs==2?0x55:0x53); //if bs=2, the load to buffer2 else load to buffer1
}
void get(uchar bs) //get data from AT buffer1 or buffer2, bs: buffer select
{
uchar i;
for(i=0;i<10;i++)
comm[i]=0;
comc=5; //3 bytes AD, 1 byte don't care bytes, 1 byte start byte
ty=1;
bufc=(bs==2?520:256);
cs1_c;
i=SPI0SR;
SPI0DR=(bs==2?0x56:0x54);
} //*/
interrupt 19 void spi0(void) //spi interrupt
{
uchar tmp;
uchar tda;
tmp=SPI0SR;
tda=SPI0DR;
if(comc==0) //comomo fininsh
{
if(ty==2) //writing condition
{
if(bufc==0||bufc==264) //finish
{
ty=0;
cs1_s;
}
else
{
bufc--;
SPI0DR=buf[bufc];
}
}
else if(ty==1) //reading condition
{
if(bufc==0||bufc==264) //finish
{
ty=0;
cs1_s;
}
else
{
bufc--;
buf[bufc]=tda;
SPI0DR=0x00;
}
}
else //no data bytes needed
cs1_s;
}
else //sending comom
{
comc--;
SPI0DR=comm[comc];
}
}
void d_out(uint ds) //将第ds,ds+1 block的数据发送到串口
{
uint j;
uchar tmp;
out(1,ds);
delay(10);
get(1);
delay(10);
out(2,ds+1);
delay(10);
get(2);
for(j=0;j<256;j++) //保存的数据发到串口
{
tmp=SCI0SR1; //数据发到串口
SCI0DRL=buf[j];
delay(1);
}
PORTB^=0x0f;
for(j=264;j<(264+256);j++)
{
tmp=SCI0SR1; //数据发到串口
SCI0DRL=buf[j];
delay(1);
}
PORTB^=0x0f;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -