2#include "freertos/FreeRTOS.h"
3#include "freertos/task.h"
6#include "esp_rom_sys.h"
10static const char *
TAG =
"i2c_u8g2";
13#define I2C_MASTER_NUM CONFIG_I2C_U8G2_I2C_PORT
14#define I2C_MASTER_SDA_IO CONFIG_I2C_U8G2_SDA_GPIO
15#define I2C_MASTER_SCL_IO CONFIG_I2C_U8G2_SCL_GPIO
16#define I2C_FREQ_HZ CONFIG_I2C_U8G2_FREQ_HZ
17#define I2C_DISPLAY_ADDRESS CONFIG_I2C_U8G2_DISPLAY_ADDRESS
18#define I2C_PROBE_TIMEOUT_MS_MAX 100U
20#ifdef CONFIG_I2C_U8G2_INTERNAL_PULLUP
21#define I2C_INTERNAL_PULLUP true
23#define I2C_INTERNAL_PULLUP false
36 uint16_t probe_timeout_ms)
38 size_t found_count = 0;
40 for (uint16_t address = 0x08; address <= 0x77; ++address) {
41 if (i2c_master_probe(
s_i2c_bus_handle, address, probe_timeout_ms) == ESP_OK) {
42 if (found_count < max_found) {
43 found_addresses[found_count] = (uint8_t)address;
56 char buffer[96] = {0};
59 if (total_count == 0) {
60 ESP_LOGW(
TAG,
"I2C scan found no responding devices");
64 for (
size_t i = 0; i < stored_count; ++i) {
65 int wrote = snprintf(buffer + offset,
sizeof(buffer) - offset,
66 (i == 0) ?
"0x%02X" :
", 0x%02X",
68 if (wrote <= 0 || (
size_t)wrote >= (
sizeof(buffer) - offset)) {
71 offset += (size_t)wrote;
74 if (total_count > stored_count && offset + 4 <
sizeof(buffer)) {
75 snprintf(buffer + offset,
sizeof(buffer) - offset,
", ...");
78 ESP_LOGW(
TAG,
"I2C scan detected %u device(s): %s",
79 (
unsigned int)total_count,
80 buffer[0] !=
'\0' ? buffer :
"(list truncated)");
84 uint16_t probe_timeout_ms,
85 uint8_t *resolved_address)
87 uint8_t alternate_address;
90 probe_ret = i2c_master_probe(
s_i2c_bus_handle, preferred_address, probe_timeout_ms);
91 if (probe_ret == ESP_OK) {
92 *resolved_address = preferred_address;
96 if (probe_ret == ESP_ERR_TIMEOUT) {
100 if (preferred_address == 0x3C) {
101 alternate_address = 0x3D;
102 }
else if (preferred_address == 0x3D) {
103 alternate_address = 0x3C;
105 return ESP_ERR_NOT_FOUND;
108 probe_ret = i2c_master_probe(
s_i2c_bus_handle, alternate_address, probe_timeout_ms);
109 if (probe_ret == ESP_OK) {
111 "Configured display address 0x%02X not found, using 0x%02X",
114 *resolved_address = alternate_address;
131 static uint8_t buf_idx;
134 case U8X8_MSG_BYTE_INIT: {
135 i2c_device_config_t dev_config = {
136 .dev_addr_length = I2C_ADDR_BIT_LEN_7,
140 .flags.disable_ack_check =
false,
146 ESP_LOGE(
TAG,
"Failed to add display device to I2C bus (%s)", esp_err_to_name(ret));
151 if (handle != NULL) {
155 ESP_LOGI(
TAG,
"I2C master driver initialized successfully");
159 case U8X8_MSG_BYTE_START_TRANSFER:
163 case U8X8_MSG_BYTE_SET_DC:
166 case U8X8_MSG_BYTE_SEND:
167 for (
size_t i = 0; i < arg_int; ++i) {
168 if (buf_idx >=
sizeof(buffer)) {
169 ESP_LOGE(
TAG,
"U8G2 transfer buffer overflow (%u >= %u)",
170 (
unsigned int)buf_idx,
171 (
unsigned int)
sizeof(buffer));
174 buffer[buf_idx++] = *((uint8_t *)arg_ptr + i);
178 case U8X8_MSG_BYTE_END_TRANSFER:
187 ESP_LOGW(
TAG,
"Display I2C transfer failed, suppressing further updates (%s)",
188 esp_err_to_name(ret));
202 uint8_t arg_int,
void *arg_ptr)
208 case U8X8_MSG_GPIO_AND_DELAY_INIT:
209 ESP_LOGI(
TAG,
"GPIO and delay initialization completed");
212 case U8X8_MSG_DELAY_MILLI:
213 vTaskDelay(pdMS_TO_TICKS(arg_int));
216 case U8X8_MSG_DELAY_10MICRO:
217 esp_rom_delay_us(arg_int * 10);
220 case U8X8_MSG_DELAY_100NANO:
221 __asm__ __volatile__(
"nop");
224 case U8X8_MSG_DELAY_I2C:
228 esp_rom_delay_us(5 / arg_int);
231 case U8X8_MSG_GPIO_RESET:
260 uint8_t resolved_address;
261 uint16_t probe_timeout_ms;
263 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG,
TAG,
"handle is NULL");
264 ESP_RETURN_ON_FALSE(config != NULL, ESP_ERR_INVALID_ARG,
TAG,
"config is NULL");
265 ESP_RETURN_ON_FALSE(config->
timeout_ms > 0, ESP_ERR_INVALID_ARG,
TAG,
"timeout_ms must be > 0");
266 ESP_RETURN_ON_FALSE(config->
scl_speed_hz > 0, ESP_ERR_INVALID_ARG,
TAG,
"scl_speed_hz must be > 0");
268 "device_address must be 7-bit");
270 memset(handle, 0,
sizeof(*handle));
284 i2c_master_bus_config_t bus_config = {
288 .clk_source = I2C_CLK_SRC_DEFAULT,
289 .glitch_ignore_cnt = 7,
291 .trans_queue_depth = 0,
296 TAG,
"Failed to create I2C master bus");
301 if (probe_ret != ESP_OK) {
302 if (probe_ret == ESP_ERR_NOT_FOUND) {
303 uint8_t found_addresses[12];
305 sizeof(found_addresses),
309 (total_found <
sizeof(found_addresses)) ? total_found :
sizeof(found_addresses),
311 }
else if (probe_ret == ESP_ERR_TIMEOUT) {
313 "I2C probe timed out; skipping full bus scan to avoid timeout spam");
316 "I2C probe failed before scan (%s)",
317 esp_err_to_name(probe_ret));
321 "No I2C display detected at 0x%02X (SDA=%d, SCL=%d, %uHz)",
328 return (probe_ret == ESP_ERR_NOT_FOUND) ? ESP_ERR_NOT_FOUND : probe_ret;
337 vTaskDelay(pdMS_TO_TICKS(10));
342 if (setup_fn == NULL) {
343 setup_fn = u8g2_Setup_ssd1315_i2c_128x64_noname_f;
346 setup_fn(&handle->
u8g2,
353 u8g2_InitDisplay(&handle->
u8g2);
354 u8g2_SetPowerSave(&handle->
u8g2, 0);
357 u8g2_ClearBuffer(&handle->
u8g2);
358 u8g2_SetFont(&handle->
u8g2, u8g2_font_6x10_tf);
359 u8g2_DrawStr(&handle->
u8g2, 0, 12,
"Init OK");
360 u8g2_SendBuffer(&handle->
u8g2);
361 ESP_LOGI(
TAG,
"Test draw completed");
369 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG,
TAG,
"handle is NULL");
373 TAG,
"Failed to remove display device");
380 TAG,
"Failed to delete I2C master bus");
399 return &handle->
u8g2;
404 ESP_RETURN_ON_FALSE(handle != NULL, ESP_ERR_INVALID_ARG,
TAG,
"handle is NULL");
405 ESP_RETURN_ON_FALSE(handle->
initialized, ESP_ERR_INVALID_STATE,
TAG,
"display not initialized");
407 u8g2_SetPowerSave(&handle->
u8g2, enable ? 1 : 0);
413 ESP_LOGI(
TAG,
"Geometric Shapes");
415 u8g2_ClearBuffer(u8g2);
418 u8g2_SetFont(u8g2, u8g2_font_ncenB08_tr);
419 u8g2_DrawStr(u8g2, 35, 12,
"Shapes");
422 u8g2_DrawBox(u8g2, 10, 20, 20, 15);
425 u8g2_DrawFrame(u8g2, 35, 20, 20, 15);
428 u8g2_DrawCircle(u8g2, 70, 27, 8, U8G2_DRAW_ALL);
431 u8g2_DrawDisc(u8g2, 95, 27, 6, U8G2_DRAW_ALL);
434 u8g2_DrawLine(u8g2, 10, 45, 50, 45);
435 u8g2_DrawLine(u8g2, 10, 50, 30, 60);
436 u8g2_DrawLine(u8g2, 30, 50, 50, 60);
439 u8g2_DrawTriangle(u8g2, 70, 45, 85, 60, 55, 60);
441 u8g2_SendBuffer(u8g2);
#define CONFIG_I2C_U8G2_TIMEOUT_MS
#define I2C_MASTER_SCL_IO
i2c_u8g2_config_t i2c_u8g2_config_default(void)
Fill a config with menuconfig-backed defaults.
static bool s_display_available
static i2c_master_dev_handle_t s_display_dev_handle
static uint8_t s_device_address
static uint8_t i2c_u8g2_byte_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
static void i2c_u8g2_log_scan_results(const uint8_t *found_addresses, size_t stored_count, size_t total_count)
static esp_err_t i2c_u8g2_resolve_display_address(uint8_t preferred_address, uint16_t probe_timeout_ms, uint8_t *resolved_address)
esp_err_t i2c_u8g2_deinit(i2c_u8g2_handle_t *handle)
Deinitialize display device and I2C bus.
#define I2C_PROBE_TIMEOUT_MS_MAX
u8g2_t * i2c_u8g2_get_u8g2(i2c_u8g2_handle_t *handle)
Get the U8G2 object for draw operations.
static i2c_u8g2_handle_t * i2c_u8g2_handle_from_u8x8(u8x8_t *u8x8)
#define I2C_INTERNAL_PULLUP
static size_t i2c_u8g2_scan_bus(uint8_t *found_addresses, size_t max_found, uint16_t probe_timeout_ms)
static uint16_t s_timeout_ms
#define I2C_DISPLAY_ADDRESS
static uint32_t s_scl_speed_hz
void demo_shapes(u8g2_t *u8g2)
esp_err_t i2c_u8g2_init(i2c_u8g2_handle_t *handle, const i2c_u8g2_config_t *config)
Initialize I2C bus and U8G2 display.
esp_err_t i2c_u8g2_set_power_save(i2c_u8g2_handle_t *handle, bool enable)
Set display power save mode.
static i2c_u8g2_handle_t * s_active_handle
static i2c_master_bus_handle_t s_i2c_bus_handle
static uint8_t i2c_u8g2_gpio_and_delay_cb(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
#define I2C_MASTER_SDA_IO
Internal I2C + U8G2 display integration APIs.
void(* i2c_u8g2_setup_fn_t)(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb)
U8G2 setup callback signature.
#define I2C_U8G2_TX_BUFFER_SIZE
Configuration for I2C-backed U8G2 display initialization.
const u8g2_cb_t * rotation
i2c_u8g2_setup_fn_t setup_fn
bool enable_internal_pullup
Display handle containing runtime driver state.
i2c_master_bus_handle_t i2c_bus_handle
i2c_master_dev_handle_t display_dev_handle