From 1a7cd8b43f1656b90ff98b8d84f8ea8fe06e6d78 Mon Sep 17 00:00:00 2001 From: ZXY_LINUX <1308465141@qq.com> Date: Wed, 29 Mar 2023 11:52:54 +0800 Subject: [PATCH] =?UTF-8?q?[driver]=E6=B7=BB=E5=8A=A0ESP32=5FC3=20I2C?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/ESP32_C3/drivers/Kconfig | 13 ++- bsp/ESP32_C3/drivers/drv_hw_i2c.c | 133 ++++++++++++++++++++++++++++++ bsp/ESP32_C3/drivers/drv_hw_i2c.h | 32 +++++++ bsp/ESP32_C3/rtconfig.h | 17 +++- 4 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 bsp/ESP32_C3/drivers/drv_hw_i2c.c create mode 100644 bsp/ESP32_C3/drivers/drv_hw_i2c.h diff --git a/bsp/ESP32_C3/drivers/Kconfig b/bsp/ESP32_C3/drivers/Kconfig index 20e042af38..a256fa1abf 100644 --- a/bsp/ESP32_C3/drivers/Kconfig +++ b/bsp/ESP32_C3/drivers/Kconfig @@ -1,6 +1,6 @@ config SOC_ESPRESSIF - bool + bool config SOC_ESP32_C3 bool @@ -24,7 +24,7 @@ choice bool "LUATOS ESP32C3 board" config BSP_BOARD_HX_EXP32C3 - bool "HONGXU ESP32C3 board" + bool "HONGXU ESP32C3 board" endchoice menu "Onboard Peripheral Drivers" @@ -57,12 +57,17 @@ menu "On-chip Peripheral Drivers" bool "Enable GPIO" select RT_USING_PIN default y - + config BSP_USING_UART - bool "Enable UART" + bool "Enable UART" select RT_USING_SERIAL select RT_USING_SERIAL_V1 default y + + config BSP_USING_I2C0 + bool "Enable I2C0" + select RT_USING_I2C + default n endmenu endmenu diff --git a/bsp/ESP32_C3/drivers/drv_hw_i2c.c b/bsp/ESP32_C3/drivers/drv_hw_i2c.c new file mode 100644 index 0000000000..031b8c1174 --- /dev/null +++ b/bsp/ESP32_C3/drivers/drv_hw_i2c.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + *2023-03-28 Zxy first version + */ +#ifdef RT_USING_I2C +#include "drv_hw_i2c.h" +#include "driver/i2c.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/components/driver/include/driver/i2c.h +#include "hal/i2c_types.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/tools/mocks/hal/include/hal/i2c_types.h +#include "esp_err.h" +//#include "portmacro.h"//bsp/ESP32_C3/packages/FreeRTOS_Wrapper-latest/FreeRTOS/portable/rt-thread/portmacro.h + +struct esp32_i2c +{ + struct rt_i2c_bus_device bus; + + i2c_config_t *base; + + char *device_name; +}; + +// #if defined(BSP_USING_I2C0) +static struct esp32_i2c i2c0 = {0}; +// #endif + +static rt_size_t _master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num) +{ + i2c_cmd_handle_t cmd;//创建流程 + rt_size_t ret = (0); + rt_uint32_t index = 0; + // struct esp32_i2c *esp32_i2c = RT_NULL; + struct rt_i2c_msg *msg = RT_NULL; + i2c_rw_t direction;//w-0 r-1 + esp_err_t result = ESP_OK; + + RT_ASSERT(bus != RT_NULL); + + // esp32_i2c = (struct esp32_i2c *)bus; + + for(index = 0; index < num; index++) + { + msg = &msgs[index]; + direction = ((msg->flags & RT_I2C_RD) ? I2C_MASTER_READ : I2C_MASTER_WRITE); + + if (!(msg->flags & RT_I2C_NO_START)) + { + /* Start condition and slave address. */ + cmd = i2c_cmd_link_create();//创建流程 + i2c_master_start(cmd);//启动流程录入 + result = i2c_master_write_byte(cmd, msg->addr << 1 | WRITE_BIT, ACK_CHECK_EN);//发送起始信号和从设备地址 + i2c_master_stop(cmd);//流程录入完毕 + ret = i2c_master_cmd_begin(I2C_NUMBER(0), cmd, 1000 / portTICK_PERIOD_MS);//执行流程 + i2c_cmd_link_delete(cmd);//删除流程任务 + if (ret != ESP_OK) return ret; + } + + if (result == ESP_OK) + { + if (direction == I2C_MASTER_WRITE) + { + /* Transmit data. */ + cmd = i2c_cmd_link_create();//创建流程 + i2c_master_start(cmd);//启动流程录入 + result = i2c_master_write_byte(cmd, msg->buf, ACK_CHECK_EN); + i2c_master_stop(cmd);//流程录入完毕 + ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);//执行流程 + i2c_cmd_link_delete(cmd);//删除流程任务 + } + else + { + /* Receive Data. */ + cmd = i2c_cmd_link_create();//创建流程 + i2c_master_start(cmd);//启动流程录入 + result = i2c_master_read_byte(cmd, msg->buf, ACK_VAL); + i2c_master_stop(cmd);//流程录入完毕 + ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);//执行流程 + i2c_cmd_link_delete(cmd);//删除流程任务 + } + } + } + + if (result == ESP_OK) + { + ret = index; + } + + return ret; +} + +static rt_size_t _slave_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg msgs[], rt_uint32_t num) +{ + return -RT_ENOSYS; +} + +static rt_err_t _i2c_bus_control(struct rt_i2c_bus_device *bus, rt_uint32_t cmd, rt_uint32_t arg) +{ + return -RT_EINVAL; +} + +static const struct rt_i2c_bus_device_ops i2c_ops = +{ + _master_xfer, + _slave_xfer, + _i2c_bus_control, +}; + +int rt_hw_i2c_init(void) +{ + i2c0.base = &i2c0; + i2c0.device_name = "i2c0"; + i2c0.bus.ops = &i2c_ops; + int i2c_master_port = I2C_MASTER_NUM;//iic0 + i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = I2C_MASTER_SDA_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_io_num = I2C_MASTER_SCL_IO, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = 100000, + // .clk_flags = 0, /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */ + }; + + i2c_param_config(i2c_master_port, &conf);//配置完成 + i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);// I2C 设备的初始化 + rt_i2c_bus_device_register(&i2c0.bus, i2c0.device_name); + return RT_EOK; +} +INIT_BOARD_EXPORT(rt_hw_i2c_init); +#endif /* RT_USING_I2C */ \ No newline at end of file diff --git a/bsp/ESP32_C3/drivers/drv_hw_i2c.h b/bsp/ESP32_C3/drivers/drv_hw_i2c.h new file mode 100644 index 0000000000..de2e8a39a6 --- /dev/null +++ b/bsp/ESP32_C3/drivers/drv_hw_i2c.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + *2023-03-28 Zxy first version + */ +#ifndef __DRV_HW_I2C_H__ +#define __DRV_HW_I2C_H__ + +#include +#include +#include "sdkconfig.h" + +#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */ +#define READ_BIT I2C_MASTER_READ /*!< I2C master read */ +#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ +#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ +#define ACK_VAL 0x0 /*!< I2C ack value */ +#define NACK_VAL 0x1 /*!< I2C nack value */ +#define _I2C_NUMBER(num) I2C_NUM_##num +#define I2C_NUMBER(num) _I2C_NUMBER(num) +#define I2C_MASTER_NUM I2C_NUMBER(0) /*!< I2C port number for master dev */ +#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL /*!< gpio number for I2C master clock */ +#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA /*!< gpio number for I2C master data */ +#define CONFIG_I2C_MASTER_SCL 6 +#define CONFIG_I2C_MASTER_SDA 5 +int rt_hw_i2c_init(void); + +#endif /* __DRV_HW_I2C_H__ */ diff --git a/bsp/ESP32_C3/rtconfig.h b/bsp/ESP32_C3/rtconfig.h index a0dfe5c1c0..3b44a0e338 100644 --- a/bsp/ESP32_C3/rtconfig.h +++ b/bsp/ESP32_C3/rtconfig.h @@ -7,7 +7,7 @@ /* RT-Thread Kernel */ #define RT_NAME_MAX 8 -#define RT_ALIGN_SIZE 8 +#define RT_ALIGN_SIZE 4 #define RT_THREAD_PRIORITY_32 #define RT_THREAD_PRIORITY_MAX 32 #define RT_TICK_PER_SECOND 1000 @@ -65,6 +65,7 @@ /* Device Drivers */ #define RT_USING_DEVICE_IPC +#define RT_UNAMED_PIPE_NUMBER 64 #define RT_USING_SERIAL #define RT_USING_SERIAL_V1 #define RT_SERIAL_RB_BUFSZ 64 @@ -130,9 +131,6 @@ /* u8g2: a monochrome graphic library */ -/* PainterEngine: A cross-platform graphics application framework written in C language */ - - /* tools packages */ @@ -154,6 +152,11 @@ /* peripheral libraries and drivers */ +/* sensors drivers */ + + +/* touch drivers */ + #define PKG_USING_ESP_IDF #define PKG_USING_ESP_IDF_LATEST_VERSION @@ -163,6 +166,9 @@ /* AI packages */ +/* Signal Processing and Control Algorithm Packages */ + + /* miscellaneous packages */ /* project laboratory */ @@ -195,10 +201,13 @@ /* Communication */ + /* Device Control */ + /* Other */ + /* Signal IO */ -- GitLab