文章目录

  • main.c
  • W25QXX.h
  • W25QXX.c

main.c

printf("\r\n SPI-W25Qxxx Example \r\n\r\n");/*##-1- Read the device ID  ########################*/BSP_W25Qx_Init();BSP_W25Qx_Read_ID(ID);printf(" W25Qxxx ID is : 0x%02X 0x%02X \r\n\r\n",ID[0],ID[1]);/*##-2- Erase Block ##################################*/if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)printf(" SPI Erase Block ok\r\n");elseError_Handler();/*##-3- Written to the flash ########################*//* fill buffer */for(i =0;i<0x100;i++){wData[i] = i;rData[i] = 0;}if(BSP_W25Qx_Write(wData,0x00,0x100)== W25Qx_OK)printf(" SPI Write ok\r\n");elseError_Handler();/*##-4- Read the flash     ########################*/if(BSP_W25Qx_Read(rData,0x00,0x100)== W25Qx_OK)printf(" SPI Read ok\r\n\r\n");elseError_Handler();printf("SPI Read Data : \r\n");for(i =0;i<0x100;i++)printf("0x%02X  ",rData[i]);printf("\r\n\r\n");/*##-5- check date          ########################*/   if(memcmp(wData,rData,0x100) == 0 )printf(" W25Q128FV SPI Test OK\r\n");elseprintf(" W25Q128FV SPI Test False\r\n");

W25QXX.h

/*********************************************************************************************************
*
* File                : W25Qx.h
* Hardware Environment:
* Build Environment   : RealView MDK-ARM  Version: 5.15
* Version             : V1.0
* By                  :
*
*                                  (c) Copyright 2005-2015, WaveShare
*                                       http://www.waveshare.net
*                                          All Rights Reserved
*
*********************************************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __W25QXX_H
#define __W25QXX_H#ifdef __cplusplusextern "C" {#endif /* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm32f4xx_hal_spi.h"/** @addtogroup BSP* @{*/ /** @addtogroup Components* @{*/ /** @addtogroup W25Q128FV* @{*//** @defgroup W25Q128FV_Exported_Types* @{*//*** @}*/ /** @defgroup W25Q128FV_Exported_Constants* @{*//** * @brief  W25Q128FV Configuration  */
//#define W25Q128FV_FLASH_SIZE                  0x1000000 /* 128 MBits => 16MBytes */
#define W25Q128FV_FLASH_SIZE                  0x200000 /* 16 MBits => 2*1024*1024Bytes */2*4*4
#define W25Q128FV_BLOCK_SIZE                 0x10000   /* 256*16*16 Bytes */
#define W25Q128FV_SECTOR_SIZE              0x1000    /* 256*16 Bytes */
#define W25Q128FV_PAGE_SIZE                   0x100     /* 256 bytes */#define W25Q128FV_DUMMY_CYCLES_READ           4
#define W25Q128FV_DUMMY_CYCLES_READ_QUAD      10#define W25Q128FV_BULK_ERASE_MAX_TIME         250000
#define W25Q128FV_SECTOR_ERASE_MAX_TIME       3000
#define W25Q128FV_SUBSECTOR_ERASE_MAX_TIME    800
#define W25Qx_TIMEOUT_VALUE 1000/** * @brief  W25Q128FV Commands  */
/* Reset Operations */
#define RESET_ENABLE_CMD                     0x66
#define RESET_MEMORY_CMD                     0x99#define ENTER_QPI_MODE_CMD                   0x38
#define EXIT_QPI_MODE_CMD                    0xFF/* Identification Operations */
#define READ_ID_CMD                          0x90
#define DUAL_READ_ID_CMD                     0x92
#define QUAD_READ_ID_CMD                     0x94
#define READ_JEDEC_ID_CMD                    0x9F/* Read Operations */
#define READ_CMD                             0x03
#define FAST_READ_CMD                        0x0B
#define DUAL_OUT_FAST_READ_CMD               0x3B
#define DUAL_INOUT_FAST_READ_CMD             0xBB
#define QUAD_OUT_FAST_READ_CMD               0x6B
#define QUAD_INOUT_FAST_READ_CMD             0xEB/* Write Operations */
#define WRITE_ENABLE_CMD                     0x06
#define WRITE_DISABLE_CMD                    0x04/* Register Operations */
#define READ_STATUS_REG1_CMD                  0x05
#define READ_STATUS_REG2_CMD                  0x35
#define READ_STATUS_REG3_CMD                  0x15#define WRITE_STATUS_REG1_CMD                 0x01
#define WRITE_STATUS_REG2_CMD                 0x31
#define WRITE_STATUS_REG3_CMD                 0x11/* Program Operations */
#define PAGE_PROG_CMD                        0x02
#define QUAD_INPUT_PAGE_PROG_CMD             0x32/* Erase Operations */
#define SECTOR_ERASE_CMD                     0x20
#define CHIP_ERASE_CMD                       0xC7#define PROG_ERASE_RESUME_CMD                0x7A
#define PROG_ERASE_SUSPEND_CMD               0x75/* Flag Status Register */
#define W25Q128FV_FSR_BUSY                    ((uint8_t)0x01)    /*!< busy */
#define W25Q128FV_FSR_WREN                    ((uint8_t)0x02)    /*!< write enable */
#define W25Q128FV_FSR_QE                      ((uint8_t)0x02)    /*!< quad enable */#define W25Qx_Enable()           HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_RESET)
#define W25Qx_Disable()         HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET)#define W25Qx_OK            ((uint8_t)0x00)
#define W25Qx_ERROR         ((uint8_t)0x01)
#define W25Qx_BUSY          ((uint8_t)0x02)
#define W25Qx_TIMEOUT               ((uint8_t)0x03)uint8_t BSP_W25Qx_Init(void);
static void BSP_W25Qx_Reset(void);
static uint8_t BSP_W25Qx_GetStatus(void);
uint8_t BSP_W25Qx_WriteEnable(void);
void BSP_W25Qx_Read_ID(uint8_t *ID);
uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size);
uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size);
uint8_t BSP_W25Qx_Erase_Block(uint32_t Address);
uint8_t BSP_W25Qx_Erase_Chip(void);
extern SPI_HandleTypeDef hspi2;
/*** @}*//** @defgroup W25Q128FV_Exported_Functions* @{*/
/*** @}*/ /*** @}*/ /*** @}*/ /*** @}*/#ifdef __cplusplus
}
#endif#endif /* __W25Qx_H */

W25QXX.c

/*********************************************************************************************************
*
* File                : ws_W25Qx.c
* Hardware Environment:
* Build Environment   : RealView MDK-ARM  Version: 4.20
* Version             : V1.0
* By                  :
*
*                                  (c) Copyright 2005-2011, WaveShare
*                                       http://www.waveshare.net
*                                          All Rights Reserved
*
*********************************************************************************************************/#include "W25QXX.h"/*** @brief  Initializes the W25Q128FV interface.* @retval None*/
uint8_t BSP_W25Qx_Init(void)
{ /* Reset W25Qxxx */BSP_W25Qx_Reset();return BSP_W25Qx_GetStatus();
}/*** @brief  This function reset the W25Qx.* @retval None*/
static void BSP_W25Qx_Reset(void)
{uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};W25Qx_Enable();/* Send the reset command */HAL_SPI_Transmit(&hspi2, cmd, 2, W25Qx_TIMEOUT_VALUE);    W25Qx_Disable();}/*** @brief  Reads current status of the W25Q128FV.* @retval W25Q128FV memory status*/
static uint8_t BSP_W25Qx_GetStatus(void)
{uint8_t cmd[] = {READ_STATUS_REG1_CMD};uint8_t status;W25Qx_Enable();/* Send the read status command */HAL_SPI_Transmit(&hspi2, cmd, 1, W25Qx_TIMEOUT_VALUE); /* Reception of the data */HAL_SPI_Receive(&hspi2,&status, 1, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();/* Check the value of the register */if((status & W25Q128FV_FSR_BUSY) != 0){return W25Qx_BUSY;}else{return W25Qx_OK;}
}/*** @brief  This function send a Write Enable and wait it is effective.* @retval None*/
uint8_t BSP_W25Qx_WriteEnable(void)
{uint8_t cmd[] = {WRITE_ENABLE_CMD};uint32_t tickstart = HAL_GetTick();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi2, cmd, 1, W25Qx_TIMEOUT_VALUE);  /*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY){/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE){        return W25Qx_TIMEOUT;}}return W25Qx_OK;
}/*** @brief  Read Manufacture/Device ID.* @param  return value address* @retval None*/
void BSP_W25Qx_Read_ID(uint8_t *ID)
{uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi2, cmd, 4, W25Qx_TIMEOUT_VALUE); /* Reception of the data */HAL_SPI_Receive(&hspi2,ID, 2, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();}/*** @brief  Reads an amount of data from the QSPI memory.* @param  pData: Pointer to data to be read* @param  ReadAddr: Read start address* @param  Size: Size of data to read    * @retval QSPI memory status*/
uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
{uint8_t cmd[4];/* Configure the command */cmd[0] = READ_CMD;cmd[1] = (uint8_t)(ReadAddr >> 16);cmd[2] = (uint8_t)(ReadAddr >> 8);cmd[3] = (uint8_t)(ReadAddr);W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi2, cmd, 4, W25Qx_TIMEOUT_VALUE);  /* Reception of the data */if (HAL_SPI_Receive(&hspi2, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK){return W25Qx_ERROR;}W25Qx_Disable();return W25Qx_OK;
}/*** @brief  Writes an amount of data to the QSPI memory.* @param  pData: Pointer to data to be written* @param  WriteAddr: Write start address* @param  Size: Size of data to write,No more than 256byte.    * @retval QSPI memory status*/
uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
{uint8_t cmd[4];uint32_t end_addr, current_size, current_addr;uint32_t tickstart = HAL_GetTick();/* Calculation of the size between the write address and the end of the page */current_addr = 0;while (current_addr <= WriteAddr){current_addr += W25Q128FV_PAGE_SIZE;}current_size = current_addr - WriteAddr;/* Check if the size of the data is less than the remaining place in the page */if (current_size > Size){current_size = Size;}/* Initialize the adress variables */current_addr = WriteAddr;end_addr = WriteAddr + Size;/* Perform the write page by page */do{/* Configure the command */cmd[0] = PAGE_PROG_CMD;cmd[1] = (uint8_t)(current_addr >> 16);cmd[2] = (uint8_t)(current_addr >> 8);cmd[3] = (uint8_t)(current_addr);/* Enable write operations */BSP_W25Qx_WriteEnable();W25Qx_Enable();/* Send the command */if (HAL_SPI_Transmit(&hspi2,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK){return W25Qx_ERROR;}/* Transmission of the data */if (HAL_SPI_Transmit(&hspi2, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK){return W25Qx_ERROR;}W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY){/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE){        return W25Qx_TIMEOUT;}}/* Update the address and size variables for next page programming */current_addr += current_size;pData += current_size;current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE;} while (current_addr < end_addr);return W25Qx_OK;
}/*** @brief  Erases the specified block of the QSPI memory. * @param  BlockAddress: Block address to erase  * @retval QSPI memory status*/
uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
{uint8_t cmd[4];uint32_t tickstart = HAL_GetTick();cmd[0] = SECTOR_ERASE_CMD;cmd[1] = (uint8_t)(Address >> 16);cmd[2] = (uint8_t)(Address >> 8);cmd[3] = (uint8_t)(Address);/* Enable write operations */BSP_W25Qx_WriteEnable();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi2, cmd, 4, W25Qx_TIMEOUT_VALUE); /*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY){/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME){        return W25Qx_TIMEOUT;}}return W25Qx_OK;
}/*** @brief  Erases the entire QSPI memory.This function will take a very long time.* @retval QSPI memory status*/
uint8_t BSP_W25Qx_Erase_Chip(void)
{uint8_t cmd[4];uint32_t tickstart = HAL_GetTick();cmd[0] = SECTOR_ERASE_CMD;/* Enable write operations */BSP_W25Qx_WriteEnable();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi2, cmd, 1, W25Qx_TIMEOUT_VALUE);   /*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() != W25Qx_BUSY){/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME){        return W25Qx_TIMEOUT;}}return W25Qx_OK;
}

STM32F407VET6读写GD25Q16(W25Q16)相关推荐

  1. 2M字节Flash Rom存储器W25Q16/W25X16 认识Flash

    原地址:http://www.51hei.com/bbs/dpj-41029-1.html 认识Flash Rom FlashRom 是快速擦写只读存储器,也就是我们常说的"闪存" ...

  2. Flash Rom存储器W25Q16/W25X16

    认识Flash Rom FlashRom 是快速擦写只读存储器,也就是我们常说的"闪存",单片机程序存储器就是Flash Rom,所谓"闪存",就是一种非易失性 ...

  3. 【STM32CubeMX学习】SPI读写W25Q16

    1.SPI总线 SPI分为主从工作模式,通常有一个主设备和一个或多个从设备,本文中MCU为主机,W25Q16为从机. SPI通信有以下四根线: MISO:主设备数据输入,从设备数据输出. MOSI:主 ...

  4. 基于STM32实现W25Q16读写操作(spi)

    文章目录 前言 一.W25Q16 1.介绍 2.SPI 2.1. 简介 2.2. 特性 2.3. 功能说明 2.4. 工作模式 2.5. 引脚说明 二.代码开发 1.SPI初始化 2.读取厂商ID 2 ...

  5. STM32F7实现SPI读写,读取W25Q16型号

    SPI协议的原理,网上大把的资料可以找到,这里记录一下SPI的初始化过程,以即以读取W25Q16型号为例的一个简单的SPI读写过程. CubeMX配置: SPI模式 有只发送.只接收.半双工和全双工模 ...

  6. STM32F407ZGT6 fatfs出现挂载成功,但是文件读写失败的原因

    使用正点原子的板子f407,使用野火代码参考博客 2021-07-25 野火板子25MHz晶振改为正点原子8MH晶振_Car12-CSDN博客 参考源码 修复了野火的bug,使用cubemx初始化代码 ...

  7. 华芯微特SWM260读写W25Q128

    W25Q128的读写采用的是SPI接口:本人移植的程序是在STM32上验证过的,只需要修改相应的底层函数即可: 初始化SPI接口: void MX_SPI_Flash_Init(void) {SPI_ ...

  8. STM32学习之SPI协议(读写FLASH)

    关于STM32学习分享 第八章 SPI协议(读写FLASH) 文章目录 关于STM32学习分享 前言 二.代码 1.spi_flash.c 2.spi_flash.h 3.main.c 总结 前言 开 ...

  9. STM-32:SPI通信协议/W25Q64简介—软件SPI读写W25Q64

    目录 一.SPI简介 1.1电路模式 1.2通信原理 1.3SPI时序基本单元 1.3.1起始和终止 1.3.2交换字节 二.W25Q64 2.1W25Q64简介 2.2W25Q64硬件电路 2.3W ...

最新文章

  1. echarts怎么用在php,在Vue.JS中怎样使用echarts
  2. Fiat–Shamir heuristic 启发式的应用 理解 代码实现
  3. 485有时候从机接收指令没反应_原来微信发语音不好听,是你没掌握这2个技巧!快去试试吧...
  4. 一点MATLAB程序加速技巧
  5. 【题解】p1064 金明的预算方案
  6. 数据预测之BP神经网络具体应用以及matlab实现
  7. 解决ubuntu下pdf中文不显示或乱码问题
  8. leetcode - 1039. 多边形三角剖分的最低得分
  9. linux系统修复找不到原安装,Linux 系统用安装盘来修复GRUB 详解
  10. LIstView和TreeView相关联
  11. 商住楼和住宅楼的区别
  12. Fences 2.1 - 高效实用的桌面图标栅栏分类管理软件神器 (桌面图标再多也不会乱)
  13. 蓝桥杯试题 算法提高 Cutting Chains
  14. 仿班级聊天室(DOM原型法)并且用localStorage存储消息记录
  15. 与众不同 独树一帜,传智播客2018春季课程发布会在京举行
  16. wazhu之agent功能详解
  17. STM32学习-keil 调试问题1:单步可以执行 全速运行不可以
  18. c语言程序设计论文背单词系统,C语言课程设计-背单词系统(含程序)
  19. 很好的在线端口扫描网站
  20. JSJ—案例谈面向对象

热门文章

  1. 鼠标上下滚轮不灵敏的修复方法
  2. 线上线下一体化趋势下,零售品牌如何利用线上营销为营收赋能?
  3. android studio lambda插件,Android studio配置lambda表达式教程
  4. java 数字大小写转换工具类--适用于打印收据
  5. 关于腾讯云服务器的域名备案流程(尽力图文并茂)
  6. Python——解一元二次方程
  7. Linux SA--HugePage,HPET
  8. PAAS容器安全防护
  9. 最新ITIL考试题库(中英对照版初级)
  10. 用python对CSV文件中的数据进行统计分析