初学者请教,读ID,写数组再读写,显示都为0,是怎么回事?望大神赐教,代码如下:
[mw_shl_code=c,true]#include "usart.h"
#include "spi_flash.h"
//pin
#define SPI_SCLK_PIN GPIO_Pin_3
#define SPI_MISO_PIN GPIO_Pin_4
#define SPI_MOSI_PIN GPIO_Pin_5
#define SPI_CS_PIN GPIO_Pin_6
#define SPI_GPIO_PORT GPIOB
#define SPI_CS_LOW() GPIO_ResetBits(SPI_GPIO_PORT,SPI_CS_PIN) //Chip Select pin set low
#define SPI_CS_HIGH() GPIO_SetBits(SPI_GPIO_PORT,SPI_CS_PIN) //Chip Select pin set low
static u8 SPI_FLASH_SendByte(u8 byte);
static void SPI_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
GPIO_InitStructure.GPIO_Pin = SPI_SCLK_PIN | SPI_MOSI_PIN | SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SPI_GPIO_PORT,&GPIO_InitStructure); //init CLK MO MI
GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_SCLK_PIN, GPIO_AF_SPI3);
GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MOSI_PIN, GPIO_AF_SPI3);
GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MISO_PIN, GPIO_AF_SPI3);
GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN; //init CS
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_6); //CS==1
SPI_Cmd(SPI3, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
}
static u8 SPI_FLASH_SendByte(u8 byte)
{
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI3, byte);
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI3);
}
u32 Read_AT25_ID(void)
{
u32 device_id = 0;
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_READ_ID);
device_id |= SPI_FLASH_SendByte(0xff)<<16;
device_id |= SPI_FLASH_SendByte(0xff)<<8;
device_id |= SPI_FLASH_SendByte(0xff);
SPI_CS_HIGH();
return device_id;
}
static void SPI_FLASH_Write_Enable(void)
{
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_WRITE_ENABLE);
SPI_CS_HIGH();
}
static void SPI_FLASH_WaitForWriteEnd(void)
{
uint8_t FLASH_Status = 0;
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_READ_STATUSREGISTER);
do
{
FLASH_Status = SPI_FLASH_SendByte(0xff);
}while((FLASH_Status&0x01) == 0x01);
SPI_CS_HIGH();
}
static void SPI_FLASH_Write_StatusRegister1(void)
{
// uint8_t FLASH_Status = 0;
SPI_FLASH_Write_Enable();
SPI_FLASH_WaitForWriteEnd();
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_WRITE_STATUSREGISTER_BYTE1);
SPI_FLASH_SendByte(0x00);
SPI_CS_HIGH();
}
//2á3yò»¸ö4k¿é
//Dst_Addr:¿éμØÖ· ¸ù¾Yêμ¼êèYá¿éèÖÃ
//2á3yò»¸öé½ÇøμÄ×îéùê±¼ä:150ms
static void SPI_FLASH_Erase_Block(u32 Block_Addr)
{
Block_Addr*=4096;
SPI_FLASH_Write_Enable();
SPI_FLASH_WaitForWriteEnd();
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_ERASE_4K);
SPI_FLASH_SendByte((u8)((Block_Addr)>>16)); //·¢Ëí24bitμØÖ·
SPI_FLASH_SendByte((u8)((Block_Addr)>>8));
SPI_FLASH_SendByte((u8)Block_Addr);
SPI_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
}
static void SPI_FLASH_Erase_Chip(void)
{
SPI_FLASH_Write_Enable();
SPI_FLASH_WaitForWriteEnd();
SPI_CS_LOW();
SPI_FLASH_SendByte(AT25_ERASE_CHIP);
SPI_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
}
//¶áè¡SPI FLASH
//ÔúÖ¸¶¨μØÖ·¿a꼶áè¡Ö¸¶¨3¤¶èμÄêy¾Y
//pBuffer:êy¾Y′æ′¢Çø
//ReadAddr:¿a꼶áè¡μÄμØÖ·(24bit)
//NumByteToRead:òa¶áè¡μÄ×Ö½úêy(×î′ó65535)
void AT25DF_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
u16 i;
SPI_CS_LOW(); //ê1ÄüÆ÷¼t
SPI_FLASH_SendByte(AT25_READ_ARRAY_0B); //·¢Ëí¶áè¡Ãüáî
SPI_FLASH_SendByte((u8)((ReadAddr)>>16)); //·¢Ëí24bitμØÖ·
SPI_FLASH_SendByte((u8)((ReadAddr)>>8));
SPI_FLASH_SendByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer=SPI_FLASH_SendByte(0XFF); //Ñ-»·¶áêy
}
SPI_CS_HIGH();
}
//SPIÔúò»ò3(0~65535)ÄúD′èëéùóú256¸ö×Ö½úμÄêy¾Y
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èë×î′ó256×Ö½úμÄêy¾Y
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó256),¸Ãêy2»ó|¸Ã3¬1y¸Ãò3μÄê£óà×Ö½úêy!!!
void AT25DF_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
SPI_FLASH_Write_Enable(); //SET WEL
SPI_CS_LOW(); //ê1ÄüÆ÷¼t
SPI_FLASH_SendByte(AT25_PAGE_PROGRAME); //·¢ËíD′ò3Ãüáî
SPI_FLASH_SendByte((u8)((WriteAddr)>>16)); //·¢Ëí24bitμØÖ·
SPI_FLASH_SendByte((u8)((WriteAddr)>>8));
SPI_FLASH_SendByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)SPI_FLASH_SendByte(pBuffer);//Ñ-»·D′êy
SPI_CS_HIGH(); //è¡ÏûƬѡ
SPI_FLASH_WaitForWriteEnd(); //μè′yD′èë½áêø
}
//ÎT¼ìÑéD′SPI FLASH
//±ØDëè·±£ËùD′μÄμØÖ··¶Î§ÄúμÄêy¾Yè«2¿Îa0XFF,·ñÔòÔú·Ç0XFF′|D′èëμÄêy¾Y½«ê§°ü!
//¾ßóD×Ô¶ˉ»»ò31|Äü
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èëÖ¸¶¨3¤¶èμÄêy¾Y,μ«êÇòaè·±£μØÖ·2»Ô½½ç!
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó65535)
//CHECK OK
void AT25DF_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr%256; //μ¥ò3ê£óàμÄ×Ö½úêy
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//2»′óóú256¸ö×Ö½ú
while(1) //one time write =pageremain
{
AT25DF_Write_Page(pBuffer,WriteAddr,pageremain); //ÏèóÃáãËéμÄ¿Õ¼ä
if(NumByteToWrite==pageremain)break;//D′èë½áêøáË
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //¼õè¥òѾ-D′èëáËμÄ×Ö½úêy
if(NumByteToWrite>256)pageremain=256; //ò»′οéòÔD′èë256¸ö×Ö½ú
else pageremain=NumByteToWrite; //2»1»256¸ö×Ö½úáË
}
};
}
//D′SPI FLASH
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èëÖ¸¶¨3¤¶èμÄêy¾Y
//¸Ãoˉêy′ø2á3y2ù×÷!
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó65535)
u8 AT25DF_BUFFER[4096];
void AT25DF_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 blockpos;
u16 blockoff;
u16 blockremain;
u16 i;
u8 * AT25DF_BUF;
AT25DF_BUF=AT25DF_BUFFER;
blockpos=WriteAddr/4096;//¿éμØÖ·
blockoff=WriteAddr%4096;//Ôú¿éÄúμÄÆ«òÆ
blockremain=4096-blockoff;//¿éê£óà¿Õ¼ä′óD¡
printf("ad:%X,nb:%X
",WriteAddr,NumByteToWrite);//2aêÔóÃ
if(NumByteToWrite<=blockremain)blockremain=NumByteToWrite;//2»′óóú4096¸ö×Ö½ú
while(1)
{
AT25DF_Read(AT25DF_BUF,blockpos*4096,4096);//¶á3öÕû¸ö¿éμÄÄúèY
for(i=0;i<blockremain;i++)//D£Ñéêy¾Y
{
if(AT25DF_BUF[blockoff+i]!=0XFF)break;//Dèòa2á3y
}
if(i<blockremain)//Dèòa2á3y
{
SPI_FLASH_Erase_Block(blockpos); //2á3yÕa¸ö¿é
for(i=0;i<blockremain;i++) //¸′ÖÆ
{
AT25DF_BUF[i+blockoff]=pBuffer;
}
AT25DF_Write_NoCheck(AT25DF_BUF,blockpos*4096,4096);//D′èëÕû¸ö¿é
}else AT25DF_Write_NoCheck(pBuffer,WriteAddr,blockremain);//D′òѾ-2á3yáËμÄ,Ö±½óD′èë¿éê£óàÇø¼ä.
if(NumByteToWrite==blockremain)break;//D′èë½áêøáË
else//D′èëÎ′½áêø
{
blockpos++;//¿éμØÖ·Ôö1
blockoff=0;//Æ«òÆλÖÃÎa0
pBuffer+=blockremain; //Ö¸ÕëÆ«òÆ
WriteAddr+=blockremain;//D′μØÖ·Æ«òÆ
NumByteToWrite-=blockremain; //×Ö½úêyμY¼õ
if(NumByteToWrite>4096)blockremain=4096; //ÏÂò»¸ö¿é»1êÇD′2»íê
else blockremain=NumByteToWrite; //ÏÂò»¸ö¿é¿éòÔD′íêáË
}
}
}
void spi_flash_sample(void)
{
char text[]={"i am a chinese"};
char text2[15];
SPI_GPIO_Configuration();
printf("spi init finished
");
printf("flash id is: %d
",Read_AT25_ID());
SPI_FLASH_Erase_Chip();
printf("spi erase finished
");
AT25DF_Write((u8*)text,0x00,sizeof(text));
printf("spi write finished
");
AT25DF_Read((u8*)text2,0x00,sizeof(text));
printf("spi read data : %s
",text2);
}
[/mw_shl_code]
GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_SCLK_PIN, GPIO_AF_SPI3);这个地方用错了
谢谢
一周热门 更多>