G30TH Timer and boot.py and board debugging

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

G30TH Timer and boot.py and board debugging

Post by jgriessen » Sun Aug 07, 2016 1:32 am

I don't have any error messages and no indication that any timer code is running -- no compare outputs,
no debugging outputs from the code. It's as if the code is not being run.

Code: Select all

import pyb, micropython
micropython.alloc_emergency_exception_buf(100)

debug_pin = pyb.Pin('JP6', pyb.Pin.OUT_PP)

    #Use with pyb.freq(96000000) and prescaler=11 for .125 usec timer ticks.
xfmr_pulse_period = 0x1F40   #(1000 usec * 8)-> hex
xfmr_pulse_w = 0xa0          #(20 usec * 8)-> hex
xfmr_pulse_pos_half-cycle = 1


    #to give .125 usec timer ticks counting up:
t1 = pyb.Timer(1,  prescaler=11, period=xfmr_pulse_period, mode=pyb.Timer.UP)

    #xfmr_pulse_half-cycle timing.     (for rollover callback)
    #JP3 output -- just an interrrupt.  Compare generates interrupt:  
t1ch2 = t1.channel(2, pyb.Timer.OC_TOGGLE, compare=xfmr_pulse_period,  polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP3)


    #xfmr_pulse_pos_half-cycle driver pin.     (positive driving transistor)
    #Control output pin JP5 active high.  Compare turns off output pin at xfmr_pulse_w count:  
t1ch1 = t1.channel(1, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP5)

    #NOT(xfmr_pulse_pos_half-cycle) driver pin.  (negative driving transistor)
    #Control output pin JP4 active high.  Compare turns off output pin at xfmr_pulse_w count:  
t1ch3 = t1.channel(3, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP4)

def t1ch2_pulses_start_cb
    global xfmr_pulse_pos_half-cycle
    if xfmr_pulse_pos_half-cycle == 1:
        #xfmr_pulse positive half-cycle off-time just ended
        #turn on driver for following negative half-cycle
        xfmr_pulse_pos_half-cycle = 0
        t1ch1 = t1.channel(1, pyb.Timer.OC_FORCED_ACTIVE, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP5)
        t1ch1 = t1.channel(1, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP5)
	debug_pin.value(0)
	debug_pin.value(1)
	debug_pin.value(0)
        #pyb.Pin.board.JP5.value(1)
    else:
        #xfmr_pulse negative half-cycle off-time just ended
        #turn on driver for following positive half-cycle
        xfmr_pulse_pos_half-cycle = 1
        t1ch3 = t1.channel(3, mode=Timer.OC_FORCED_ACTIVE, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP4)
        t1ch3 = t1.channel(3, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w, polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP4)
        #pyb.Pin.board.JP4.value(1)
	debug_pin.value(1)
	debug_pin.value(0)
	debug_pin.value(1)

t1ch2.callback(t1ch2_pulses_start_cb)


while True:
    pyb.delay(1)
    debug_pin.value(0)
    pyb.delay(1)
    debug_pin.value(1)

How do you use that error memory: micropython.alloc_emergency_exception_buf(100)
?

I still can get a repl after each reset, and still
can toggle a pin with code like:

pin3 = pyb.Pin('JP3', pyb.Pin.OUT_PP)
pin3.value(1)
JP4, JP5, JP6,JP3 all test out OK to change volt value after setting this way.
Perhaps the initialization is not complete on those as started up with the new platform file.
pin=pyb.Pin.board.JP4 and so on may not have all the needed data entered such as active HIGH...

pyb.freq() reports 96 96 24 48 MHz. I saw something about a limit on those...

I ran another program that Nathan ran OK and hooked up an LED to JP6 as if JP6 could only pull down
and nothing after reset.

My boot.py is:
cat /flash/boot.py

Code: Select all

# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal

import machine
import pyb
pyb.freq(96000000)
pyb.main('min-test.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse

/home/john> cat /flash/min-test.py

Code: Select all

import pyb
from pyb import Timer

# timer 4 will be created with a frequency of 10 kHz
tim=pyb.Timer(4,freq=10000);
# attach the timer to the LED GPIO, turning the brightness OFF to begin
tchannel = tim.channel(3, Timer.PWM, pin=pyb.Pin.board.JP6, pulse_width_percent=8)
pyb.delay(70)
tchannel. pulse_width_percent=80)
pyb.delay(700)
tchannel. pulse_width_percent=30)
pyb.delay(700)
tchannel. pulse_width_percent=60)
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by dhylands » Sun Aug 07, 2016 7:37 am

I would rename min-test.py to min_test.py and not change the name of main.py.

Then I would get to the REPL and do: import min_test (you can't import python files with a dash in the name).

Then you'll see any errors reported on the REPL. At least this is how I do it. I only set pyb.main once I'm happy that everything is working properly.

The purpose of micropython.alloc_emergency_exception_buf(100) is to allow exceptions which occur during interrupt handlers to be reported with traceback information rather than just a MemoryError.

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by jgriessen » Sun Aug 07, 2016 2:23 pm

I'll start using import min_test.py and such, thanks for the hint.

I noticed another wrong assumption I was making -- but it may be trouble also.
when I used the reset# of G30TH, I was holding it low for 1/10 to 1/4 second and got strange slow changing outputs,
2 Hz, on JP32, the PWM out, and output stopped after some seconds when it should have stayed on forever. When I hold reset# low for 1.5+ seconds. I get 2 usec pulses, (but I changed more than one thing at a time...). The early slow/strange outputs could have been response to totally bad code with missing ('s and syntax mistakes.
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by dhylands » Sun Aug 07, 2016 3:59 pm

RESET should only need to be held low for about 20 microseconds.

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by jgriessen » Mon Aug 08, 2016 6:38 pm

I used import to test some new code -- very helpful.

What kinds of lint checkers are available?
pylint3 says error even when micropython import is happy...

Code: Select all

pylint3 min_test.py
No config file found, using default configuration
************* Module min_test
E: 12, 0: invalid token (syntax-error)
I am using debian jessie on my laptop. It is not easy, no package, for pylint3 for jessie.
John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by dhylands » Mon Aug 08, 2016 7:09 pm

Can you share your python code?

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by jgriessen » Tue Aug 09, 2016 4:18 am

Can you share your python code?
Sure. Here's what I have running (some things working as expected, some not...)
This program has outputs on JP3,4,5,6. JP3 and 6 work OK when not using the USB serial
and they are affected and stop after 2 seconds when rshell is attempting to stay logged in.
I think that means I need to up the priority of interrupts for the callback.

Code: Select all

# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal

import machine
import pyb
pyb.freq(96000000)
pyb.main('hv_pulser.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse

Code: Select all

# hv_pulser.py
import pyb
import micropython
from pyb import Timer

micropython.alloc_emergency_exception_buf(100)

debug_pin = pyb.Pin('JP6', pyb.Pin.OUT_PP)

# Use with pyb.freq(96000000) and prescaler=11 for .125 usec timer ticks.
xfmr_pulse_period = 0x1F40   # (1000 usec * 8)-> hex
xfmr_pulse_w = 0xa0          # (20 usec * 8)-> hex
xfmr_pulse_pos_half_cycle = 1

# to give .125 usec timer ticks counting up:
t1 = pyb.Timer(1, prescaler=11, period=xfmr_pulse_period, mode=Timer.UP)

# xfmr_pulse_half_cycle timing.     (for rollover callback)
# JP3 output and interrrupt.  Compare generates interrupt:
t1ch2 = t1.channel(2, pyb.Timer.OC_TOGGLE, compare=xfmr_pulse_period,
                   polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP3)   # working!

# Define pins so they can be set with pinx.value()on the fly.
neg_drive_pin = pyb.Pin('JP5')
neg_drive_pin.init(mode=Pin.AF_PP, af=1)
pos_drive_pin = pyb.Pin('JP4')
pos_drive_pin.init(mode=Pin.AF_PP, af=1)


# NOT(xfmr_pulse_pos_half_cycle) driver pin.  (negative driving transistor)
# Compare turns off output pin at xfmr_pulse_w count:
t1ch1 = t1.channel(1, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w,
                   polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP5)

# xfmr_pulse_pos_half_cycle driver pin.  (positive driving transistor)
# Compare turns off output pin at xfmr_pulse_w count:
t1ch3 = t1.channel(3, pyb.Timer.OC_INACTIVE, compare=xfmr_pulse_w,
                   polarity=pyb.Timer.HIGH, pin=pyb.Pin.board.JP4)

# debug stuff:
pos_drive_pin.value(1)
neg_drive_pin.value(1)


def t1ch2_pulses_start_cb():
    "start pulse rising edge"
    global xfmr_pulse_pos_half_cycle
    if xfmr_pulse_pos_half_cycle == 1:
        # xfmr_pulse positive half_cycle off-time just ended
        # turn on driver for following negative half_cycle
        xfmr_pulse_pos_half_cycle = 0
        neg_drive_pin.value(1)

        debug_pin.value(0)
        debug_pin.value(1)
        debug_pin.value(0)

        # pyb.Pin.board.JP5.value(1)
    else:
        # xfmr_pulse negative half_cycle off-time just ended
        # turn on driver for following positive xfmr_pulse_pos_half_cycle = 1
        xfmr_pulse_pos_half_cycle = 1
        pos_drive_pin.value(1)

        # pyb.Pin.board.JP4.value(1)
        debug_pin.value(1)
        debug_pin.value(0)
        debug_pin.value(1)

t1ch2.callback(t1ch2_pulses_start_cb)


while True:
    pyb.delay(1)
    debug_pin.value(0)
    pyb.delay(1)
    debug_pin.value(1)

John Griessen blog.kitmatic.com

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by dhylands » Tue Aug 09, 2016 5:47 am

I was able to run pylint3 on both of those files and just got expected errors (about missing pyb and machine, etc).

User avatar
jgriessen
Posts: 191
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: G30TH Timer and boot.py and board debugging

Post by jgriessen » Tue Aug 09, 2016 9:35 am

Yes, I am using a linter. I just could not get pylint3 on my laptop.

The lack of results and answers about the Timer.OC_INACTIVE mode means I'll be trying
to use the callback from Timer.OC_TOGGLE along with a Timer.PWM callback.
From my min_test.py program I see that the PWM pulse_width comes first -- it's not at the end of the period.
So that callback should be at the right time to toggle a flag about which of two outputs to pulse next. Not sure of a good way to disable a PWM pulse every other time. That's why I wanted Timer.OC_INACTIVE mode -- All you have to do is set the output before the time frame you want it, since the timer will only change it to low.

I'll be looking up a register write way to set a Timer.OC_INACTIVE mode output, or... disable a Timer.PWM pulse based on a flag.
John Griessen blog.kitmatic.com

Post Reply