adafruit / circuitpython

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

BLE/UART - Nordic dongle stops working - probably entering safe mode #9198

Closed gkecskes78 closed 6 months ago

gkecskes78 commented 7 months ago

CircuitPython version

Adafruit CircuitPython 9.0.4 on 2024-04-16; PCA10059 nRF52840 Dongle with nRF52840
Board ID:pca10059
UID:44AF58CE0AE80785

Code/REPL

import time, board, _bleio, pwmio
from adafruit_ble import BLERadio
from adafruit_ble.services.nordic import UARTService
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement

ble=BLERadio()
uart=UARTService()
advert=ProvideServicesAdvertisement(uart)
_bleio.adapter.enabled=True

time.sleep(2)
print('\n------------------- Start')
print('BLE addr: {}'.format(_bleio.adapter.address))
print('Device name: {}'.format(_bleio.adapter.name))
LED1=pwmio.PWMOut(board.LED1)

connected=False
counter=0
while True:
    if not ble.connected:
        if ble.advertising==False:
            ble.start_advertising(advert)
        if connected==True:
            LED1.duty_cycle=65535
            print("Connection lost")
        connected=False
    if ble.connected:
        if connected==False:
            counter+=1
            LED1.duty_cycle=60000
            print("\nConnection Nr. "+str(counter)+" established")
        for b in ble.connections:
            if connected==False:
                print("ble.connections: "+str(ble.connections))
        connected=True

Behavior

I am periodically setting up a BLE connection between 2 nordic dongles, both having the same CPO version 9.0.4. Recently a bug relating setting up a BLE connection and activating UART service was fixed - this is working now perfect. https://github.com/adafruit/Adafruit_CircuitPython_BLE/issues/192

The current issue now is related to the dongle, which gets connected. The dongle which gets connected, suddenly stops working after a short time. When it stops working, is random, it could happen after 20 loops, but also after 120. This is a resulting screenshot: REPL

Description

might be related to 192

Additional information

gkecskes78 commented 7 months ago

The following code I am using on the other dongle to connect and activate UART: main.zip

dhalbert commented 7 months ago

If you remove the PWMOut use, does it make any difference? Do you only have nRF52840 dongles to try, or do you also have, say, a Feather nRF52840? The latter uses external flash for CIRCUITPY.

Style note: if x: in this context can be used instead of if x == True:. It is idiomatic in Python. Similarly, one would say if not x: instead of if x==False:.

gkecskes78 commented 7 months ago

Thanks for the style note hint, I updated my code accordingly. Unfortunately no difference without PWMOut. We are working with nordic dongles(PCA10059) only but we have some nordic DK (PCA10056) as well. Could it be helpful to check on them ?

dhalbert commented 7 months ago

We are working with nordic dongles(PCA10059) only but we have some nordic DK (PCA10056) as well. Could it be helpful to check on them ?

Yes, please try. The CircuitPython build for PCA10059 uses on-board external flash chips for CIRCUITPY. If there is some issue with the chip-internal-flash CIRCUITPY code, then that would be a clue and differentiator.

gkecskes78 commented 7 months ago

I can confirm the same behaviour on PCA10056, CP version 9.0.4 (pca10056_bootloader-0.8.3_s140_6.1.1) and without PWMOut :( Let me know if I can help further.

dhalbert commented 6 months ago

I have reproduced this problem. with a simplified version of your test programs on a Feather and ItsyBitsy nRF52840 I see that it's looping forever in ble_drv_add_event_handler_entry(). I think this may be due to insufficient locking around a data structure, but need to look further.

EDIT: it was a gc problem.