KevinOConnor / can2040

Software CAN bus implementation for rp2040 micro-controllers
GNU General Public License v3.0
636 stars 63 forks source link

Don't resychronize transmit bit timing #32

Closed KevinOConnor closed 1 year ago

KevinOConnor commented 1 year ago

This PR changes the code so that CanTx bit transmit timing is no longer dependent on the received CanRx bit timing.

It is normal for there to be 100+ nanoseconds of delay between a change to the CanTx line and its reception on the CanRx line. At higher canbus frequencies this delay could result in significant skew to the tx bit timing.

This problem wasn't always seen because the CanRx time resynchronizing code would only stretch the bit timing if it detected a passive to dominant edge 1/16th of the bit time delayed - a delay larger than that would be ignored.

Looking at the canbus spec, it seems to indicate that tx bit timing should be dependent on CanRx bit resynchronization, but only if it detects a faster transmitter. (That is, it should only resynchronize to faster transmitters, not slower transmitters.) It's not immediately clear that this more advanced logic could be implemented in the PIO. This PR just entirely disables bit timing on transmits (that is, it wont resynchronize to faster transmitters nor slower transmitters).

In addition to the bit timing change, this PR also adds a check for a received message with the same id as a requested transmit, but with different payload. (An improved error check that may help catch obscure corner cases with different tx and rx sampling points.)

The PR also increases the slow transmitter detection range (it can resynchronize to an edge that is up to 1/8th of a bit time delayed).

This PR is currently intended for discussion and testing.

-Kevin

KevinOConnor commented 1 year ago

I was able to rework this branch so that the PIO "tx" state machine will perform bit time synchronization, but only to faster transmitters. This should retain the benefits of bit time synchronization and avoid the problem of "self feedback" causing improper bit timing. This also makes the implementation more closely follow the canbus specification.

I'm inclined to go forward with merging of this PR.

If you run tests on this branch, let me know your results (success or failure).

-Kevin