spi概述
SPI是串行外設(shè)接口(Serial Peripheral Interface)的縮寫,是一種高速的,全雙工,同步的通信總線,并且在芯片的管腳上只占用四根線,節(jié)約了芯片的管腳,同時(shí)為PCB的布局上節(jié)省空間,提供方便,正是出于這種簡(jiǎn)單易用的特性,越來(lái)越多的芯片集成了這種通信協(xié)議,比如 EEPROM,F(xiàn)LASH,實(shí)時(shí)時(shí)鐘,AD轉(zhuǎn)換器。 W25Q64 是一款SPI接口的Flash芯片,其存儲(chǔ)空間為 64Mbit,相當(dāng)于8M字節(jié)。W25Q64可以支持 SPI 的模式 0 和模式 3,也就是 CPOL=0/CPHA=0 和CPOL=1/CPHA=1 這兩種模式。 最近在弄ST和GD的課程,需要GD樣片的可以加群申請(qǐng):6_15061293 。
視頻教程
https://www.bilibili.com/video/BV16W4y147R1/
樣品申請(qǐng)
https://www.wjx.top/vm/wFGhGPF.aspx#
csdn課程
課程更加詳細(xì)。
https://download.csdn.net/course/detail/37144
生成例程
這里準(zhǔn)備了自己繪制的開發(fā)板進(jìn)行驗(yàn)證。
SPI配置
在開發(fā)板中有arduino接口,配置這幾個(gè)接口為spi。
本次實(shí)驗(yàn)使用的SPI與Flash通信,配置如下。 SPI的通信原理很簡(jiǎn)單,它以主從方式工作,這種模式通常有一個(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í)鐘信號(hào),由主設(shè)備產(chǎn)生; (4)CS – Chip Select,從設(shè)備使能信號(hào),由主設(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í)鐘信號(hào)的改變(上沿和下沿為一次),才能完成8位數(shù)據(jù)的傳輸。 時(shí)鐘信號(hào)線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í)鐘極性和相位來(lái)通訊。 最后,SPI接口的一個(gè)缺點(diǎn):沒有指定的流控制,沒有應(yīng)答機(jī)制確認(rèn)是否接收到數(shù)據(jù)。
NOR Flash
NOR Flash是一種非易失閃存技術(shù),是Intel在1988年創(chuàng)建。是市場(chǎ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ù)位初始化
對(duì)于復(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 */
for(int i=0;i< 2;i++){
spi_SendRcvByte(SPI0,cmd[i]);
}
// HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);
W25Qx_Disable();
}
ID
對(duì)于兆易創(chuàng)新W25Q64,主要有三種查詢ID方式。
可以使用90H查詢?cè)O(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);
for(int i=0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
for(int i=0;i< 2;i++)
{
ID[i]=spi_SendRcvByte(SPI0,0x00);
}
W25Qx_Disable();
}
讀取數(shù)據(jù)
對(duì)于兆易創(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];
uint8_t status;
/* 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);
for(int i= 0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/* Reception of the data */
// if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
// {
// return W25Qx_ERROR;
// }
for(int i= 0;i< Size;i++)
pData[i]=spi_SendRcvByte(SPI0,0x00);
if (status != 0x00)
{
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
/**
* @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=0 ;
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);
for(int i =0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
delay_1ms(1);
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(tickstart > W25Q128FV_SECTOR_ERASE_MAX_TIME)
{
return W25Qx_TIMEOUT;
}
}
return W25Qx_OK;
}
寫數(shù)據(jù)
對(duì)于寫數(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 =0;
/* 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;
// }
for(int i=0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/* Transmission of the data */
// if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
// {
// return W25Qx_ERROR;
// }
for(int i=0;i< current_size;i++)
spi_SendRcvByte(SPI0,pData[i]);
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(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;
}
對(duì)flash的0x1000地址進(jìn)行寫數(shù)據(jù),指令如下。
BSP_W25Qx_Write(wData2,0x1000,0x000a);
keil配置
microlib 進(jìn)行了高度優(yōu)化以使代碼變得很小。 它的功能比缺省 C 庫(kù)少,并且根本不具備某些 ISO C 特性。 某些庫(kù)函數(shù)的運(yùn)行速度也比較慢,如果要使用printf(),必須開啟。
使能GPIO
void GPIO_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOD);
rcu_periph_clock_enable(RCU_SPI0);
rcu_periph_clock_enable(RCU_AF);
/* SPI0 GPIO config:SCK/PA5, MISO/PA6, MOSI/PA7 */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5 | GPIO_PIN_7);
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
/* PD14 as NSS */
gpio_init(GPIOD, GPIO_MODE_IPD, GPIO_OSPEED_MAX, GPIO_PIN_14);
gpio_init(GPIOD, GPIO_MODE_OUT_PP, GPIO_OSPEED_MAX, GPIO_PIN_14);
}
SPI初始化
SPI掛載在APB2線上。
下面將SPI0分頻256倍,那么速率為120M/256=468.75KHz。
void SPI_Init(void)
{
spi_parameter_struct spi_init_struct;
/* SPI0 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;//雙工模式
spi_init_struct.device_mode = SPI_MASTER;//作為master,提供SCLK
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;//8bit模式
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;//mode0
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_256;// 分頻比
spi_init_struct.endian = SPI_ENDIAN_MSB;//高位在前
spi_init(SPI0, &spi_init_struct);
SET_SPI0_NSS_HIGH
/* SPI enable */
spi_enable(SPI0);
}
使能串口
void UART_Init(void)
{
/* 使能GPI0A,用PA9、PA10為串口 */
rcu_periph_clock_enable(RCU_GPIOA);
/*使能串口0的時(shí)鐘 */
rcu_periph_clock_enable(RCU_USART0);
/*配置USARTx_Tx(PA9)為復(fù)用推挽輸出*/
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/*配置USARTx_RxPA9)為浮空輸入 */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART 配置 */
usart_deinit(USART0);//重置串口0
usart_baudrate_set(USART0, 115200U);//設(shè)置串口0的波特率為115200
usart_word_length_set(USART0, USART_WL_8BIT); // 幀數(shù)據(jù)字長(zhǎng)
usart_stop_bit_set(USART0, USART_STB_1BIT); // 停止位1位
usart_parity_config(USART0, USART_PM_NONE); // 無(wú)奇偶校驗(yàn)位
usart_receive_config(USART0, USART_RECEIVE_ENABLE);//使能接收器
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);//使能發(fā)送器
usart_enable(USART0);//使能USART
}
開啟串口DMA接收
void UART_DMA_Init(void)
{
dma_parameter_struct dma_init_struct;
// 時(shí)鐘開啟
rcu_periph_clock_enable(RCU_DMA0);
/* USART0 DMA 接收配置*/
dma_deinit(DMA0, DMA_CH4);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; /* 外設(shè)到內(nèi)存 */
dma_init_struct.memory_addr = (uint32_t)ReceiveBuff; /* 設(shè)置內(nèi)存接收基地址 */
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; /* 內(nèi)存地址遞增 */
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; /* 8位內(nèi)存數(shù)據(jù) */
dma_init_struct.number = sizeof(ReceiveBuff);
dma_init_struct.periph_addr = ((uint32_t)0x40013804); /* 外設(shè)基地址,USART數(shù)據(jù)寄存器地址 */
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; /* 外設(shè)地址不遞增 */
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; /* 8位外設(shè)數(shù)據(jù) */
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; /* 最高DMA通道優(yōu)先級(jí) */
dma_init(DMA0, DMA_CH4, &dma_init_struct); /* 按照結(jié)構(gòu)體的配置初始化DMA */
dma_circulation_enable(DMA0, DMA_CH4); /* 關(guān)閉DMA循環(huán)模式 */
dma_memory_to_memory_disable(DMA0, DMA_CH4); /* DMA內(nèi)存到內(nèi)存模式不開啟 */
dma_channel_enable(DMA0, DMA_CH4); /* 使能DMA傳輸 */
usart_dma_receive_config(USART0, USART_DENR_ENABLE); /* USART0 DMA接收模式開啟 */
}
串口中斷設(shè)置
void UART_nvic_Init(void)
{
nvic_irq_enable(USART0_IRQn, 0, 0); /* USART中斷設(shè)置,搶占優(yōu)先級(jí)0,子優(yōu)先級(jí)0 */
usart_interrupt_enable(USART0, USART_INT_IDLE); /* 使能USART0空閑中斷 */
}
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"
#include "systick.h"
/**
* @brief spi數(shù)據(jù)傳輸函數(shù)
* @param spi_per spi外設(shè)
* @param byte 發(fā)送字節(jié)
* @return 接收字節(jié)
*/
uint8_t spi_SendRcvByte(uint32_t spi_per,uint8_t byte)
{
uint8_t data;
while(RESET == spi_i2s_flag_get(spi_per, SPI_FLAG_TBE));
spi_i2s_data_transmit(spi_per, byte);
while(SET == spi_i2s_flag_get(spi_per, SPI_FLAG_TRANS));
while(RESET == spi_i2s_flag_get(spi_per, SPI_FLAG_RBNE));
data=spi_i2s_data_receive(spi_per);
while(SET == spi_i2s_flag_get(spi_per, SPI_FLAG_TRANS));
return data;
}
//void spi_write_byte(uint32_t spi_periph, uint8_t data)
//{
// while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TBE));//發(fā)送緩沖區(qū)空標(biāo)志
// spi_i2s_data_transmit(spi_periph, data);
//}
//uint8_t spi_read_byte(uint32_t spi_periph)
//{
// uint8_t read_i=0;
// while(RESET == spi_i2s_flag_get(spi_periph, SPI_FLAG_RBNE));//接收緩沖區(qū)非空標(biāo)志
// read_i=spi_i2s_data_receive(spi_periph);
// while(SET == spi_i2s_flag_get(spi_periph, SPI_FLAG_TRANS));
// return read_i;
//}
/**
* @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 */
for(int i=0;i< 2;i++){
spi_SendRcvByte(SPI0,cmd[i]);
}
// 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 */
// spi_write_byte(SPI0, cmd[0]);
//HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
// status=spi_read_byte(SPI0);
//HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
spi_SendRcvByte(SPI0,cmd[0]);
status=spi_SendRcvByte(SPI0,0x00);
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();
uint32_t tickstart=0;
/*Select the FLASH: Chip Select low */
W25Qx_Enable();
/* Send the read ID command */
// HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
spi_SendRcvByte(SPI0,cmd[0]);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(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);
for(int i=0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
for(int i=0;i< 2;i++)
{
ID[i]=spi_SendRcvByte(SPI0,0x00);
}
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];
uint8_t status;
/* 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);
for(int i= 0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/* Reception of the data */
// if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
// {
// return W25Qx_ERROR;
// }
for(int i= 0;i< Size;i++)
pData[i]=spi_SendRcvByte(SPI0,0x00);
if (status != 0x00)
{
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 =0;
/* 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;
// }
for(int i=0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/* Transmission of the data */
// if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
// {
// return W25Qx_ERROR;
// }
for(int i=0;i< current_size;i++)
spi_SendRcvByte(SPI0,pData[i]);
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(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=0 ;
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);
for(int i =0;i< 4;i++)
spi_SendRcvByte(SPI0,cmd[i]);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
delay_1ms(1);
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() == W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(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 ;
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);
spi_SendRcvByte(SPI0,cmd[0]);
/*Deselect the FLASH: Chip Select high */
W25Qx_Disable();
/* Wait the end of Flash writing */
while(BSP_W25Qx_GetStatus() != W25Qx_BUSY)
{
tickstart++;
/* Check for the Timeout */
if(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 "gd32f30x.h"
#include "gd32f30x_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 10000
#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() gpio_bit_reset(GPIOD,GPIO_PIN_14)
#define W25Qx_Disable() gpio_bit_set(GPIOD,GPIO_PIN_14)
#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ù)組定義
#define SET_SPI0_NSS_HIGH gpio_bit_set(GPIOD,GPIO_PIN_14);
#define SET_SPI0_NSS_LOW gpio_bit_reset(GPIOD,GPIO_PIN_14);
#define BUFFERSIZE 255 //可以接收的最大字符個(gè)數(shù)
uint8_t ReceiveBuff[BUFFERSIZE]; //接收緩沖區(qū)
uint8_t recv_end_flag = 0,Rx_len;//接收完成中斷標(biāo)志,接收到字符長(zhǎng)度
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;
void uart_data(void);
串口重定向
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t)ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
串口中斷設(shè)置
/* 串口0中斷服務(wù)程序 */
void USART0_IRQHandler(void)
{
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) //空閑中斷
{
usart_interrupt_flag_clear(USART0,USART_INT_FLAG_IDLE); /* 清除空閑中斷標(biāo)志位 */
usart_data_receive(USART0); /* 清除接收完成標(biāo)志位 */
dma_channel_disable(DMA0, DMA_CH4); /* 關(guān)閉DMA傳輸 */
uint32_t temp;
temp = dma_transfer_number_get(DMA0,DMA_CH4);//獲取DMA當(dāng)前還有多少未填充
Rx_len = BUFFERSIZE - temp; //計(jì)算串口接收到的數(shù)據(jù)個(gè)數(shù)
recv_end_flag = 1;
// USART_RX_NUM = sizeof(dma_buffer) - dma_transfer_number_get(DMA0,DMA_CH4);
// printf("RECV %d date:%srn", USART_RX_NUM, dma_buffer);
// memset(&dma_buffer ,'?',sizeof(dma_buffer));
// /* 重新設(shè)置DMA傳輸 */
// dma_memory_address_config(DMA0,DMA_CH4,(uint32_t)dma_buffer);
// dma_transfer_number_config(DMA0,DMA_CH4,sizeof(dma_buffer));
// dma_channel_enable(DMA0, DMA_CH4); /* 開啟DMA傳輸 */
}
}
主程序
讀取ID和flash數(shù)據(jù)及擦除。
printf("GD Nor Flash案例n");
/*##-1- Read the device ID ########################*/
BSP_W25Qx_Init();//鍒濆鍖朩25Q64
BSP_W25Qx_Read_ID(ID);//璇誨彇ID
//get_DeviceId();
if((ID[0] != 0xC8) | (ID[1] != 0x16))
{
printf("error");
while(1);
// Error_Handler();//濡傛灉 ID涓嶅鎵撳嵃閿欒
}
else//ID姝g‘錛屾墦鍗癐D
{
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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)
printf("讀取原始的0個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
{
printf("error");
while(1);
}
/*打印數(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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取原始的17個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
{
printf("error");
while(1);
}
/*打印數(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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取原始的34個(gè)扇區(qū)數(shù)據(jù)成功!n");
else
{
printf("error");
while(1);
}
/*打印數(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
{
printf("error");
while(1);
}
/*##-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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
printf("清零第0扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
{
printf("error");
while(1);
}
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
printf("讀取第0扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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
{
printf("error");
while(1);
}
/*##-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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
printf("清零第2扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
{
printf("error");
while(1);
}
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取第17扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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
{
printf("error");
while(1);
}
/*##-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ù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
printf("清零第34扇區(qū)前0x200的數(shù)據(jù)成功!rn");
else
{
printf("error");
while(1);
}
/*##-3- Read the flash ########################*/
/*讀取數(shù)據(jù),rData讀取數(shù)據(jù)的指針,起始地址0x00,讀取數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取第34扇區(qū)前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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");
主程序。
while (1){
uart_data();
delay_1ms(10);
}
數(shù)據(jù)處理
void uart_data(void)
{
if(recv_end_flag ==1)//接收完成標(biāo)志
{
if(ReceiveBuff[0]==0x00)
{
printf("寫入數(shù)據(jù)長(zhǎng)度:%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
{
printf("error");
while(1);
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x00,寫入數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
printf("扇區(qū)0數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
{
printf("error");
while(1);
}
if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
printf("讀取扇區(qū)0前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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ù)長(zhǎng)度:%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
{
printf("error");
while(1);
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x11000,寫入數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
printf("扇區(qū)17數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
{
printf("error");
while(1);
}
if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
printf("讀取扇區(qū)17前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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ù)長(zhǎng)度:%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
{
printf("error");
while(1);
}
/*寫入數(shù)據(jù),wData寫入數(shù)據(jù)的指針,起始地址0x22000,寫入數(shù)據(jù)長(zhǎng)度0x200*/
if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
printf("扇區(qū)34數(shù)據(jù)成功~~~~~~~~~~~~~~~~~~~~~~~~~~!rn");
else
{
printf("error");
while(1);
}
if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
printf("讀取扇區(qū)34前0x200數(shù)據(jù)成功!rnrn");
else
{
printf("error");
while(1);
}
/*打印數(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ù)長(zhǎng)度清零
recv_end_flag=0;//接收標(biāo)志位清零
//開啟下一次接收
memset(&ReceiveBuff ,'?',sizeof(ReceiveBuff));
/* 重新設(shè)置DMA傳輸 */
dma_memory_address_config(DMA0,DMA_CH4,(uint32_t)ReceiveBuff);
dma_transfer_number_config(DMA0,DMA_CH4,sizeof(ReceiveBuff));
dma_channel_enable(DMA0, DMA_CH4); /* 開啟DMA傳輸 */
}
}
演示
W25Q64芯片型號(hào)的ID為0XEF17,下方讀取為0XC816,所以讀取成功。
開機(jī)會(huì)打印出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號(hào)位置開始寫入數(shù)據(jù)01 02 03 04。
輸入:00 28 11 12 13 14 15 向扇區(qū)0的的40(28是十六進(jìn)制)號(hào)位置開始寫入數(shù)據(jù)11 12 13 14 15。
輸入:17 10 aa bb 向扇區(qū)17的的16(10是十六進(jìn)制)號(hào)位置開始寫入數(shù)據(jù)aa bb。
審核編輯 黃宇
-
芯片
+關(guān)注
關(guān)注
455文章
50812瀏覽量
423576 -
SPI
+關(guān)注
關(guān)注
17文章
1706瀏覽量
91578 -
固件庫(kù)
+關(guān)注
關(guān)注
2文章
97瀏覽量
14940
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論