今天坐下SPI0驱动W25Q80,首先查看电路图:
这里用的SPI0,软件NSS作为CS选通信号。
MiniDock对应 电路:
GD32VF103的SPI带CRC校验,但是本例子未用。
使用的引脚SPI代码比较简单:
/*
* SPI0引脚配置
* PA5-->SCK
* PA6-->MISO
* PA7-->MOSI
* PB9-->SS
*/
void spi_flash_init(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_GPIOA);
//rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_SPI0);
/* SPI0_SCK(PA5), SPI0_MISO(PA6) and SPI0_MOSI(PA7) GPIO pin configuration */
//gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5| GPIO_PIN_7);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5| GPIO_PIN_6| GPIO_PIN_7);
//gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
/* SPI0_CS(PB9) GPIO pin configuration */
//gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* chip select invalid*/
//SPI_FLASH_CS(1);
/* SPI0 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_8 ;
spi_init_struct.endian = SPI_ENDIAN_MSB;;
spi_init(SPI0, &spi_init_struct);
/* set crc polynomial */
spi_crc_polynomial_set(SPI0,7);
/* enable SPI0 */
spi_enable(SPI0);
//spi_readwritebyte(0XFF);
}
//SPIx 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
uint8_t spi_readwritebyte(uint8_t txData)
{
uint8_t retry=0;
while (spi_i2s_flag_get(SPI0,SPI_FLAG_TBE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
{
retry++;
if(retry>200)return 0;
}
spi_i2s_data_transmit(SPI0,txData); //通过外设SPIx发送一个数据
retry=0;
while (spi_i2s_flag_get(SPI0,SPI_FLAG_RBNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位
{
retry++;
if(retry>200)return 0;
}
return spi_i2s_data_receive(SPI0); //返回通过SPIx最近接收的数据
}
/////////////////
/*
* w25xxx.c
*
* Created on: 2020年10月19日
* Author: Administrator
*/
#include "w25xxx.h"
uint16_t SPI_FLASH_TYPE=W25Q80;//默认就是25Q64
//4Kbytes为一个Sector
//16个扇区为1个Block
//W25X80
//容量为1M字节,共有16个Block,256个Sector
//初始化SPI FLASH的IO口
void SPI_Flash_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
SPI_FLASH_CS(0);
spi_flash_init(); //初始化SPI
SPI_FLASH_TYPE=SPI_Flash_ReadID();//读取FLASH ID.
}
//读取SPI_FLASH的状态寄存器
//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
uint8_t SPI_Flash_ReadSR(void)
{
uint8_t byte=0;
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_ReadStatusReg); //发送读取状态寄存器命令
byte=spi_readwritebyte(0Xff); //读取一个字节
SPI_FLASH_CS(1); //取消片选
return byte;
}
//写SPI_FLASH状态寄存器
//只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!!
void SPI_FLASH_Write_SR(uint8_t sr)
{
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_WriteStatusReg); //发送写取状态寄存器命令
spi_readwritebyte(sr); //写入一个字节
SPI_FLASH_CS(1); //取消片选
}
//SPI_FLASH写使能
//将WEL置位
void SPI_FLASH_Write_Enable(void)
{
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_WriteEnable); //发送写使能
SPI_FLASH_CS(1); //取消片选
}
//SPI_FLASH写禁止
//将WEL清零
void SPI_FLASH_Write_Disable(void)
{
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_WriteDisable); //发送写禁止指令
SPI_FLASH_CS(1); //取消片选
}
//读取芯片ID W25X16的ID:0XEF14
uint16_t SPI_Flash_ReadID(void)
{
uint16_t Temp = 0;
SPI_FLASH_CS(0);
delay_1us(10);
spi_readwritebyte(0x90);//发送读取ID命令
spi_readwritebyte(0x00);
spi_readwritebyte(0x00);
spi_readwritebyte(0x00);
Temp|=(unsigned int)spi_readwritebyte(0xFF)<<8;
Temp|=spi_readwritebyte(0xFF);
//spi_readwritebyte(0x00);
SPI_FLASH_CS(1);
delay_1us(10);
return Temp;
}
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void SPI_Flash_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead)
{
uint16_t i;
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_ReadData); //发送读取命令
spi_readwritebyte((uint8_t)((ReadAddr)>>16)); //发送24bit地址
spi_readwritebyte((uint8_t)((ReadAddr)>>8));
spi_readwritebyte((uint8_t)ReadAddr);
for(i=0;i
{
pBuffer=spi_readwritebyte(0XFF); //循环读数
}
SPI_FLASH_CS(1); //取消片选
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void SPI_Flash_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint16_t i;
SPI_FLASH_Write_Enable(); //SET WEL
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_PageProgram); //发送写页命令
spi_readwritebyte((uint8_t)((WriteAddr)>>16)); //发送24bit地址
spi_readwritebyte((uint8_t)((WriteAddr)>>8));
spi_readwritebyte((uint8_t)WriteAddr);
for(i=0;i);//循环写数
SPI_FLASH_CS(1); //取消片选
SPI_Flash_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void SPI_Flash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint16_t pageremain;
pageremain=256-WriteAddr%256; //单页剩余的字节数
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
while(1)
{
SPI_Flash_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)
uint8_t SPI_FLASH_BUF[4096];
void SPI_Flash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
secpos=WriteAddr/4096;//扇区地址 0~511 for w25x16
secoff=WriteAddr%4096;//在扇区内的偏移
secremain=4096-secoff;//扇区剩余空间大小
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
while(1)
{
SPI_Flash_Read(SPI_FLASH_BUF,secpos*4096,4096);//读出整个扇区的内容
for(i=0;i
{
if(SPI_FLASH_BUF[secoff+i]!=0XFF)break;//需要擦除
}
if(i
{
SPI_Flash_Erase_Sector(secpos);//擦除这个扇区
for(i=0;i
{
SPI_FLASH_BUF[i+secoff]=pBuffer;
}
SPI_Flash_Write_NoCheck(SPI_FLASH_BUF,secpos*4096,4096);//写入整个扇区
}else SPI_Flash_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; //下一个扇区可以写完了
}
};
}
//擦除整个芯片
//整片擦除时间:
//W25X16:25s
//W25X32:40s
//W25X64:40s
//等待时间超长...
void SPI_Flash_Erase_Chip(void)
{
SPI_FLASH_Write_Enable(); //SET WEL
SPI_Flash_Wait_Busy();
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_ChipErase); //发送片擦除命令
SPI_FLASH_CS(1); //取消片选
SPI_Flash_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 0~511 for w25x16
//擦除一个山区的最少时间:150ms
void SPI_Flash_Erase_Sector(uint32_t Dst_Addr)
{
Dst_Addr*=4096;
SPI_FLASH_Write_Enable(); //SET WEL
SPI_Flash_Wait_Busy();
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_SectorErase); //发送扇区擦除指令
spi_readwritebyte((uint8_t)((Dst_Addr)>>16)); //发送24bit地址
spi_readwritebyte((uint8_t)((Dst_Addr)>>8));
spi_readwritebyte((uint8_t)Dst_Addr);
SPI_FLASH_CS(1); //取消片选
SPI_Flash_Wait_Busy(); //等待擦除完成
}
//等待空闲
void SPI_Flash_Wait_Busy(void)
{
while ((SPI_Flash_ReadSR()&0x01)==0x01); // 等待BUSY位清空
}
//进入掉电模式
void SPI_Flash_PowerDown(void)
{
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_PowerDown); //发送掉电命令
SPI_FLASH_CS(1); //取消片选
delay_1us(3); //等待TPD
}
//唤醒
void SPI_Flash_WAKEUP(void)
{
SPI_FLASH_CS(0); //使能器件
spi_readwritebyte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
SPI_FLASH_CS(1); //取消片选
delay_1us(3); //等待TRES1
main里面调用:
SPI_Flash_Init(); //SPI FLASH 初始化
//while(SPI_Flash_ReadID()!=W25Q80) //检测不到W25Q64
//{
// printf("25Q80 Check Failed!rn");
// delay_1ms(500);
// printf("Please Check! rn");
// delay_1ms(500);
// gd_rvstar_led_toggle(LED2);//LED1闪烁
printf("the FLASH ID is 0x%x rn",SPI_Flash_ReadID());
//}
printf("25Q80 Ready! rn");
FLASH_SIZE=1*1024*1024; //FLASH 大小为1M字节
printf("Start Write W25Q80.... rn");
SPI_Flash_Write((uint8_t*)TEXT_Buffer2,FLASH_SIZE-100,SIZE2); //从倒数第100个地址处开始,写入SIZE长度的数据
printf("W25Q80 Write Finished! rn");
printf("Start Read W25Q80.... rn");
SPI_Flash_Read(datatemp,FLASH_SIZE-100,SIZE); //从倒数第100个地址处开始,读出SIZE个字节
printf("The Data Read Is: %s rn",datatemp); //提示传送完成 显示读到的字符串
gd_rvstar_led_off(LED2);
我的这块扩展版不知道怎么回事,W25Q80 id一直不正常,但是读写正常,之前在STM32上面也是,好奇怪。
测试串口输出如下:
|