LPC1788的內(nèi)部EEPROM使用
Lpc1788內(nèi)置有eeprom
使用代碼
#ifndef __E2PRONINCHIP_H_
#define __E2PROMINCHIP_H
#include "common.h"
#include "sys.h"
#include "debugserial.h"
#define EEPROM_CLOCK 375000
void e2prom_init(void);
u8 e2prom_write_8(u8 page,u8 addr,u8 value);
u8 e2prom_write_16(u8 page,u8 addr,u16 value);
u8 e2prom_write_32(u8 page,u8 addr,u32 value);
u8 e2prom_read_8(u8 page,u8 addr,u8*value);
u8 e2prom_read_16(u8 page,u8 addr,u16*value);
u8 e2prom_read_32(u8 page,u8 addr,u32*value);
u8 e2prom_program(u8 page);
u8 e2prom_test(void);
#endif
#include "e2prominchip.h"
//有一些寄存器只能寫(xiě)不能讀,用起來(lái)的時(shí)候要小心& "子類的應(yīng)用,如果發(fā)生總線中斷可能是這些因素引起的
//eeprom有一個(gè)64的頁(yè)緩存,寫(xiě)入的時(shí)候不需要頁(yè)地址只需要頁(yè)偏移地址先寫(xiě)到這個(gè)頁(yè)里面
//然后寫(xiě)入頁(yè)地址,也緩存直接寫(xiě)入整個(gè)頁(yè)面(應(yīng)該是匹配 不同 寫(xiě)入)
void e2prom_init(void)
{
u32 val;
LPC_EEPROM->PWRDWN=0x00;//清除掉電
LPC_EEPROM->CLKDIV=((u16)(SystemCoreClock/EEPROM_CLOCK))-1;//設(shè)置時(shí)鐘
val=((((SystemCoreClock/1000000)*15)/1000)+1);
val|=(((((SystemCoreClock/1000000)*55)/1000)+1)<<8);//設(shè)置等待時(shí)間 NS級(jí)別
val|=(((((SystemCoreClock/1000000)*35)/1000)+1)<<16);
LPC_EEPROM->WSTATE=val;
LPC_EEPROM->INT_CLR_STATUS=(1<<26)|(1<<28);//清除中斷狀態(tài)
// LPC_EEPROM->INT_CLR_ENABLE = (1<<26)|(1<<28);//關(guān)閉中斷
}
static u8 write_8(u8 page,u8 addr,u8 value)
{
u16 dataddr=0;
if(page>=64||addr>=64)
{
return1;
}
dataddr=(addr&0x3f);
LPC_EEPROM->ADDR=dataddr;//寫(xiě)入地址
LPC_EEPROM->CMD=3;//非連續(xù)八位寫(xiě)入
LPC_EEPROM->INT_CLR_STATUS=(1<<26)|(1<<28);//清除中斷狀態(tài)
LPC_EEPROM->WDATA=value;
while(!(LPC_EEPROM->INT_STATUS&(1<<26)));//等待寫(xiě)入完成
LPC_EEPROM->INT_CLR_STATUS=(1<<26);//清除
return0;
}
u8 e2prom_write_8(u8 page,u8 addr,u8 value)
{
if(write_8(page,addr,value))return1;
returne2prom_program(page);
}
u8 e2prom_write_16(u8 page,u8 addr,u16 value)
{
u8 ret;
ret=write_8(page,addr,(u8)(value>>8));
if(ret)returnret;
ret=write_8(page,addr+1,(u8)value);
if(ret)returnret;
returne2prom_program(page);
}
u8 e2prom_write_32(u8 page,u8 addr,u32 value)
{
u8 ret;
ret=write_8(page,addr,(u8)(value>>24));
if(ret)returnret;
ret=write_8(page,addr+1,(u8)(value>>16));
if(ret)returnret;
ret=write_8(page,addr+2,(u8)(value>>8));
if(ret)returnret;
ret=write_8(page,addr+3,(u8)value);
if(ret)returnret;
returne2prom_program(page);
}
u8 e2prom_read_8(u8 page,u8 addr,u8*value)
{
u16 dataddr=0;
LPC_EEPROM->INT_CLR_STATUS=(1<<26)|(1<<28);//清除中斷狀態(tài)
if(page>=64||addr>=64)
{
return1;
}
dataddr=page;
dataddr<<=6;
dataddr|=(addr&0x3f);
LPC_EEPROM->ADDR=dataddr;//寫(xiě)入地址
LPC_EEPROM->CMD=0;//非連續(xù)八位讀出
while(!(LPC_EEPROM->INT_STATUS&(1<<26)));//等待讀出完成
*value=LPC_EEPROM->RDATA;
LPC_EEPROM->INT_CLR_STATUS=(1<<26);//清除
return0;
}
u8 e2prom_read_16(u8 page,u8 addr,u16*value)
{
u8 a,b,ret;
ret=e2prom_read_8(page,addr,&a);
if(ret)returnret;
ret=e2prom_read_8(page,addr+1,&b);
if(ret)returnret;
*value=a;
*value<<=8;
*value+=b;
return0;
}
u8 e2prom_read_32(u8 page,u8 addr,u32*value)
{
u8 a,b,c,d,ret;
ret=e2prom_read_8(page,addr,&a);
if(ret)returnret;
ret=e2prom_read_8(page,addr+1,&b);
if(ret)returnret;
ret=e2prom_read_8(page,addr+2,&c);
if(ret)returnret;
ret=e2prom_read_8(page,addr+3,&d);
if(ret)returnret;
*value=a;
*value<<=8;
*value+=b;
*value<<=8;
*value+=c;
*value<<=8;
*value+=d;
return0;
}
u8 e2prom_program(u8 page)
{
u16 dataddr=0;
LPC_EEPROM->INT_CLR_STATUS=(1<<28);//清除編程中斷
dataddr=(page&0x1f);
dataddr<<=6;
LPC_EEPROM->ADDR=dataddr;//寫(xiě)入地址
LPC_EEPROM->CMD=6;//編程指令
while(!(LPC_EEPROM->INT_STATUS&(1<<28)));
return0;
}
u8 e2prom_test(void)
{
u8 a,b,c,d;
u32 read;
e2prom_write_32(1,0,0xaabbccdd);
e2prom_read_8(1,0,&a);
e2prom_read_8(1,1,&b);
e2prom_read_8(1,2,&c);
e2prom_read_8(1,3,&d);
printf("read a value is %2xrn",a);
printf("read b value is %2xrn",b);
printf("read c value is %2xrn",c);
printf("read d value is %2xrn",d);
e2prom_read_32(1,0,&read);
printf("read 32 value is %8xrn",read);
return0;
}