用SPI1读写SPI EEPROM M95040. clock和MOSI都能正常工作,就是MISO上没有信号。用示波器看信号,MOSI信号符合时序。但是MISO上就是没有信号。 还有个很奇怪的问题。就是单步执行,发现能够写入,也能读出数量,任意修改写入某地址的数据,都能正确读出。但是全速运行时读出来的都是0xFF.是不是延时的问题?
程序如下:请高手看看。谢谢! /* EEPROM instructions */ #define WREN 0x06 #define WRDI 0x04 #define RDSR 0x05 #define WRSR 0x01 #define READ 0x03 #define WRITE 0x02 /* Private variables ---------------------------------------------------------*/ SPI_InitTypeDef SPI_InitStructure;
/*----------------------------------------------------------------------------- ROUTINE NAME : SPI_Init INPUT/OUTPUT : None
DESCRIPTION : Configure the SPI peripheral
COMMENTS : -----------------------------------------------------------------------------*/
void SPI_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); /* SPI1 configuration */ SPI_I2S_DeInit(SPI1); 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_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */ SPI_Cmd(SPI1, ENABLE);
}
/*----------------------------------------------------------------------------- ROUTINE NAME : Chk_WP_Bit INPUT/OUTPUT : None/Boolean
DESCRIPTION : Return 'TRUE' if write is still in progress in the EEPROM (M95040)
COMMENTS : -----------------------------------------------------------------------------*/
bool Chk_WP_Bit(void) { u8 i; SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //SPI_SendByte(RDSR); //SPI_SendByte(RDSR); while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET); SPI1->DR=RDSR; while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET); i=SPI1->DR; while((SPI1->SR & SPI_I2S_FLAG_TXE) == RESET); SPI1->DR=RDSR; while((SPI1->SR & SPI_I2S_FLAG_RXNE) == RESET); i=SPI1->DR; GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable
if(((SPI1->DR)|0xFE)==0xFE) return(TRUE);//(ValBit(SPIDR,0)) return(TRUE); else return(FALSE); }
/*----------------------------------------------------------------------------- ROUTINE NAME : Read_EEPROM INPUT/OUTPUT : u8 (adress) / u8 (data)
DESCRIPTION : Return data located at the adress specified
COMMENTS : -----------------------------------------------------------------------------*/ u8 Read_EEPROM(u8 add) { u8 data;
SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable GPIO_ResetBits(GPIOA,GPIO_Pin_4); SPI_SendByte(READ); SPI_SendByte(add); data=SPI_ReadByte(); GPIO_SetBits(GPIOA,GPIO_Pin_4); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable return (data); } /*----------------------------------------------------------------------------- ROUTINE NAME : Send_EEPROM INPUT/OUTPUT : u8,u8 (adress,data) / None
DESCRIPTION : Send data byte to the adress specified
COMMENTS : -----------------------------------------------------------------------------*/
void Send_EEPROM(u8 add, u8 data) {
SPI_Cmd(SPI1, ENABLE);//SetBit(SPICR,6); // spi enable GPIO_ResetBits(GPIOA,GPIO_Pin_4);//ClrBit(PADR,6);
SPI_SendByte(WREN); GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//ClrBit(PADR,6); SPI_SendByte(WRITE); SPI_SendByte(add); SPI_SendByte(data);
GPIO_SetBits(GPIOA,GPIO_Pin_4);//SetBit(PADR,6); SPI_Cmd(SPI1, DISABLE);//ClrBit(SPICR,6); // spi disable
}
/******************************************************************************* * Function Name : SPI_SendByte * Description : Sends a byte through the SPI interface and return the byte * received from the SPI bus. * Input : byte : byte to send. * Output : None * Return : The value of the received byte. *******************************************************************************/ u8 SPI_SendByte(u8 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_I2S_SendData(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_I2S_ReceiveData(SPI1); } /******************************************************************************* * Function Name : SPI_ReadByte * Description : Reads a byte from the SPI Flash. * This function must be used only if the Start_Read_Sequence * function has been previously called. * Input : None * Output : None * Return : Byte Read from the SPI Flash. *******************************************************************************/ u8 SPI_ReadByte(void) { return (SPI_SendByte(0x55)); }
/*-----------------------------------------------------------------------------*/ void Delay_us(u16 us) { while(us--); } /*----------------------------------------------------------------------------- ROUTINE NAME : Delay_ms INPUT/OUTPUT : ms
DESCRIPTION :
COMMENTS : -----------------------------------------------------------------------------*/
void Delay_ms(u16 ms) { u16 i,j; for(i=0;i<ms;i++) { for(j=0;j<15;j++) { Delay_us(1); } } } |
|