Skip to content

Commit

Permalink
Transfer gpio 2 control to the can module if probed so.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjr committed Nov 3, 2024
1 parent 9787623 commit 6518578
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
89 changes: 48 additions & 41 deletions main/canbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ void CANbus::driverInstall( twai_mode_t mode ){
twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT( _tx_io, _rx_io, mode );
ESP_LOGI(FNAME, "default alerts %X", g_config.alerts_enabled);
g_config.alerts_enabled |= TWAI_ALERT_TX_IDLE | TWAI_ALERT_TX_SUCCESS | TWAI_ALERT_TX_FAILED;
if ( _slope_support ) {
g_config.bus_off_io = GPIO_NUM_2;
}
g_config.rx_queue_len = 15; // 1.5x the need of one NMEA sentence
g_config.tx_queue_len = 15;
ESP_LOGI(FNAME, "my alerts %X", g_config.alerts_enabled);
Expand Down Expand Up @@ -102,14 +105,10 @@ void CANbus::driverInstall( twai_mode_t mode ){
//Start TWAI driver
if (twai_start() == ESP_OK) {
ESP_LOGI(FNAME,"Driver started");
delay(10);
_ready_initialized = true;
// Set RS pin
// bus_off_io may operate invers, so for now set this here
if( _slope_support ){
if ( _slope_support ) {
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
}
delay(10);
_ready_initialized = true;
} else {
twai_driver_uninstall();
ESP_LOGI(FNAME,"Failed to start driver");
Expand Down Expand Up @@ -411,49 +410,59 @@ bool CANbus::sendNMEA( const SString& msg ){
return ret;
}

bool CANbus::selfTest( bool rs ){
bool CANbus::selfTest()
{
ESP_LOGI(FNAME,"CAN bus selftest");
_slope_support = rs;
if( !_slope_support ){
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_2, 1 );
}

// Pretend slope control off and probe the reaction on GPIO 2 here
_slope_support = false;
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
// in case of GPIO 2 wired to CAN this would inhibit sending and cause a failing test
gpio_set_level(GPIO_NUM_2, 1);

driverInstall( TWAI_MODE_NO_ACK );
bool res=false;
int id=0x100;
delay(100);
twai_clear_receive_queue();
for( int i=0; i<10; i++ ){ // repeat test 10x
char tx[10] = { "1827364" };
int len = strlen(tx);
// there might be data from a remote device
for (int slope=0; slope<2; slope++)
{
twai_clear_receive_queue();
if( !sendData( id, tx,len, 1 ) ){
ESP_LOGW(FNAME,"CAN bus selftest TX FAILED");
recover();
}
delay(2);
SString msg;
int rxid;
int bytes = receive( &rxid, msg, 10 );
ESP_LOGI(FNAME,"RX CAN bus message bytes:%d, id:%04x, data:%s", bytes, id, msg.c_str() );
if( bytes != 7 || rxid != id ){
ESP_LOGW(FNAME,"CAN bus selftest RX call FAILED bytes:%d rxid%d recm:%s", bytes, rxid, msg.c_str() );
delay(i);
for( int i=0; i<3; i++ ){ // repeat test 3x
char tx[10] = { "1827364" };
int len = strlen(tx);
// there might be data from a remote device
twai_clear_receive_queue();
if( !sendData( id, tx,len, 1 ) ){
ESP_LOGW(FNAME,"CAN bus selftest TX FAILED");
recover();
}
delay(2);
SString msg;
int rxid;
int bytes = receive( &rxid, msg, 2 );
ESP_LOGI(FNAME,"RX CAN bus message bytes:%d, id:%04x, data:%s", bytes, id, msg.c_str() );
if( bytes != 7 || rxid != id ){
ESP_LOGW(FNAME,"CAN bus selftest RX call FAILED bytes:%d rxid%d recm:%s", bytes, rxid, msg.c_str() );
twai_clear_receive_queue();
}
else if( memcmp( msg.c_str() ,tx, len ) == 0 ){
ESP_LOGI(FNAME,"RX CAN bus OKAY");
res=true;
break;
}
}
else if( memcmp( msg.c_str() ,tx, len ) == 0 ){
ESP_LOGI(FNAME,"RX CAN bus OKAY");
res=true;
break;
}
if ( res ) break;

_slope_support = true;
gpio_set_level(GPIO_NUM_2, 0);
ESP_LOGI(FNAME,"CAN slope support on.");
}
if( res ){
if( res ) {
ESP_LOGW(FNAME,"CAN bus selftest TX/RX OKAY");
}else{
}
else {
ESP_LOGW(FNAME,"CAN bus selftest TX/RX FAILED");
}
restart();
driverUninstall();
return res;
}

Expand All @@ -464,9 +473,7 @@ bool CANbus::sendData( int id, const char* msg, int length, int self ){
return false;
}
xSemaphoreTake(sendMutex,portMAX_DELAY );
if( _slope_support ){
gpio_set_level(GPIO_NUM_2, 0 );
}

twai_message_t message;
memset( &message, 0, sizeof( message ) );
message.identifier = id;
Expand Down
2 changes: 1 addition & 1 deletion main/canbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CANbus {
void begin();
void restart();
void recover();
bool selfTest(bool rs_bit);
bool selfTest();
bool GotNewClient() const { return _new_can_client_connected; }
void ResetNewClient() { _new_can_client_connected = false; }
bool connectedXCV() { return _connected_xcv; };
Expand Down
29 changes: 11 additions & 18 deletions main/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,29 +1326,22 @@ void system_startup(void *args){
if( Audio::haveCAT5171() ) // todo && CAN configured
{
CAN = new CANbus();
if( CAN->selfTest(false) ){ // series 2023 has fixed slope control, prio slope bit for AHRS temperature control
logged_tests += "CAN Interface: ";
if( CAN->selfTest() ) { // series 2023 has fixed slope control, prio slope bit for AHRS temperature control
resultCAN = "OK";
ESP_LOGE(FNAME,"CAN Bus selftest (no RS): OK");
logged_tests += "CAN Interface: OK\n";
if( hardwareRevision.get() != XCVARIO_23 ){
ESP_LOGE(FNAME,"CAN Bus selftest (%sRS): OK", CAN->hasSlopeSupport() ? "" : "no ");
logged_tests += "OK\n";
if ( CAN->hasSlopeSupport() ) {
hardwareRevision.set(XCVARIO_22); // XCV-22, CAN but no AHRS temperature control
} else {
ESP_LOGI(FNAME,"CAN Bus selftest without RS control OK: set hardwareRevision 5 (XCV-23)");
hardwareRevision.set(XCVARIO_23); // XCV-23, including AHRS temperature control
}
}
else{
if( CAN->selfTest(true) ){ // if slope bit is to be handled, there is no temperature control
resultCAN = "OK";
ESP_LOGE(FNAME,"CAN Bus selftest RS: OK");
logged_tests += "CAN Interface: OK\n";
if( hardwareRevision.get() != XCVARIO_22 ){
hardwareRevision.set(XCVARIO_22); // XCV-22, CAN but no AHRS temperature control
}
}
else{
resultCAN = "FAIL";
logged_tests += "CAN Bus selftest: FAILED\n";
ESP_LOGE(FNAME,"Error: CAN Interface failed");
}
else {
resultCAN = "FAIL";
logged_tests += "CAN Bus selftest: FAILED\n";
ESP_LOGE(FNAME,"Error: CAN Interface failed");
}
}

Expand Down

0 comments on commit 6518578

Please sign in to comment.