endail / hx711-pico-c

Implementation of single and multiple HX711 use via RP2040's state machine
https://endail.github.io/hx711-pico-c/
MIT License
35 stars 7 forks source link

>2 bits in RX FIFO leaves residual data when retrieving gain #50

Closed endail closed 1 year ago

endail commented 1 year ago

https://github.com/endail/hx711-pico-c/blob/595a2f27faf883b91d65399fcad18726edc5365c/src/hx711_noblock.pio#L123

An out instruction moves n bits out of the OSR, but does not reset the output shift count to 0. The result is that if >2 bits of data are put into the SM, an out which only moves 2 bits will leave the rest in there. The following iteration of the main SM loop means that an incorrect gain value will be set.

The out should be replaced with either:

  1. a mov which shifts the entire contents and resets the output shift counter to 0 (RP2040 datasheet, pg. 326);

  2. a out which shifts out 32 bits; or

  3. a out which shifts out 2 bits and then a out null, 32 to discard any remaining bits.

The advantage of 1 and 2 is that it can be done in one instruction. The disadvantage is that an erroneous gain value will make the SM pulse the clock pin an invalid number of times.

The advantage of 3 is that it restricts the number of clock pulses to a maximum of 4 (which is still incorrect; valid range is 0-2). The disadvantage is that it takes two instructions.

I think an out which shifts the entire 32 bits (or whatever the contents of the FIFO is) out is the best solution. C can make sure the value being pushed into the FIFO is valid. I don't think the SM should be used for validation.

endail commented 1 year ago

Fixed in multi-use-dma.