中级会员
 
- 积分
- 297
- 金钱
- 297
- 注册时间
- 2016-8-10
- 在线时间
- 51 小时
|
1金钱
现在是只能读取设备ID,然后以后再读取的数,就一直是循环读出设备ID和生产的ID 如果写状态寄存器0x0000的话 在读状态寄存器时候返回0xff
//发送字节
uint8_t SPI_Flash_SendData(uint8_t byte)
{
/*! Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/*!Send byte through the SPI1 peripheral */
SPI_SendData8(SPI1, byte);
/*! Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
/*! Return the byte read from the SPI bus */
return SPI_ReceiveData8(SPI1);
}
/******************************
Read_addr:开始读数据的地址
NumByteToRead :要读取的字节数
pBuffer :指向的数组
*******************************/
void SPI_Flash_Read(uint8_t* pBuffer,uint32_t Read_addr,uint16_t NumByteToRead)
{
uint16_t i;
NSS_Reset(0);
SPI_Flash_SendData(W25Q32_Read_Data);
SPI_Flash_SendData((uint8_t)Read_addr>>16);
SPI_Flash_SendData((uint8_t)Read_addr>>8);
SPI_Flash_SendData((uint8_t)Read_addr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=SPI_Flash_SendData(0x00);
}
NSS_Reset(1);
}
/*******************************
读取设备ID
*******************************/
uint8_t SPI_Flash_ReadID(void)
{
uint8_t Temp1 = 0;
uint8_t Temp2=0;
NSS_Reset(1);
/*! Send "RDID " instruction */
SPI_Flash_SendData(W25_Read_Manufacturer);
SPI_Flash_SendData(0x00);
SPI_Flash_SendData(0x00);
SPI_Flash_SendData(0x00);
/*! Read a byte from the FLASH */
Temp1 = SPI_Flash_SendData(0x00);
printf("\r\n The ManufacturerID is 0x%x \r\n",Temp1);
Temp2= SPI_Flash_SendData(0x00);
NSS_Reset(0);
return Temp2;
}
//Flash 扇区擦除
void SPI_FLASH_SectorErase(uint32_t SectorAddr)
{
NSS_Reset(1);
/* 发送使能写命令 */
/*使能写命令是一个写状态寄存器的命令,所以要等待该命令执
行完,才能写其他命令*/
/* 发送擦除扇区命令 */
SPI_Flash_SendData(W25Q32_Sector_Erase);
/* 发送要擦除的起始地址 */
SPI_Flash_SendData((SectorAddr & 0xFF0000) >> 16);
/* Send SectorAddr medium nibble address byte */
SPI_Flash_SendData((SectorAddr & 0xFF00) >> 8);
/* Send SectorAddr low nibble address byte */
SPI_Flash_SendData(SectorAddr & 0xFF);
/* 等待擦除完成*/
NSS_Reset(0);
}
//读取FLASH的状态寄存器
//BIT 7 6 5 4 3 2 1 0
// SPR RV TB BP2 BP1 BP0 WEL BUSY
//WEL:写使能锁定
//BUSY:忙标记位(写任务|擦除)
//默认:0x00
uint8_t SPI_Flash_ReadSR(void)
{
uint8_t byte=0;
NSS_Reset(1);
SPI_Flash_SendData(Read_RegStatus_1);
byte=SPI_Flash_SendData(0x00);
NSS_Reset(0);
return byte;
}
/*******************************
写状态寄存器
*******************************/
uint8_t SPI_Flash_WriteSR(uint16_t data)
{
NSS_Reset(1);
SPI_Flash_SendData(W25Q32_Write_ENABLE);
SPI_Flash_SendData(W25Q32_WriteStatusReg); //写reg命令
SPI_Flash_SendData((uint8_t)data);
SPI_Flash_SendData((uint8_t)data>>8);
SPI_Flash_SendData(W25Q32_Write_DISABLE);
NSS_Reset(0);
return 0;
}
/*******************************
等待空闲
*******************************/
void SPI_Flash_Wait_Busy(void)
{
while ((SPI_Flash_ReadSR()&0x01)==0x01);
}
/*******************************
FLASH写使能
*******************************/
void SPI_FLASH_Write_Enable(void)
{
NSS_Reset(1); //CS=0
SPI_Flash_SendData(W25Q32_Write_ENABLE);
NSS_Reset(0); //CS=1
}
/*******************************
擦除整个芯片,擦出时间:40s
*******************************/
void SPI_Flash_Erase_Chip(void)
{
SPI_FLASH_Write_Enable();
SPI_Flash_Wait_Busy();
NSS_Reset(1);
SPI_Flash_SendData(W25Q32_Chip_Erase);
NSS_Reset(0);
SPI_Flash_Wait_Busy();
}
/*******************************
在指定地址开始写入最大256字节的数据
SPI在一页(0~65535)内写入少于256个字节的数据
*******************************/
void SPI_Flash_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint16_t i;
SPI_FLASH_Write_Enable();
NSS_Reset(1);
SPI_Flash_SendData(W25Q32_Page_Program);
SPI_Flash_SendData((uint8_t)((WriteAddr)>>16));
SPI_Flash_SendData((uint8_t)((WriteAddr)>>8));
SPI_Flash_SendData((uint8_t)WriteAddr);
for(i=0;i<NumByteToWrite;i++) SPI_Flash_SendData(pBuffer[i]);
NSS_Reset(0);
SPI_Flash_Wait_Busy();
}
#include "main.h"
#include "stm32f0xx.h"
#include "led.h"
#include "usart.h"
#include "SPI.h"
#include "Flash.h"
uint8_t TX_Buffer[256];
uint8_t RX_Buffer[256];
void Delay(void)
{
int x,y;
for(x=1000;x>0;x--)
for(y=1000;y>0;y--);
}
void W25Q32_test(void)
{
uint16_t x,y;
uint8_t temp;
SPI_Flash_WriteSR(0x0000);
temp=SPI_Flash_ReadSR();//读状态寄存器
printf("\r\n The Register Status is 0x%x \r\n",temp);
Delay();
SPI_Flash_Erase_Chip();
for(x=0;x<256;x++)
{
TX_Buffer[x]=0x55;
}
Delay();
Delay();
//SPI_Flash_Write_Page(TX_Buffer,0x000FFF,256);//写256字节
SPI_Flash_Read(RX_Buffer,0x001FFF,256); //从地址0读256字节
for(y=0;y<256;y++)
{
printf("\r\n The RX_Buffer[%d] is 0x%x \r\n",y,RX_Buffer[y]);
}
}
int main(void)
{ //uint8_t DeviceID;
led_Init();
usart_Init();
MySPI_Config();
SPI_Flash_Config();
printf("\r\n Hello! \r\n");
// DeviceID=SPI_Flash_ReadID(); //获取设备ID
//printf("\r\n The DeviceID is 0x%x \r\n",DeviceID);
Delay();
W25Q32_test(); //测试W25Q32
}
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until transmit data register is empty */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}
return ch;
}
|
|