rt-thread/bsp/nv32f100x/lib/src/eeprom.c

346 lines
8.3 KiB
C
Raw Normal View History

2017-09-20 14:21:18 +08:00
#include "flash.h"
#include "eeprom.h"
#include <string.h>
/******************************************************************************
* Global variables
******************************************************************************/
/******************************************************************************
* Constants and macros
******************************************************************************/
/******************************************************************************
* Local types
******************************************************************************/
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* Local variables
******************************************************************************/
/******************************************************************************
* Local functions
******************************************************************************/
/******************************************************************************
*
* EEPROM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>eeprom
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>adr<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>512bytes eeprom
*
******************************************************************************/
uint16_t Adress_Js(uint32_t adr)
{
uint16_t err = EEPROM_ERR_SUCCESS;
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
if(adr >1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
return(err);
}
/******************************************************************************
*
* EEPROM <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>eeprom
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>adr<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>512bytes eeprom
*
******************************************************************************/
uint16_t EEPROM_Erase(uint32_t adr)
{
uint16_t err = EEPROM_ERR_SUCCESS;
uint32_t e_adr;
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
if(adr >1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
e_adr=adr+EEPROM_START_ADR;
err = Flash_EraseSector(e_adr);
return(err);
}
/******************************************************************************
*
* EEPROM <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>eeprom
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*
******************************************************************************/
uint32_t EEPROM_Read(uint32_t adr)
{
uint16_t err = EEPROM_ERR_SUCCESS;
uint32_t e_adr;
uint32_t data;
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
if(adr >1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
e_adr=adr+EEPROM_START_ADR;
data = M32(e_adr);
return(data);
}
/******************************************************************************
*
* EEPROM д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>eeprom
*д֮ǰ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>eeprom<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>д
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿգ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>512bytes sector<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>sram<EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>Ҫд<EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD>
*Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD>flash<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>eeprom<EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*
******************************************************************************/
uint16_t EEPROM_Write(uint32_t adr, uint32_t Data)
{
uint32_t err = EEPROM_ERR_SUCCESS;
uint32_t e_adr;
uint32_t r_data;
uint16_t i;
uint32_t start_adr;
// uint32_t modify_adr;
uint32_t EEPROM_DATA[128];
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
if(adr >1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
r_data = EEPROM_Read(adr);
e_adr=adr+EEPROM_START_ADR;
if(r_data== EEPROM_BLANK) //<2F><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4>λ<EFBFBD><CEBB><EFBFBD>ǿյģ<D5B5><C4A3><EFBFBD>ֱ<EFBFBD><D6B1>д
{
err= Flash_Program1LongWord(e_adr,Data);
}
else if((r_data&Data) == Data)//<2F><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4>λ<EFBFBD>ö<EFBFBD>Ӧ<EFBFBD><D3A6>bit<69><74><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>Ҳ<EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>ֱ<EFBFBD><D6B1>д
{
err= Flash_Program1LongWord(e_adr,Data);
}
else if(r_data == Data) //<2F><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>£<EFBFBD><C2A3>Ͳ<EFBFBD><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κβ<CEBA><CEB2><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
{
return(err);
}
else
{
start_adr = e_adr & EEPROM_SECTOR_MASK; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sector<6F><72>ͷ<EFBFBD><CDB7>ַ
for( i=0;i<128;i++ ) //<2F><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4>λ<EFBFBD>ò<EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><D5A3><EFBFBD><EFBFBD>Ȱ<EFBFBD>flash<73><68><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sram<61>У<EFBFBD><D0A3>޸<EFBFBD>
{
EEPROM_DATA[i] = M32(start_adr + 4*i);
}
EEPROM_DATA[(adr&EEPROM_ARRAY_ADR_MASK)>>2] = Data; //<2F>޸<EFBFBD>SRAM <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
err=EEPROM_Erase(adr);
err=Flash_Program(start_adr,(uint8_t*)EEPROM_DATA,512);//Ȼ<><C8BB>д<EFBFBD><D0B4>flash
}
return(err);
}
/******************************************************************************
*
*Byte д<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
******************************************************************************/
uint16_t EEPROM_WriteByte(uint32_t adr, uint8_t Data)
{
uint32_t err = EEPROM_ERR_SUCCESS;
uint32_t data_mask;
uint32_t r_data;
uint32_t data_m0;
uint32_t data_m1;
uint32_t word_adr = adr &0x3fc;
uint32_t b_sit= adr & 0x3;
//<2F><><EFBFBD>ø<EFBFBD>λΪFF
data_m0 = Data << b_sit*8;
data_mask = 0xFFFFFFFF<<(b_sit+1)*8;
// printf("datam0:=0x%x \n",data_m0);
//Ȼ<><C8BB><EFBFBD>õ<EFBFBD>λΪFF
data_m1 = 0xFFFFFFFF >> (32-b_sit*8);
data_m1 = data_m1 | data_m0 | data_mask ;
// printf("datam1:=0x%x \n",data_m1);
r_data = EEPROM_Read(word_adr);
// printf("r_data:=0x%x \n",r_data);
//<2F><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
data_m1 = data_m1 & r_data;
// printf("data_m1:=0x%x \n",data_m1); ;
err = EEPROM_Write(word_adr , data_m1);
return(err);
}
/******************************************************************************
*
*Byte <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
******************************************************************************/
uint8_t EEPROM_ReadByte(uint32_t adr)
{
uint32_t r_data;
uint32_t word_adr = adr &0x3fc;
uint32_t b_sit= adr & 0x3;
uint8_t data;
r_data = EEPROM_Read(word_adr);
data = (r_data>>b_sit*8)& 0xff;
return(data);
}
/******************************************************************************
*
*д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>дһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊbytesize<EFBFBD><EFBFBD><EFBFBD><EFBFBD>eeprom
*<EFBFBD>Ȱ<EFBFBD>1k<EFBFBD><EFBFBD>eeprom<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>sram<EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD>޸<EFBFBD>Ҫд<EFBFBD><EFBFBD>λ<EFBFBD>ã<EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD><EFBFBD><EFBFBD>
*<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>п<EFBFBD><EFBFBD><EFBFBD>2K eeprom <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2K Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD>Եġ<EFBFBD>
******************************************************************************/
uint16_t EERPOM_Writeup4byte(uint32_t adr,uint8_t *pData,uint32_t length)
{
uint8_t buf[512];
uint8_t *pbuf;
uint32_t e_adr;
uint32_t e_sec;
uint32_t e_offset;
uint32_t a;
uint32_t err = EEPROM_ERR_SUCCESS;
#ifdef IAR
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
#endif
if((adr + length )>1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
e_adr=adr+EEPROM_START_ADR;
e_sec=e_adr & EEPROM_SECTOR_MASK;
e_offset=e_adr & 0x1ff;
while (length>0){
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>С<EFBFBD><D0A1>512 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѭ<EFBFBD><D1AD>
if (e_offset||(length<512)){
pbuf=buf;
a=512-e_offset;
a=(length>a?a:length);
memcpy(buf,(uint8_t*)e_sec,512);
memcpy(&buf[e_offset],pData,a);
pData+=a;
length-=a;
e_offset=0;
}else{ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>0<EFBFBD>ҳ<EFBFBD><D2B3>ȴ<EFBFBD><C8B4><EFBFBD>512 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pbuf=pData;
pData+=512;
length-=512;
}
err=Flash_EraseSector(e_sec);
err=Flash_Program(e_sec,(uint8_t*)pbuf,512);//Ȼ<><C8BB>д<EFBFBD><D0B4>flash
e_sec+=0x200;
}
return err;
}
/*
uint16_t EERPOM_Writeup4byte(uint32_t adr,uint8_t *pData,uint32_t bytesize)
{
uint32_t err = EEPROM_ERR_SUCCESS;
uint32_t e_adr;
uint16_t i;
uint32_t start_adr;
uint32_t EEPROM_DATA[256];
uint32_t longword = bytesize >>2;
uint32_t *pwData = (uint32_t*)pData ;
err=Adress_Js(adr);
if(adr+bytesize >1024) //<2F><><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>ĵ<EFBFBD>ַ,<2C><><EFBFBD><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵĸ<DDB5><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1024<32><34><EFBFBD>򱨴<EFBFBD>
{
err = EEPROM_ADR_OverFlow;
return(err);
}
e_adr=adr+EEPROM_START_ADR;
start_adr = e_adr & EEPROM_SECTOR_MASK; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>sector ͷ<><CDB7>ַ
for( i=0;i<256;i++ ) //<2F>Ȱ<EFBFBD><C8B0><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1>sram
{
EEPROM_DATA[i] = M32(start_adr + 4*i);
}
for( i=0 ;i<longword ;i++) //Ȼ<><C8BB><EFBFBD>޸<EFBFBD>Ҫд<D2AA>ĵ<EFBFBD>ַ
{
EEPROM_DATA[(adr>>2)+i] = *pwData++;
}
//<2F><>erase<73><65>2<EFBFBD><32>eeprom secoter
err=EEPROM_Erase(0x000);
err=EEPROM_Erase(0x200);
err=Flash_Program(start_adr,(uint8_t*)EEPROM_DATA,1024);//Ȼ<><C8BB>д<EFBFBD><D0B4>flash
return(err);
}
*/