adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.11k stars 1.22k forks source link

analogio refuse to change the value #3962

Closed dglaude closed 3 years ago

dglaude commented 3 years ago

Testing on

Adafruit CircuitPython 6.1.0-rc.0 on 2021-01-06; Adafruit Feather M0 Express with samd21g18

analogio seems to fail when I try to change the value:

import board
from analogio import AnalogOut
analog_out = AnalogOut(board.A0)
analog_out.value = 1000
analog_out.value = 2000

The result is and error on the line that set the value to 2000:

main.py output: Traceback (most recent call last): File "main.py", line 5, in ValueError: Object has been deinitialized and can no longer be used. Create a new object.

When trying the guide code from https://learn.adafruit.com/circuitpython-essentials/circuitpython-analog-out it fail on the line: analog_out.value = i

dhalbert commented 3 years ago

Reproduced on SAMD21, but SAMD51 is OK.

6.0.1 is OK on SAMD21.

dhalbert commented 3 years ago

@jepler I bisected this to https://github.com/adafruit/circuitpython/pull/3541/commits/fb768dfc14ab3b98905927f984a080e551513ea8

What I see when debugging is that the AnalogOut object is getting smashed between the first and second call to common_hal_analogio_analogout_set_value(). It seems to be OK immediately after the dac_sync_write().

OK:

$2 = {base = {type = 0x35cc0 <analogio_analogout_type>}, descriptor = {device = {hw = 0x42004800}, sel_ch = {{
        buffer = 0x0, length = 0}}}, channel = 1 '\001', deinited = false}

later:

$4 = {base = {type = 0x35cc0 <analogio_analogout_type>}, descriptor = {device = {hw = 0x42004800}, sel_ch = {{
        buffer = 0x0, length = 0}}}, channel = 224 '\340', deinited = 125}
dhalbert commented 3 years ago

Channel number is getting set wrong; will fix.