adafruit / circuitpython

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

Board enters safe mode after disconnect from 1 of 2 BLE connections. #9573

Open FlyingThings opened 2 months ago

FlyingThings commented 2 months ago

CircuitPython version

Adafruit CircuitPython 9.1.1 on 2024-07-23; FeatherS3 with ESP32S3
Board ID:unexpectedmaker_feathers3
UID:0740D1DA1CCA

Code/REPL

import board
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
import neopixel
import time

def monitor():
    ble = BLERadio()
    #uart = UARTService()
    #advertisement = ProvideServicesAdvertisement(uart)
    pix = neopixel.NeoPixel(board.NEOPIXEL,10)
    tog = True
    last_time_tog = time.monotonic()

    while True:
        try:
            #connect to two uart devices.
            while ble.connected and any(UARTService in connection for connection in ble.connections) and len(ble.connections) == 2:

                try:
                    if right_paw_uart.in_waiting:
                        print(right_paw_uart.read(4))
                    if left_paw_uart.in_waiting:
                        print(left_paw_uart.read(4))
                    if time.monotonic()-last_time_tog>=1:
                        last_time_tog = time.monotonic()
                        print('here')
                        if tog:
                            pix.fill(0xff0000)
                            tog = not tog
                        else:
                            pix.fill(0)
                            tog = not tog
                except ConnectionError:
                    print('NOT CONNECTED')
                    break

            print("disconnected, scanning", len(ble.connections))
            try:
                for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=1):
                    if UARTService not in advertisement.services:
                        continue
                    #ble.connect(advertisement)
                    if advertisement.complete_name == "RIGHTPAW":
                        right_paw = ble.connect(advertisement)
                        right_paw_uart = right_paw[UARTService]
                    if advertisement.complete_name == "LEFTPAW":
                        left_paw = ble.connect(advertisement)
                        left_paw_uart = left_paw[UARTService]
                    print(f"connected {advertisement.complete_name}")
                    break
            except ConnectionError:
                print('connection error:not connected')
            ble.stop_scan()
        except ConnectionError as er:
            print('IDK its a connection error:',er)
            time.sleep(1)
monitor()

Behavior

Board will connect to 2 devices "LEFTPAW" and "RIGHTPAW" normally, and receive messages as expected. Restarting one of the connected devices once is usually OK, code will reconnect normally. After a number of restarts of a connected device (sometimes up to 3 restarts) this board will hang, and then finally crash to safe mode. Here is the caught error immediately before safe mode:

Connection has been disconnected and can no longer be used. Create a new connection.

And the safe mode message:

You are in safe mode because: CircuitPython core code crashed hard. Whoops! Hard fault: memory access or instruction error. Please file an issue with your program at github.com/adafruit/circuitpython/issues. Press reset to exit safe mode.

Description

No response

Additional information

I have similar code running without issue on a Feather NRF52840. The connected devices are Circuit Playground Bluefruit boards running CircuitPython.

tannewt commented 1 month ago

Please retest with the absolute newest build.

FlyingThings commented 1 month ago

Same issue as described above. Here is the error after it restarts into safe mode:

Adafruit CircuitPython 9.1.3-2-gfad755d5e7 on 2024-09-12; FeatherS3 with ESP32S3
>>>
soft reboot

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.
Press reset to exit safe mode.

I appreciate the help!

tannewt commented 1 month ago

Thanks for the retest! It looks like you got the latest of the 9.1.x branch. Please try https://adafruit-circuit-python.s3.amazonaws.com/bin/unexpectedmaker_feathers3/en_US/adafruit-circuitpython-unexpectedmaker_feathers3-en_US-20240913-main-PR9619-64cb762.uf2 which is on the main branch and includes IDF updates.

dhalbert commented 1 month ago

Could you try 9.2.0-beta.0?

FlyingThings commented 1 month ago

9.1.x same behavior. Crashes when connected device disconnects.

Adafruit CircuitPython 9.2.0-alpha.2351-35-g64cb76213f on 2024-09-13; FeatherS3 with ESP32S3
>>>
soft reboot

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
Please file an issue with your program at github.com/adafruit/circuitpython/issues.
Press reset to exit safe mode.

Press any key to enter the REPL. Use CTRL-D to reload.

9.2.0 beta.0 has a little bit different behavior, can initially connect, and reconnect, but one a message is received it crashes.


Adafruit CircuitPython 9.2.0-beta.0 on 2024-09-17; FeatherS3 with ESP32S3
>>>
soft reboot

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
dhalbert commented 1 month ago

If you just connect one device, and then restart that device a few times, do you see the same or a similar issue? I am interested in knowing if we need to test with two "paws" or not.

FlyingThings commented 3 weeks ago

OK retested with just one connection. Still crashing once a message is sent from the peripheral.

Adafruit CircuitPython 9.2.0-beta.0 on 2024-09-17; FeatherS3 with ESP32S3
>>>
soft reboot

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.

also tried the latest beta and same behavior

Adafruit CircuitPython 9.2.0-beta.1-1-g55823ba3b0 on 2024-10-11; FeatherS3 with ESP32S3
>>>
soft reboot

Auto-reload is off.
Running in safe mode! Not running saved code.

You are in safe mode because:
CircuitPython core code crashed hard. Whoops!
Hard fault: memory access or instruction error.
dhalbert commented 3 weeks ago

Could you post or point to the code for the LEFTPAW and RIGHTPAW devices? Confirming that these are CPB's.

dhalbert commented 2 weeks ago

I have been trying this with some similar code, and haven't yet been able to reproduce. I'm moving this forward to 9.x.x for now.