Move I2C into a separate component.

This commit is contained in:
Shaun Reed 2025-11-01 16:46:15 -04:00
parent 7d87b35b1c
commit 83473c4899
11 changed files with 2340 additions and 98 deletions

4
esp/cpp/08_dht11-lcd/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
managed_components
dependencies.lock
sdkconfig.old

View File

@ -1,7 +1,6 @@
idf_component_register(
SRCS
main.cpp
main.h i2c.h
main.h
INCLUDE_DIRS .
REQUIRES driver
)

View File

@ -1,3 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
idf: '>=5.3.0'
idf: '>=5.3.0'
i2c:
path: ../../components/i2c

View File

@ -13,10 +13,8 @@
#define PIN_SCL GPIO_NUM_22
#define PIN_RST (-1)
I2C i2c(PIN_SDA, PIN_SCL, PIN_RST);
extern "C" void app_main(void)
{
init_i2c(PIN_SDA, PIN_SCL);
}

View File

@ -0,0 +1,13 @@
# Components
These are examples of creating custom components for use in local projects or eventually published to the ESP-IDF component registry.
Official component examples can be found in the [ESP-BSP project](https://github.com/espressif/esp-bsp/tree/master/components).
The `idf.py` tool can be used to create a new component:
```bash
source /path/to/esp-idf/export.sh
idf.py create-component my-component
```

View File

@ -1,92 +0,0 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef I2C_H
#define I2C_H
#define I2C_BUS_PORT 0
#include <esp_log.h>
#include <driver/i2c_master.h>
/**
* Encapsulates ESP I2C creation and usage.
*/
struct I2C {
/**
* Construct and initialize an ESP I2C master bus.
* An I2C constructor may only be called one time in any application.
*
* @param sda GPIO pin number for SDA
* @param scl GPIO pin number for SCL
* @param rst GPIO pin number for RST
*/
I2C(gpio_num_t sda, gpio_num_t scl, int rst = -1) :
I2C((i2c_master_bus_config_t) {
.i2c_port = I2C_BUS_PORT,
.sda_io_num = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags {
.enable_internal_pullup = true,
},
},
rst
) { }
/**
* Construct an ESP I2C master bus given a specific ESP I2C configuration.
* An I2C constructor may only be called one time in any application.
*
* @param config ESP I2C master bus configuration.
* @param rst GPIO pin number for RST
*/
explicit I2C(i2c_master_bus_config_t config, int rst = -1) :
esp_bus_config_(config),
rst_num_(rst)
{
i2c_master_bus_handle_t i2c;
ESP_LOGI(TAG, "Initializing new master I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&esp_bus_config_, &i2c));
}
~I2C() = default;
//
// GETTERS
/**
* ESP I2C master bus handle getter.
* This will fail if an I2C instance was never constructed.
*/
static i2c_master_bus_handle_t get()
{
i2c_master_bus_handle_t i2c = nullptr;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &i2c));
return i2c;
}
//
// PUBLIC MEMBERS
/// ESP I2C master bus configuration used during initialization.
i2c_master_bus_config_t esp_bus_config_;
/// RST GPIO pin number.
int rst_num_;
private:
//
// PRIVATE MEMBERS
/// Tag used for ESP logging.
constexpr static const char *TAG = "I2C";
};
#endif //I2C_H

View File

@ -0,0 +1,5 @@
idf_component_register(
SRCS "i2c.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES "driver"
)

View File

@ -0,0 +1,2 @@
#include <stdio.h>
#include "i2c.h"

View File

@ -0,0 +1,5 @@
version: "0.0.1"
description: ESP I2C helper component
url: https://git.shaunreed.com/shaunrd0/klips/tree/master/esp/cpp/components/i2c
dependencies:
idf: ">=5.3"

View File

@ -0,0 +1,66 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
#ifndef I2C_H
#define I2C_H
#define I2C_BUS_PORT 0
#include <esp_log.h>
#include <driver/i2c_master.h>
/// Tag used for ESP logging.
static const char* TAG = "I2C component";
/**
* Construct an ESP I2C master bus given a specific ESP I2C configuration.
* An I2C constructor may only be called one time in any application.
*
* @param config ESP I2C master bus configuration.
*/
inline void init_config_i2c(const i2c_master_bus_config_t config)
{
i2c_master_bus_handle_t i2c;
ESP_LOGI(TAG, "Initializing new master I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&config, &i2c));
}
/**
* Construct and initialize an ESP I2C master bus.
* An I2C constructor may only be called one time in any application.
*
* @param sda GPIO pin for SDA
* @param scl GPIO pin for SCL
*/
inline void init_i2c(gpio_num_t sda, gpio_num_t scl)
{
return init_config_i2c((i2c_master_bus_config_t){
.i2c_port = I2C_BUS_PORT,
.sda_io_num = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags = {
.enable_internal_pullup = true,
},
}
);
}
/**
* ESP I2C master bus handle getter.
* This will fail if an I2C instance was never constructed.
*/
static i2c_master_bus_handle_t get()
{
i2c_master_bus_handle_t i2c = NULL;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &i2c));
return i2c;
}
#endif //I2C_H

File diff suppressed because it is too large Load Diff