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

Restarting SM clears clock divider #11

Closed endail closed 2 years ago

endail commented 2 years ago

Clock is set in state machine init function:

https://github.com/endail/hx711-pico-c/blob/12b88c5504876f0f8ef057a4bc405077e503d6d9/src/hx711_noblock.pio#L159-L163

But powering up from hx711_set_power restarts the SM, which also clears the clock setting.

https://github.com/endail/hx711-pico-c/blob/12b88c5504876f0f8ef057a4bc405077e503d6d9/src/hx711.c#L195

pio_sm_restart

Restart a state machine with a known state.

This method clears the ISR, shift counters, clock divider counter pin write flags, delay counter, latched EXEC instruction, and IRQ wait condition.

https://raspberrypi.github.io/pico-sdk-doxygen/group__hardware__pio.html#ga7b89b93dd0553c77538c70baf6a7e4f0

endail commented 2 years ago

Rather than doing a hard restart of the state machine, just clear the FIFOs and then put the PC back to the first instruction.

https://github.com/endail/hx711-pico-c/blob/8fc15f22a685d02d74facd68f926dfc737dd1713/src/hx711.c#L192-L203

This way the clock divider stays in place.