Page 1 of 1

是否有硬件iic的使用demo

Posted: 2024年 Mar 8日 09:51
by hzb

sdk版本:TLSR8258_TuyaOS-3.8.0


Re: 是否有硬件iic的使用demo

Posted: 2024年 Mar 8日 10:29
by huanghuan

1.你好,你可以使用drv层接口,以下为test demo

Code: Select all

#define MODULE_TEST_I2C		0

#if MODULE_TEST_I2C

#if	defined(MCU_CORE_826x)
	#define TEST_I2C_PIN_GROUP		I2C_PIN_GPIOA
#elif defined(MCU_CORE_8258)
	#define TEST_I2C_PIN_GROUP		I2C_GPIO_GROUP_A3A4
#elif defined(MCU_CORE_8278)
	#define TEST_I2C_PIN_SDA		I2C_GPIO_SDA_A3
	#define TEST_I2C_PIN_SCL		I2C_GPIO_SCL_A4
#elif defined(MCU_CORE_B91)
	#define TEST_I2C_PIN_SDA		I2C_GPIO_SDA_B3
	#define TEST_I2C_PIN_SCL		I2C_GPIO_SCL_B2
#endif

/* I2C Clock */
#define I2C_CLOCK					200000//200K

/* I2C slave ID */
#define I2C_SLAVE_ID				0x5A
#define I2C_SLAVE_ADDR				0x48000//0x8800
#define I2C_SLAVE_ADDR_LEN			3//2

#define DBG_DATA_LEN				16
//write buff
u8 i2c_master_tx_buff[DBG_DATA_LEN] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff};
//read buff
u8 i2c_master_rx_buff[DBG_DATA_LEN] = {0x00};

void i2c_master_test_init(void){
	drv_i2c_master_init(I2C_CLOCK);

#if	defined(MCU_CORE_826x) || defined(MCU_CORE_8258)
	drv_i2c_gpio_set(TEST_I2C_PIN_GROUP);
#elif defined(MCU_CORE_8278) || defined(MCU_CORE_B91)
	drv_i2c_gpio_set(TEST_I2C_PIN_SDA, TEST_I2C_PIN_SCL);
#endif
}

void i2c_master_test(void){
	WaitMs(1000);   //1S

gpio_toggle(GPIO_PB7);

i2c_master_tx_buff[0] += 1;
i2c_master_tx_buff[0] &= 0xff;

drv_i2c_write_series(I2C_SLAVE_ID, I2C_SLAVE_ADDR, I2C_SLAVE_ADDR_LEN, i2c_master_tx_buff, DBG_DATA_LEN);

WaitMs(1000);   //1S

drv_i2c_read_series(I2C_SLAVE_ID, I2C_SLAVE_ADDR, I2C_SLAVE_ADDR_LEN, i2c_master_rx_buff, DBG_DATA_LEN);
}

void moduleTest_i2c(void){
	WaitMs(2000);//leave enough time for SWS_reset when power on

drv_gpio_func_set(GPIO_PB7);
drv_gpio_output_en(GPIO_PB7, 1); 		//enable output
drv_gpio_input_en(GPIO_PB7, 0);		//disable input
drv_gpio_write(GPIO_PB7, 1);              	//LED On

i2c_master_test_init();

while(1){
	i2c_master_test();
}
}
#endif

Re: 是否有硬件iic的使用demo

Posted: 2024年 Mar 8日 23:48
by hzb

好的,感谢,我测试下


Re: 是否有硬件iic的使用demo

Posted: 2024年 Mar 11日 21:03
by hzb

根据以上例程修改了:
另外创建定时器tal_sw_timer_create, tal_sw_timer_start 定时读写,用逻辑分析仪采样了下,发现没任何波形输出。

Code: Select all

 #define TEST_I2C_PIN_GROUP		I2C_GPIO_GROUP_C0C1
 /* I2C Clock */
#define I2C_CLOCK					200000//200K
/* I2C slave ID */
#define I2C_SLAVE_ID				0x38
#define I2C_SLAVE_ADDR_LEN			1
//write buff
unsigned char i2c_master_tx_buff[16];
//read buff
unsigned char i2c_master_rx_buff[16];

void aht20_measure_handle(TIMER_ID timer_id, VOID_T *arg)
{
	/*.... */
	i2c_master_tx_buff[0] = 0x33;
	i2c_master_tx_buff[0] = 0x00;
	drv_i2c_write_series(I2C_SLAVE_ID, AHT20_MEASURE_REG, I2C_SLAVE_ADDR_LEN, i2c_master_tx_buff, 2);
	/*...*/
	drv_i2c_read_series(I2C_SLAVE_ID, AHT20_STATUS_REG, I2C_SLAVE_ADDR_LEN, i2c_master_rx_buff, 7);
}

请问是什么原因呢?