From 0013e8d9edc98a5a22aaea48df0ede40ef7b271d Mon Sep 17 00:00:00 2001 From: impressionyang Date: Mon, 24 Mar 2025 17:04:51 +0800 Subject: [PATCH] =?UTF-8?q?feat=E2=9C=A8:=20=E6=B7=BB=E5=8A=A0I2C=20Master?= =?UTF-8?q?=E7=9A=84BUS=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/APP/main_app/main_app.c | 2 +- main/CMakeLists.txt | 2 + .../imp_esp_i2c_master_dev_op.c | 122 ++++++++++++++++++ .../imp_esp_i2c_master_dev_op.h | 90 +++++++++++++ main/utilities/u8g2/port/imp_ug82_port.c | 79 ++++++------ 5 files changed, 258 insertions(+), 37 deletions(-) create mode 100644 main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.c create mode 100644 main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.h diff --git a/main/APP/main_app/main_app.c b/main/APP/main_app/main_app.c index 4b024c0..32daacb 100644 --- a/main/APP/main_app/main_app.c +++ b/main/APP/main_app/main_app.c @@ -107,7 +107,7 @@ uint8_t _main_task_wake_up_services() imp_main_task_table[IMP_TASK_ID_EXT_TRANS_SERVICE_TASK], 2048, NULL, 10, NULL); xTaskCreate(imp_display_service_task, - imp_main_task_table[IMP_TASK_ID_DISP_SERVICE_TASK], 2048, NULL, + imp_main_task_table[IMP_TASK_ID_DISP_SERVICE_TASK], 8192, NULL, 11, NULL); return 0; } diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 2b167b8..d04294d 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -15,6 +15,7 @@ idf_component_register( "utilities/u8g2/port" "services/display_service" "components/screen_pages/test_page" + "components/imp_esp_i2c_master_dev_op" EXCLUDE_SRCS "utilities/imp_util_ring_queue/ring_queue_test.c" @@ -36,6 +37,7 @@ idf_component_register( "utilities/u8g2/port" "services/display_service" "components/screen_pages/test_page" + "components/imp_esp_i2c_master_dev_op" LDFRAGMENTS "utilities/letter_shell/port/esp-idf/shell.lf" diff --git a/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.c b/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.c new file mode 100644 index 0000000..988662e --- /dev/null +++ b/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.c @@ -0,0 +1,122 @@ +/** + * @file imp_esp_i2c_master_dev_op.c + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2025-03-24 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2025-03-24 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ +#include "imp_esp_i2c_master_dev_op.h" +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "driver/i2c_master.h" +#include +/* define --------------------------------------------------------------------*/ +#define I2C_BUS_0_SCL_PIN 34 +#define I2C_BUS_0_SDA_PIN 33 +/* typedef -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ + +static uint8_t is_bus_0_inited = 0; + +i2c_master_bus_handle_t bus0_handle; +static imp_esp_i2c_master_dev_op_t i2c_bus_0_handle = { 0 }; +/* Private function(only *.c) -----------------------------------------------*/ + +void _imp_esp_i2c_master_dev_op_bus_init( + imp_esp_i2c_master_dev_op_port_num_e port) +{ + if (port == EM_IMP_ESP_I2C_PORT_0) { + i2c_master_bus_config_t bus_config = { + .i2c_port = I2C_NUM_0, + .sda_io_num = I2C_BUS_0_SDA_PIN, + .scl_io_num = I2C_BUS_0_SCL_PIN, + .clk_source = I2C_CLK_SRC_DEFAULT, + .glitch_ignore_cnt = 7, + .flags.enable_internal_pullup = true, + }; + i2c_new_master_bus(&bus_config, &bus0_handle); + } +} + +void _imp_esp_i2c_master_dev_op_handle_init(imp_esp_i2c_master_dev_op_t* handle) +{ + uint8_t bit_len = handle->chip_addr_is_7bit == 1 ? 0 : 1; + i2c_device_config_t dev_config = { + .dev_addr_length = bit_len, + .device_address = handle->chip_addr, + .scl_speed_hz = handle->bus_speed, + }; + i2c_master_bus_add_device(handle->bus_handle, &dev_config, + &(handle->dev_handle)); +} + +/* Exported functions --------------------------------------------------------*/ +uint8_t +imp_esp_i2c_master_dev_op_init(imp_esp_i2c_master_dev_op_t* handle, + imp_esp_i2c_master_dev_op_port_num_e port, + uint8_t chip_addr_is_7bit, uint16_t chip_addr, + uint32_t bus_speed) +{ + handle->port = port; + handle->chip_addr_is_7bit = chip_addr_is_7bit; + handle->chip_addr = chip_addr; + handle->bus_speed = bus_speed; + + switch (port) { + case EM_IMP_ESP_I2C_PORT_0: + if (!is_bus_0_inited) { + _imp_esp_i2c_master_dev_op_bus_init(EM_IMP_ESP_I2C_PORT_0); + is_bus_0_inited = 1; + } + handle->bus_handle = bus0_handle; + _imp_esp_i2c_master_dev_op_handle_init(handle); + break; + default: + break; + } + return 0; +} +uint8_t imp_esp_i2c_master_dev_op_read(imp_esp_i2c_master_dev_op_t* handle, + uint8_t* reg, uint16_t reg_len, + uint8_t* data, uint16_t data_len) +{ + if (NULL == handle->bus_handle || NULL == handle->dev_handle) { + return 0; + } + esp_err_t err = + i2c_master_transmit_receive(handle->dev_handle, reg, reg_len, data, + data_len, 1000 / portTICK_PERIOD_MS); + + return 0; +} +uint8_t imp_esp_i2c_master_dev_op_write(imp_esp_i2c_master_dev_op_t* handle, + uint8_t* data, uint16_t data_len) +{ + if (NULL == handle->bus_handle || NULL == handle->dev_handle) { + return 0; + } + esp_err_t err = i2c_master_transmit(handle->dev_handle, data, data_len, + 1000 / portTICK_PERIOD_MS); + return 0; +} +/* + * EOF + */ \ No newline at end of file diff --git a/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.h b/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.h new file mode 100644 index 0000000..8470fd5 --- /dev/null +++ b/main/components/imp_esp_i2c_master_dev_op/imp_esp_i2c_master_dev_op.h @@ -0,0 +1,90 @@ +/** + * @file imp_esp_i2c_master_dev_op_master_dev_op.h + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2025-03-24 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2025-03-24 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __IMP_ESP_I2C_MASTER_DEV_OP_H__ +#define __IMP_ESP_I2C_MASTER_DEV_OP_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include +#include "driver/i2c_master.h" +/* define --------------------------------------------------------------------*/ +/* typedef -------------------------------------------------------------------*/ +typedef enum __imp_esp_i2c_master_dev_op_port_num_e__ +{ + EM_IMP_ESP_I2C_PORT_0 = 0x00, + EM_IMP_ESP_I2C_PORT_1, + EM_IMP_ESP_I2C_PORT_2, +} imp_esp_i2c_master_dev_op_port_num_e; + +typedef struct __imp_esp_i2c_master_dev_op_t__ +{ + i2c_port_t port; + uint8_t is_dev_init; + uint8_t chip_addr_is_7bit; + uint16_t chip_addr; + uint32_t bus_speed; + i2c_master_dev_handle_t dev_handle; + i2c_master_bus_handle_t bus_handle; +} imp_esp_i2c_master_dev_op_t; +/* variables -----------------------------------------------------------------*/ +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ +uint8_t + imp_esp_i2c_master_dev_op_init(imp_esp_i2c_master_dev_op_t* handle, + imp_esp_i2c_master_dev_op_port_num_e port, + uint8_t chip_addr_is_7bit, uint16_t chip_addr, + uint32_t bus_speed); +uint8_t imp_esp_i2c_master_dev_op_read(imp_esp_i2c_master_dev_op_t* bus, + uint8_t* reg, uint16_t reg_len, + uint8_t* data, uint16_t data_len); + +/** + * + * @brief reg in data + * @param [in] bus + * @param [in] data + * @param [in] data_len + * @return uint8_t + * @date 2025-03-24 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2025-03-24 Alvin Young 新建 + *
+ */ +uint8_t imp_esp_i2c_master_dev_op_write(imp_esp_i2c_master_dev_op_t* bus, + uint8_t* data, uint16_t data_len); +#ifdef __cplusplus +} +#endif +#endif //__IMP_ESP_I2C_MASTER_DEV_OP_H__ + /* + * EOF + */ \ No newline at end of file diff --git a/main/utilities/u8g2/port/imp_ug82_port.c b/main/utilities/u8g2/port/imp_ug82_port.c index 985f253..75d98aa 100644 --- a/main/utilities/u8g2/port/imp_ug82_port.c +++ b/main/utilities/u8g2/port/imp_ug82_port.c @@ -24,7 +24,8 @@ #include #include #include "esp_log.h" -#include "driver/i2c.h" +// #include "driver/i2c.h" +#include "imp_esp_i2c_master_dev_op.h" #include "u8g2.h" #include "u8x8.h" #include @@ -42,14 +43,16 @@ /* typedef -------------------------------------------------------------------*/ /* variables -----------------------------------------------------------------*/ -i2c_config_t i2c_config = { - .mode = I2C_MODE_MASTER, // set master mod - .sda_io_num = I2C_SDA_IO, // set sda pin - .scl_io_num = I2C_SCL_IO, // set scl pin - .sda_pullup_en = GPIO_PULLUP_ENABLE, // pull up mode - .scl_pullup_en = GPIO_PULLUP_ENABLE, // pull up mode - .master.clk_speed = 4000000 // set speed at 100KHz -}; +// i2c_config_t i2c_config = { +// .mode = I2C_MODE_MASTER, // set master mod +// .sda_io_num = I2C_SDA_IO, // set sda pin +// .scl_io_num = I2C_SCL_IO, // set scl pin +// .sda_pullup_en = GPIO_PULLUP_ENABLE, // pull up mode +// .scl_pullup_en = GPIO_PULLUP_ENABLE, // pull up mode +// .master.clk_speed = 4000000 // set speed at 100KHz +// }; + +imp_esp_i2c_master_dev_op_t i2c_oled_handle; /* Private function(only *.c) -----------------------------------------------*/ @@ -69,8 +72,10 @@ i2c_config_t i2c_config = { */ static void _imp_i2c_ssd_oled_init(void) { - i2c_param_config(I2C_NUM_0, &i2c_config); - i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); + // i2c_param_config(I2C_NUM_0, &i2c_config); + // i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); + imp_esp_i2c_master_dev_op_init(&i2c_oled_handle, EM_IMP_ESP_I2C_PORT_0, 1, + 0x3C, 4000000); } /* Exported functions --------------------------------------------------------*/ @@ -94,13 +99,15 @@ static void _imp_i2c_ssd_oled_init(void) */ void imp_i2c_ssd_oled_write(uint8_t addr, uint32_t idx, uint8_t* data) { - i2c_cmd_handle_t handler = i2c_cmd_link_create(); - i2c_master_start(handler); - i2c_master_write_byte(handler, addr | WRITE_BIT, ACK_CHECK_EN); - i2c_master_write(handler, data, idx, 2); - i2c_master_stop(handler); - i2c_master_cmd_begin(I2C_NUM_0, handler, pdMS_TO_TICKS(100)); - i2c_cmd_link_delete(handler); + // i2c_cmd_handle_t handler = i2c_cmd_link_create(); + // i2c_master_start(handler); + // i2c_master_write_byte(handler, addr | WRITE_BIT, ACK_CHECK_EN); + // i2c_master_write(handler, data, idx, 2); + // i2c_master_stop(handler); + // i2c_master_cmd_begin(I2C_NUM_0, handler, pdMS_TO_TICKS(100)); + // i2c_cmd_link_delete(handler); + // imp_esp_i2c_master_dev_op_write(&i2c_oled_handle, &addr, 1); + imp_esp_i2c_master_dev_op_write(&i2c_oled_handle, data, idx); } /** @@ -141,34 +148,34 @@ uint8_t imp_i2c_ssd_oled_8x8_gpio_delay(u8x8_t* u8x8, uint8_t msg, uint8_t u8x8_byte_i2c(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) { static uint8_t buffer - [32]; /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ + [32]; /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ static uint8_t buf_idx; uint8_t* data; // cdc_printf("i2c addr is 0x%02x\r\n", u8x8_GetI2CAddress(u8x8)); - + switch (msg) { case U8X8_MSG_BYTE_SEND: - data = (uint8_t*)arg_ptr; - while (arg_int > 0) { - buffer[buf_idx++] = *data; - data++; - arg_int--; - } - break; + data = (uint8_t*)arg_ptr; + while (arg_int > 0) { + buffer[buf_idx++] = *data; + data++; + arg_int--; + } + break; case U8X8_MSG_BYTE_INIT: - /* add your custom code to init i2c subsystem */ - break; + /* add your custom code to init i2c subsystem */ + break; case U8X8_MSG_BYTE_SET_DC: - /* ignored for i2c */ - break; + /* ignored for i2c */ + break; case U8X8_MSG_BYTE_START_TRANSFER: - buf_idx = 0; - break; + buf_idx = 0; + break; case U8X8_MSG_BYTE_END_TRANSFER: - imp_i2c_ssd_oled_write(u8x8_GetI2CAddress(u8x8), buf_idx, buffer); - break; + imp_i2c_ssd_oled_write(u8x8_GetI2CAddress(u8x8), buf_idx, buffer); + break; default: - return 0; + return 0; } return 1; }