Statemachine restart not working correctly

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
TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Statemachine restart not working correctly

Post by TheSilverBullet » Sat Jul 23, 2022 4:12 pm

Sorry to bother you guys again, but I believe I found another one. The StateMachines behave strange once a sm.restart has been issued. Tiny bit of code to test the behaviour:

Code: Select all

#!/usr/bin/python3

from time import sleep_ms
from rp2 import asm_pio, StateMachine

@asm_pio()
def pio_prog_wrap():
    wrap_target()
    mov (isr, y)
    push(block)
    wrap()

@asm_pio()
def pio_prog_loop():
    label('Loop')
    mov (isr, y)
    push(block)
    jmp('Loop')

# prepare eight state machines
sm = []
for i in range(8):
    sm.append(StateMachine(i))
    print(sm[i])
    sm[i].init(pio_prog_loop)   # use either the one with loop (both are OK)
    # sm[i].init(pio_prog_wrap) # or the one with wrap
    sm[i].exec(f'set(y, {10+i})')   # give each sm individual data to return
    sm[i].active(True)

def see_what_we_get():
    for x in range(10):
        gotdata = False
        for i in range(8):
            if sm[i].rx_fifo() > 0:
                print(f'sm[{i}]:{sm[i].get()}', end=' ')
                gotdata = True
        print()
        if not gotdata:
            sleep_ms(100)
    print()

see_what_we_get()
for i in range(8):
    sm[i].restart()
see_what_we_get()
And here's what happens. First one is OK, second one looks strange.

Code: Select all

~/tmp$ picorun testpio.py 
StateMachine(0)
StateMachine(1)
StateMachine(2)
StateMachine(3)
StateMachine(4)
StateMachine(5)
StateMachine(6)
StateMachine(7)
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 

sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[4]:14 
sm[0]:10 sm[4]:14 
sm[0]:10 sm[4]:14 
sm[0]:10 sm[4]:14 
sm[0]:10 sm[4]:14 
sm[0]:10 sm[4]:14 
It looks like after a restart only the first StateMachine in each PIO works.

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

Re: Statemachine restart not working correctly

Post by Roberthh » Sun Jul 24, 2022 6:47 am

Could you check with your code, wheher the sm.restart() method from this lib works? https://github.com/robert-hh/RP2040-Exa ... r/rp2_util

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

Re: Statemachine restart not working correctly

Post by Roberthh » Sun Jul 24, 2022 7:21 am

I found the fix and made a PR. This was a real bug and should be reported at the code repository. Not needed now and more. But there are also may questions raised in the repository which should be asked in the forum.

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Statemachine restart not working correctly

Post by TheSilverBullet » Sun Jul 24, 2022 10:27 am

Roberthh wrote:
Sun Jul 24, 2022 7:21 am
I found the fix and made a PR. This was a real bug and should be reported at the code repository. Not needed now and more. But there are also may questions raised in the repository which should be asked in the forum.
Hi Robert,
thanks for the quick response, very much appreciated. Unfortunately I'm traveling at the moment so I'm not able to test any fixes. Sorry about that. Just wanted to ring a bell about the StateMachine before leaving.
I will test and report as soon as I'm back. (will take a while)
But I'll check all the information here and am available for any question.
Thanks a lot.

Norbert

PS. Should have take a RPi nano with me…

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

Re: Statemachine restart not working correctly

Post by Roberthh » Sun Jul 24, 2022 12:31 pm

No hurry. When I run your script with the firmware fix, I get:

Code: Select all

>>> import restart
StateMachine(0)
StateMachine(1)
StateMachine(2)
StateMachine(3)
StateMachine(4)
StateMachine(5)
StateMachine(6)
StateMachine(7)
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 

sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 
sm[0]:10 sm[1]:11 sm[2]:12 sm[3]:13 sm[4]:14 sm[5]:15 sm[6]:16 sm[7]:17 

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: Statemachine restart not working correctly

Post by TheSilverBullet » Mon Jul 25, 2022 7:52 am

Roberthh wrote:
Sun Jul 24, 2022 12:31 pm
No hurry. When I run your script with the firmware fix, I get:
Perfect! Double thumbs up. Thanks for the help.

Post Reply