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的Keil环境搭建简述

以GD32F30x为例 从GD官网下载开发文件: GD32F30x_AddOn_V2.1.0.rar包含Keil所需的gd32相关芯片的pack: GD32F30x_Demo_Suites_V2.3.0.rar包

MOS管驱动电路有几种,看完就明白了

MOS管因为其导通内阻低,开关速度快,因此被广泛应用在开关电源上。而用好一个MOS管,其驱动电路的设计就很关键。下面分享几种常用的驱动电路。 1 电源IC直接驱动 电源IC直接驱动是最简单的驱动方式&