EEPROM 无法读写。
#include "stm32f10x.h"
#define WREN 0x06 /* Set Write Enable Latch */
#define WRDI 0x04 /* Reset Write Enable Latch */
#define RDSR 0x05 /* Read Status Register */
#define WRSR 0x01 /* Write Status Register */
#define READ 0x03 /* Read Data From Memory Array */
#define WRITE 0x02 /* Write Data to Memory Array */
#define CS_EN GPIO_ResetBits(GPIOD,GPIO_Pin_2)
#define CS_DI GPIO_SetBits(GPIOD,GPIO_Pin_2);
void RCC_Config(void);
void GPIO_Config(void);
void SPI_Config(void);
uint8_t AT25320_GetStatus(void);
void AT25320_WaitReady(void);
void SPI_WriteByte(uint8_t data);
uint8_t SPI_ReadByte(void);
uint8_t AT25320_ReadByte(uint16_t addr);
void AT25320_WriteByte(uint16_t addr,uint8_t data);
void RCC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef gpio;
gpio.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;/* CLK and MOSI*/
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&gpio);
gpio.GPIO_Pin = GPIO_Pin_6;/* MISO */
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&gpio);
gpio.GPIO_Pin = GPIO_Pin_2;/* CS */
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&gpio);
}
void SPI_Config()
{
SPI_InitTypeDef spi1;
SPI_I2S_DeInit(SPI1);
spi1.SPI_Direction = SPI_Direction_2Lines_FullDuplex;/* ?????¤ */
spi1.SPI_Mode = SPI_Mode_Master;
spi1.SPI_DataSize = SPI_DataSize_8b;
spi1.SPI_CPOL = SPI_CPOL_Low; /* ????0 */
spi1.SPI_CPHA = SPI_CPHA_1Edge;
spi1.SPI_NSS = SPI_NSS_Soft;
spi1.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;/* SPI???? 72MHz/4 = 18MHz*/
spi1.SPI_FirstBit = SPI_FirstBit_MSB;
spi1.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&spi1);
SPI_Cmd(SPI1,ENABLE);
}
uint8_t AT25320_GetStatus(void)
{
uint8_t tmp;
CS_EN;
SPI_WriteByte(RDSR);
tmp = SPI_ReadByte();
CS_DI;
return tmp;
}
void AT25320_WaitReady(void)
{
while((0x01 & AT25320_GetStatus())!=0);
}
void SPI_WriteByte(uint8_t data)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,data);
}
uint8_t SPI_ReadByte(void)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
uint8_t AT25320_ReadByte(uint16_t addr)
{
uint8_t tmp;
if(addr >4096)
return 0;
AT25320_WaitReady();
SPI_Cmd(SPI1,ENABLE);
CS_EN;
SPI_WriteByte(READ);
SPI_WriteByte((uint8_t)((addr & 0x0F00)>>8));/* A11-A8*/
SPI_WriteByte((uint8_t)(addr & 0x00FF));/* A7-A0*/
tmp = SPI_ReadByte();
CS_DI;
SPI_Cmd(SPI1,DISABLE);
return tmp;
}
void AT25320_WriteByte(uint16_t addr,uint8_t data)
{
if(addr > 4096)
return;
AT25320_WaitReady();
SPI_Cmd(SPI1,ENABLE);
CS_EN;
SPI_WriteByte(WREN);
CS_DI;
CS_EN;
SPI_WriteByte(WRITE);
SPI_WriteByte((uint8_t)((addr & 0x0F00)>>8));/* A11-A8*/
SPI_WriteByte((uint8_t)(addr & 0x00FF));/* A7-A0*/
SPI_WriteByte(data);
CS_DI;
SPI_Cmd(SPI1,DISABLE);
}
int main(void)
{
uint16_t i;
uint8_t buf[10];
RCC_Config();
GPIO_Config();
SPI_Config();