Skip to content

Improved Restart() function#468

Open
phatpaul wants to merge 1 commit intottlappalainen:masterfrom
phatpaul:driver-restart
Open

Improved Restart() function#468
phatpaul wants to merge 1 commit intottlappalainen:masterfrom
phatpaul:driver-restart

Conversation

@phatpaul
Copy link
Copy Markdown
Contributor

@phatpaul phatpaul commented Sep 9, 2025

Fixes Restart() function so it triggers a full reset of the state. Now Restart() can be called (i.e. by the CAN driver after it detects bus error and performs bus recovery and bus restart) and the stack will properly reinitialize and restart the address claim.

This should not cause any harm to applications that don't make use of the Restart() function.

Usage example:

This is how my phatpaul/NMEA2000_esp32_autorestart driver class monitors the lower level driver and can restart the stack when necessary. With this I can unplug/replug the CAN bus and it will restart the address claim automatically.

---
title: NMEA2000_esp32xx Driver State Machine
---
stateDiagram-v2

    [*] --> STOPPED
    
    STOPPED --> RESTARTING: (autoRecoveryEnabled && timeout)<br/>twai_start()<br/>SendDummyFrame()
    STOPPED --> RUNNING: started by N2K stack
    
    RESTARTING --> RUNNING: tNMEA2000..Restart()
    RESTARTING --> STOPPED: (TxErr>0)<br/>reset timeout
    
    RUNNING --> RECOVERING: CAN bus error
    RUNNING --> STOPPED: (TxErr>=128)<br/>reset timeout
    
    RECOVERING --> STOPPED: (CAN bus recovered)<br/>reset timeout
Loading

…Restart() can be called by the CAN driver after it detects bus error and performs bus recovery and bus restart.
@ttlappalainen
Copy link
Copy Markdown
Owner

I have to study this, since I have on own library different solution, which I have not had time to publish. Which controller you use?

@phatpaul
Copy link
Copy Markdown
Contributor Author

The controller/driver choice shouldn't matter for the purposes of this PR. The model that I'm proposing should work for any controller/driver. I don't think that error detection and restart logic should belong inside the NMEA2000 library core, since you probably can't find a one-size-fits-all solution for every scenario. Let the NMEA2000_xxx driver, which inherits and extends the functions of your library, implement the best lifecycle plan for that unique controller.

Anyway, I'm using ESP32 (on module ESP32-Pico-Mini-02-8n2r) ==> ISO1042 isolated CAN PHY.
The ESP32 has several silicon hardware errata and the TWAI driver freezes up sometimes. My driver tries to workaround these issues by restarting when it detects error conditions. Additionally, in some cases it is necessary to restart the NMEA2000 library as well. I.e. If disconnected for a while, then reconnected we should restart address claim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants