Raspberry Pico Statemachine Restart

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
GerryKeely
Posts: 5
Joined: Wed Feb 17, 2021 10:12 pm

Raspberry Pico Statemachine Restart

Post by GerryKeely » Thu Apr 22, 2021 11:53 am

Hi

What is the purpose of the new statemachine restart function in MP 1.15.
A small sample of its use would be appreciated.
Regards & Thank you

Gerry

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Raspberry Pico Statemachine Restart

Post by hippy » Thu Apr 22, 2021 12:44 pm

Looking at the source code it appears to offer the ability to set the execution address of a StateMachine. It effectively allows 'sm.exec("jmp(...)")' though I haven't examined its exact implementation. It seems it may only allow restart from an initial address rather than an arbitrary address as there is no parameter allowed. Thus an example would probably be ...

Code: Select all

sm = StateMachine( ... )

# Do things

sm.restart()
Related discussion : https://www.raspberrypi.org/forums/view ... 6&t=308077

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Raspberry Pico Statemachine Restart

Post by Roberthh » Thu Apr 22, 2021 1:00 pm

The intention is to restart the state machine again from the initial address in it#s initial state. That is useful for state machines that stuck in a certain state, e.g. because the input data was incomplete. Then, timeout checking can be done outside the state machine.

A good example is the simplified DHT22 driver in this document; https://github.com/robert-hh/RP2040-Exa ... r/rp2_util, which does no timeout-checking in the state machine code.
The sample code uses the viper version of sm.restart(), but besides that it demonstrates the application.

For jumps to other addresses you can indeed use the sm.exec() method. But since this jump requires a hardcoded address, and that is usually not known in the Python code, it is hardly ever useful.

GerryKeely
Posts: 5
Joined: Wed Feb 17, 2021 10:12 pm

Re: Raspberry Pico Statemachine Restart

Post by GerryKeely » Thu Apr 22, 2021 1:34 pm

Hippy/Roberthh
Thank you both for your prompt replies.

regards

Gerry

Post Reply