spi概述
SPI是串行外設(shè)接口(Serial Peripheral Interface)的縮寫,是一種高速的,全雙工,同步的通信總線,并且在芯片的管腳上只占用四根線,節(jié)約了芯片的管腳,同時(shí)為PCB的布局上節(jié)省空間,提供方便,正是出于這種簡單易用的特性,越來越多的芯片集成了這種通信協(xié)議,比如 EEPROM,F(xiàn)LASH,實(shí)時(shí)時(shí)鐘,AD轉(zhuǎn)換器。 W25Q64 是一款SPI接口的Flash芯片,其存儲空間為 64Mbit,相當(dāng)于8M字節(jié)。W25Q64可以支持 SPI 的模式 0 和模式 3,也就是 CPOL=0/CPHA=0 和CPOL=1/CPHA=1 這兩種模式。 最近在弄ST和GD的課程,需要GD樣片的可以加群申請:6_15061293 。
視頻教學(xué)
[https://www.bilibili.com/video/BV13g411z7gi/]
csdn課程
課程更加詳細(xì)。
[https://download.csdn.net/course/detail/37152]
生成例程
這里準(zhǔn)備了自己繪制的開發(fā)板進(jìn)行驗(yàn)證。
配置時(shí)鐘樹,配置時(shí)鐘為64M。
查看原理圖,PA9和PA10設(shè)置為開發(fā)板的串口。
配置串口。
由于需要輸入數(shù)據(jù),開啟DMA進(jìn)行接收。
中斷。
SPI配置
在開發(fā)板中有arduino接口,配置這幾個(gè)接口為spi。
本次實(shí)驗(yàn)使用的SPI與Flash通信,配置如下。 SPI的通信原理很簡單,它以主從方式工作,這種模式通常有一個(gè)主設(shè)備和一個(gè)或多個(gè)從設(shè)備,需要至少4根線,事實(shí)上3根也可以(單向傳輸時(shí))。也是所有基于SPI的設(shè)備共有的,它們是MISO(主設(shè)備數(shù)據(jù)輸入)、MOSI(主設(shè)備數(shù)據(jù)輸出)、SCLK(時(shí)鐘)、CS(片選)。
(1)MISO– Master Input Slave Output,主設(shè)備數(shù)據(jù)輸入,從設(shè)備數(shù)據(jù)輸出;
(2)MOSI– Master Output Slave Input,主設(shè)備數(shù)據(jù)輸出,從設(shè)備數(shù)據(jù)輸入;
(3)SCLK – Serial Clock,時(shí)鐘信號,由主設(shè)備產(chǎn)生;
(4)CS – Chip Select,從設(shè)備使能信號,由主設(shè)備控制。
負(fù)責(zé)通訊的3根線了。通訊是通過數(shù)據(jù)交換完成的,這里先要知道SPI是串行通訊協(xié)議,也就是說數(shù)據(jù)是一位一位的傳輸?shù)?。這就是SCLK時(shí)鐘線存在的原因,由SCLK提供時(shí)鐘脈沖,SDI,SDO則基于此脈沖完成數(shù)據(jù)傳輸。數(shù)據(jù)輸出通過 SDO線,數(shù)據(jù)在時(shí)鐘上升沿或下降沿時(shí)改變,在緊接著的下降沿或上升沿被讀取。完成一位數(shù)據(jù)傳輸,輸入也使用同樣原理。因此,至少需要8次時(shí)鐘信號的改變(上沿和下沿為一次),才能完成8位數(shù)據(jù)的傳輸。 時(shí)鐘信號線SCLK只能由主設(shè)備控制,從設(shè)備不能控制。同樣,在一個(gè)基于SPI的設(shè)備中,至少有一個(gè)主設(shè)備。這樣的傳輸方式有一個(gè)優(yōu)點(diǎn),在數(shù)據(jù)位的傳輸過程中可以暫停,也就是時(shí)鐘的周期可以為不等寬,因?yàn)闀r(shí)鐘線由主設(shè)備控制,當(dāng)沒有時(shí)鐘跳變時(shí),從設(shè)備不采集或傳送數(shù)據(jù)。SPI還是一個(gè)數(shù)據(jù)交換協(xié)議:因?yàn)镾PI的數(shù)據(jù)輸入和輸出線獨(dú)立,所以允許同時(shí)完成數(shù)據(jù)的輸入和輸出。芯片集成的SPI串行同步時(shí)鐘極性和相位可以通過寄存器配置,IO模擬的SPI串行同步時(shí)鐘需要根據(jù)從設(shè)備支持的時(shí)鐘極性和相位來通訊。 最后,SPI接口的一個(gè)缺點(diǎn):沒有指定的流控制,沒有應(yīng)答機(jī)制確認(rèn)是否接收到數(shù)據(jù)。
其中,CS是從芯片是否被主芯片選中的控制信號,也就是說只有片選信號為預(yù)先規(guī)定的使能信號時(shí)(高電位或低電位),主芯片對此從芯片的操作才有效。這就使在同一條總線上連接多個(gè)SPI設(shè)備成為可能。 隨便配置一個(gè)端口為CS片選,并且命名為CS。
NOR Flash
NOR Flash是一種非易失閃存技術(shù),是Intel在1988年創(chuàng)建。是市場上兩種主要的非易失閃存技術(shù)之一。 以GD25Q64E為例,該 Flash為64M-bit大小,即8192K-Byte。
W25Q64將8M的容量分為127個(gè)塊(Block),每個(gè)塊大小為64K字節(jié),每個(gè)塊又分為16個(gè)扇區(qū)(Sector),每個(gè)扇區(qū)4K個(gè)字節(jié)。W25Q64的最小擦除單位為一個(gè)扇區(qū),也就是每次必須擦除4K個(gè)字節(jié)。 即4K16128=8192K=8M
W25Q64的原理及應(yīng)用
復(fù)位初始化
對于復(fù)位,需要發(fā)送0x66和0x99
代碼中的初始化。
/* Reset Operations */
#define RESET_ENABLE_CMD 0x66
#define RESET_MEMORY_CMD 0x99
/**
* @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(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
}
ID
對于兆易創(chuàng)新W25Q64,主要有三種查詢ID方式。
可以使用90H查詢設(shè)備ID,以判斷是否是W25Q64設(shè)備。
/* Identification Operations */
#define READ_ID_CMD 0x9F
/**
* @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(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
}
讀取數(shù)據(jù)
對于兆易創(chuàng)新W25Q64,讀取數(shù)據(jù)使用0x03指令,后面添加需要讀取的數(shù)據(jù)地址。 數(shù)據(jù)可以一直進(jìn)行讀取,當(dāng)不需要讀取數(shù)據(jù)時(shí)候?qū)⑵xCS拉高,關(guān)閉時(shí)鐘SCLK即可。
#define READ_CMD 0x03
/**
* @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(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
W25Qx_Disable();
return W25Qx_OK;
}
以讀取10個(gè)數(shù)據(jù)為例子,波形如下所示。
BSP_W25Qx_Read(rData2,0x1000,0x00a);
擦除扇區(qū)
最小的擦除單位是扇區(qū),擦除指令為0x20和3字節(jié)的地址。
#define SECTOR_ERASE_CMD 0x20
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(&hspi1, 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;
}
寫數(shù)據(jù)
對于寫數(shù)據(jù)到flash中,使用0x02指令進(jìn)行寫數(shù)據(jù),后面還需要指定24位地址,才能進(jìn)行寫數(shù)據(jù)。
#define PAGE_PROG_CMD 0x02
/**
* @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)//判斷地址屬于哪一扇區(qū)開始
{
current_addr += W25Q128FV_PAGE_SIZE;//0x100- > 256 bytes
}
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(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
/* Transmission of the data */
if (HAL_SPI_Transmit(&hspi1, 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;
}
對flash的0x1000地址進(jìn)行寫數(shù)據(jù),指令如下。
BSP_W25Qx_Write(wData2,0x1000,0x000a);
波形如下所示。
W25Qx.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 "W25Qx.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(&hspi1, 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(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,&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(&hspi1, 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(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,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(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
if (HAL_SPI_Receive(&hspi1, 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)//判斷地址屬于哪一扇區(qū)開始
{
current_addr += W25Q128FV_PAGE_SIZE;//0x100- > 256 bytes
}
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(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
{
return W25Qx_ERROR;
}
/* Transmission of the data */
if (HAL_SPI_Transmit(&hspi1, 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(&hspi1, 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(&hspi1, 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;
}
W25Qx.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 __W25Qx_H
#define __W25Qx_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx.h"
#include "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_SECTOR_SIZE 0x10000 /* 256 sectors of 64KBytes */
#define W25Q128FV_SUBSECTOR_SIZE 0x1000 /* 4096 subsectors of 4kBytes */
#define W25Q128FV_PAGE_SIZE 0x100 /* 65536 pages of 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(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)
#define W25Qx_Disable() HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, 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);
/**
* @}
*/
/** @defgroup W25Q128FV_Exported_Functions
* @{
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* __W25Qx_H */
案例
向0扇區(qū)(0塊0扇區(qū)),17扇區(qū)(1塊1扇區(qū)),34扇區(qū)(2塊2扇區(qū))分別寫入0x200的數(shù)據(jù)。
頭文件定義
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "W25Qx.h"
/* USER CODE END Includes */
串口接收和flash數(shù)組定義
/* USER CODE BEGIN PV */
#define BUFFERSIZE 255 //可以接收的最大字符個(gè)數(shù)
uint8_t ReceiveBuff[BUFFERSIZE]; //接收緩沖區(qū)
uint8_t recv_end_flag = 0,Rx_len;//接收完成中斷標(biāo)志,接收到字符長度
uint8_t wData1[0x200];
uint8_t wData2[0x200];
uint8_t wData3[0x200];
uint8_t rData1[0x200];
uint8_t rData2[0x200];
uint8_t rData3[0x200];
uint8_t ID[4];
uint32_t i;
uint8_t flag[1] ;
int i_flag = 0;
/* USER CODE END PV */
串口重定向
/* USER CODE BEGIN PFP */
void uart1_data(void);
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END PFP */
串口中斷設(shè)置
#include "stm32f1xx_it.c"文件中斷外部變量引用:
/* USER CODE BEGIN 0 */
#define BUFFERSIZE 255 //可接收的最大數(shù)據(jù)量
extern uint8_t recv_end_flag,Rx_len,bootfirst;
/* USER CODE END 0 */
串口2中斷函數(shù):
/**
* @brief This function handles USART2 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART2_IRQn 1 */
uint32_t temp;
if(USART1 == huart1.Instance)//判斷是否為串口2中斷
{
if(RESET != __HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE))//如果為串口2
{
__HAL_UART_CLEAR_IDLEFLAG(&huart1);//清除中斷標(biāo)志
HAL_UART_DMAStop(&huart1);//停止DMA接收
temp = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);//獲取DMA當(dāng)前還有多少未填充
Rx_len = BUFFERSIZE - temp; //計(jì)算串口接收到的數(shù)據(jù)個(gè)數(shù)
recv_end_flag = 1;
}
}
/* USER CODE END USART2_IRQn 1 */
}
主程序
讀取ID和flash數(shù)據(jù)及擦除。
/* USER CODE BEGIN 2 */
printf("GD Nor Flash案例n");
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能串口1 IDLE中斷
/*##-1- Read the device ID ########################*/
BSP_W25Qx_Init();//初始化W25Q128
BSP_W25Qx_Read_ID(ID);//讀取ID
if((ID[0] != 0xC8) | (ID[1] != 0x16))
{
Error_Handler();//如果 ID不對打印錯(cuò)誤
}
else//ID正確,打印ID
{
printf("W25Q64 ID : ");
for(i=0;i< 2;i++)
{
printf("0x%02X ",ID[i]);
}
printf("rnrn");
}
/**************************讀取第0扇區(qū)數(shù)據(jù)**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)
printf("讀取原始的0個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取原始的0個(gè)扇區(qū)數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n0扇區(qū)第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData1[i]);
}
printf("n");
/**************************讀取第17扇區(qū)數(shù)據(jù)**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x1000,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取原始的17個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取原始的2個(gè)扇區(qū)數(shù)據(jù)為:");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n17扇區(qū)第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData2[i]);
}
printf("n");
/**************************讀取第34扇區(qū)數(shù)據(jù)**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x2000,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取原始的34個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取原始的34個(gè)扇區(qū)數(shù)據(jù)為: ");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n34扇區(qū)第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData3[i]);
}
printf("n");
/**************************清除第0扇區(qū)數(shù)據(jù)為0**************************************************************/
/*##-2- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*##-2- Written to the flash ########################*/
/* fill buffer */
printf(" 初始化數(shù)據(jù),清零第0扇區(qū)前0x200的數(shù)據(jù)!rn");
for(i =0;i< 0x200;i ++)
{
wData1[i] = 0;
rData1[i] = 0;
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x00,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
printf("清零第0扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
Error_Handler();
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
printf("讀取第0扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取第0扇區(qū)前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData1[i]);
}
printf("n");
/**************************清除第17扇區(qū)數(shù)據(jù)為0**************************************************************/
/*##-2- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*##-2- Written to the flash ########################*/
/* fill buffer */
printf(" 初始化數(shù)據(jù),清零第17扇區(qū)前0x200的數(shù)據(jù)!rn");
for(i =0;i< 0x200;i ++)
{
wData2[i] = 0;
rData2[i] = 0;
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x1000,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
printf("清零第2扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
Error_Handler();
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取第17扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取第17扇區(qū)前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData2[i]);
}
printf("n");
/**************************清除第34扇區(qū)數(shù)據(jù)為0**************************************************************/
/*##-2- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*##-2- Written to the flash ########################*/
/* fill buffer */
printf(" 初始化數(shù)據(jù),清零第34扇區(qū)前0x200的數(shù)據(jù)!rn");
for(i =0;i< 0x200;i ++)
{
wData3[i] = 0;
rData3[i] = 0;
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x22000,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
printf("清零第34扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
Error_Handler();
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取第34扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取第34扇區(qū)前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData3[i]);
}
printf("n");
/* USER CODE END 2 */
主程序。
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
uart2_data();
HAL_Delay(100);
}
/* USER CODE END 3 */
數(shù)據(jù)處理
/* USER CODE BEGIN 4 */
void uart2_data(void)
{
if(recv_end_flag ==1)//接收完成標(biāo)志
{
if(ReceiveBuff[0]==0x00)
{
printf("寫入數(shù)據(jù)長度:%dn",Rx_len-2);
for(int i =0;i< Rx_len-2;i++)
{
wData1[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];
}
/*##-2- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x00,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
printf("扇區(qū)0數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
Error_Handler();
if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
printf("讀取扇區(qū)0前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取扇區(qū)0前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",wData1[i]);
}
printf("n");
}
else if(ReceiveBuff[0]==0x17)
{
printf("寫入數(shù)據(jù)長度:%dn",Rx_len-2);
for(int i =0;i< Rx_len-2;i++)
{
// Data[i]=ReceiveBuff[i+2];
wData2[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];
}
/*##-17- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x11000,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
printf("扇區(qū)17數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
Error_Handler();
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取扇區(qū)17前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取扇區(qū)17前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData2[i]);
}
printf("n");
}
else if(ReceiveBuff[0]==0x34)
{
printf("寫入數(shù)據(jù)長度:%dn",Rx_len-2);
for(int i =0;i< Rx_len-2;i++)
{
// Data[i]=ReceiveBuff[i+2];
wData3[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];
}
/*##-22- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)
printf(" QSPI Erase Block okrn");
else
Error_Handler();
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x22000,寫入數(shù)據(jù)長度0x200*/
if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
printf("扇區(qū)34數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
Error_Handler();
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取扇區(qū)34前0x200數(shù)據(jù)成功!rnrn");
else
Error_Handler();
/*打印數(shù)據(jù)*/
printf("讀取扇區(qū)34前0x200數(shù)據(jù)為: rn");
for(i =0;i< 0x200;i++)
{
if(i%20==0)
printf("n第%d到%d的數(shù)據(jù)為:rn",i,i+19);
printf("0x%02X ",rData3[i]);
}
printf("n");
}
else
printf("輸入錯(cuò)誤!");
for(int i = 0; i < Rx_len ; i++) //清空接收緩存區(qū)
ReceiveBuff[i]=0;//置0
Rx_len=0;//接收數(shù)據(jù)長度清零
recv_end_flag=0;//接收標(biāo)志位清零
//開啟下一次接收
HAL_UART_Receive_DMA(&huart1,(uint8_t*)ReceiveBuff,BUFFERSIZE);
}
}
/* USER CODE END 4 */
演示
W25Q64芯片型號的ID為0XEF17,下方讀取為0XC816,所以讀取成功。
開機(jī)會打印出0,17,34扇區(qū)的前0x200個(gè)數(shù)據(jù)。
打印完原始數(shù)據(jù)之后將數(shù)據(jù)全部清零,清零完成如下圖所示。
串口定義了ReceiveBuff[0]的數(shù)據(jù)為寫入什么扇區(qū),ReceiveBuff[0]為1寫入扇區(qū)1,ReceiveBuff[0]為2寫入扇區(qū)2,ReceiveBuff[0]為3寫入扇區(qū)3,若為其他數(shù)據(jù),則打印輸入錯(cuò)誤;ReceiveBuff[1]則為寫入的位置。 輸入:00 05 01 02 03 04 向扇區(qū)0的的05號位置開始寫入數(shù)據(jù)01 02 03 04。
輸入:00 28 11 12 13 14 15 向扇區(qū)0的的40(28是十六進(jìn)制)號位置開始寫入數(shù)據(jù)11 12 13 14 15。
輸入:17 10 aa bb 向扇區(qū)17的的16(10是十六進(jìn)制)號位置開始寫入數(shù)據(jù)aa bb。
審核編輯:湯梓紅
-
FlaSh
+關(guān)注
關(guān)注
10文章
1635瀏覽量
148016 -
SPI
+關(guān)注
關(guān)注
17文章
1706瀏覽量
91578 -
兆易創(chuàng)新
+關(guān)注
關(guān)注
23文章
605瀏覽量
80627 -
stm32cubemx
+關(guān)注
關(guān)注
5文章
283瀏覽量
14807 -
gd32f303
+關(guān)注
關(guān)注
4文章
38瀏覽量
3693
發(fā)布評論請先 登錄
相關(guān)推薦
評論