GD32F450用I2C读写EEPROM代码移植

GD32F450用I2C读写EEPROM代码移植

最近在学习GD32F450芯片的一些基本驱动功能。本文是基于《STM32库开发实战指南——基于野火挑战者开发板》,《GD32F4xx_User_Manual_CN_V2.2》,以及【GD32F4xx_Demo_Suites_V2.3.0】等相关资料进行代码移植的,相关资料网上都有。

单字节写入

/**
	* @brief write one byte data to I2C EEPROM 
	* @param pBuffer: pointer of buffer
	* @param WriteAddr: addr of EEPROM to be written
	* @retval success return 1, failure return 0
	*/
uint8_t I2C_EE_ByteWrite(uint8_t* pBuffer, uint8_t WriteAddr)
{
		I2CTimeout = I2CT_FLAG_TIMEOUT;
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send a start condition to I2C bus */
		i2c_start_on_bus(I2C1);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until SBSEND bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send slave(EEPROM) address to I2C bus */
    i2c_master_addressing(I2C1, EEPROM_ADDRESS_WRITE, I2C_TRANSMITTER);
	  I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until ADDSEND bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* clear ADDSEND bit */
    i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
    /* wait until the transmit data buffer is empty */
    while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
		/* WriteAddr transmission */
		i2c_data_transmit(I2C1, WriteAddr);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until the TBE bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
		/* data transmission */
		i2c_data_transmit(I2C1, *pBuffer);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until the TBE bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C1);
    while(I2C_CTL0(I2C1)&0x0200);
		return 1;
}

多字节写入和页写入都是同样的逻辑

版权声明:本文为CSDN博主「rrrrrrrrrstart」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43438751/article/details/122225558

GD32F450用I2C读写EEPROM代码移植

最近在学习GD32F450芯片的一些基本驱动功能。本文是基于《STM32库开发实战指南——基于野火挑战者开发板》,《GD32F4xx_User_Manual_CN_V2.2》,以及【GD32F4xx_Demo_Suites_V2.3.0】等相关资料进行代码移植的,相关资料网上都有。

单字节写入

/**
	* @brief write one byte data to I2C EEPROM 
	* @param pBuffer: pointer of buffer
	* @param WriteAddr: addr of EEPROM to be written
	* @retval success return 1, failure return 0
	*/
uint8_t I2C_EE_ByteWrite(uint8_t* pBuffer, uint8_t WriteAddr)
{
		I2CTimeout = I2CT_FLAG_TIMEOUT;
    /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send a start condition to I2C bus */
		i2c_start_on_bus(I2C1);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until SBSEND bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send slave(EEPROM) address to I2C bus */
    i2c_master_addressing(I2C1, EEPROM_ADDRESS_WRITE, I2C_TRANSMITTER);
	  I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until ADDSEND bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND))
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* clear ADDSEND bit */
    i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
    /* wait until the transmit data buffer is empty */
    while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
		/* WriteAddr transmission */
		i2c_data_transmit(I2C1, WriteAddr);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until the TBE bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
		/* data transmission */
		i2c_data_transmit(I2C1, *pBuffer);
		I2CTimeout = I2CT_FLAG_TIMEOUT;
		/* wait until the TBE bit is set */
		while(!i2c_flag_get(I2C1, I2C_FLAG_TBE))		
		{
		if ((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(0);
		}
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C1);
    while(I2C_CTL0(I2C1)&0x0200);
		return 1;
}

多字节写入和页写入都是同样的逻辑

版权声明:本文为CSDN博主「rrrrrrrrrstart」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43438751/article/details/122225558

生成海报
点赞 0

rrrrrrrrrstart

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

GD32F450用I2C读写EEPROM代码移植

GD32F450用I2C读写EEPROM代码移植 最近在学习GD32F450芯片的一些基本驱动功能。本文是基于《STM32库开发实战指南——基于野火挑战者开发板》,《GD32F4xx_User_Manual_CN_V2.2》&#

GD32三种低功耗例程

GD32F303ZET6三种低功耗例程 睡眠模式例程:MCU的UART3接收到数据 ,进入UART3接收中断  即唤醒睡眠模式。 int main(void) { /******** 本实验测试单片机睡眠模