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:
a mov which shifts the entire contents and resets the output shift counter to 0 (RP2040 datasheet, pg. 326);
a out which shifts out 32 bits; or
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.
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, anout
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:a
mov
which shifts the entire contents and resets the output shift counter to 0 (RP2040 datasheet, pg. 326);a
out
which shifts out 32 bits; ora
out
which shifts out 2 bits and then aout 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.