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

Comprehensive CAN testing suite for ESP32-C6 with ESP-IDF v5.5 TWAI API and SN65 transceiver. More...

#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "TestFramework.h"
#include "base/BaseCan.h"
#include "mcu/esp32/EspCan.h"
#include <atomic>
#include <memory>
#include <vector>
Include dependency graph for CanComprehensiveTest.cpp:

Functions

void verify_can_pin_states ()
 Verify CAN pin states before testing.
 
void test_receive_callback_enhanced (const hf_can_message_t &message, void *user_data)
 Test callback for received CAN messages (enhanced version with user data)
 
hf_can_message_t create_test_message (uint32_t id, bool extended=false, uint8_t dlc=8)
 Create a test CAN message.
 
bool wait_for_event (EventBits_t bits, uint32_t timeout_ms)
 Wait for events with timeout.
 
bool test_basic_initialization () noexcept
 Test basic initialization and state management.
 
bool test_message_transmission () noexcept
 Test message transmission and reception.
 
bool test_acceptance_filtering () noexcept
 Test acceptance filtering.
 
bool test_advanced_timing () noexcept
 Test advanced timing configuration.
 
bool test_statistics_diagnostics () noexcept
 Test statistics and diagnostics.
 
bool test_batch_transmission () noexcept
 Test batch message transmission.
 
bool test_error_handling () noexcept
 Test error handling and recovery.
 
bool test_espcan_comprehensive_functionality () noexcept
 Comprehensive test to validate ALL EspCan functionality This test systematically validates every aspect of the EspCan implementation.
 
bool test_can_initialization () noexcept
 
bool test_can_self_test_mode () noexcept
 
bool test_can_message_transmission () noexcept
 
bool test_can_acceptance_filtering () noexcept
 
bool test_can_advanced_timing () noexcept
 
bool test_can_error_handling () noexcept
 
bool test_can_bus_recovery () noexcept
 
bool test_can_batch_transmission () noexcept
 
bool test_can_high_throughput () noexcept
 
bool test_external_physical_loopback () noexcept
 
bool test_loopback_comparison () noexcept
 
bool test_sn65_transceiver_integration () noexcept
 
bool test_can_signal_quality () noexcept
 
void app_main (void)
 

Variables

static const char * TAG = "CAN_Test"
 
static TestResults g_test_results
 
static constexpr uint32_t TEST_CAN_ID_STANDARD = 0x123
 
static constexpr uint32_t TEST_CAN_ID_EXTENDED = 0x12345678
 
static constexpr uint32_t TEST_BAUD_RATE = 500000
 
static constexpr uint32_t TEST_TIMEOUT_MS = 5000
 
static constexpr hf_pin_num_t TEST_TX_PIN = 4
 
static constexpr hf_pin_num_t TEST_RX_PIN = 5
 
static constexpr int MESSAGE_RECEIVED_BIT = BIT0
 
static constexpr int ERROR_OCCURRED_BIT = BIT1
 
static constexpr int STATE_CHANGED_BIT = BIT2
 
static EventGroupHandle_t test_event_group = nullptr
 
static std::atomic< uint32_t > messages_received {0}
 
static std::atomic< uint32_t > errors_detected {0}
 
static hf_can_message_t last_received_message {}
 
static constexpr bool ENABLE_CORE_TESTS = true
 
static constexpr bool ENABLE_ADVANCED_TESTS = true
 
static constexpr bool ENABLE_ERROR_TESTS = true
 
static constexpr bool ENABLE_PERFORMANCE_TESTS = true
 
static constexpr bool ENABLE_TRANSCEIVER_TESTS
 

Detailed Description

Comprehensive CAN testing suite for ESP32-C6 with ESP-IDF v5.5 TWAI API and SN65 transceiver.

This comprehensive test suite validates all EspCan functionality including:

  • ESP-IDF v5.5 TWAI node-based API compliance
  • ESP32-C6 TWAI controller operation
  • SN65 CAN transceiver integration
  • Advanced filtering and timing configuration
  • Event-driven callback systems
  • Single-callback per event with user data pointer
  • Error handling and bus recovery
  • Performance and stress testing
  • Self-test and loopback modes

Hardware Requirements:

  • ESP32-C6 DevKit
  • SN65HVD230/SN65HVD232 CAN transceiver
  • CAN bus termination resistors (120Ω)
  • Optional: Second CAN node for full bus testing

Wiring for ESP32-C6 + SN65:

  • GPIO4 (TX) -> SN65 CTX pin
  • GPIO5 (RX) -> SN65 CRX pin
  • 3.3V -> SN65 VCC
  • GND -> SN65 GND
  • SN65 CANH/CANL -> CAN bus

For External Loopback Testing:

  • Connect: SN65 CANH -> 120Ω resistor -> SN65 CANL
  • DO NOT short TWAI TX/RX lines directly!

IMPORTANT: Two loopback modes are tested:

  1. INTERNAL LOOPBACK (enable_loopback=true):
    • Uses ESP32's internal hardware loopback
    • TX and RX on same pin (GPIO4)
    • No external hardware required
    • Interrupt callbacks work correctly
    • Used for: message_transmission, acceptance_filtering, batch_transmission, high_throughput, bus_recovery, self_test_mode
  2. EXTERNAL LOOPBACK (enable_loopback=false):
    • Requires proper CAN bus loopback AFTER the transceiver
    • Connect: SN65 CANH -> 120Ω termination resistor -> SN65 CANL
    • Uses real CAN transceiver hardware with proper CAN bus signaling
    • Tests actual CAN bus communication with differential signaling
    • Used for: external_physical_loopback (in SN65 transceiver section)
    • NOTE: Shorting TWAI TX/RX lines directly does NOT work!

Test Progression Indicator:

GPIO14 toggles HIGH/LOW after each test completion for visual feedback. Test sections are indicated by 5 blinks on GPIO14 (like SpiComprehensiveTest). This allows monitoring test progress without serial output.

Author
Nebiyu Tadesse
Date
2025

Function Documentation

◆ app_main()

void app_main ( void )

◆ create_test_message()

hf_can_message_t create_test_message ( uint32_t id,
bool extended = false,
uint8_t dlc = 8 )

Create a test CAN message.

◆ test_acceptance_filtering()

bool test_acceptance_filtering ( )
noexcept

Test acceptance filtering.

◆ test_advanced_timing()

bool test_advanced_timing ( )
noexcept

Test advanced timing configuration.

◆ test_basic_initialization()

bool test_basic_initialization ( )
noexcept

Test basic initialization and state management.

◆ test_batch_transmission()

bool test_batch_transmission ( )
noexcept

Test batch message transmission.

◆ test_can_acceptance_filtering()

bool test_can_acceptance_filtering ( )
noexcept

◆ test_can_advanced_timing()

bool test_can_advanced_timing ( )
noexcept

◆ test_can_batch_transmission()

bool test_can_batch_transmission ( )
noexcept

◆ test_can_bus_recovery()

bool test_can_bus_recovery ( )
noexcept

◆ test_can_error_handling()

bool test_can_error_handling ( )
noexcept

◆ test_can_high_throughput()

bool test_can_high_throughput ( )
noexcept

◆ test_can_initialization()

bool test_can_initialization ( )
noexcept

◆ test_can_message_transmission()

bool test_can_message_transmission ( )
noexcept

◆ test_can_self_test_mode()

bool test_can_self_test_mode ( )
noexcept

◆ test_can_signal_quality()

bool test_can_signal_quality ( )
noexcept

◆ test_error_handling()

bool test_error_handling ( )
noexcept

Test error handling and recovery.

◆ test_espcan_comprehensive_functionality()

bool test_espcan_comprehensive_functionality ( )
noexcept

Comprehensive test to validate ALL EspCan functionality This test systematically validates every aspect of the EspCan implementation.

◆ test_external_physical_loopback()

bool test_external_physical_loopback ( )
noexcept

◆ test_loopback_comparison()

bool test_loopback_comparison ( )
noexcept

◆ test_message_transmission()

bool test_message_transmission ( )
noexcept

Test message transmission and reception.

◆ test_receive_callback_enhanced()

void test_receive_callback_enhanced ( const hf_can_message_t & message,
void * user_data )

Test callback for received CAN messages (enhanced version with user data)

◆ test_sn65_transceiver_integration()

bool test_sn65_transceiver_integration ( )
noexcept

◆ test_statistics_diagnostics()

bool test_statistics_diagnostics ( )
noexcept

Test statistics and diagnostics.

◆ verify_can_pin_states()

void verify_can_pin_states ( )

Verify CAN pin states before testing.

◆ wait_for_event()

bool wait_for_event ( EventBits_t bits,
uint32_t timeout_ms )

Wait for events with timeout.

Variable Documentation

◆ ENABLE_ADVANCED_TESTS

constexpr bool ENABLE_ADVANCED_TESTS = true
staticconstexpr

◆ ENABLE_CORE_TESTS

constexpr bool ENABLE_CORE_TESTS = true
staticconstexpr

◆ ENABLE_ERROR_TESTS

constexpr bool ENABLE_ERROR_TESTS = true
staticconstexpr

◆ ENABLE_PERFORMANCE_TESTS

constexpr bool ENABLE_PERFORMANCE_TESTS = true
staticconstexpr

◆ ENABLE_TRANSCEIVER_TESTS

constexpr bool ENABLE_TRANSCEIVER_TESTS
staticconstexpr
Initial value:
=
true

◆ ERROR_OCCURRED_BIT

constexpr int ERROR_OCCURRED_BIT = BIT1
staticconstexpr

◆ errors_detected

std::atomic<uint32_t> errors_detected {0}
static

◆ g_test_results

TestResults g_test_results
static

◆ last_received_message

hf_can_message_t last_received_message {}
static

◆ MESSAGE_RECEIVED_BIT

constexpr int MESSAGE_RECEIVED_BIT = BIT0
staticconstexpr

◆ messages_received

std::atomic<uint32_t> messages_received {0}
static

◆ STATE_CHANGED_BIT

constexpr int STATE_CHANGED_BIT = BIT2
staticconstexpr

◆ TAG

const char* TAG = "CAN_Test"
static

◆ TEST_BAUD_RATE

constexpr uint32_t TEST_BAUD_RATE = 500000
staticconstexpr

◆ TEST_CAN_ID_EXTENDED

constexpr uint32_t TEST_CAN_ID_EXTENDED = 0x12345678
staticconstexpr

◆ TEST_CAN_ID_STANDARD

constexpr uint32_t TEST_CAN_ID_STANDARD = 0x123
staticconstexpr

◆ test_event_group

EventGroupHandle_t test_event_group = nullptr
static

◆ TEST_RX_PIN

constexpr hf_pin_num_t TEST_RX_PIN = 5
staticconstexpr

◆ TEST_TIMEOUT_MS

constexpr uint32_t TEST_TIMEOUT_MS = 5000
staticconstexpr

◆ TEST_TX_PIN

constexpr hf_pin_num_t TEST_TX_PIN = 4
staticconstexpr