📄 spiflash.c
字号:
#include "DSP28_Device.h"
#define SPI_SEL_REG *(Uint16 *)0x2004
#define M_SPI 0X02
unsigned int m;
unsigned int from_flash;
void WriteFlash(unsigned int flash_data1);
//void ReadFlash(unsigned int flash_data2);
void delay(unsigned int t);
void main(void)
{ unsigned int to_flash;
unsigned int j;
/*初始化系统*/
InitSysCtrl();
/* 关中断 */
DINT;
IER = 0x0000;
IFR = 0x0000;
/* 初始化PIE控制寄存器 */
InitPieCtrl();
/* 初始化PIE参数表 */
InitPieVectTable();
/* 初始化外设寄存器 */
InitPeripherals();
///初始化IO口
InitGpio();
///初始化SPI
SPI_SEL_REG=M_SPI;
InitSpi();
/*设置CPU*/
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// GpioDataRegs.GPASET.all|=0x8000;
to_flash = 0x50; //EWSR
WriteFlash(to_flash);
delay(1000);
//写状态寄存器
to_flash = 0x01; //WRSR
WriteFlash(to_flash);
//状态寄存器数据为0
to_flash = 0x00;
WriteFlash(to_flash);
delay(5000);
//芯片擦除
to_flash = 0x06; //允许写(WREN)
WriteFlash(to_flash);
delay(2000);
to_flash = 0x60; //芯片擦除命令
WriteFlash(to_flash);
for(j=0;j<10;j++) //120ms
{
delay(40000); //10ms
}
//向FLASH写数据
to_flash = 0x06; //允许写(WREN)
WriteFlash(to_flash);
delay(1000);
to_flash = 0x02; //字节编程命令
WriteFlash(to_flash);
delay(100);
to_flash = 0x00; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 0x00; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 0x05; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 12; //数据
WriteFlash(to_flash);
delay(10000);
//从FLASH读数据
to_flash = 0x03; //读命令
WriteFlash(to_flash);
delay(100); //2us
to_flash = 0x00; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 0x00; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 0x05; //FLASH地址
WriteFlash(to_flash);
delay(100);
to_flash = 0xff; //延长CE
WriteFlash(to_flash);
delay(100);
to_flash = 0xff; //延长CE
WriteFlash(to_flash);
delay(100);
from_flash = SpiaRegs.SPIRXBUF;
delay(10000);
delay(10); //运行到这里
}
void WriteFlash(unsigned int flash_data1)
{
unsigned short int data;
data = 0x0000;
data = (flash_data1<<8);
while(1)
{
if(Spi_TxReady() == 1)
{
SpiaRegs.SPITXBUF = data;
delay(50);
break;
}
}
// delay(10000);
}
void delay(unsigned int t)
{
while(t>0)
t--;
}
//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -