I was working on a small project with the pyboard 1.1 that requires using timer interrupts. And these interrupts should be controlled externally over REPL. I have 2 files, one of them will get loaded onto the pyboard to define the timer and interrupts, the other file is what I run on my PC to control the REPL and manipulate the data I receive over REPL.
The file that gets loaded into pyboard (pyb_test.py):
Code: Select all
import pyb
import utime
import micropython
# Initialize the interrupt timer and the LED to indicate the state
led = pyb.LED(2)
pulse_max = 250000
pulse_min = 250000
stop = False
#############################
# Set up the pins and pulse timer
pin_a = pyb.Pin('X1', pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
pin_b = pyb.Pin('X2', pyb.Pin.AF_PP, pull=pyb.Pin.PULL_NONE, af=pyb.Pin.AF1_TIM2)
pulse = pyb.Timer(2, prescaler=0, period=5000000)
pulse_channel = pulse.channel(1, pyb.Timer.ENC_A)
#############################
tim = pyb.Timer(4)
tim.init(freq=2)
def tick(tim):
global led
global pulse
global pulse_max
global pulse_min
mypulse = pulse.counter()
mytime = utime.ticks_us()
print(mypulse, mytime) # Return the pulse and time ticks
if mypulse >= pulse_max:
pulse_max = mypulse
if mypulse <= pulse_min:
pulse_min = mypulse
print(pulse_max, pulse_min)
led.toggle() # toggle the LED
def start_pyb():
tim.callback(lambda t: tick(tim)) # Start the interrupt timer
def stop_pyb():
pyb.Timer(4).callback(None)
pyb.Timer(4).deinit()
pyb.LED(2).off()
def initposcounter(value):
global pulse
pulse.counter(value)
def get_pulse_minmax():
global pulse_max
global pulse_min
print("abcdef")
print(pulse_max, pulse_min)
initposcounter(250000)
Code: Select all
import pyboard
import time
# Initialize PyBoard and connect in REPL mode
pyb = pyboard.Pyboard('COM10')
pyb.enter_raw_repl()
# Execute the file that reports the pulse and time ticks
pyb.execfile('pyb_test.py')
pyb.exec("start_pyb()")
# Read the reported values 50 times (50 for min and max and 50 for the position and time tick)
for a in range(100):
print(pyb.read_until(1, b'\r\n', 3))
pyb.exec("stop_pyb()")
print("here")
# Exit REPL and close the connection
pyb.exit_raw_repl()
pyb.close()
What can I do to solve this issue? I have tried quite a lot of things, but it seems like once I start the callback, I can't do anything else with the pyboard until I do a soft reset.
Many thanks in advance.