HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
I2cComprehensiveTest.cpp File Reference

Comprehensive I2C testing suite for ESP32-C6 DevKit-M-1 (noexcept) More...

#include "base/BaseI2c.h"
#include "mcu/esp32/EspI2c.h"
#include "mcu/esp32/utils/EspTypes_I2C.h"
#include <algorithm>
#include <memory>
#include <vector>
#include "TestFramework.h"
#include "mcu/esp32/EspGpio.h"
Include dependency graph for I2cComprehensiveTest.cpp:

Macros

#define CREATE_TEST_BUS_INLINE(bus_var_name, mode_val)
 

Functions

hf_i2c_master_bus_config_t create_test_bus_config (hf_i2c_mode_t mode, uint32_t freq=STANDARD_FREQ, hf_i2c_clock_source_t clk_source=hf_i2c_clock_source_t::HF_I2C_CLK_SRC_DEFAULT, hf_i2c_glitch_filter_t glitch_filter=hf_i2c_glitch_filter_t::HF_I2C_GLITCH_FILTER_7_CYCLES, bool allow_power_down=false) noexcept
 
bool test_i2c_espidf_direct_api () noexcept
 Test I2C using ESP-IDF API directly (bypassing our wrapper)
 
bool test_i2c_espidf_wrapper_replica () noexcept
 Test I2C using EspI2cBus wrapper (replicating ESP-IDF direct test)
 
bool test_i2c_espidf_wrapper_replica_continuous () noexcept
 Test I2C using EspI2cBus wrapper continuously (stability test)
 
bool test_i2c_bus_initialization () noexcept
 
bool test_i2c_bus_deinitialization () noexcept
 
bool test_i2c_configuration_validation () noexcept
 
bool test_i2c_device_creation () noexcept
 
bool test_i2c_device_management () noexcept
 
bool test_i2c_device_probing () noexcept
 
bool test_i2c_bus_scanning () noexcept
 
bool test_i2c_write_operations () noexcept
 
bool test_i2c_read_operations () noexcept
 
bool test_i2c_write_read_operations () noexcept
 
bool test_i2c_error_handling () noexcept
 
bool test_i2c_timeout_handling () noexcept
 
bool test_i2c_multi_device_operations () noexcept
 
bool test_i2c_clock_speeds () noexcept
 
bool test_i2c_address_modes () noexcept
 
bool test_i2c_esp_specific_features () noexcept
 
bool test_i2c_thread_safety () noexcept
 
bool test_i2c_performance () noexcept
 
bool test_i2c_edge_cases () noexcept
 
bool test_i2c_power_management () noexcept
 
bool test_i2c_async_operations () noexcept
 
bool test_i2c_async_timeout_handling () noexcept
 
bool test_i2c_async_multiple_operations () noexcept
 
bool test_i2c_index_based_access () noexcept
 
bool test_i2c_sync_mode () noexcept
 
bool test_i2c_async_mode () noexcept
 
bool test_i2c_mode_switching () noexcept
 
bool test_i2c_basic_functionality () noexcept
 
bool test_i2c_probe_methods_comparison () noexcept
 
bool verify_device_functionality (BaseI2c *device) noexcept
 
void app_main (void)
 

Variables

static const char * TAG = "I2C_Test"
 
static constexpr bool ENABLE_CORE_TESTS
 
static constexpr bool ENABLE_OPERATION_TESTS
 
static constexpr bool ENABLE_ADVANCED_TESTS
 
static constexpr bool ENABLE_PERFORMANCE_TESTS = false
 
static constexpr bool ENABLE_SPECIALIZED_TESTS
 
static TestResults g_test_results
 
static constexpr i2c_port_t I2C_PORT_NUM = I2C_NUM_0
 
static constexpr hf_pin_num_t TEST_SDA_PIN = 21
 
static constexpr hf_pin_num_t TEST_SCL_PIN = 22
 
static constexpr uint16_t TEST_DEVICE_ADDR_1 = 0x48
 
static constexpr uint16_t TEST_DEVICE_ADDR_2 = 0x50
 
static constexpr uint16_t NONEXISTENT_ADDR = 0x7E
 
static constexpr uint32_t STANDARD_FREQ = 100000
 
static constexpr uint32_t FAST_FREQ = 400000
 
static constexpr uint32_t FAST_PLUS_FREQ = 1000000
 
static constexpr uint32_t TIMEOUT_VERY_FAST_MS = 100
 
static constexpr uint32_t TIMEOUT_FAST_MS = 100
 
static constexpr uint32_t TIMEOUT_STANDARD_MS = 100
 
static constexpr uint32_t TIMEOUT_MEDIUM_MS = 500
 
static constexpr uint32_t TIMEOUT_LONG_MS = 1000
 
static constexpr uint32_t TIMEOUT_VERY_LONG_MS = 2000
 
static constexpr uint32_t TIMEOUT_EXTENDED_MS = 10000
 

Detailed Description

Comprehensive I2C testing suite for ESP32-C6 DevKit-M-1 (noexcept)

This file contains comprehensive testing for the ESP I2C implementation including:

  • Bus initialization and configuration validation
  • Device creation and management
  • Data transfer operations (read/write/write-read)
  • Error handling and recovery
  • Bus scanning and device probing
  • Multi-device scenarios
  • ESP-specific features (clock sources, glitch filtering, power management)
  • Thread safety verification
  • Performance and timing tests
  • Edge cases and fault injection
Author
Nebiyu Tadesse
Date
2025

Macro Definition Documentation

◆ CREATE_TEST_BUS_INLINE

#define CREATE_TEST_BUS_INLINE ( bus_var_name,
mode_val )
Value:
hf_i2c_master_bus_config_t bus_config = {}; \
bus_config.i2c_port = I2C_PORT_NUM; \
bus_config.sda_io_num = TEST_SDA_PIN; \
bus_config.scl_io_num = TEST_SCL_PIN; \
bus_config.mode = mode_val; \
bus_config.trans_queue_depth = (mode_val == hf_i2c_mode_t::HF_I2C_MODE_ASYNC) ? 32 : 0; \
bus_config.intr_priority = 0; \
bus_config.flags.enable_internal_pullup = true; \
bus_config.flags.allow_pd = false; \
auto bus_var_name = std::make_unique<EspI2cBus>(bus_config); \
if (!bus_var_name->Initialize()) { \
ESP_LOGE(TAG, "Failed to initialize I2C bus"); \
return false; \
}
@ HF_I2C_GLITCH_FILTER_7_CYCLES
7 cycle filter
@ HF_I2C_MODE_ASYNC
Async mode: non-blocking operations only, with queue.
@ HF_I2C_CLK_SRC_DEFAULT
Default clock source (APB for ESP32-C6)
static const char * TAG
Definition I2cComprehensiveTest.cpp:33
static constexpr i2c_port_t I2C_PORT_NUM
Definition I2cComprehensiveTest.cpp:61
static constexpr hf_pin_num_t TEST_SCL_PIN
Definition I2cComprehensiveTest.cpp:63
static constexpr hf_pin_num_t TEST_SDA_PIN
Definition I2cComprehensiveTest.cpp:62
I2C master bus configuration structure.
Definition EspTypes_I2C.h:195
hf_pin_num_t sda_io_num
SDA GPIO pin number.
Definition EspTypes_I2C.h:197
struct hf_i2c_master_bus_config_t::@0 flags
bool allow_pd
Definition EspTypes_I2C.h:206
uint32_t trans_queue_depth
Transaction queue depth for async ops.
Definition EspTypes_I2C.h:200
i2c_port_t i2c_port
I2C port number (0 to MAX_PORTS-1)
Definition EspTypes_I2C.h:196
hf_pin_num_t scl_io_num
SCL GPIO pin number.
Definition EspTypes_I2C.h:198
hf_i2c_mode_t mode
Operation mode (sync/async)
Definition EspTypes_I2C.h:199
hf_i2c_clock_source_t clk_source
Clock source selection.
Definition EspTypes_I2C.h:201
uint32_t intr_priority
Interrupt priority (0-7, 0=lowest)
Definition EspTypes_I2C.h:203
bool enable_internal_pullup
Definition EspTypes_I2C.h:205
hf_i2c_glitch_filter_t glitch_ignore_cnt
Digital glitch filter length.
Definition EspTypes_I2C.h:202

Function Documentation

◆ app_main()

void app_main ( void )

◆ create_test_bus_config()

hf_i2c_master_bus_config_t create_test_bus_config ( hf_i2c_mode_t mode,
uint32_t freq = STANDARD_FREQ,
hf_i2c_clock_source_t clk_source = hf_i2c_clock_source_t::HF_I2C_CLK_SRC_DEFAULT,
hf_i2c_glitch_filter_t glitch_filter = hf_i2c_glitch_filter_t::HF_I2C_GLITCH_FILTER_7_CYCLES,
bool allow_power_down = false )
noexcept

◆ test_i2c_address_modes()

bool test_i2c_address_modes ( )
noexcept

◆ test_i2c_async_mode()

bool test_i2c_async_mode ( )
noexcept

◆ test_i2c_async_multiple_operations()

bool test_i2c_async_multiple_operations ( )
noexcept

◆ test_i2c_async_operations()

bool test_i2c_async_operations ( )
noexcept

◆ test_i2c_async_timeout_handling()

bool test_i2c_async_timeout_handling ( )
noexcept

◆ test_i2c_basic_functionality()

bool test_i2c_basic_functionality ( )
noexcept

◆ test_i2c_bus_deinitialization()

bool test_i2c_bus_deinitialization ( )
noexcept

◆ test_i2c_bus_initialization()

bool test_i2c_bus_initialization ( )
noexcept

◆ test_i2c_bus_scanning()

bool test_i2c_bus_scanning ( )
noexcept

◆ test_i2c_clock_speeds()

bool test_i2c_clock_speeds ( )
noexcept

◆ test_i2c_configuration_validation()

bool test_i2c_configuration_validation ( )
noexcept

◆ test_i2c_device_creation()

bool test_i2c_device_creation ( )
noexcept

◆ test_i2c_device_management()

bool test_i2c_device_management ( )
noexcept

◆ test_i2c_device_probing()

bool test_i2c_device_probing ( )
noexcept

◆ test_i2c_edge_cases()

bool test_i2c_edge_cases ( )
noexcept

◆ test_i2c_error_handling()

bool test_i2c_error_handling ( )
noexcept

◆ test_i2c_esp_specific_features()

bool test_i2c_esp_specific_features ( )
noexcept

◆ test_i2c_espidf_direct_api()

bool test_i2c_espidf_direct_api ( )
noexcept

Test I2C using ESP-IDF API directly (bypassing our wrapper)

Returns
true if successful, false otherwise
Note
This test runs FIRST to verify ESP-IDF I2C driver functionality
Runs in a loop with sufficient time to thoroughly test I2C operations

◆ test_i2c_espidf_wrapper_replica()

bool test_i2c_espidf_wrapper_replica ( )
noexcept

Test I2C using EspI2cBus wrapper (replicating ESP-IDF direct test)

Returns
true if successful, false otherwise
Note
This test runs SECOND to compare EspI2cBus wrapper with ESP-IDF direct API
Uses identical configuration and test pattern as ESP-IDF direct test
Runs in a loop with sufficient time to thoroughly test I2C operations

◆ test_i2c_espidf_wrapper_replica_continuous()

bool test_i2c_espidf_wrapper_replica_continuous ( )
noexcept

Test I2C using EspI2cBus wrapper continuously (stability test)

Returns
true if successful, false otherwise
Note
This test runs the replica test in a loop to verify stability

◆ test_i2c_index_based_access()

bool test_i2c_index_based_access ( )
noexcept

◆ test_i2c_mode_switching()

bool test_i2c_mode_switching ( )
noexcept

◆ test_i2c_multi_device_operations()

bool test_i2c_multi_device_operations ( )
noexcept

◆ test_i2c_performance()

bool test_i2c_performance ( )
noexcept

◆ test_i2c_power_management()

bool test_i2c_power_management ( )
noexcept

◆ test_i2c_probe_methods_comparison()

bool test_i2c_probe_methods_comparison ( )
noexcept

◆ test_i2c_read_operations()

bool test_i2c_read_operations ( )
noexcept

◆ test_i2c_sync_mode()

bool test_i2c_sync_mode ( )
noexcept

◆ test_i2c_thread_safety()

bool test_i2c_thread_safety ( )
noexcept

◆ test_i2c_timeout_handling()

bool test_i2c_timeout_handling ( )
noexcept

◆ test_i2c_write_operations()

bool test_i2c_write_operations ( )
noexcept

◆ test_i2c_write_read_operations()

bool test_i2c_write_read_operations ( )
noexcept

◆ verify_device_functionality()

bool verify_device_functionality ( BaseI2c * device)
noexcept

Variable Documentation

◆ ENABLE_ADVANCED_TESTS

constexpr bool ENABLE_ADVANCED_TESTS
staticconstexpr
Initial value:
=
false

◆ ENABLE_CORE_TESTS

constexpr bool ENABLE_CORE_TESTS
staticconstexpr
Initial value:
=
false

◆ ENABLE_OPERATION_TESTS

constexpr bool ENABLE_OPERATION_TESTS
staticconstexpr
Initial value:
=
false

◆ ENABLE_PERFORMANCE_TESTS

constexpr bool ENABLE_PERFORMANCE_TESTS = false
staticconstexpr

◆ ENABLE_SPECIALIZED_TESTS

constexpr bool ENABLE_SPECIALIZED_TESTS
staticconstexpr
Initial value:
=
true

◆ FAST_FREQ

constexpr uint32_t FAST_FREQ = 400000
staticconstexpr

◆ FAST_PLUS_FREQ

constexpr uint32_t FAST_PLUS_FREQ = 1000000
staticconstexpr

◆ g_test_results

TestResults g_test_results
static

◆ I2C_PORT_NUM

constexpr i2c_port_t I2C_PORT_NUM = I2C_NUM_0
staticconstexpr

◆ NONEXISTENT_ADDR

constexpr uint16_t NONEXISTENT_ADDR = 0x7E
staticconstexpr

◆ STANDARD_FREQ

constexpr uint32_t STANDARD_FREQ = 100000
staticconstexpr

◆ TAG

const char* TAG = "I2C_Test"
static

◆ TEST_DEVICE_ADDR_1

constexpr uint16_t TEST_DEVICE_ADDR_1 = 0x48
staticconstexpr

◆ TEST_DEVICE_ADDR_2

constexpr uint16_t TEST_DEVICE_ADDR_2 = 0x50
staticconstexpr

◆ TEST_SCL_PIN

constexpr hf_pin_num_t TEST_SCL_PIN = 22
staticconstexpr

◆ TEST_SDA_PIN

constexpr hf_pin_num_t TEST_SDA_PIN = 21
staticconstexpr

◆ TIMEOUT_EXTENDED_MS

constexpr uint32_t TIMEOUT_EXTENDED_MS = 10000
staticconstexpr

◆ TIMEOUT_FAST_MS

constexpr uint32_t TIMEOUT_FAST_MS = 100
staticconstexpr

◆ TIMEOUT_LONG_MS

constexpr uint32_t TIMEOUT_LONG_MS = 1000
staticconstexpr

◆ TIMEOUT_MEDIUM_MS

constexpr uint32_t TIMEOUT_MEDIUM_MS = 500
staticconstexpr

◆ TIMEOUT_STANDARD_MS

constexpr uint32_t TIMEOUT_STANDARD_MS = 100
staticconstexpr

◆ TIMEOUT_VERY_FAST_MS

constexpr uint32_t TIMEOUT_VERY_FAST_MS = 100
staticconstexpr

◆ TIMEOUT_VERY_LONG_MS

constexpr uint32_t TIMEOUT_VERY_LONG_MS = 2000
staticconstexpr