Skip to content

Commit

Permalink
pico-sdk: update btstack_config.h from pico-examples
Browse files Browse the repository at this point in the history
Update pico-sdk debug instructions.
  • Loading branch information
ricardoquesada committed Dec 29, 2024
1 parent db67037 commit 626da3e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 30 deletions.
62 changes: 60 additions & 2 deletions examples/pico_w/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Follow these instructions

And then build

```sh
```shell
mkdir build
cd build
cmake ..
Expand All @@ -36,7 +36,7 @@ make -j

If `PICO_BOARD` is not specified, it will use `pico_w`. To compile it for Pico 2 W, do:

```sh
```shell
# From the recently crated "build" folder
cmake -DPICO_BOARD=pico2_w ..
make -j
Expand All @@ -48,6 +48,64 @@ Use this guide if you are not sure how to do it:

* <https://projects.raspberrypi.org/en/projects/get-started-pico-w/>

## Debugging

In case you need to debug it using `gdb`, the recommended way is to get a [Pico Debug Probe module][pico_probe], and
follow the [Pico Debug Probe instructions][pico_probe_doc].

TL;DR: Open 3 terminals, and do:

### Terminal 1

```shell
# Edit CMakeList.txt file and enable UART console, and disable USB console
vim CMakeList.txt

# Make sure you have these lines:
> pico_enable_stdio_usb(bluepad32_picow_example_app 0)
> pico_enable_stdio_uart(bluepad32_picow_example_app 1)
```

```shell
# Compile in debug mode
mkdir build
cd build
cmake -DPICO_BOARD=pico_w -DCMAKE_BUILD_TYPE=Debug ..
make -j
```

```shell
# Flash it using OpenOCD
sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000" -c "program bluepad32_picow_example_app.elf verify reset exit"
```

```shell
# Open OpenOCD
sudo openocd -f interface/cmsis-dap.cfg -f target/rp2040.cfg -c "adapter speed 5000"
```

### Terminal 2

```shell
arm-none-eabi-gdb bluepad32_picow_example_app.elf
> target remote localhost:3333
> monitor reset init
> continue
```

### Terminal 3

```shell
# macOS
tio /dev/tty.usbmodem21202

# Linux
tio /dev/ttyACM0
```

[pico_probe]: https://www.raspberrypi.com/products/debug-probe/
[pico_probe_doc]: https://www.raspberrypi.com/documentation/microcontrollers/debug-probe.html

## License

- Example code: licensed under Public Domain.
Expand Down
40 changes: 29 additions & 11 deletions examples/pico_w/src/btstack_config.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
#ifndef _PICO_BTSTACK_BTSTACK_CONFIG_H
#define _PICO_BTSTACK_BTSTACK_CONFIG_H

// Copy & paste from, with some custom changes:
// https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/config/btstack_config.h

// BTstack features that can be enabled
#define ENABLE_LE_PERIPHERAL
#define ENABLE_LE_CENTRAL
#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
#define ENABLE_LOG_INFO
#define ENABLE_LOG_ERROR
#define ENABLE_PRINTF_HEXDUMP
#define ENABLE_SCO_OVER_HCI

#ifdef ENABLE_BLE
#define ENABLE_GATT_CLIENT_PAIRING
#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
#define ENABLE_LE_CENTRAL
#define ENABLE_LE_DATA_LENGTH_EXTENSION
#define ENABLE_LE_PERIPHERAL
#define ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION
#define ENABLE_LE_SECURE_CONNECTIONS
#else
#error "BP32: ENABLE_BLE should be defined"
#endif

#ifdef ENABLE_CLASSIC
#define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
#define ENABLE_GOEP_L2CAP
#else
#error "BP32: ENABLE_CLASSIC should be defined"
#endif

#if defined(ENABLE_CLASSIC) && defined(ENABLE_BLE)
#define ENABLE_CROSS_TRANSPORT_KEY_DERIVATION
#endif

// BTstack configuration. buffers, sizes, ...
#define HCI_OUTGOING_PRE_BUFFER_SIZE 4
#define HCI_ACL_PAYLOAD_SIZE (1691 + 4)
Expand Down Expand Up @@ -51,8 +74,7 @@
#define NVM_NUM_LINK_KEYS 16

// We don't give btstack a malloc, so use a fixed-size ATT DB.
// #define MAX_ATT_DB_SIZE 512
#define HAVE_MALLOC
#define MAX_ATT_DB_SIZE 512

// BTstack HAL configuration
#define HAVE_EMBEDDED_TIME_MS
Expand All @@ -68,11 +90,7 @@

#define HAVE_BTSTACK_STDIN

// To get the audio demos working even with HCI dump at 115200, this truncates long ACL packetws
// To get the audio demos working even with HCI dump at 115200, this truncates long ACL packets
// #define HCI_DUMP_STDOUT_MAX_SIZE_ACL 100

#ifdef ENABLE_CLASSIC
#define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
#endif

#endif // MICROPY_INCLUDED_EXTMOD_BTSTACK_BTSTACK_CONFIG_H
#endif // _PICO_BTSTACK_BTSTACK_CONFIG_H
31 changes: 17 additions & 14 deletions src/components/bluepad32/bt/uni_bt_le.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
* Execution order:
* uni_bt_le_on_gap_event_advertising_report()
* -> hog_connect()
* sm_packet_handler()
* uni_sm_packet_handler()
* wait for SM_EVENT_REENCRYPTION_COMPLETE or SM_EVENT_PAIRING_COMPLETE
* device_information_packet_handler()
* uni_device_information_packet_handler()
* wait for GATTSERVICE_SUBEVENT_DEVICE_INFORMATION_DONE
* hids_client_packet_handler()
* uni_hids_client_packet_handler()
* wait for GATTSERVICE_SUBEVENT_HID_SERVICE_CONNECTED
* uni_hid_device_set_ready()
*/
Expand Down Expand Up @@ -243,7 +243,7 @@ static void parse_report(const uint8_t* packet, uint16_t size) {
uni_hid_device_process_controller(device);
}

static void hids_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
static void uni_hids_client_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
uint8_t status;
uint16_t hids_cid;
uni_hid_device_t* device;
Expand All @@ -257,14 +257,14 @@ static void hids_client_packet_handler(uint8_t packet_type, uint16_t channel, ui
#if 0
// FIXME: Bug in BTStack??? This comparison fails because packet_type is HCI_EVENT_GATTSERVICE_META
if (packet_type != HCI_EVENT_PACKET) {
loge("hids_client_packet_handler: unsupported packet type: %#x\n", packet_type);
loge("uni_hids_client_packet_handler: unsupported packet type: %#x\n", packet_type);
return;
}
#endif

event_type = hci_event_packet_get_type(packet);
if (event_type != HCI_EVENT_GATTSERVICE_META) {
loge("hids_client_packet_handler: unsupported event type: %#x\n", event_type);
loge("uni_hids_client_packet_handler: unsupported event type: %#x\n", event_type);
return;
}

Expand Down Expand Up @@ -345,7 +345,10 @@ static void hids_client_packet_handler(uint8_t packet_type, uint16_t channel, ui
}
}

static void device_information_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
static void uni_device_information_packet_handler(uint8_t packet_type,
uint16_t channel,
uint8_t* packet,
uint16_t size) {
uint8_t code;
uint8_t status;
uint8_t att_status;
Expand All @@ -358,13 +361,13 @@ static void device_information_packet_handler(uint8_t packet_type, uint16_t chan
UNUSED(size);

if (packet_type != HCI_EVENT_PACKET) {
loge("device_information_packet_handler: unsupported packet type: %#x\n", packet_type);
loge("uni_device_information_packet_handler: unsupported packet type: %#x\n", packet_type);
return;
}

event_type = hci_event_packet_get_type(packet);
if (event_type != HCI_EVENT_GATTSERVICE_META) {
loge("device_information_packet_handler: unsupported event type: %#x\n", event_type);
loge("uni_device_information_packet_handler: unsupported event type: %#x\n", event_type);
return;
}

Expand Down Expand Up @@ -392,7 +395,7 @@ static void device_information_packet_handler(uint8_t packet_type, uint16_t chan

// Continue - query primary services.
logi("Search for HID service, con_handle: %#x\n", con_handle);
status = hids_client_connect(con_handle, hids_client_packet_handler, HID_PROTOCOL_MODE_REPORT,
status = hids_client_connect(con_handle, uni_hids_client_packet_handler, HID_PROTOCOL_MODE_REPORT,
&hids_cid);
if (status == ERROR_CODE_COMMAND_DISALLOWED) {
logi("HID client connection failed with COMMAND_DISALLOWED, ignoring \n");
Expand Down Expand Up @@ -538,7 +541,7 @@ static void device_information_packet_handler(uint8_t packet_type, uint16_t chan
* pairing. It also receives events generated during Identity Resolving see
* Listing SMPacketHandler.
*/
static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
static void uni_sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) {
bd_addr_t addr;
uni_hid_device_t* device;
uint8_t status;
Expand All @@ -550,7 +553,7 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* pa
ARG_UNUSED(size);

if (packet_type != HCI_EVENT_PACKET) {
loge("sm_packet_handler: unsupported packet type: %#x\n", packet_type);
loge("uni_sm_packet_handler: unsupported packet type: %#x\n", packet_type);
return;
}

Expand Down Expand Up @@ -666,7 +669,7 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t* pa

if (request_device_information_query) {
logi("Requesting device information\n");
status = device_information_service_client_query(con_handle, device_information_packet_handler);
status = device_information_service_client_query(con_handle, uni_device_information_packet_handler);
if (status != ERROR_CODE_SUCCESS) {
loge("Failed to set device information client: %#x\n", status);
}
Expand Down Expand Up @@ -870,7 +873,7 @@ void uni_bt_le_delete_bonded_keys(void) {

void uni_bt_le_setup(void) {
// register for events from Security Manager
sm_event_callback_registration.callback = &sm_packet_handler;
sm_event_callback_registration.callback = &uni_sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);

// Setup LE device db
Expand Down
6 changes: 3 additions & 3 deletions src/components/bluepad32/bt/uni_bt_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static fn_t setup_fns[] = {
&setup_set_event_filter,
};
static setup_state_t setup_state = SETUP_STATE_BTSTACK_IN_PROGRESS;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t uni_hci_event_callback_registration;

// SDP
// #define MAX_ATTRIBUTE_VALUE_SIZE 300
Expand Down Expand Up @@ -144,8 +144,8 @@ int uni_bt_setup(void) {
logi("BLE support: %s\n", ble_enabled ? "enabled" : "disabled");

// register for HCI events
hci_event_callback_registration.callback = &uni_bt_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
uni_hci_event_callback_registration.callback = &uni_bt_packet_handler;
hci_add_event_handler(&uni_hci_event_callback_registration);

if (IS_ENABLED(UNI_ENABLE_BREDR) && bredr_enabled)
uni_bt_bredr_setup();
Expand Down

0 comments on commit 626da3e

Please sign in to comment.