GD32F103硬件SPI与EEPROM(M95)实现数据读写
[复制链接]
本帖最后由 nine_e 于 2024-4-24 20:28 编辑
各位大佬帮我看看这个EEPROM读出为什么没有数据!!!!!!!!!!!!
//////////--------------------------------------初始化------------------------------------------//////////////
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
// NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_8 | GPIO_Pin_10;// GPIO_Pin_1--imu1 GPIO_Pin_4--imu2 GPIO_Pin_8--eeprom
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
CS_L_HIGH;
CS_R_HIGH;
CS_eeprom_HIGH;
GPIO_SetBits(GPIOA , GPIO_Pin_10);
}
void eepromIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC and GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB.09 as Input pull-up */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB , GPIO_Pin_0);
}
//这里针是对SPI1的初始化
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );//PORTA时钟使能
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );//SPI1时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA 5/6/7复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7); //PA567 上拉
SPI_Cmd(SPI1, DISABLE);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串行同步时钟的空闲状态为低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //串行同步时钟的第1个跳变沿(上升或下降)数据被采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为16
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
SPI_InitStructure.SPI_CRCPolynomial = 10; //CRC值计算的多项式
SPI_Init(SPI1, &SPI_InitStructure); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_Cmd(SPI1, ENABLE); //使能SPI外设
//SPI1_ReadWriteByte(0xff);//启动传输
}
//////////--------------------------------------EEPROM------------------------------------------//////////////
EepromOperations_t EEPROM_Read(uint8_t* RxData, uint16_t ReadAddr, uint16_t NumByteToRead)
{
/* We gonna send all commands in one packet of 3 bytes */
uint8_t header[3];
header[0] = EEPROM_READ; // Send "Read from Memory" instruction
header[1] = ReadAddr >> 8; // Send 16-bit address
header[2] = ReadAddr;
// Select the EEPROM: Chip Select low
EEPROM_CS_LOW();
/* Send WriteAddr address byte to read from */
EEPROM_SendInstruction(header, 3);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // 读取接收数据
RxData = SPI_I2S_ReceiveData(SPI1);
printf("out RxData=%x\r\n",RxData);
// Deselect the EEPROM: Chip Select high
EEPROM_CS_HIGH();
return EEPROM_STATUS_COMPLETE;
}
EepromOperations_t EEPROM_Write(uint8_t* TxData, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
uint16_t sEE_DataNum = 0;
EepromOperations_t pageWriteStatus = EEPROM_STATUS_PENDING;
Addr = WriteAddr % EEPROM_PAGE_SIZE;
count = EEPROM_PAGE_SIZE - Addr;
NumOfPage = NumByteToWrite / EEPROM_PAGE_SIZE;
NumOfSingle = NumByteToWrite % EEPROM_PAGE_SIZE;
if (Addr == 0) { /* WriteAddr is EEPROM_PAGESIZE aligned */
if (NumOfPage == 0) { /* NumByteToWrite < EEPROM_PAGESIZE */
sEE_DataNum = NumByteToWrite;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
} else { /* NumByteToWrite > EEPROM_PAGESIZE */
while (NumOfPage--) {
sEE_DataNum = EEPROM_PAGE_SIZE;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
WriteAddr += EEPROM_PAGE_SIZE;
TxData += EEPROM_PAGE_SIZE;
}
sEE_DataNum = NumOfSingle;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
}
} else { /* WriteAddr is not EEPROM_PAGESIZE aligned */
if (NumOfPage == 0) { /* NumByteToWrite < EEPROM_PAGESIZE */
if (NumOfSingle > count) { /* (NumByteToWrite + WriteAddr) > EEPROM_PAGESIZE */
temp = NumOfSingle - count;
sEE_DataNum = count;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
WriteAddr += count;
TxData += count;
sEE_DataNum = temp;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
} else {
sEE_DataNum = NumByteToWrite;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
}
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
} else { /* NumByteToWrite > EEPROM_PAGESIZE */
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / EEPROM_PAGE_SIZE;
NumOfSingle = NumByteToWrite % EEPROM_PAGE_SIZE;
sEE_DataNum = count;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
WriteAddr += count;
TxData += count;
while (NumOfPage--) {
sEE_DataNum = EEPROM_PAGE_SIZE;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
WriteAddr += EEPROM_PAGE_SIZE;
TxData += EEPROM_PAGE_SIZE;
}
if (NumOfSingle != 0) {
sEE_DataNum = NumOfSingle;
pageWriteStatus = EEPROM_WritePage(TxData, WriteAddr, sEE_DataNum);
if (pageWriteStatus != EEPROM_STATUS_COMPLETE) {
return pageWriteStatus;
}
}
}
}
return EEPROM_STATUS_COMPLETE;
}
|