Sitelet https://github.com/micropython/micropython/issues/5686
Skip to content

ESP23 - Scanning Bluetooth Characteristics results in Kernel Panics #5686

Description

@JohannesNeu

Hello,

I have modified some bluetooth code to read the characteristics of a xiamoi flower meter (hardcoded a mac address for my first tests). I can scan the devices and list the services using the interrupt method described in the documentation. But as soon as I try to retrieve the characteristics I get a kernel panic.

This is the code I use:

import time
import bluetooth
import binascii
from micropython import const

_IRQ_CENTRAL_CONNECT                 = const(1 << 0)
_IRQ_CENTRAL_DISCONNECT              = const(1 << 1)
_IRQ_GATTS_WRITE                     = const(1 << 2)
_IRQ_GATTS_READ_REQUEST              = const(1 << 3)
_IRQ_SCAN_RESULT                     = const(1 << 4)
_IRQ_SCAN_COMPLETE                   = const(1 << 5)
_IRQ_PERIPHERAL_CONNECT              = const(1 << 6)
_IRQ_PERIPHERAL_DISCONNECT           = const(1 << 7)
_IRQ_GATTC_SERVICE_RESULT            = const(1 << 8)
_IRQ_GATTC_CHARACTERISTIC_RESULT     = const(1 << 9)
_IRQ_GATTC_DESCRIPTOR_RESULT         = const(1 << 10)
_IRQ_GATTC_READ_RESULT               = const(1 << 11)
_IRQ_GATTC_WRITE_STATUS              = const(1 << 12)
_IRQ_GATTC_NOTIFY                    = const(1 << 13)
_IRQ_GATTC_INDICATE                  = const(1 << 14)

def bt_irq(event, data):
    if event == _IRQ_CENTRAL_CONNECT:
        conn_handle, addr_type, addr = data

    elif event == _IRQ_CENTRAL_DISCONNECT:
        conn_handle, addr_type, addr = data

    elif event == _IRQ_GATTS_WRITE:
        conn_handle, attr_handle = data

    elif event == _IRQ_GATTS_READ_REQUEST:
        conn_handle, attr_handle = data

    elif event == _IRQ_SCAN_RESULT:
        addr_type, addr, connectable, rssi, adv_data = data
        print(addr)
        if addr == b'\xc4|\x8df\xdd\xf0':
            print(data)
            bt.gap_connect(addr_type, addr, 2000)

    elif event == _IRQ_SCAN_COMPLETE:
        pass

    elif event == _IRQ_PERIPHERAL_CONNECT:
        conn_handle, addr_type, addr = data
        bt.gattc_discover_services(conn_handle)

    elif event == _IRQ_PERIPHERAL_DISCONNECT:
        conn_handle, addr_type, addr = data

    elif event == _IRQ_GATTC_SERVICE_RESULT:
        conn_handle, start_handle, end_handle, uuid = data
        if str(uuid) == "UUID16(0xfef5)":
            bt.gattc_discover_characteristics(conn_handle, start_handle, end_handle)

    elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
        conn_handle, def_handle, value_handle, properties, uuid = data
        print(data)

    elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
        conn_handle, dsc_handle, uuid = data

    elif event == _IRQ_GATTC_READ_RESULT:
        conn_handle, value_handle, char_data = data

    elif event == _IRQ_GATTC_WRITE_STATUS:
        conn_handle, value_handle, status = data

    elif event == _IRQ_GATTC_NOTIFY:
        conn_handle, value_handle, notify_data = data

    elif event == _IRQ_GATTC_INDICATE:
        conn_handle, value_handle, notify_data = data

bt = bluetooth.BLE()
bt.active(True)
bt.irq(handler=bt_irq)
bt.gap_scan(10000, 10000, 10000)

time.sleep(20)

and this is the error message i see in repl:

Entering REPL. Use Control-X to exit.
>
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
>>> 
>>> import irqtry
I (232800) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
b'(\xff<\xb5#.'
b'\xc4|\x8df\xdd\xf0'
(0, b'\xc4|\x8df\xdd\xf0', True, -64, b'\x02\x01\x06\x03\x02\x95\xfe\x15\x16\x95\xfeq \x98\x00\x00\xf0\xddf\x8d|\xc4\r\x07\x10\x03\x00\x00\x00')
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x40151b34  PS      : 0x00060430  A0      : 0x80158bdb  A1      : 0x3ffbb530  
A2      : 0x3ffc6e2c  A3      : 0x00000000  A4      : 0x3f4286fe  A5      : 0x3ffc2b94  
A6      : 0x00000003  A7      : 0x3ffc2b68  A8      : 0x00000000  A9      : 0x3ffbb4f0  
A10     : 0x3ffc6e2c  A11     : 0x00000000  A12     : 0x00000004  A13     : 0x3ffbb530  
A14     : 0x00000001  A15     : 0x00000000  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000004  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000000000000000000000000000000000000000000000000000

Backtrace: 0x40151b31:0x3ffbb530 0x40158bd8:0x3ffbb560 0x40153af9:0x3ffbb5a0 0x40155f31:0x3ffbb5d0 0x40153781:0x3ffbb600 0x40153793:0x3ffbb620 0x401526a6:0x3ffbb640 0x400f78c7:0x3ffbb660 0x40097615:0x3ffbb680

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4988
load:0x40078000,len:10404
load:0x40080400,len:5680
entry 0x400806bc
I (519) cpu_start: Pro cpu up.
I (519) cpu_start: Application information:
I (519) cpu_start: Compile time:     Dec 20 2019 07:56:38
I (522) cpu_start: ELF file SHA256:  0000000000000000...
I (528) cpu_start: ESP-IDF:          v4.0-beta1
I (533) cpu_start: Starting app cpu, entry point is 0x40083014
I (526) cpu_start: App cpu up.
I (544) heap_init: Initializing. RAM available for dynamic allocation:
I (551) heap_init: At 3FFAFF10 len 000000F0 (0 KiB): DRAM
I (557) heap_init: At 3FFB6388 len 00001C78 (7 KiB): DRAM
I (563) heap_init: At 3FFB9A20 len 00004108 (16 KiB): DRAM
I (569) heap_init: At 3FFBDB5C len 00000004 (0 KiB): DRAM
I (575) heap_init: At 3FFCC8A0 len 00013760 (77 KiB): DRAM
I (581) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (588) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (594) heap_init: At 40099FB8 len 00006048 (24 KiB): IRAM
I (600) cpu_start: Pro cpu start user code
I (619) spi_flash: detected chip: generic
I (619) spi_flash: flash io: dio
I (619) cpu_start: Chip Revision: 1
W (621) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
I (632) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
>>> 

My tests were done with esp32-idf4-20191220-v1.12.bin on a JZK® ESP-32S ESP32 Development Board as well as a TTGO ESP32.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions