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

Comprehensive PWM testing suite for ESP32 family (noexcept) More...

#include "TestFramework.h"
#include "base/BasePwm.h"
#include "mcu/esp32/EspGpio.h"
#include "mcu/esp32/EspPwm.h"
#include <array>
Include dependency graph for PwmComprehensiveTest.cpp:

Functions

hf_pwm_unit_config_t create_test_config () noexcept
 Create a default PWM configuration for testing.
 
hf_pwm_unit_config_t create_fade_test_config () noexcept
 Create a PWM configuration specifically for fade testing.
 
hf_pwm_unit_config_t create_basic_with_fade_config () noexcept
 Create a PWM configuration for basic mode with fade enabled (for channel enable operations)
 
hf_pwm_channel_config_t create_test_channel_config (hf_gpio_num_t gpio_pin, hf_u32_t frequency_hz=HF_PWM_DEFAULT_FREQUENCY, hf_u8_t resolution_bits=HF_PWM_DEFAULT_RESOLUTION) noexcept
 Create a default channel configuration for testing with explicit resolution control.
 
hf_pwm_channel_config_t create_test_channel_config_with_duty (hf_gpio_num_t gpio_pin, float duty_percentage, hf_u32_t frequency_hz=HF_PWM_DEFAULT_FREQUENCY, hf_u8_t resolution_bits=HF_PWM_DEFAULT_RESOLUTION) noexcept
 Create channel configuration with specific duty cycle percentage.
 
bool test_constructor_default () noexcept
 Test PWM constructor variations and object creation.
 
bool test_destructor_cleanup () noexcept
 
bool test_initialization_states () noexcept
 Test PWM initialization state management and lifecycle.
 
bool test_lazy_initialization () noexcept
 
bool test_mode_configuration () noexcept
 
bool test_clock_source_configuration () noexcept
 
bool test_channel_configuration () noexcept
 
bool test_channel_enable_disable () noexcept
 
bool test_duty_cycle_control () noexcept
 Test comprehensive duty cycle control functionality.
 
bool test_frequency_control () noexcept
 
bool test_phase_shift_control () noexcept
 
bool test_synchronized_operations () noexcept
 
bool test_complementary_outputs () noexcept
 
bool test_hardware_fade () noexcept
 
bool test_idle_level_control () noexcept
 
bool test_timer_management () noexcept
 Test comprehensive LEDC timer resource management.
 
bool test_status_reporting () noexcept
 
bool test_statistics_and_diagnostics () noexcept
 
void test_fade_callback_ch0 (hf_channel_id_t channel_id)
 
void test_fade_callback_ch1 (hf_channel_id_t channel_id)
 
bool test_callbacks () noexcept
 
bool test_basic_mode_without_fade () noexcept
 Test basic mode without fade functionality.
 
bool test_fade_mode_functionality () noexcept
 Test fade mode functionality.
 
bool test_resolution_specific_duty_cycles () noexcept
 Test resolution-specific duty cycle accuracy (NEW CRITICAL TEST)
 
bool test_frequency_resolution_validation () noexcept
 Test frequency/resolution validation (NEW CRITICAL TEST)
 
bool test_enhanced_validation_system () noexcept
 Test enhanced validation system with clock source awareness (NEW)
 
bool test_percentage_consistency_across_resolutions () noexcept
 Test percentage consistency across different resolutions (NEW)
 
bool test_resolution_control_methods () noexcept
 Test direct resolution control methods (NEW)
 
bool test_resolution_aware_duty_calculations () noexcept
 Test resolution-aware duty cycle calculations (NEW)
 
bool test_edge_cases () noexcept
 
bool test_stress_scenarios () noexcept
 
bool test_timer_health_check_and_recovery () noexcept
 Test timer health check and recovery mechanisms (NEW)
 
bool test_safe_eviction_policies () noexcept
 Test safe eviction policies (NEW CRITICAL SAFETY TEST)
 
void app_main (void)
 

Variables

static const char * TAG = "PWM_Test"
 
static TestResults g_test_results
 
static constexpr bool ENABLE_CORE_TESTS = true
 
static constexpr bool ENABLE_CONFIGURATION_TESTS
 
static constexpr bool ENABLE_CHANNEL_TESTS = true
 
static constexpr bool ENABLE_CONTROL_TESTS = true
 
static constexpr bool ENABLE_ADVANCED_TESTS
 
static constexpr bool ENABLE_ESP_SPECIFIC_TESTS
 
static constexpr bool ENABLE_RESOLUTION_TESTS = true
 
static constexpr bool ENABLE_DIAGNOSTIC_TESTS = true
 
static constexpr bool ENABLE_STRESS_TESTS = true
 
static volatile bool g_fade_callback_called [HF_PWM_MAX_CHANNELS] = {false}
 
static volatile hf_channel_id_t g_last_fade_channel = 0xFF
 

Detailed Description

Comprehensive PWM testing suite for ESP32 family (noexcept)

This comprehensive test suite validates all functionality of the EspPwm class across different ESP32 variants, with particular focus on LEDC peripheral capabilities and constraints:

Test Coverage:

  • Lifecycle Management: Constructor/Destructor, Initialize/Deinitialize
  • Configuration: Modes, clock sources, unit configuration
  • Channel Management: Configure, enable/disable, validation
  • PWM Control: Duty cycle, frequency, resolution control
  • Advanced Features: Synchronized operations, complementary outputs
  • ESP32-Specific: Hardware fade, idle levels, timer management
  • LEDC Validation: Clock source constraints, frequency/resolution limits
  • Resource Management: Timer allocation, eviction policies, health checks
  • Status & Diagnostics: Statistics, error reporting, capability detection
  • Callbacks: Fade callback mechanisms (ESP-IDF LEDC native support only)
  • Edge Cases & Stress: Boundary conditions, resource exhaustion, recovery

Hardware Requirements:

  • ESP32 development board (any variant: ESP32, ESP32-S2/S3, ESP32-C3/C6, ESP32-H2)
  • GPIO pins for PWM output testing (configurable)
  • Optional: Logic analyzer or oscilloscope for signal verification

Test Progression Indicator:

GPIO14 toggles HIGH/LOW after each test completion for visual feedback. This allows monitoring test progress without serial output.

Note
This test suite is designed to be variant-agnostic and will adapt to the specific LEDC capabilities of the target ESP32 variant.

Function Documentation

◆ app_main()

void app_main ( void )

◆ create_basic_with_fade_config()

hf_pwm_unit_config_t create_basic_with_fade_config ( )
noexcept

Create a PWM configuration for basic mode with fade enabled (for channel enable operations)

◆ create_fade_test_config()

hf_pwm_unit_config_t create_fade_test_config ( )
noexcept

Create a PWM configuration specifically for fade testing.

◆ create_test_channel_config()

hf_pwm_channel_config_t create_test_channel_config ( hf_gpio_num_t gpio_pin,
hf_u32_t frequency_hz = HF_PWM_DEFAULT_FREQUENCY,
hf_u8_t resolution_bits = HF_PWM_DEFAULT_RESOLUTION )
noexcept

Create a default channel configuration for testing with explicit resolution control.

Parameters
gpio_pinGPIO pin number for PWM output
frequency_hzPWM frequency in Hz (default: 1kHz)
resolution_bitsPWM resolution in bits (default: 10-bit)
Returns
Configured PWM channel configuration for testing

Creates a standard channel configuration with:

  • 50% initial duty cycle
  • APB clock source preference
  • Basic PWM mode
  • No output inversion
  • Low priority (non-critical)

◆ create_test_channel_config_with_duty()

hf_pwm_channel_config_t create_test_channel_config_with_duty ( hf_gpio_num_t gpio_pin,
float duty_percentage,
hf_u32_t frequency_hz = HF_PWM_DEFAULT_FREQUENCY,
hf_u8_t resolution_bits = HF_PWM_DEFAULT_RESOLUTION )
noexcept

Create channel configuration with specific duty cycle percentage.

◆ create_test_config()

hf_pwm_unit_config_t create_test_config ( )
noexcept

Create a default PWM configuration for testing.

Returns
Configured PWM unit configuration for basic testing

Creates a standard test configuration with:

  • Basic PWM mode (no fade)
  • APB clock source (80MHz)
  • Interrupts enabled
  • Fade functionality disabled for basic testing

◆ test_basic_mode_without_fade()

bool test_basic_mode_without_fade ( )
noexcept

Test basic mode without fade functionality.

◆ test_callbacks()

bool test_callbacks ( )
noexcept

◆ test_channel_configuration()

bool test_channel_configuration ( )
noexcept

◆ test_channel_enable_disable()

bool test_channel_enable_disable ( )
noexcept

◆ test_clock_source_configuration()

bool test_clock_source_configuration ( )
noexcept

◆ test_complementary_outputs()

bool test_complementary_outputs ( )
noexcept

◆ test_constructor_default()

bool test_constructor_default ( )
noexcept

Test PWM constructor variations and object creation.

Returns
true if all constructor tests pass, false otherwise

Validates proper object construction without hardware initialization:

  • Default constructor with minimal configuration
  • Constructor with explicit unit configuration
Note
No hardware initialization occurs during construction (lazy initialization pattern)
Warning
All constructors must complete without exceptions (noexcept specification)

◆ test_destructor_cleanup()

bool test_destructor_cleanup ( )
noexcept

◆ test_duty_cycle_control()

bool test_duty_cycle_control ( )
noexcept

Test comprehensive duty cycle control functionality.

Returns
true if all duty cycle tests pass, false otherwise

Validates precise duty cycle control across full range:

  • Float Interface: Tests 0%, 25%, 50%, 75%, 100% duty cycles
  • Raw Interface: Tests raw values 0, 256, 512, 768, 1023 (10-bit)
  • Input Validation: Tests rejection of invalid values (-0.1, 1.1)
  • Accuracy Verification: Confirms readback values match set values
Note
Uses GPIO 2 for PWM output with 1kHz frequency @ 10-bit resolution
Warning
Duty cycle accuracy should be within ±1% of commanded value
See also
test_resolution_specific_duty_cycles() for resolution-specific testing

◆ test_edge_cases()

bool test_edge_cases ( )
noexcept

◆ test_enhanced_validation_system()

bool test_enhanced_validation_system ( )
noexcept

Test enhanced validation system with clock source awareness (NEW)

Returns
true if all validation system tests pass, false otherwise

Comprehensive validation of the LEDC peripheral constraint system:

Phase 1: Clock Source Validation

  • Tests APB clock (80MHz) with various frequency/resolution combinations
  • Validates hardware constraint formula: freq × (2^resolution) ≤ clock_freq
  • Verifies proper error reporting for invalid combinations

Phase 2: Dynamic Resolution Calculation

  • Tests maximum achievable resolution for given frequencies
  • Validates theoretical vs. practical resolution limits
  • Confirms hardware constraint calculations

Phase 3: Enhanced Duty Cycle Validation

  • Tests overflow protection for different resolutions
  • Validates automatic clamping of out-of-range values
  • Confirms resolution-specific duty cycle ranges

Phase 4: Auto-Fallback Functionality

  • Tests automatic resolution adjustment for problematic combinations
  • Validates fallback resolution selection algorithms
  • Confirms graceful handling of impossible combinations
Note
This test validates the core LEDC peripheral constraint system
Warning
Tests may fail on ESP32 variants with different LEDC capabilities
See also
test_frequency_resolution_validation() for basic constraint testing
SetFrequencyWithAutoFallback() for automatic resolution adjustment

◆ test_fade_callback_ch0()

void test_fade_callback_ch0 ( hf_channel_id_t channel_id)

◆ test_fade_callback_ch1()

void test_fade_callback_ch1 ( hf_channel_id_t channel_id)

◆ test_fade_mode_functionality()

bool test_fade_mode_functionality ( )
noexcept

Test fade mode functionality.

◆ test_frequency_control()

bool test_frequency_control ( )
noexcept

◆ test_frequency_resolution_validation()

bool test_frequency_resolution_validation ( )
noexcept

Test frequency/resolution validation (NEW CRITICAL TEST)

◆ test_hardware_fade()

bool test_hardware_fade ( )
noexcept

◆ test_idle_level_control()

bool test_idle_level_control ( )
noexcept

◆ test_initialization_states()

bool test_initialization_states ( )
noexcept

Test PWM initialization state management and lifecycle.

Returns
true if all initialization state tests pass, false otherwise

Validates proper initialization state transitions:

  • Initial uninitialized state after construction
  • Manual initialization with Initialize() method
  • Double initialization protection (returns ALREADY_INITIALIZED)
  • Proper deinitialization with Deinitialize() method
  • State consistency throughout lifecycle
Note
Tests the explicit initialization path (not lazy initialization)
Warning
All state transitions must be atomic and thread-safe

◆ test_lazy_initialization()

bool test_lazy_initialization ( )
noexcept

◆ test_mode_configuration()

bool test_mode_configuration ( )
noexcept

◆ test_percentage_consistency_across_resolutions()

bool test_percentage_consistency_across_resolutions ( )
noexcept

Test percentage consistency across different resolutions (NEW)

◆ test_phase_shift_control()

bool test_phase_shift_control ( )
noexcept

◆ test_resolution_aware_duty_calculations()

bool test_resolution_aware_duty_calculations ( )
noexcept

Test resolution-aware duty cycle calculations (NEW)

◆ test_resolution_control_methods()

bool test_resolution_control_methods ( )
noexcept

Test direct resolution control methods (NEW)

◆ test_resolution_specific_duty_cycles()

bool test_resolution_specific_duty_cycles ( )
noexcept

Test resolution-specific duty cycle accuracy (NEW CRITICAL TEST)

◆ test_safe_eviction_policies()

bool test_safe_eviction_policies ( )
noexcept

Test safe eviction policies (NEW CRITICAL SAFETY TEST)

◆ test_statistics_and_diagnostics()

bool test_statistics_and_diagnostics ( )
noexcept

◆ test_status_reporting()

bool test_status_reporting ( )
noexcept

◆ test_stress_scenarios()

bool test_stress_scenarios ( )
noexcept

◆ test_synchronized_operations()

bool test_synchronized_operations ( )
noexcept

◆ test_timer_health_check_and_recovery()

bool test_timer_health_check_and_recovery ( )
noexcept

Test timer health check and recovery mechanisms (NEW)

◆ test_timer_management()

bool test_timer_management ( )
noexcept

Test comprehensive LEDC timer resource management.

Returns
true if all timer management tests pass, false otherwise

Validates advanced timer allocation and management features:

Phase 1: Basic Timer Allocation

  • Tests automatic timer assignment for different frequency/resolution combinations
  • Validates timer sharing optimization for compatible frequencies
  • Confirms proper timer resource tracking

Phase 2: Timer Exhaustion Scenarios

  • Tests behavior when all timers are allocated with incompatible combinations
  • Validates proper error reporting (TIMER_CONFLICT)
  • Confirms system stability under resource pressure

Phase 3: Compatible Frequency Reuse

  • Tests timer sharing for frequencies within tolerance (±5%)
  • Validates resource optimization and efficiency

Phase 4: Timer Recovery

  • Tests timer resource recovery after channel release
  • Validates automatic cleanup and reallocation

Phase 5: Forced Timer Assignment

  • Tests manual timer assignment with ForceTimerAssignment()
  • Validates override of automatic allocation

Phase 6: Diagnostics Validation

  • Tests statistics and diagnostics reporting accuracy
  • Validates resource usage tracking
Note
This test exercises the core LEDC timer management algorithms
Warning
Timer allocation behavior may vary between ESP32 variants
See also
FindOrAllocateTimer() for Timer Module allocation implementation
ForceTimerAssignment() for manual Timer Module control

Variable Documentation

◆ ENABLE_ADVANCED_TESTS

constexpr bool ENABLE_ADVANCED_TESTS
staticconstexpr
Initial value:
=
true

◆ ENABLE_CHANNEL_TESTS

constexpr bool ENABLE_CHANNEL_TESTS = true
staticconstexpr

◆ ENABLE_CONFIGURATION_TESTS

constexpr bool ENABLE_CONFIGURATION_TESTS
staticconstexpr
Initial value:
=
true

◆ ENABLE_CONTROL_TESTS

constexpr bool ENABLE_CONTROL_TESTS = true
staticconstexpr

◆ ENABLE_CORE_TESTS

constexpr bool ENABLE_CORE_TESTS = true
staticconstexpr

◆ ENABLE_DIAGNOSTIC_TESTS

constexpr bool ENABLE_DIAGNOSTIC_TESTS = true
staticconstexpr

◆ ENABLE_ESP_SPECIFIC_TESTS

constexpr bool ENABLE_ESP_SPECIFIC_TESTS
staticconstexpr
Initial value:
=
true

◆ ENABLE_RESOLUTION_TESTS

constexpr bool ENABLE_RESOLUTION_TESTS = true
staticconstexpr

◆ ENABLE_STRESS_TESTS

constexpr bool ENABLE_STRESS_TESTS = true
staticconstexpr

◆ g_fade_callback_called

volatile bool g_fade_callback_called[HF_PWM_MAX_CHANNELS] = {false}
static

◆ g_last_fade_channel

volatile hf_channel_id_t g_last_fade_channel = 0xFF
static

◆ g_test_results

TestResults g_test_results
static

◆ TAG

const char* TAG = "PWM_Test"
static