QT Py RP2040 forgets CircuitPython

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Kattni,

Here you go. This ran fine until I hit the reset button. Then I had to hit it 6 times to get it running again. It only requires the neopixel.py library.
It will print to the console, cpus[0] and cpus[1] temperatures and fade to a new random color in the neopixel every second.

Bruce

Code: Select all

#
#             **********   QT Py RP2040   **********
# adafruit-circuitpython-adafruit_qtpy_rp2040-en_US-6.2.0.uf2
# Internal Library
import os
import rtc
import time
import board
import busio
import random
from digitalio import DigitalInOut, Direction, Pull
import microcontroller
from microcontroller import pin
# External Library( /lib/ )
import neopixel

########################################
#      Factory Reset:                  #
#      import storage                  #
#      storage.erase_filesystem()      #
########################################
print("Program START! QT Py RP2040")

print("             os.name: ", end="")
print(os.uname)
print(" cpus[0].temperature: ", end="")
print(microcontroller.cpus[0].temperature)
print("   cpus[0].frequency: ", end="")
print(microcontroller.cpus[0].frequency)
print(" cpus[1].temperature: ", end="")
print(microcontroller.cpus[1].temperature)
print("   cpus[1].frequency: ", end="")
print(microcontroller.cpus[1].frequency)
print("              cpu.id: ", end="")
print(microcontroller.cpu.uid)
print("         cpu.voltage: ", end="")
print(microcontroller.cpu.voltage)
print("")
time.sleep(2) # Show GREEN LED if we are good

DoW    = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
months = ("Null", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

print(microcontroller.cpus[0].temperature, microcontroller.cpus[0].temperature*1.8+32)
print(microcontroller.cpus[1].temperature, microcontroller.cpus[1].temperature*1.8+32)

global OldRed
global OldGreen
global OldBlue
OldRed     = 0
OldGreen   = 0
OldBlue    = 0

x          = 0
NUM_PIXELS = 1            #  if =1 use onboard NeoPixel
if(NUM_PIXELS == 1):      #  NEOPIXEL Internal NeoPixel
  STRING = board.NEOPIXEL #
  ORDER = neopixel.GRB    #  Neo_1=Green, Neo_2=Red, Neo_3=Blue R&G reversed
  BRIGHTNESS = 0.10       #
  Neo_1_Max  = 150        #  Green
  Neo_2_Max  =  50        #  Red
  Neo_3_Max  = 250        #  Blue
else:                     #  RX External NeoPixel(s)
  STRING = board.RX       #
  ORDER = neopixel.RGB    #  Neo_1=Red, Neo_2=Green, Neo_3=Blue R&G normal
  BRIGHTNESS = 0.25       #
  Neo_1_Max  = 250        #  Red
  Neo_2_Max  = 250        #  Green
  Neo_3_Max  = 250        #  Blue
print(STRING, NUM_PIXELS, ORDER)
# pixelString = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, bpp=3, brightness=BRIGHTNESS, auto_write=True, pixel_order=ORDER)
pixelString = neopixel.NeoPixel(STRING, NUM_PIXELS, bpp=3, brightness=BRIGHTNESS, auto_write=False, pixel_order=ORDER)
RED_led = DigitalInOut(board.TX)
RED_led.direction = Direction.OUTPUT
RED_led.value = False
print(microcontroller.pin.GPIO11)
#################################################
#################################################
def port_info():
  board_pins = []
  for pin in dir(microcontroller.pin):
    if isinstance(getattr(microcontroller.pin, pin), microcontroller.Pin):
      pins = []
      for alias in dir(board):
        if getattr(board, alias) is getattr(microcontroller.pin, pin):
           pins.append("board.{}".format(alias))
      if len(pins) > 0:
        board_pins.append(" ".join(pins))
  for pins in sorted(board_pins):
    print(pins)
#################################################
def fillStringRandom(NewRed, NewGreen, NewBlue):
#  NewRed   = random.randint(0, 255)
#  NewGreen = random.randint(0, 255)
#  NewBlue  = random.randint(0, 255)
  for x in range(NUM_PIXELS):
    pixelString[x] = ((NewRed, NewGreen, NewBlue))
  pixelString.show()
  RED_led.value = not RED_led.value
#################################################
def fadeRandomColors(NewRed, NewGreen, NewBlue):
  global OldRed
  global OldGreen
  global OldBlue
#  print(OldRed, OldGreen, OldBlue)
#  print(NewRed, NewGreen, NewBlue)
  while True:
    delta = 0
    if(NewRed != OldRed):
      delta = NewRed - OldRed
      delta = delta/(abs(delta))
      OldRed = OldRed + delta
    if(NewGreen != OldGreen):
      delta = NewGreen - OldGreen
      delta = delta/(abs(delta))
      OldGreen = OldGreen + delta
    if(NewBlue != OldBlue):
      delta = NewBlue - OldBlue
      delta = delta/(abs(delta))
      OldBlue = OldBlue + delta
    pixelString[x] = ((OldRed, OldGreen, OldBlue))
    pixelString.show()
    time.sleep(0.001)
    if(delta == 0):
      break
  OldRed   = NewRed
  OldGreen = NewGreen
  OldBlue  = NewBlue
  RED_led.value = not RED_led.value
  return(OldRed, OldGreen, OldBlue)
#################################################
#################################################
if True:
  port_info()
#################################################
if True:
  print("Random colors")
  while True:
    time.sleep(1)
#    Jj = time.monotonic() #######
    CPU_Temp = microcontroller.cpus[0].temperature
    print("   CPU0 Temperature: {:02.6}*C\t{:02.6}*F".format(CPU_Temp, CPU_Temp*1.8+32))
    CPU_Temp = microcontroller.cpus[1].temperature
    print("   CPU1 Temperature: {:02.6}*C\t{:02.6}*F".format(CPU_Temp, CPU_Temp*1.8+32))
    NewRed   = random.randint(0, 255)
    NewGreen = random.randint(0, 255)
    NewBlue  = random.randint(0, 255)
    if(NUM_PIXELS == 1):
      fadeRandomColors(NewRed, NewGreen, NewBlue)
    else:
      time.sleep(0.2)
      fillStringRandom(NewRed, NewGreen, NewBlue)
#    fillStringRandom(NewRed, NewGreen, NewBlue)
    print("")
#    print(time.monotonic() - Jj) #######
    NewRed   = random.randint(0, 255)
    NewGreen = random.randint(0, 255)
    NewBlue  = random.randint(0, 255)
    if(NUM_PIXELS == 1):
      fadeRandomColors(NewRed, NewGreen, NewBlue)
    else:
      time.sleep(0.2)
      fillStringRandom(NewRed, NewGreen, NewBlue)
  # While End
#################################################
print("The End")
#################################################
Last edited by blakebr on Tue May 11, 2021 1:43 pm, edited 1 time in total.

User avatar
kattni
 
Posts: 132
Joined: Fri Aug 18, 2017 6:33 pm

Re: QT Py RP2040 forgets CircuitPython

Post by kattni »

This example runs fine for me. Comes back up after clicking the reset button, as well as after unplugging it from USB and plugging it back in.

Thank you for dimming the NeoPixel. :)

I'm at a loss. I think at this point I need to involve one of my teammates. He's out for today though, so I will email him and hopefully he can take a look soon.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Kattni,

Yes, that little neopixel is Very bright.
What is happening does not make any sense.

If you need...
I am plugged into an HP-ProDesk running up to date Windows 10.
I get the same results on a USB 2.0 port on the front, a cable from a USB 3.0 port the back and a 60 watt 5.2 volt stand alone USB charger.
If we get to the point when I need to send an offending QT Py back, I will be on vacation for a 11 days starting this Thursday.
I will have my phone and iPad.

Bruce

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: QT Py RP2040 forgets CircuitPython

Post by danhalbert »

Does this issue happen only with your code.py, or does it also happen with no code.py or an innocuous code.py, such as the `print("Hello World")` that is put there after you erase the filesystem?

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Hello Dan,

OK with flash_nuke.uf2
OK with QT_PY_RP2040.uf2
~~ with adafruit-circuitpython-adafruit_qtpy_rp2040-en_US-6.2.0.uf2
Loads fine and LED shows green LED getting bright and dim.
Pull USB-C power and reinsert - fail. 8 resets before it starts.

Now for my code.py
Bad with print("Hello Ada Fruit") ;-) Bunch of resets after pulling power.

Bruce

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: QT Py RP2040 forgets CircuitPython

Post by danhalbert »

We have a weak hypothesis about what might be wrong (external flash timing), and may be giving you some test builds in a few days.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Dan,

My bride and I are going on a trip starting tomorrow for 11 days. We will be back home on the 25th.
This will be our first trip in over a year. I am positive you are sick of quarantine too, no matter how smart it is to do so.
Williamsburg, OBX, and our son and DIL in Charlotte, NC.

If you rush something to me today I can probably try it today, otherwise take your time making the test builds.

Bruce

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: QT Py RP2040 forgets CircuitPython

Post by danhalbert »

We have reproduced this on a couple of different RP2040 boards. We believe we can fix it with software; it's some kind of marginal timing problem. We'll be back in touch.

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Dan,

I win the obscure bug award for May 2021.
I am glad you were able to reproduce it. I was worried it was me.
I will be glad to test anything you need tested.

Bruce

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: QT Py RP2040 forgets CircuitPython

Post by danhalbert »

When you return, try out this UF2. You will need to unzip it before loading, of course. It has an extra-long delay to wait for the crystal oscillator to start.
Attachments
qtpy-6.3.0-test-clock-fix.uf2.zip
(422.94 KiB) Downloaded 4 times

User avatar
jamor3s
 
Posts: 3
Joined: Mon May 10, 2021 4:54 pm

Re: QT Py RP2040 forgets CircuitPython

Post by jamor3s »

Hi Dan,
I been having the same issue described by Bruce but in my case, trying to use MicroPython on the FeaterRP2040 (I'm building a UF2 from scratch, selecting ADAFRUIT_FEATHER_RP2040 as BOARD).

I was wondering if you could attach to this thread a DIFF of the changes done to overcome the timing issues on boot. Even if not directly applicable for my use case, I'll try to patch my Micropython and generate a new UF2.

Thanks a lot for your support,
Jose

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: QT Py RP2040 forgets CircuitPython

Post by danhalbert »

@jamor3s - glad to hear from you. It is a one-line change, in the pico-sdk:

Code: Select all

diff --git a/src/rp2_common/hardware_xosc/xosc.c b/src/rp2_common/hardware_xosc/xosc.c
index 977f0bd..9e43723 100644
--- a/src/rp2_common/hardware_xosc/xosc.c
+++ b/src/rp2_common/hardware_xosc/xosc.c
@@ -20,7 +20,7 @@ void xosc_init(void) {
 
     // Set xosc startup delay
     uint32_t startup_delay = (((12 * MHZ) / 1000) + 128) / 256;
-    xosc_hw->startup = startup_delay;
+    xosc_hw->startup = startup_delay * 64;
 
     // Set the enable bit now that we have set freq range and startup delay
     hw_set_bits(&xosc_hw->ctrl, XOSC_CTRL_ENABLE_VALUE_ENABLE << XOSC_CTRL_ENABLE_LSB);
There is also an interesting pico-sdk PR discussion going on here that we are participating in: https://github.com/raspberrypi/pico-sdk/pull/401

User avatar
jamor3s
 
Posts: 3
Joined: Mon May 10, 2021 4:54 pm

Re: QT Py RP2040 forgets CircuitPython

Post by jamor3s »

Hi @danhalbert,
After applying this patch, I can confirm that Micropython works flawlessly both after soft and hard resets.

@blakebr I hope you don't mind me having invaded your thread, surely this will fix your issue too ;)

Thanks again

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Jose,

Not at all. I was happy to see you experienced the same symptoms I was. Happier yet a probable solution was found.

Bruce

User avatar
blakebr
 
Posts: 942
Joined: Tue Apr 17, 2012 6:23 pm

Re: QT Py RP2040 forgets CircuitPython

Post by blakebr »

Dan,

Good news: 100% good with USB power pull, 10 for 10.
90% good with reset button pushed, 9 for 10.
One instance when the NeoPixel illuminated green and stayed there.
Another ten tests, 10 for 10 good.

Bruce

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”