将合并的字库烧录到外部spi flash,这里使用串口烧录,通过串口上位机发送bin文件,stm32串口接收到字符后写入spi flash里。
使用USB转TTL模块连接板子串口1,更新字库过程比较慢12M的字库文件串口烧录大约10分钟左右。
TTL模块TXD – 板子RXD
TTL模块RXD – 板子TXD
TTL模块GND – 板子GND
#include "sysconfig.h"
#include "w25qxx.h"
#include "printer.h"
//#define ERASE_FLASH 1
#define UART_WRITE_FLASH 1
int main(void)
{
Rcc_init();
SysTickConfig();
Uart1_Config();
Printer_Init();
Led_Init();
#ifdef ERASE_FLASH
// for(i=0; i<200; i++)
// {
// W25QXX_Erase_Sector(addr);
// addr+=0x1000;
// }
W25QXX_Erase_Chip();
printf("erase chip\r\n");
while(1)
{
}
#endif
while(1)
{
}
}
#if UART_WRITE_FLASH
u32 zk_addr = 0;
void USART1_IRQHandler(void)
{
u8 ch;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
{
ch = USART_ReceiveData(USART1);
// printf(" %02x ", ch);
W25QXX_Write_Page(&ch,zk_addr++,1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
#endif
void Uart1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_9; //TXD --PA9
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_10; //RXD --PA10
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//配置串口,初始化串口1
USART_InitStruct.USART_BaudRate = UART_BAUD_RATE;
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_Parity =USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStruct);
//初始化串口接收中断
// USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//使能串口1
USART_Cmd(USART1,ENABLE);
Uart1_ITConfig();
}
void Uart1_ITConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//AFIO时钟打开
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//选择中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//设置中断抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//s设置中断响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//串口中断初始化寄存器
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//设置串口中断模式为接收中断
}
u32 zk_addr = 0;
//串口中断服务函数
void USART1_IRQHandler(void)
{
u8 ch;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET) //判断串口1发生的中断是否为接收中断
{
ch = USART_ReceiveData(USART1); //接收数据
// printf(" %02x ", ch);
W25QXX_Write_Page(&ch,zk_addr++,1);//发送1字节给SPIFLASH
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清中断标志位
}
}
spi.c实现
#define SPI1_GPIO RCC_APB2Periph_GPIOA
#define SPI1_NSS_PORT GPIOA
#define SPI1_NSS_PIN GPIO_Pin_4
#define SPI1_SCK_PORT GPIOA
#define SPI1_SCK_PIN GPIO_Pin_5
#define SPI1_MISO_PORT GPIOA
#define SPI1_MISO_PIN GPIO_Pin_6
#define SPI1_MOSI_PORT GPIOA
#define SPI1_MOSI_PIN GPIO_Pin_7
#include "sysconfig.h"
//#include "spi.h"
void SPI1_Gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = SPI1_NSS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_NSS_PORT, &GPIO_InitStructure);
GPIO_SetBits(SPI1_NSS_PORT, SPI1_NSS_PIN);
GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_SCK_PORT, &GPIO_InitStructure);
GPIO_SetBits(SPI1_SCK_PORT, SPI1_SCK_PIN);
#ifdef MCU_GD32
GPIO_InitStructure.GPIO_Pin = SPI1_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_MISO_PORT, &GPIO_InitStructure);
#else
GPIO_InitStructure.GPIO_Pin = SPI1_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_MISO_PORT, &GPIO_InitStructure);
#endif
GPIO_InitStructure.GPIO_Pin = SPI1_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_MOSI_PORT, &GPIO_InitStructure);
GPIO_SetBits(SPI1_MOSI_PORT, SPI1_MOSI_PIN);
}
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置为双线全双工模式
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主机模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置为8位数据长度
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //设置时钟线空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //设置时钟线的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //设置NSS信号由软件(使用SSI位)管理
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //设置波特率预分频值为64分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //设置数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //设置CRC计算的多项式
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //使能SPI
}
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTB时钟使能
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // PB12 推挽
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15上拉
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步时钟的空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步时钟的第二个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //定义波特率预分频的值:波特率预分频值为256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI2, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI2, ENABLE); //使能SPI外设
// SPI2_ReadWriteByte(0xff);//启动传输
}
/******************************************************************************
* FunctionName : SPI1_SetSpeed
* Description : SPI1设置速度
* Parameters : none
* Returns : none
*******************************************************************************/
void SPI1_SetSpeed(uint16_t SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI_Cmd(SPI1, DISABLE);
SPI1->CR1 &= 0xFFC7;
SPI1->CR1 |= SPI_BaudRatePrescaler;
SPI_Cmd(SPI1, ENABLE);
}
//SPI 速度设置函数
//SpeedSet:
//SPI_BaudRatePrescaler_2 2分频
//SPI_BaudRatePrescaler_8 8分频
//SPI_BaudRatePrescaler_16 16分频
//SPI_BaudRatePrescaler_256 256分频
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI2->CR1&=0XFFC7;
SPI2->CR1|=SPI_BaudRatePrescaler; //设置SPI2速度
SPI_Cmd(SPI2,ENABLE);
}
/******************************************************************************
* FunctionName : SPI1_ReadWriteByte
* Description : SPI1读写一个字节
* Parameters : none
* Returns : none
*******************************************************************************/
uint8_t SPI1_ReadWriteByte(uint8_t txdata)
{
uint8_t retry;
retry = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查发送缓存是否空闲
{
retry++;
if (retry > 200)
return 0;
}
SPI_I2S_SendData(SPI1, txdata); //发送一个数据
retry = 0;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) //检查接收缓存是否非空
{
retry++;
if (retry > 200)
return 0;
}
return SPI_I2S_ReceiveData(SPI1); //返回接收到的一个数据
}
//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI2, TxData); //通过外设SPIx发送一个数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI2); //返回通过SPIx最近接收的数据
}
w25qxx.c和w25xqq.h文件
#include "w25qxx.h"
u16 W25QXX_TYPE = W25Q128; //默认是W25Q128
//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q256
//容量丿32M字节,共朿512个Block_8192个Sector
//初始化SPI FLASH的IO叿
void W25QXX_Init(void)
{
u8 temp;
W25QXX_CS(1); //SPI FLASH不?中
SPI1_Init(); //初始化SPI
SPI1_SetSpeed(SPI_BaudRatePrescaler_2);//设置丿18M时钟,高?模弿
W25QXX_TYPE = W25QXX_ReadID();//读取FLASH ID.
printf("W25QXX type =%02x \r\n", W25QXX_TYPE);
if(W25QXX_TYPE == W25Q256)
{
temp = W25QXX_ReadSR(3); //读取状濁寄存器3,判断地坿模式
if((temp & 0X01) == 0) //如果不是4字节地址模式,则进兿4字节地址模式
{
W25QXX_CS(0); //选中
SPI1_ReadWriteByte(W25X_Enable4ByteAddr);//发?进兿4字节地址模式指令
W25QXX_CS(1); //取消片?
}
}
}
//读取W25QXX的状态寄存器
//BIT7 6 5 4 3 2 1 0
//SPR RV TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状濁寄存器保护使,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设罿
//WEL:写使能锁宿
//BUSY:忙标记位(1,忿;0,空闲)
//默认:0x00
//状濁寄存器2_
//BIT7 6 5 4 3 2 1 0
//SUS CMP LB3 LB2 LB1 (R) QE SRP1
//状濁寄存器3_
//BIT7 6 5 4 3 2 1 0
//HOLD/RST DRV1 DRV0 (R) (R) WPS ADP ADS
//regno:状濁寄存器号,茿:1~3
//返回倿:状濁寄存器倿
u8 W25QXX_ReadSR(u8 regno)
{
u8 byte=0,command=0;
switch(regno)
{
case 1:
command = W25X_ReadStatusReg1; //读状态寄存器1指令
break;
case 2:
command = W25X_ReadStatusReg2; //读状态寄存器2指令
break;
case 3:
command = W25X_ReadStatusReg3; //读状态寄存器3指令
break;
default:
command = W25X_ReadStatusReg1;
break;
}
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(command); //发?读取状态寄存器命令
byte = SPI1_ReadWriteByte(0Xff); //读取丿个字芿
W25QXX_CS(1); //取消片?
return byte;
}
//写W25QXX状濁寄存器
void W25QXX_Write_SR(u8 regno,u8 sr)
{
u8 command = 0;
switch(regno)
{
case 1:
command = W25X_WriteStatusReg1; //写状态寄存器1指令
break;
case 2:
command = W25X_WriteStatusReg2; //写状态寄存器2指令
break;
case 3:
command = W25X_WriteStatusReg3; //写状态寄存器3指令
break;
default:
command = W25X_WriteStatusReg1;
break;
}
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(command); //发?写取状态寄存器命令
SPI1_ReadWriteByte(sr); //写入丿个字芿
W25QXX_CS(1); //取消片?
}
//W25QXX写使胿
//将WEL置位
void W25QXX_Write_Enable(void)
{
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_WriteEnable); //发?写使能
W25QXX_CS(1); //取消片?
}
//W25QXX写禁歿
//将WEL清零
void W25QXX_Write_Disable(void)
{
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_WriteDisable); //发?写禁止指令
W25QXX_CS(1); //取消片?
}
//读取芯片ID
//返回值如丿:
//0XEF13,表示芯片型号为W25Q80
//0XEF14,表示芯片型号为W25Q16
//0XEF15,表示芯片型号为W25Q32
//0XEF16,表示芯片型号为W25Q64
//0XEF17,表示芯片型号为W25Q128
u16 W25QXX_ReadID(void)
{
u16 Temp = 0;
W25QXX_CS(0);
SPI1_ReadWriteByte(0x90);//发?读取ID命令
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(0x00);
SPI1_ReadWriteByte(0x00);
Temp|=SPI1_ReadWriteByte(0xFF)<<8;
Temp|=SPI1_ReadWriteByte(0xFF);
W25QXX_CS(1);
return Temp;
}
//读取SPI FLASH
//在指定地坿弿始读取指定长度的数据
//pBuffer:数据存储匿
//ReadAddr:弿始读取的地址(24bit)
//NumByteToReW25QXX_Readad:要读取的字节敿(朿夿65535)
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
u16 i;
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_ReadData); //发?读取命仿
if(W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址丿4字节的,要发送最髿8使
{
SPI1_ReadWriteByte((u8)((ReadAddr)>>24));
}
SPI1_ReadWriteByte((u8)((ReadAddr)>>16)); //发?24bit地址
SPI1_ReadWriteByte((u8)((ReadAddr)>>8));
SPI1_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=SPI1_ReadWriteByte(0XFF); //循环读数
}
W25QXX_CS(1);
}
//SPI在一顿(0~65535)内写入少亿256个字节的数据
//在指定地坿弿始写入最夿256字节的数捿
//pBuffer:数据存储匿
//WriteAddr:弿始写入的地址(24bit)
//NumByteToWrite:要写入的字节敿(朿夿256),该数不应该超过该页的剩余字节敿!!!
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
W25QXX_Write_Enable(); //SET WEL
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_PageProgram); //发?写页命仿
if(W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址丿4字节的,要发送最髿8使
{
SPI1_ReadWriteByte((u8)((WriteAddr)>>24));
}
SPI1_ReadWriteByte((u8)((WriteAddr)>>16)); //发?24bit地址
SPI1_ReadWriteByte((u8)((WriteAddr)>>8));
SPI1_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)SPI1_ReadWriteByte(pBuffer[i]);//循环写数
W25QXX_CS(1); //取消片?
W25QXX_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保承写的地址范围内的数据全部丿0XFF,否则在非0XFF处写入的数据将失贿!
//具有自动换页功能
//在指定地坿弿始写入指定长度的数据,但是要确保地坿不越畿!
//pBuffer:数据存储匿
//WriteAddr:弿始写入的地址(24bit)
//NumByteToWrite:要写入的字节敿(朿夿65535)
//CHECK OK
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr%256; //单页剩余的字节数
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大亿256个字芿
while(1)
{
W25QXX_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)break;//写入结束亿
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //减去已经写入了的字节敿
if(NumByteToWrite>256)pageremain=256; //丿次可以写兿256个字芿
else pageremain=NumByteToWrite; //不够256个字节了
}
};
}
//写SPI FLASH
//在指定地坿弿始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储匿
//WriteAddr:弿始写入的地址(24bit)
//NumByteToWrite:要写入的字节敿(朿夿65535)
u8 W25QXX_BUFFER[4096];
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
u8 * W25QXX_BUF;
W25QXX_BUF=W25QXX_BUFFER;
secpos=WriteAddr/4096;//扇区地址
secoff=WriteAddr%4096;//在扇区内的偏秿
secremain=4096-secoff;//扇区剩余空间大小
//printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试甿
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大亿4096个字芿
while(1)
{
W25QXX_Read(W25QXX_BUF,secpos*4096,4096);//读出整个扇区的内宿
for(i=0;i<secremain;i++)//校验数据
{
if(W25QXX_BUF[secoff+i]!=0XFF)break;//霿要擦陿
}
if(i<secremain)//霿要擦陿
{
W25QXX_Erase_Sector(secpos); //擦除这个扇区
for(i=0;i<secremain;i++) //复制
{
W25QXX_BUF[i+secoff]=pBuffer[i];
}
W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);//写入整个扇区
}else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了皿,直接写入扇区剩余区间.
if(NumByteToWrite==secremain)break;//写入结束亿
else//写入未结板
{
secpos++;//扇区地址墿1
secoff=0;//偏移位置丿0
pBuffer+=secremain; //指针偏移
WriteAddr+=secremain; //写地坿偏移
NumByteToWrite-=secremain; //字节数?减
if(NumByteToWrite>4096)secremain=4096;//下一个扇区还是写不完
else secremain=NumByteToWrite; //下一个扇区可以写完了
}
};
}
//擦除整个芯片
//等待时间超长...
void W25QXX_Erase_Chip(void)
{
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_ChipErase); //发?片擦除命令
W25QXX_CS(1); //取消片?
W25QXX_Wait_Busy(); //等待芯片擦除结束
}
//擦除丿个扇匿
//Dst_Addr:扇区地址 根据实际容量设置
//擦除丿个山区的朿少时闿:150ms
void W25QXX_Erase_Sector(u32 Dst_Addr)
{
//监视falsh擦除情况,测试甿
//printf("fe:%x\r\n",Dst_Addr);
Dst_Addr*=4096;
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_SectorErase); //发?扇区擦除指仿
if(W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址丿4字节的,要发送最髿8使
{
SPI1_ReadWriteByte((u8)((Dst_Addr)>>24));
}
SPI1_ReadWriteByte((u8)((Dst_Addr)>>16)); //发?24bit地址
SPI1_ReadWriteByte((u8)((Dst_Addr)>>8));
SPI1_ReadWriteByte((u8)Dst_Addr);
W25QXX_CS(1); //取消片?
W25QXX_Wait_Busy(); //等待擦除完成
}
//等待空闲
void W25QXX_Wait_Busy(void)
{
while((W25QXX_ReadSR(1)&0x01)==0x01); // 等待BUSY位清穿
}
//进入掉电模式
void W25QXX_PowerDown(void)
{
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_PowerDown); //发?掉电命仿
W25QXX_CS(1); //取消片?
//delay_us(3); //等待TPD
}
//唤醒
void W25QXX_WAKEUP(void)
{
W25QXX_CS(0); //使能器件
SPI1_ReadWriteByte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
W25QXX_CS(1); //取消片?
//delay_us(3); //等待TRES1
}
#ifndef __FLASH_H
#define __FLASH_H
#include "sysconfig.h"
#define W25Q80 0XEF13
#define W25Q16 0XEF14
#define W25Q32 0XEF15
#define W25Q64 0XEF16
#define W25Q128 0XEF17
#define W25Q256 0XC218
extern u16 W25QXX_TYPE;
#define W25QXX_CS(x) x? GPIO_SetBits(GPIOA, GPIO_Pin_4):GPIO_ResetBits(GPIOA, GPIO_Pin_4)
//指令表
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg1 0x05
#define W25X_ReadStatusReg2 0x35
#define W25X_ReadStatusReg3 0x15
#define W25X_WriteStatusReg1 0x01
#define W25X_WriteStatusReg2 0x31
#define W25X_WriteStatusReg3 0x11
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#define W25X_Enable4ByteAddr 0xB7
#define W25X_Exit4ByteAddr 0xE9
void W25QXX_Init(void);
u16 W25QXX_ReadID(void); //读取FLASH ID
u8 W25QXX_ReadSR(u8 regno); //读取状态寄存器
void W25QXX_4ByteAddr_Enable(void); //使能4字节地址模式
void W25QXX_Write_SR(u8 regno,u8 sr); //写状态寄存器
void W25QXX_Write_Enable(void); //写使能
void W25QXX_Write_Disable(void); //写保护
void W25QXX_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);
void W25QXX_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead); //读取flash
void W25QXX_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);//写入flash
void W25QXX_Erase_Chip(void); //整片擦除
void W25QXX_Erase_Sector(u32 Dst_Addr); //扇区擦除
void W25QXX_Wait_Busy(void); //等待空闲
void W25QXX_PowerDown(void); //进入掉电模式
void W25QXX_WAKEUP(void); //唤醒
#endif