单片机存储芯片W25QXX与AT24C02详解
一、FLASH W25QXX
(1) W25QXX芯片简介
W25Q128是华邦公司推出的一款SPI接口的NOR Flash芯片,其存储空间为128Mbit,相当于16M字节。W25Q128V芯片是串行闪存,可以通过标准/两线/四线SPI控制。W25Q128一次最多可编程256个字节。页面可以按扇区擦除、块擦除、整个芯片擦除。
W25Q128 的擦写周期多达 10W 次,具有 20 年的数据保存期限,支持电压为 2.7~3.6V,W25Q128 支持标准的 SPI,还支持双输出/四输出的 SPI,最大 SPI 时钟可以到 80Mhz(双输出时相当于 160Mhz,四输出时相当于 320M)。
W25Q128存储容量共128M-bit/ 16M-byte。
W25Q64存储容量共64M-bit/ 8M-byte。
(2) W25QXX芯片引脚说明
- 第1脚CS是SPI总线的片选使能接口,SPI总线支持在一条总线上连接多个芯片,为了区分当前通信的是哪个芯片,就为每个芯片连接一个独立的CS片选接口,单片机想和哪个芯片通信,就向哪个芯片的CS引脚输出低电平。
- 第2脚D0是SPI总线的数据输出接口。
- 第3脚WP是硬件写保护接口,当向此引脚输入低电平,芯片将禁止写入数据。反之,可正常写入数据。
- 第4脚GND是公共地。
- 第5脚DI是SPI总线的数据输入接口。
- 第6脚CLK是SPI总线的时钟输入接口。
- 第7脚HOLD是状态保持接口。当向此引脚输入低电平,芯片将禁止任何操作,当向此引脚输入高电平,可正常操作芯片。
- 第8脚VCC是电源供电接口,输入2.7-3.6V的电源。
(3) W25QXX芯片读错误原因
- 写数据之前Flash里面的数据不是0xFF就必须先擦除,然后才能写数据。擦除即将Flash里面的数据恢复为0xFF的过程。
- 上电后设备自动处于写禁用状态(Write Enable Latch, WEL为0,WEL是只读位)。在Page Program, Sector Erase, Block Erase, Chip Erase or Write Status Register instruction(页编程、区擦除、块擦除、芯片擦除或者写状态寄存器指令)之前必须先进行写使能指令。在编程、擦除、写状态寄存器指令完成后,WEL自动变成0。
- W25Q64BV是使用四线SPI兼容总线:串行时钟:(CLK),片选(CS),串行数据输入(DI),串行数据输出(DO)。标准SPI指令是MCU在CLK的上升沿使用DI输入引脚串行写指令,地址,数据到设备。在CLK的下降沿用DO输出引脚从设备读取数据或状态。
-
W25Q128芯片第一次接收指令集的时候不会做出响应,所以在初始化时向芯片随便发送一个指令集(如0xFF),芯片才能正常响应。(针对部分单片机)
spi_start();//读取ID前向芯片发送一个FF指令
spi_swapByte(0xFF);
spi_stop();
w25q128_readID(&ID);//正常读取ID - 配置SPI:SPI的工作模式配置为 0,即 CPOL = 0,CPHA = 0
- 写内存时需要注意Flash的一个要点,Flash编程只能将1写为0,而不能将0写成1,写1靠擦除。所以需要在写内存的时候将内存擦除,使用内存擦除指令擦除内存(擦除的最小单位是扇区),内存变为0xFF,然后再写内存。
- Flash的写的有个特性跟EEPROM一样,就是它的一页是256个Byte,也就是在写入的时候,一次最多可以写入256个字节的数据,超过了需要自行在代码中处理,一次最多编程256字节,写超的话会对当前页的前面数据进行覆盖。
- 写入完成之后必须要等待一定的时间,不然马上写入第二次写入会失败W25Q128_Wait_Busy(); //等待写入结束
- 芯片可以从当前位置读完整个芯片数据;芯片只能单页写,写之前内容需要被擦除;
- 写入数据程序进入硬件中断 可能是堆栈溢出,比如FReeRTOS里面每一个任务分配的空间是128BYTE,超过内存卡死
(4) STM32CubeMX配置SPI通信(HAL库)
以stm32F407战舰版举例,由于战舰版和W25QXX芯片引脚已经连接完成,分别是PB3-PB5,使用cubemx配置SPI1时,但是软件会默认选择PA4-PA6复用SPI1,但是我们需要使用PB3-PB5怎么处理
硬件片选和软件片选的区别
所谓硬件片选指的是SPI本身具有片选信号,当我们通过SPI发送数据时,SPI外设自动拉低CS信号使能从机,发送完成后自动拉高CS信号释放从机,这个过程是不需要软件操作的。而软件片选则是需要使用GPIO作为片选信号,SPI在发送数据之前,需要先通过软件设置作为片选信号的GPIO输出低电平,发送完成之后再设置该GPIO输出高电平。一般选择软件片选。
(5) 写W25Q128驱动代码
W25Q128何GD25Q128相同
gd25q128.c
#include "gd25q128.h"
uint16_t GD25Q128_TYPE = 0;
uint32_t GD25Q128_SIZE = 0;
uint8_t GD25Q128_UID[8];
static void delay_us(uint32_t us)
{
uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
while (delay--)
{
;
}
}
//SPI读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
static uint8_t GD25Q128_SPI_ReadWriteByte(uint8_t TxData)
{
uint8_t RxData = 0X00;
if(HAL_SPI_TransmitReceive(&hspi1, &TxData, &RxData, 1, 1000) != HAL_OK)
{
RxData = 0XFF;
}
return RxData;
}
//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q128
//容量为16M字节,共有128个Block,4096个Sector
//初始化SPI FLASH的IO口
int GD25Q128_Init(void)
{
MX_SPI1_Init();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET);//将HOLD置高,操作eeprom
GD25Q128_CS_L(); /* 拉低选中 */
GD25Q128_SPI_ReadWriteByte(0XFF);
GD25Q128_CS_H(); /* 拉高取消 */
GD25Q128_TYPE = GD25Q128_ReadID(); // 读取FLASH ID.
GD25Q128_SIZE = GD25Q128_ReadCapacity(); // 读取容量
GD25Q128_ReadUniqueID(GD25Q128_UID); // 读取唯一ID
if((GD25Q128_TYPE & 0XC800) != 0XC800)
{
return -1;
}
return 0;
}
//读取GD25Q128的状态寄存器
//BIT7 6 5 4 3 2 1 0
//SPR RV TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状态寄存器保护位,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设置
//WEL:写使能锁定
//BUSY:忙标记位(1,忙;0,空闲)
//默认:0x00
uint8_t GD25Q128_ReadSR(void)
{
uint8_t byte = 0;
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_ReadStatusReg); //发送读取状态寄存器命令
byte = GD25Q128_SPI_ReadWriteByte(0Xff); //读取一个字节
GD25Q128_CS_H(); //取消片选
return byte;
}
//写GD25Q128状态寄存器
//只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
void GD25Q128_Write_SR(uint8_t sr)
{
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_WriteStatusReg); //发送写取状态寄存器命令
GD25Q128_SPI_ReadWriteByte(sr); //写入一个字节
GD25Q128_CS_H(); //取消片选
}
//GD25Q128写使能
//将WEL置位
void GD25Q128_Write_Enable(void)
{
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_WriteEnable); //发送写使能
GD25Q128_CS_H(); //取消片选
}
//GD25Q128写禁止
//将WEL清零
void GD25Q128_Write_Disable(void)
{
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_WriteDisable); //发送写禁止指令
GD25Q128_CS_H(); //取消片选
}
//读取芯片ID
//返回值如下:
//0XC817,表示芯片型号为GD25Q128
uint16_t GD25Q128_ReadID(void)
{
uint16_t Temp = 0;
GD25Q128_CS_L();
GD25Q128_SPI_ReadWriteByte(0x90); //发送读取ID命令
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
Temp |= GD25Q128_SPI_ReadWriteByte(0xFF) << 8;
Temp |= GD25Q128_SPI_ReadWriteByte(0xFF);
GD25Q128_CS_H();
return Temp;
}
uint32_t GD25Q128_ReadCapacity(void)
{
int i = 0;
uint8_t arr[4] = {0,0,0,0};
GD25Q128_CS_L();
GD25Q128_SPI_ReadWriteByte(0x5A);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x84);
GD25Q128_SPI_ReadWriteByte(0x00);
for(i = 0; i < sizeof(arr); i++)
{
arr[i] = GD25Q128_SPI_ReadWriteByte(0xFF);
}
GD25Q128_CS_H();
return ((((*(uint32_t *)arr)) + 1) >> 3);
}
void GD25Q128_ReadUniqueID(uint8_t UID[8])
{
int i = 0;
GD25Q128_CS_L();
GD25Q128_SPI_ReadWriteByte(0x4B);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
GD25Q128_SPI_ReadWriteByte(0x00);
for(i = 0; i < 8; i++)
{
UID[i] = GD25Q128_SPI_ReadWriteByte(0xFF);
}
GD25Q128_CS_H();
}
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void GD25Q128_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
uint16_t i;
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_ReadData); //发送读取命令
GD25Q128_SPI_ReadWriteByte((uint8_t)((ReadAddr) >> 16)); //发送24bit地址
GD25Q128_SPI_ReadWriteByte((uint8_t)((ReadAddr) >> 8));
GD25Q128_SPI_ReadWriteByte((uint8_t)ReadAddr);
for (i = 0; i < NumByteToRead; i++)
{
pBuffer[i] = GD25Q128_SPI_ReadWriteByte(0XFF); //循环读数
}
GD25Q128_CS_H();
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void GD25Q128_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t i;
GD25Q128_Write_Enable(); //SET WEL
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_PageProgram); //发送写页命令
GD25Q128_SPI_ReadWriteByte((uint8_t)((WriteAddr) >> 16)); //发送24bit地址
GD25Q128_SPI_ReadWriteByte((uint8_t)((WriteAddr) >> 8));
GD25Q128_SPI_ReadWriteByte((uint8_t)WriteAddr);
for (i = 0; i < NumByteToWrite; i++)
GD25Q128_SPI_ReadWriteByte(pBuffer[i]); //循环写数
GD25Q128_CS_H(); //取消片选
GD25Q128_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void GD25Q128_Write_NoCheck(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t pageremain;
pageremain = 256 - WriteAddr % 256; //单页剩余的字节数
if (NumByteToWrite <= pageremain)
pageremain = NumByteToWrite; //不大于256个字节
while (1)
{
GD25Q128_Write_Page(pBuffer, WriteAddr, pageremain);
if (NumByteToWrite == pageremain)
break; //写入结束了
else //NumByteToWrite>pageremain
{
pBuffer += pageremain;
WriteAddr += pageremain;
NumByteToWrite -= pageremain; //减去已经写入了的字节数
if (NumByteToWrite > 256)
pageremain = 256; //一次可以写入256个字节
else
pageremain = NumByteToWrite; //不够256个字节了
}
};
}
//写SPI FLASH
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
uint8_t GD25Q128_BUFFER[4096];
void GD25Q128_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
uint8_t *GD25Q128_BUF;
GD25Q128_BUF = GD25Q128_BUFFER;
secpos = WriteAddr / 4096; //扇区地址
secoff = WriteAddr % 4096; //在扇区内的偏移
secremain = 4096 - secoff; //扇区剩余空间大小
if (NumByteToWrite <= secremain)
secremain = NumByteToWrite; //不大于4096个字节
while (1)
{
GD25Q128_Read(GD25Q128_BUF, secpos * 4096, 4096); //读出整个扇区的内容
for (i = 0; i < secremain; i++) //校验数据
{
if (GD25Q128_BUF[secoff + i] != 0XFF)
break; //需要擦除
}
if (i < secremain) //需要擦除
{
GD25Q128_Erase_Sector(secpos); //擦除这个扇区
for (i = 0; i < secremain; i++) //复制
{
GD25Q128_BUF[i + secoff] = pBuffer[i];
}
GD25Q128_Write_NoCheck(GD25Q128_BUF, secpos * 4096, 4096); //写入整个扇区
}
else
GD25Q128_Write_NoCheck(pBuffer, WriteAddr, secremain); //写已经擦除了的,直接写入扇区剩余区间.
if (NumByteToWrite == secremain)
break; //写入结束了
else //写入未结束
{
secpos++; //扇区地址增1
secoff = 0; //偏移位置为0
pBuffer += secremain; //指针偏移
WriteAddr += secremain; //写地址偏移
NumByteToWrite -= secremain; //字节数递减
if (NumByteToWrite > 4096)
secremain = 4096; //下一个扇区还是写不完
else
secremain = NumByteToWrite; //下一个扇区可以写完了
}
};
}
//擦除整个芯片
//等待时间超长...
void GD25Q128_Erase_Chip(void)
{
GD25Q128_Write_Enable(); //SET WEL
GD25Q128_Wait_Busy();
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_ChipErase); //发送片擦除命令
GD25Q128_CS_H(); //取消片选
GD25Q128_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个山区的最少时间:150ms
void GD25Q128_Erase_Sector(uint32_t Dst_Addr)
{
//监视falsh擦除情况,测试用
Dst_Addr *= 4096;
GD25Q128_Write_Enable(); //SET WEL
GD25Q128_Wait_Busy();
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_SectorErase); //发送扇区擦除指令
GD25Q128_SPI_ReadWriteByte((uint8_t)((Dst_Addr) >> 16)); //发送24bit地址
GD25Q128_SPI_ReadWriteByte((uint8_t)((Dst_Addr) >> 8));
GD25Q128_SPI_ReadWriteByte((uint8_t)Dst_Addr);
GD25Q128_CS_H(); //取消片选
GD25Q128_Wait_Busy(); //等待擦除完成
}
//等待空闲
void GD25Q128_Wait_Busy(void)
{
while ((GD25Q128_ReadSR() & 0x01) == 0x01); // 等待BUSY位清空
}
//进入掉电模式
void GD25Q128_PowerDown(void)
{
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_PowerDown); //发送掉电命令
GD25Q128_CS_H(); //取消片选
delay_us(3); //等待TPD
}
//唤醒
void GD25Q128_WAKEUP(void)
{
GD25Q128_CS_L(); //使能器件
GD25Q128_SPI_ReadWriteByte(GD25X_ReleasePowerDown); // send GD25X_PowerDown command 0xAB
GD25Q128_CS_H(); //取消片选
delay_us(3); //等待TRES1
}
gd25q128.h
#ifndef __GD25Q128_H__
#define __GD25Q128_H__
#include "main.h"
//GD25X系列/Q系列芯片列表
#define GD25Q80 0XC813
#define GD25Q16 0XC814
#define GD25Q32 0XC815
#define GD25Q64 0XC816
#define GD25Q128 0XC817
#define GD25Q128_CS_H() SPI1_CS_HIGH()
#define GD25Q128_CS_L() SPI1_CS_LOW()
extern uint16_t GD25Q128_TYPE;
extern uint32_t GD25Q128_SIZE;
extern uint8_t GD25Q128_UID[8];
//指令表
#define GD25X_WriteEnable 0x06
#define GD25X_WriteDisable 0x04
#define GD25X_ReadStatusReg 0x05
#define GD25X_WriteStatusReg 0x01
#define GD25X_ReadData 0x03
#define GD25X_FastReadData 0x0B
#define GD25X_FastReadDual 0x3B
#define GD25X_PageProgram 0x02
#define GD25X_BlockErase 0xD8
#define GD25X_SectorErase 0x20
#define GD25X_ChipErase 0xC7
#define GD25X_PowerDown 0xB9
#define GD25X_ReleasePowerDown 0xAB
#define GD25X_DeviceID 0xAB
#define GD25X_ManufactDeviceID 0x90
#define GD25X_JedecDeviceID 0x9F
int GD25Q128_Init(void);
void GD25Q128_ReadUniqueID(uint8_t UID[8]);
uint16_t GD25Q128_ReadID(void); //读取FLASH ID
uint8_t GD25Q128_ReadSR(void); //读取状态寄存器
void GD25Q128_Write_SR(uint8_t sr); //写状态寄存器
void GD25Q128_Write_Enable(void); //写使能
void GD25Q128_Write_Disable(void); //写保护
void GD25Q128_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void GD25Q128_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead); //读取flash
void GD25Q128_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);//写入flash
void GD25Q128_Erase_Chip(void); //整片擦除
void GD25Q128_Erase_Sector(uint32_t Dst_Addr); //扇区擦除
void GD25Q128_Wait_Busy(void); //等待空闲
void GD25Q128_PowerDown(void); //进入掉电模式
void GD25Q128_WAKEUP(void); //唤醒
uint32_t GD25Q128_ReadCapacity(void);
void GD25Q128_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite);
#endif
二、FRAM MB85RC16
(1) MB85RC16芯片引脚介绍
(2) 单片机如何读多块MB85RC16
即,当主机和一个EEPROM通信时,从机地址为1010 000+WR,如果主机和多个EEPROM通信时,从机地址为1010 A2 A1 A0 + WR
(3) IIC时序分析
a. 从机地址
b. 写单个字节
c. 写一页
一页的大小是8K,写入字节长度超过8K,将会覆盖之前写入内容
d. 当前地址读
e. 指定地址读
(4) STM32CubeMX配置IIC通信(HAL库)
标准模式,100KHz时钟,7位地址
(5) MB85RC16驱动代码
#define MB85RC16_Default_I2C_Addr 0xA0
void MB85RC16_Write(uint32_t addr, uint8_t * data, uint32_t len)
{
uint8_t MB85RC16_I2C_Addr;
MB85RC16_I2C_Addr = MB85RC16_Default_I2C_Addr | ((addr>>8)<<1); //high 3-bit access address placed into I2C address
uint8_t TD[len+1];
TD[0] = addr & 0x00FF; //low 8-bit access address placed into I2C first data
memcpy(TD+1, data, len);
HAL_I2C_Master_Transmit(&hi2c1, MB85RC16_I2C_Addr, TD, len+1, 2700); //Write data
}
void MB85RC1M_Read(uint32_t addr, uint8_t * data, uint32_t len)
{
uint8_t MB85RC16_I2C_Addr;
MB85RC16_I2C_Addr = MB85RC16_Default_I2C_Addr | ((addr>>8)<<1); //high 3-bit access address placed into I2C address
uint8_t RA[1];
RA[0] = addr & 0x00FF; //low 8-bit access address placed into I2C first data
HAL_I2C_Master_Transmit(&hi2c1, MB85RC16_I2C_Addr, &RA[0], 1, 2700); //Write address for read
HAL_I2C_Master_Receive(&hi2c1, MB85RC16_I2C_Addr, data, len, 2700); //Read data
}
三、AT24CXX
(1)引脚介绍
A0,A1,A2接到GND上,地址固定为0;SCL、SDA引脚内部为开漏输出,所以需接上拉电阻;WP引脚接GND,表示芯片可读可写。
(2) GD32F470使用IIC读写AT24C02
iic.h
#ifndef __IIC_H
#define __IIC_H
#include "head.h"
#define I2C1_SPEED 400000
#define I2C1_SLAVE_ADDRESS7 0xA0
#define I2C_PAGE_SIZE 8
void i2c1_config(void);
#endif
iic.c
#include "iic.h"
void i2c1_config(void)
{
rcu_periph_clock_enable(RCU_GPIOF);/* enable GPIOF clock */
rcu_periph_clock_enable(RCU_I2C1);/* enable I2C1 clock */
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_1);/* connect PF1 to I2C1_SCL */
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_0);/* connect PF0 to I2C1_SDA */
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_0);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_1);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,GPIO_PIN_2);
gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2);
gpio_bit_write(GPIOF,GPIO_PIN_2,RESET);//写使能
rcu_periph_clock_enable(RCU_I2C1);/* enable I2C clock */
i2c_clock_config(I2C1,I2C1_SPEED,I2C_DTCY_2);/* configure I2C clock */
i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,I2C1_SLAVE_ADDRESS7);/* configure I2C address */
i2c_enable(I2C1);/* enable I2C1 */
i2c_ack_config(I2C1,I2C_ACK_ENABLE);/* enable acknowledge */
}
eeprom.h
#ifndef __EEPROM_H
#define __EEPROM_H
#include "head.h"
#define EEP_FIRST_PAGE 0x00
#define I2C_OK 0
#define I2C_FAIL 1
/* I2C read and write functions */
uint8_t i2c_24c02_test(void);
/* initialize peripherals used by the I2C EEPROM driver */
void i2c_eeprom_init(void);
/* write buffer of data to the I2C EEPROM */
void eeprom_buffer_write(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte);
/* write one byte to the I2C EEPROM */
void eeprom_byte_write(uint8_t *p_buffer, uint8_t write_address);
/* write more than one byte to the EEPROM with a single write cycle */
void eeprom_page_write(uint8_t *p_buffer, uint8_t write_address, uint8_t number_of_byte);
/* read data from the EEPROM */
void eeprom_buffer_read(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte);
/* wait for EEPROM standby state */
void eeprom_wait_standby_state(void);
#endif
eeprom.c
#include "eeprom.h"
#define EEPROM_BLOCK0_ADDRESS 0xA0
#define BUFFER_SIZE 256
uint16_t eeprom_address;
/*!
\brief I2C read and write functions
\param[in] none
\param[out] none
\retval I2C_OK or I2C_FAIL
*/
uint8_t i2c_24c02_test(void)
{
uint16_t i;
uint8_t i2c_buffer_write[BUFFER_SIZE];
uint8_t i2c_buffer_read[BUFFER_SIZE];
for(i = 0;i < BUFFER_SIZE;i++){ i2c_buffer_write[i]=i;}
eeprom_buffer_write(i2c_buffer_write,EEP_FIRST_PAGE, BUFFER_SIZE);
eeprom_buffer_read(i2c_buffer_read,EEP_FIRST_PAGE, BUFFER_SIZE);
for(i = 0;i < BUFFER_SIZE;i++){printf("0x%02X ", i2c_buffer_read[i]);}
return I2C_OK;
}
void i2c_eeprom_init(void)
{
eeprom_address = EEPROM_BLOCK0_ADDRESS;
}
void eeprom_buffer_write(uint8_t* p_buffer, uint8_t write_address, uint16_t number_of_byte)
{
uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
address = write_address % I2C_PAGE_SIZE;
count = I2C_PAGE_SIZE - address;
number_of_page = number_of_byte / I2C_PAGE_SIZE;
number_of_single = number_of_byte % I2C_PAGE_SIZE;
/* if write_address is I2C_PAGE_SIZE aligned */
if(0 == address){
while(number_of_page--){
eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE);
eeprom_wait_standby_state();
write_address += I2C_PAGE_SIZE;
p_buffer += I2C_PAGE_SIZE;
}
if(0 != number_of_single){
eeprom_page_write(p_buffer, write_address, number_of_single);
eeprom_wait_standby_state();
}
}else{
/* if write_address is not I2C_PAGE_SIZE aligned */
if(number_of_byte < count){
eeprom_page_write(p_buffer, write_address, number_of_byte);
eeprom_wait_standby_state();
}else{
number_of_byte -= count;
number_of_page = number_of_byte / I2C_PAGE_SIZE;
number_of_single = number_of_byte % I2C_PAGE_SIZE;
if(0 != count){
eeprom_page_write(p_buffer, write_address, count);
eeprom_wait_standby_state();
write_address += count;
p_buffer += count;
}
/* write page */
while(number_of_page--){
eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE);
eeprom_wait_standby_state();
write_address += I2C_PAGE_SIZE;
p_buffer += I2C_PAGE_SIZE;
}
/* write single */
if(0 != number_of_single){
eeprom_page_write(p_buffer, write_address, number_of_single);
eeprom_wait_standby_state();
}
}
}
}
void eeprom_byte_write(uint8_t* p_buffer, uint8_t write_address)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* send the EEPROM's internal address to write to : only one byte address */
i2c_data_transmit(I2C1, 0x00);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
i2c_data_transmit(I2C1, write_address);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send the byte to be written */
i2c_data_transmit(I2C1, *p_buffer);
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}
void eeprom_page_write(uint8_t* p_buffer, uint8_t write_address, uint8_t number_of_byte)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while( SET != i2c_flag_get(I2C1, I2C_FLAG_TBE));
/* send the EEPROM's internal address to write to : only one byte address */
i2c_data_transmit(I2C1, 0x00);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
i2c_data_transmit(I2C1, write_address);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* while there is data to be written */
while(number_of_byte--){
i2c_data_transmit(I2C1, *p_buffer);
/* point to the next byte to be written */
p_buffer++;
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}
void eeprom_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
if(2 == number_of_byte){
i2c_ackpos_config(I2C1,I2C_ACKPOS_NEXT);
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get( I2C1 , I2C_FLAG_TBE));
/* enable I2C1*/
i2c_enable(I2C1);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C1, 0x00);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
i2c_data_transmit(I2C1, read_address);
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, eeprom_address, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
}
/* while there is data to be read */
while(number_of_byte){
if(3 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C1, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C1);
/* point to the next location where the byte read will be saved */
p_buffer++;
/* decrement the read bytes counter */
number_of_byte--;
}
}
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
/* enable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C1,I2C_ACKPOS_CURRENT);
}
void eeprom_wait_standby_state(void)
{
__IO uint32_t val = 0;
while(1){
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, eeprom_address, I2C_TRANSMITTER);
/* keep looping till the Address is acknowledged or the AE flag is set (address not acknowledged at time) */
do{
/* get the current value of the I2C_STAT0 register */
val = I2C_STAT0(I2C1);
}while(0 == (val & (I2C_STAT0_ADDSEND | I2C_STAT0_AERR)));
/* check if the ADDSEND flag has been set */
if(val & I2C_STAT0_ADDSEND){
/* clear ADDSEND flag */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* exit the function */
return ;
} else {
/* clear the bit of AERR */
i2c_flag_clear(I2C1, I2C_FLAG_AERR);
}
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}
}
四、总结
EEPROM和FLASH区别
相同点:
- 非易失性:两者都可以在没有电源的情况下保存数据。
- 电可擦除:都可以通过电信号擦除和重新编程。
- 用途:两者都用于存储固件、配置数据等。
不同点:
-
擦除单位:
- EEPROM:可以逐个字节擦除和写入。
- Flash:通常以块(通常是数KB到数MB)的方式擦除,不能逐个字节擦除。
-
写入速度:
- EEPROM:写入速度相对较慢,通常是字节级写入。
- Flash:写入速度较快,可以在一个块中并行写入。
-
使用寿命:
- EEPROM:通常具有更高的擦写耐久性,通常为10万次擦写。
- Flash:擦写次数一般较少,通常为1万到10万次,但近年来的发展使得一些Flash类型的耐久性有所提高。
-
成本和密度:
- EEPROM:相对较贵,存储密度较低。
- Flash:更便宜,存储密度更高,因此在大容量存储解决方案中更受欢迎。
-
应用场景:
- EEPROM:常用于需要频繁更新的小规模数据,典型应用包括配置存储、校准数据等。
- Flash:被广泛用于USB闪存驱动器、SD卡、固态硬盘(SSD)等大容量存储设备。
作者:m0_61973119