nerdyscout / Arduino_MCP3x6x_Library

Library to support Microchip MPC3x6x 16/24bit analog to digital converters.
https://nerdyscout.github.io/Arduino_MCP3x6x_Library
MIT License
17 stars 9 forks source link

Fix unexpected ADC behavior due to uninitialized data being sent over SPI #14

Closed rtzoeller closed 4 months ago

rtzoeller commented 4 months ago

Zero-initialize the buffer used for ADC reads, as the buffer is always written over the wire as part of the SPI transaction. In unlucky circumstances the uninitialized buffer would encode a valid command, resetting the device or triggering other undesired behavior.

Note that uint8_t buffer[s] = {0}; won't work due to the variable-length of the array.

rtzoeller commented 4 months ago

I've put this up against the develop branch, but I'm happy to retarget or duplicate it against main if desired.

The conditions for this to cause an issue seem to be fairly application and device specific; I've reliably hit it with an STM32H7 in one application, but never hit it with an STM32L0 in another. It's UB regardless, so it'd be nice to see it fixed.

nerdyscout commented 4 months ago

thank you for contributing.

never have seen this issue, but totally agree to fix it.