ARM嵌入式系统教程的外部存储器接口实验1
程序员文章站
2022-07-03 16:33:16
...
对于ARM嵌入式系统教程的外部存储器接口实验的半字(16位)数据编程的解释:
#include <lpc21xx.h>
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
#define TRUE 1
#define FALSE 0
#define FLASH_ADDR 0x81000000 //FLASH的起始地址(分配为Bank1块)
#define GetAddr(addr) (volatile uint16 *)(FLASH_ADDR | (addr<<1))
void DelayNS(uint32 dly){
uint32 i,j;
for(i=dly;i>0;i--)
for(j=5000;j>0;j--);
}
//半字(16位)数据编程
uint8 WordProgram(uint32 Addr,uint16 Data){
volatile uint16 *ip;
uint16 temp1,temp2;
ip=GetAddr(0x5555); //转换地址0x5555
ip[0]=0xaaaa; //第一个写周期,地址0x5555,数据0xaa
ip=GetAddr(0x2aaa);
ip[0]=0x5555; //第二个写周期,地址0x2aaa,数据0x55
ip=GetAddr(0x5555);
ip[0]=0xa0a0; //第三个写周期,地址0x5555,数据0xa0
ip=(volatile uint16 *)(FLASH_ADDR | (Addr&0x1FFFFFFF));
*ip=Data; //第四个写周期,地址Addr,数据Data
while(1){ //等待操作完成
temp1=*ip;
temp2=*ip;
if(temp1==temp2){
if(temp1!=Data)
return FALSE;
else
return TRUE;
}
}
}
//芯片全片檫除
uint8 ChipErase(void){
volatile uint16 *ip;
uint16 temp1,temp2;
ip=GetAddr(0x5555);
ip[0]=0xaaaa; //第一个写周期,地址0x5555,数据0xAA
ip=GetAddr(0x2aaa);
ip[0]=0x5555; //第二个写周期,地址0x2aaa,数据0x55
ip=GetAddr(0x5555);
ip[0]=0x8080; //第三个写周期,地址0x5555,数据0x80
ip=GetAddr(0x5555);
ip[0]=0xaaaa; //第四个写周期,地址0x5555,数据0xAA
ip=GetAddr(0x2aaa);
ip[0]=0x5555; //第五个写周期,地址0x2aaa,数据0x55
ip=GetAddr(0x5555);
ip[0]=0x1010; //第六个写周期,地址0x5555,数据0x10
while(1){ //等待操作完成
temp1=*ip;
temp2=*ip;
if(temp1==temp2){
if(temp1!=0xffff)
return FALSE;
else
return TRUE;
}
}
// return TRUE;
}
int main(void){
uint8 i;
uint8 err=0;
volatile uint16 *addr;
PINSEL0=0x00000000; //设置管脚为GPIO
IO0DIR=0x00000080; //P0.7设置为输出
ChipErase(); //将FLASH全片擦除
//校验是否真正擦除,若发现有非0xffff的存储单元,设置err=1
addr=(volatile uint16 *)FLASH_ADDR+0;
for(i=0;i<100;i++){
if(0xffff!=(*addr))
err=1;
err++;
}
WordProgram(0x0,0x55AA); //向FLASH的0地址写入0x55AA
addr=(volatile uint16 *)FLASH_ADDR+0;
if(0x55AA!=(*addr)) //读出校验,若数据错误则设置err=1
err=1;
if(0==err){
IO0CLR=0x00000080; //P0.7=0;
DelayNS(20);
IO0SET=0x00000080; //P0.7=1
DelayNS(20);
}
else{
while(1){
IO0CLR=0x00000080; //P0.7=0;
DelayNS(20);
IO0SET=0x00000080; //P0.7=1
DelayNS(20);
}
}
while(1);
}