feat: 添加I2C Master的BUS功能

This commit is contained in:
Alvin Young 2025-03-24 17:04:51 +08:00
parent 3bd1e07432
commit 0013e8d9ed
5 changed files with 258 additions and 37 deletions

View File

@ -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;
}

View File

@ -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"

View File

@ -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
*
* _ _
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
* /_/ /___/ /___/
* @copyright Copyright (c) 2025 impressionyang
*
* @par :
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2025-03-24 <td>v1.0 <td>Alvin Young <td>
* </table>
*
*/
/* Define to prevent recursive inclusion -------------------------------------*/
/* Includes ------------------------------------------------------------------*/
#include "imp_esp_i2c_master_dev_op.h"
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/i2c_master.h"
#include <stdlib.h>
/* 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
*/

View File

@ -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
*
* _ _
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
* /_/ /___/ /___/
* @copyright Copyright (c) 2025 impressionyang
*
* @par :
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2025-03-24 <td>v1.0 <td>Alvin Young <td>
* </table>
*
*/
/* 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 <stdint.h>
#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 :
* <table>
* <tr><th>Date <th>Author <th>Description
* <tr><td>2025-03-24 <td>Alvin Young <td>
* </table>
*/
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
*/

View File

@ -24,7 +24,8 @@
#include <stdio.h>
#include <stdint.h>
#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 <freertos/FreeRTOS.h>
@ -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);
}
/**