diff --git a/APP_Framework/Applications/app_test/Makefile b/APP_Framework/Applications/app_test/Makefile index 227cd29d6..09eebc613 100644 --- a/APP_Framework/Applications/app_test/Makefile +++ b/APP_Framework/Applications/app_test/Makefile @@ -46,7 +46,12 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) endif ifeq ($(CONFIG_USER_TEST_I2C),y) - SRC_FILES += test_i2c.c + ifeq ($(CONFIG_ARCH_RISCV),y) + SRC_FILES += test_i2c_riscv.c + endif + ifeq ($(CONFIG_ARCH_ARM),y) + SRC_FILES += test_i2c_arm.c + endif endif ifeq ($(CONFIG_USER_TEST_UART),y) @@ -67,10 +72,10 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y) ifeq ($(CONFIG_USER_TEST_RS485),y) ifeq ($(CONFIG_ARCH_RISCV),y) - SRC_FILES += test_rs485_riscv.c + SRC_FILES += test_rs485_riscv.c endif ifeq ($(CONFIG_ARCH_ARM),y) - SRC_FILES += test_rs485_arm.c + SRC_FILES += test_rs485_arm.c endif endif diff --git a/APP_Framework/Applications/app_test/test_i2c.c b/APP_Framework/Applications/app_test/test_i2c_arm.c similarity index 100% rename from APP_Framework/Applications/app_test/test_i2c.c rename to APP_Framework/Applications/app_test/test_i2c_arm.c diff --git a/APP_Framework/Applications/app_test/test_i2c_riscv.c b/APP_Framework/Applications/app_test/test_i2c_riscv.c new file mode 100644 index 000000000..193e34d58 --- /dev/null +++ b/APP_Framework/Applications/app_test/test_i2c_riscv.c @@ -0,0 +1,81 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: test_i2c.c +* @brief: a application of i2c function +* @version: 1.1 +* @author: AIIT XUOS Lab +* @date: 2022/12/17 +*/ +#include +#include +#include +#include +#ifdef ADD_XIZI_FEATURES + +#define I2C_SLAVE_ADDRESS (0x44U) + +void TestI2C(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("IIC open successful!\n"); + + // init iic + uint16 iic_address = I2C_SLAVE_ADDRESS; + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = I2C_TYPE; + ioctl_cfg.args = (void *)&iic_address; + + if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg)) + { + printf("ioctl iic fd error %d\n", iic_fd); + PrivClose(iic_fd); + return; + } + printf("IIC configure successful!\n"); + + // I2C read and write + uint8_t data[32]; + + while (1) + { + PrivWrite(iic_fd, NONE, 0); + msleep(40); + PrivRead(iic_fd, data, 4); + + float result = ((data[2] << 8 | data[3]) >> 2) * 165.0 /( (1 << 14) - 1) - 40.0; + int temperature = result*10; + printf("Temperature : %d.%d ℃\n", temperature/10, temperature%10); + + result = ((data[0] << 8 | data[1] ) & 0x3fff) * 100.0 / ( (1 << 14) - 1); + int humidity = result*10; + printf("Humidity : %d.%d %%RH\n", humidity/10, humidity%10); + + printf("HS300X origin data1:0x%2x%2x%2x%2x\n", data[0],data[1],data[2],data[3]); + + msleep(1000); + } + + PrivClose(iic_fd); + return; +} + +PRIV_SHELL_CMD_FUNCTION(TestI2C, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR); +#endif \ No newline at end of file