一、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。
- 1页 = 256bytes
- 1 扇区 = 16页
- 1 块 = 16扇区
W25Q64存储容量共64M-bit/ 8M-byte。
- 1页 = 256bytes
- 1 扇区 = 16页
- 1 块 = 16扇区
(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) 写W25QXX驱动代码
二、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)等大容量存储设备。