W25Q128---读写

总结:通信方式是SPI,读数据可以从任何地方读,写数据和擦出数据需要按照页或者扇区或者簇为单位进行。

写数据:一次最多写一页,如果超出一页数据长度,则分几次完成。例如本芯片一个扇区为4096个字节,那么需要写16页,要进行至少16次按页写数据。

擦数据:擦数据的最小单位是一个扇区,也可以直接擦出整个芯片。

芯片介绍:

容量:16Mbytes  一页为256个字节   一个扇区为4K个字节

数据手册介绍:

The W25Q128FV array is organized into 65,536 programmable pages of 256-bytes each. Up to 256
  bytes can be programmed at a time. Pages can be erased in groups of 16 (4KB sector erase), groups of
  128 (32KB block erase), groups of 256 (64KB block erase) or the entire chip (chip erase). The
  W25Q128FV has 4,096 erasable sectors and 256 erasable blocks respectively. The small 4KB sectors
  allow for greater flexibility in applications that require data and parameter storage 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
stm32f103标准库是一种在STM32F103系列微控制器上进行开发的库文件。该库文件提供了丰富的函数和接口,可以方便地进行外设的控制与操作。在使用stm32f103标准库进行开发时,可以利用该库来实现对W25Q128闪存芯片的读写操作。 下面是一个示例的程序源码,演示了如何使用stm32f103标准库来实现W25Q128读写功能: #include "stm32f10x.h" #include "stm32f10x_spi.h" #include "stm32f10x_gpio.h" #define SPI1_CS_PIN GPIO_Pin_4 #define SPI1_CS_PORT GPIOA #define SPI1_MOSI_PIN GPIO_Pin_5 #define SPI1_MOSI_PORT GPIOA #define SPI1_MISO_PIN GPIO_Pin_6 #define SPI1_MISO_PORT GPIOA #define SPI1_SCK_PIN GPIO_Pin_7 #define SPI1_SCK_PORT GPIOA void SPI1_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; // 配置SPI引脚 GPIO_InitStructure.GPIO_Pin = SPI1_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_CS_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI1_MISO_PIN | SPI1_SCK_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_MISO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPI1_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_MOSI_PORT, &GPIO_InitStructure); // 配置SPI SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); } void W25Q128_Read(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) { GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片 SPI1->DR = 0x03; // 发送读命令 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = (ReadAddr & 0xFF0000) >> 16; // 发送地址 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = (ReadAddr & 0xFF00) >> 8; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = ReadAddr & 0xFF; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); while (NumByteToRead--) { SPI1->DR = 0xFF; // 发送读数据命令 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); *pBuffer = SPI1->DR; pBuffer++; } GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片 } void W25Q128_Write(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) { GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片 SPI1->DR = 0x06; // 发送写使能命令 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片 GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片 SPI1->DR = 0x02; // 发送写命令 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = (WriteAddr & 0xFF0000) >> 16; // 发送地址 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = (WriteAddr & 0xFF00) >> 8; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI1->DR = WriteAddr & 0xFF; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); while (NumByteToWrite--) { SPI1->DR = *pBuffer; // 发送写数据命令 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); pBuffer++; } GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片 } int main(void) { // 初始化SPI SPI1_Configuration(); // 读取数据 uint32_t addr = 0x000000; uint8_t buffer[60]; W25Q128_Read(buffer, addr, sizeof(buffer)); // 写入数据 uint8_t data_to_write[] = "Hello, World!"; uint16_t size = sizeof(data_to_write) - 1; W25Q128_Write(data_to_write, addr, size); while(1); return 0; } 上述程序实现了对W25Q128闪存芯片的读写功能。通过配置SPI1来连接STM32F103与W25Q128,在读取数据时使用了读命令和地址,读取完数据后将其存储在指定的缓冲区中。在写入数据时同样使用了写命令和地址,然后逐个字节地将数据写入闪存芯片。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值