Repository navigation
TLE5012B: validate the safety word the driver already reads - #106
Open
jajmangold wants to merge 1 commit into
Open
jajmangold wants to merge 1 commit into
jajmangold wants to merge 1 commit into
Conversation
readBytes() requests len+2 bytes, so the SSC safety word is already clocked off the wire on every read and then discarded. Checking it therefore costs no extra transaction and no extra bus time. Adds opt-in validation: when enabled, a read whose CRC fails or whose status bits report a fault is rejected and the previous good angle returned, rather than passing a corrupted angle to the control loop. Disabled by default, so existing behaviour is unchanged. safetyErrorCount() and lastSafetyWord() expose what happened. The CRC is CRC-8 poly 0x1D, seed 0xFF, over the command word followed by the data word, inverted. This was established by measurement rather than taken from the datasheet: four plausible readings were implemented and scored against a TLE5012B over 200 reads. This one matched 200/200; the three alternatives matched 0/200.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
readBytes()already requestslen + 2bytes, so the SSC safety word is clocked off the wire on every read and then discarded. This PR validates it instead.Because the bytes are already being read, this costs no extra transaction and no extra bus time.
Why
Every 15-bit value the TLE5012B returns is a legal angle, so the data word alone can never reveal that a read was corrupted — only the safety word can. At present a corrupted angle is passed straight to the control loop, which matters once the sensor is closing a position loop and a bad reading commands real current.
What it does
Opt-in, and disabled by default so existing behaviour is unchanged:
sensor.checkSafetyWord(true);With it enabled, a read whose CRC fails or whose status bits report a fault is rejected and the previous good angle returned. Two accessors expose what happened:
The CRC, determined by measurement
The algorithm is CRC-8, polynomial
0x1D, seed0xFF, over the transmitted command word followed by the returned data word, inverted.Rather than take one reading of the datasheet on trust, four plausible interpretations were implemented and scored simultaneously against a real TLE5012B over 200 reads:
Status bits read healthy 200/200 in the same run.
Testing
readBytes()builds it (0x0020 | 0x8000 | 1=0x8021)genericSTM32F103CBwith all three new methods exercised; builds clean with no warningsScope note, stated plainly: the CRC algorithm and the sensor behaviour were verified on hardware, but this exact driver was not run end to end on my board — my own firmware uses a separate SPI implementation. The integration is small and was checked against
readBytes()by inspection, but I would rather flag that than overstate it.