pico-examples icon indicating copy to clipboard operation
pico-examples copied to clipboard

Example request: show how to use pio_sm_set_clkdiv()

Open beroset opened this issue 3 years ago • 5 comments

I am trying to use the pio_sm_set_clkdiv() call in my code and had some questions about it. I had hoped to find an example of its use in the examples, but currently there are none. It might be useful to add it, perhaps as an option to the squarewave example, just to show how it works and how it is intended to be used.

beroset avatar May 04 '22 11:05 beroset

There are examples of sm_config_set_clkdiv, and AIUI the difference is largely that you'd use sm_config_set_clkdiv to configure the StateMachine before starting it, whereas pio_sm_set_clkdiv would be used to modify the clock divider (see section 3.5.5 of https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf ) of a StateMachine that's already running?

lurch avatar May 04 '22 12:05 lurch

Some clear delineation of the difference between sm_config_set_clkdiv and pio_sm_set_clkdiv and why one would use one versus the other would be helpful, both in the examples and in the SDK documentation.

beroset avatar May 04 '22 15:05 beroset

Off the top of my head, I can't really think of a reason why you'd want to change the clock-divider on an already-running StateMachine, which is probably why all of the examples use sm_config_set_clkdiv. Is there a specific reason why you want/need to use pio_sm_set_clkdiv?

lurch avatar May 04 '22 15:05 lurch

I searched for a way to change the clock divider and happened upon pio_sm_set_clkdiv first and thought I could use it. The problem is that the documentation doesn't say anything about that only being useful after the state machine is running.

For what it's worth, I am currently using both in my program. I use sm_config_set_clkdiv to establish an initial reasonable value, but then I have a user menu in which the user can bump up the clock speed to see if the device under test (attached to the Pico) can still work at higher speeds; that one uses pio_sm_set_clkdiv because the state machine is already running.

beroset avatar May 04 '22 15:05 beroset

Sorry for the inaccuracy in my earlier statement; pio_sm_set_clkdiv isn't only useful after the state-machine is running, it's just that the common usage-model in pico-sdk is to use the sm_config methods to set up the state-machines before you start them running (which IIRC is more efficient than using the pio_sm methods).

And that's just about the limit of my knowledge on this subject, so any further clarifications will have to come from @kilograham

lurch avatar May 04 '22 16:05 lurch