< I2C_PageSize */if(NumOfPage == 0) {I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);I2C_EE_WaitEepromStandbyState();}/* If NumByteToWrite > I2C_PageSize */else{while(NumOfPage--){I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2C_EE_WaitEepromStandbyState();WriteAddr +=I2C_PageSize;pBuffer += I2C_PageSize;}if(NumOfSingle!=0){I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);I2C_EE_WaitEepromStandbyState();}}}/* If WriteAddr is not I2C_PageSize aligned*/else {/* If NumByteToWrite < I2C_PageSize */if(NumOfPage== 0) {I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);I2C_EE_WaitEepromStandbyState();}/* If NumByteToWrite > I2C_PageSize */else{NumByteToWrite -= count;NumOfPage =NumByteToWrite / I2C_PageSize;NumOfSingle = NumByteToWrite % I2C_PageSize; if(count != 0){I2C_EE_PageWrite(pBuffer, WriteAddr, count);I2C_EE_WaitEepromStandbyState();WriteAddr += count;pBuffer += count;} while(NumOfPage--){I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);I2C_EE_WaitEepromStandbyState();WriteAddr +=I2C_PageSize;pBuffer += I2C_PageSize;}if(NumOfSingle != 0){I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState();}}}}/*** @brief写一个字节到I2C EEPROM中* @param*@arg pBuffer:缓冲区指针*@arg WriteAddr:写地址 * @retval无*/uint32_t I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr) {/* Send STRAT condition */I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV5 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);} I2CTimeout = I2CT_FLAG_TIMEOUT;/* Send EEPROM address for write */I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);/* Test on EV6 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(1);}/* Send the EEPROM's internal address to write to */I2C_SendData(EEPROM_I2Cx, WriteAddr);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV8 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(2);} /* Send the byte to be written */I2C_SendData(EEPROM_I2Cx, *pBuffer); I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV8 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);} /* Send STOP condition */I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);return 1;}/*** @brief在EEPROM的一个写循环中可以写多个字节 , 但一次写入的字节数*不能超过EEPROM页的大小 , AT24C02每页有8个字节* @param*@arg pBuffer:缓冲区指针*@arg WriteAddr:写地址*@arg NumByteToWrite:写的字节数* @retval无*/uint32_t I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite){I2CTimeout = I2CT_LONG_TIMEOUT;while(I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(4);} /* Send START condition */I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV5 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(5);} /* Send EEPROM address for write */I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV6 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(6);} /* Send the EEPROM's internal address to write to */I2C_SendData(EEPROM_I2Cx, WriteAddr);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV8 and clear it */while(! I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(7);} /* While there is data to be written */while(NumByteToWrite--){/* Send the current byte */I2C_SendData(EEPROM_I2Cx, *pBuffer); /* Point to the next byte to be written */pBuffer++; I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV8 and clear it */while (!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(8);} }/* Send STOP condition */I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);return 1;}/*** @brief从EEPROM里面读取一块数据 * @param*@arg pBuffer:存放从EEPROM读取的数据的缓冲区指针*@arg WriteAddr:接收数据的EEPROM的地址*@arg NumByteToWrite:要从EEPROM读取的字节数* @retval无*/uint32_t I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead){I2CTimeout = I2CT_LONG_TIMEOUT;//*((u8 *)0x4001080c) |=0x80; while(I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9);}/* Send START condition */I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);//*((u8 *)0x4001080c) &=~0x80;I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV5 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);}/* Send EEPROM address for write */I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV6 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(11);}/* Clear EV6 by setting again the PE bit */I2C_Cmd(EEPROM_I2Cx, ENABLE);/* Send the EEPROM's internal address to write to */I2C_SendData(EEPROM_I2Cx, ReadAddr);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV8 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(12);}/* Send STRAT condition a second time */I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV5 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(13);}/* Send EEPROM address for read */I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Receiver);I2CTimeout = I2CT_FLAG_TIMEOUT;/* Test on EV6 and clear it */while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(14);}/* While there is data to be read */while(NumByteToRead){if(NumByteToRead == 1){/* Disable Acknowledgement */I2C_AcknowledgeConfig(EEPROM_I2Cx, DISABLE);/* Send STOP Condition */I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);}/* Test on EV7 and clear it */I2CTimeout = I2CT_LONG_TIMEOUT;while(I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)==0){if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(3);} {/* Read a byte from the EEPROM */*pBuffer = I2C_ReceiveData(EEPROM_I2Cx);/* Point to the next location where the byte read will be saved */pBuffer++; /* Decrement the read bytes counter */NumByteToRead--;}}/* Enable Acknowledgement to be ready for another reception */I2C_AcknowledgeConfig(EEPROM_I2Cx, ENABLE);return 1;}/*** @briefWait for EEPROM Standby state * @param无* @retval 无*///通过检测EEPROM的响应 , 来判断它的内部写时序是否完成void I2C_EE_WaitEepromStandbyState(void){vu16 SR1_Tmp = 0;I2CTimeout = I2CT_LONG_TIMEOUT;do{I2CTimeout--;/* Send START condition */I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);/* Read I2C1 SR1 register */SR1_Tmp = I2C_ReadRegister(EEPROM_I2Cx, I2C_Register_SR1);/* Send EEPROM address for write */I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);}while(!(I2C_ReadRegister(EEPROM_I2Cx, I2C_Register_SR1) & 0x0002) && (I2CTimeout > 0));//I2C_Register_SR1的第二位就是I2C_FLAG_ADDR/* Clear AF flag */I2C_ClearFlag(EEPROM_I2Cx, I2C_FLAG_AF);/* STOP condition */I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE); }/*** @briefBasic management of the timeout situation.* @paramerrorCode:错误代码 , 可以用来定位是哪个环节出错.* @retval 返回0 , 表示IIC读取失败.*/staticuint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode){/* Block communication and all processes */EEPROM_ERROR("I2C 等待超时!errorCode = %d",errorCode);return 0;}/*********************************************END OF FILE**********************/
- 国内红茶品牌排行榜 各大品牌上榜理由一览
- 2021-05-04:给定一个非负整数c,你要判断是否存在两个整数a和b
- 养一只麻鸭要多少成本
- 人工种植鲜天麻多少钱一斤?
- 国内家纺品牌排行 各大品牌上榜理由一览
- 天眼查询企业信息API接口
- 一 软件工程与计算总结软件工程基础
- 姬秋丽换盆需要注意哪些事项
- 软件工程 专业课考研#1软件工程学概述
- 西安,大理是一个地方?