Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FlyingThings opened this issue Aug 29, 2024 · 12 comments
Open

Comments

@FlyingThings
Copy link

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
Copy link
Member

tannewt commented Sep 12, 2024

Please retest with the absolute newest build.

@FlyingThings
Copy link
Author

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
Copy link
Member

tannewt commented Sep 13, 2024

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
Copy link
Collaborator

Could you try 9.2.0-beta.0?

@FlyingThings
Copy link
Author

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
Copy link
Collaborator

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.

@dhalbert dhalbert modified the milestones: 9.1.x, 9.2.0 Oct 1, 2024
@dhalbert dhalbert self-assigned this Oct 4, 2024
@FlyingThings
Copy link
Author

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
Copy link
Collaborator

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

@dhalbert
Copy link
Collaborator

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.

@dhalbert dhalbert modified the milestones: 9.2.0, 9.x.x Oct 18, 2024
@hartmutbohmer
Copy link

Same issue. Connecting and disconnecting loop with prior message exchange either alternating between two connections or one connection only. Tried different timings and enhanced double cleanup mechanism.

@gkecskes78
Copy link

gkecskes78 commented Feb 12, 2025

I observed entering into safe mode on nRF dongle with 9.2.4 when accidentally tried to establish a connection to an already existing connection. Just use the attached codes, hope it helps.

code.txt
main.txt

@FlyingThings
Copy link
Author

Apologies for the late response. Crashing on 9.2.4 when peripheral sends a message.

I want to add this: the number of connections the central shows is 1 even after the peripheral turns off / disconnects.

Here is the code for one of the paws circuitplayground bluefruit. Hopefully this is enough, as crashes occur with one device restart and message.
code.txt

@dhalbert dhalbert modified the milestones: 9.x.x, 10.x.x Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants