From 379cd567a21115ed633e805a8de27396586c1ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=82=E7=85=9C=E6=B4=8B?= <1163589503@qq.com> Date: Wed, 9 Aug 2023 16:33:17 +0800 Subject: [PATCH 1/2] Fix edu-arm32 i2c. --- .../Applications/app_test/test_i2c.c | 67 +++++++++++++------ .../third_party_driver/i2c/connect_i2c.c | 10 ++- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/APP_Framework/Applications/app_test/test_i2c.c b/APP_Framework/Applications/app_test/test_i2c.c index 76881b92e..e2be2ce47 100644 --- a/APP_Framework/Applications/app_test/test_i2c.c +++ b/APP_Framework/Applications/app_test/test_i2c.c @@ -24,18 +24,16 @@ #define I2C_SLAVE_ADDRESS 0x0012U -void TestI2C(void) +int open_iic(void) { - // config IIC pin(SCL:34.SDA:35) in menuconfig int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR); if (iic_fd < 0) { - printf("open iic_fd fd error:%d\n", iic_fd); - return; + printf("[TestI2C] Open iic_fd fd error: %d\n", iic_fd); + return -ERROR; } - printf("IIC open successful!\n"); + printf("[TestI2C] IIC open successful!\n"); - // init iic uint16 iic_address = I2C_SLAVE_ADDRESS; struct PrivIoctlCfg ioctl_cfg; @@ -44,28 +42,55 @@ void TestI2C(void) if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg)) { - printf("ioctl iic fd error %d\n", iic_fd); + printf("[TestI2C] Ioctl iic fd error %d\n", iic_fd); PrivClose(iic_fd); - return; + return -ERROR; } printf("IIC configure successful!\n"); - // I2C read and write - char tmp_buff[100]; - while (1) - { - PrivTaskDelay(1000); - PrivWrite(iic_fd, "Hello World!\n", sizeof("Hello World!\n")); - printf("msg send:%s\n", "Hello World!\n"); - PrivTaskDelay(1000); - memset(tmp_buff, 0, sizeof(tmp_buff)); - PrivRead(iic_fd, tmp_buff, sizeof(tmp_buff)); - printf("msg recv:%s\n", tmp_buff); + return iic_fd; +} + +static const int nr_transmit = 15; + +void TestMasterI2c(void) +{ + char recv_buff[13] = { 0 }; + + int iic_fd = open_iic(); + if (iic_fd < 0) { + printf("[%s] Error open iic\n", __func__); + return; + } + + for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) { + // wait if you like. + PrivTaskDelay(500); + memset(recv_buff, 0, sizeof(recv_buff)); + PrivRead(iic_fd, recv_buff, sizeof(recv_buff)); + printf("[%s] Msg recv: %s\n", __func__, recv_buff); } PrivClose(iic_fd); - return; } -PRIV_SHELL_CMD_FUNCTION(TestI2C, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR); +void TestSlaveI2c(void) +{ + char send_buff[] = "Hello, World"; + + int iic_fd = open_iic(); + + for (int transmit_cnt = 0; transmit_cnt < nr_transmit; transmit_cnt++) { + // wait if you like. + PrivTaskDelay(500); + PrivWrite(iic_fd, send_buff, sizeof(send_buff)); + printf("[%s] Msg send: %s\n", __func__, send_buff); + } + + PrivClose(iic_fd); +} + +PRIV_SHELL_CMD_FUNCTION(TestMasterI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR); +PRIV_SHELL_CMD_FUNCTION(TestSlaveI2c, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR); + #endif \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/i2c/connect_i2c.c b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/i2c/connect_i2c.c index d59f7b98b..20149f915 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/i2c/connect_i2c.c +++ b/Ubiquitous/XiZi_IIoT/board/edu-arm32/third_party_driver/i2c/connect_i2c.c @@ -117,6 +117,9 @@ static uint32 I2cDrvConfigure(void *drv, struct BusConfigureInfo *configure_info static uint32 I2cMasterWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg) { + if (msg->len == 0) { + return EOK; + } uint32 i32Ret; I2C_Cmd(I2C_UNIT, ENABLE); @@ -171,6 +174,9 @@ static uint32 I2cMasterReadData(struct I2cHardwareDevice *i2c_dev, struct I2cDat } static uint32 I2cSlaveWriteData(struct I2cHardwareDevice *i2c_dev, struct I2cDataStandard *msg) { + if (msg->len == 0) { + return EOK; + } uint32 i32Ret; I2C_Cmd(I2C_UNIT, ENABLE); @@ -222,7 +228,7 @@ static uint32 I2cSlaveReadData(struct I2cHardwareDevice *i2c_dev, struct I2cData if (RESET == I2C_GetStatus(I2C_UNIT, I2C_FLAG_TRA)) { /* Slave receive data*/ i32Ret = I2C_ReceiveData(I2C_UNIT, msg->buf, msg->len, I2C_TIMEOUT); - KPrintf("Slave receive success!\r\n"); + KPrintf("Slave receive success!\r\n"); if ((LL_OK == i32Ret) || (LL_ERR_TIMEOUT == i32Ret)) { /* Wait stop condition */ @@ -336,7 +342,7 @@ int HwI2cInit(void) return ret; } -//#define I2C_TEST +// #define I2C_TEST #ifdef I2C_TEST #define USER_KEY_PORT (GPIO_PORT_I) From 0e82fac640c1b31ef62ba5a85df041ef28daa7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=82=E7=85=9C=E6=B4=8B?= <1163589503@qq.com> Date: Wed, 9 Aug 2023 17:31:06 +0800 Subject: [PATCH 2/2] Fix edu-arm32 i2c. --- APP_Framework/Applications/app_test/test_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/APP_Framework/Applications/app_test/test_i2c.c b/APP_Framework/Applications/app_test/test_i2c.c index e2be2ce47..a5a9475bd 100644 --- a/APP_Framework/Applications/app_test/test_i2c.c +++ b/APP_Framework/Applications/app_test/test_i2c.c @@ -24,7 +24,7 @@ #define I2C_SLAVE_ADDRESS 0x0012U -int open_iic(void) +int OpenIic(void) { int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR); if (iic_fd < 0)