Skip to content

Commit

Permalink
Make sure BT device address is set. (#1284)
Browse files Browse the repository at this point in the history
* Make sure BT device address is set.
* Change cyw43_hal_generate_laa_mac to match MicroPython
  • Loading branch information
peterharperuk authored Feb 27, 2023
1 parent b1d4ba5 commit d9c88c6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/rp2_common/pico_cyw43_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE})
pico_add_library(pico_btstack_hci_transport_cyw43 NOFLAG)
target_sources(pico_btstack_hci_transport_cyw43 INTERFACE
${CMAKE_CURRENT_LIST_DIR}/btstack_hci_transport_cyw43.c
${CMAKE_CURRENT_LIST_DIR}/btstack_chipset_cyw43.c
)
target_include_directories(pico_btstack_hci_transport_cyw43_headers INTERFACE
${CMAKE_CURRENT_LIST_DIR}/include
Expand Down
26 changes: 26 additions & 0 deletions src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "pico/btstack_chipset_cyw43.h"

static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer) {
hci_cmd_buffer[0] = 0x01;
hci_cmd_buffer[1] = 0xfc;
hci_cmd_buffer[2] = 0x06;
reverse_bd_addr(addr, &hci_cmd_buffer[3]);
}

static const btstack_chipset_t btstack_chipset_cyw43 = {
.name = "CYW43",
.init = NULL,
.next_command = NULL,
.set_baudrate_command = NULL,
.set_bd_addr_command = chipset_set_bd_addr_command,
};

const btstack_chipset_t * btstack_chipset_cyw43_instance(void) {
return &btstack_chipset_cyw43;
}
2 changes: 1 addition & 1 deletion src/rp2_common/pico_cyw43_driver/btstack_cyw43.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool btstack_cyw43_init(async_context_t *context) {
#endif
#endif

hci_init(hci_transport_cyw43_instance(), NULL);
hci_init(hci_transport_cyw43_instance(), NULL);

// setup TLV storage
setup_tlv();
Expand Down
10 changes: 10 additions & 0 deletions src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "hci_transport.h"
#include "hci.h"
#include "pico/btstack_hci_transport_cyw43.h"
#include "pico/btstack_chipset_cyw43.h"

// assert outgoing pre-buffer for cyw43 header is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 4)
Expand Down Expand Up @@ -59,6 +60,15 @@ static int hci_transport_cyw43_open(void) {
return err;
}

// OTP should be set in which case BT gets an address of wifi mac + 1
// If OTP is not set for some reason BT gets set to 43:43:A2:12:1F:AC.
// So for safety, set the bluetooth device address here.
bd_addr_t addr;
cyw43_hal_get_mac(0, (uint8_t*)&addr);
addr[BD_ADDR_LEN - 1]++;
hci_set_chipset(btstack_chipset_cyw43_instance());
hci_set_bd_addr(addr);

btstack_run_loop_set_data_source_handler(&transport_data_source, &hci_transport_data_source_process);
btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
btstack_run_loop_add_data_source(&transport_data_source);
Expand Down
15 changes: 9 additions & 6 deletions src/rp2_common/pico_cyw43_driver/cyw43_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,16 @@ uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num
}

// Generate a mac address if one is not set in otp
void __attribute__((weak)) cyw43_hal_generate_laa_mac(__unused int idx, uint8_t buf[6]) {
void __attribute__((weak)) cyw43_hal_generate_laa_mac(int idx, uint8_t buf[6]) {
CYW43_DEBUG("Warning. No mac in otp. Generating mac from board id\n");
pico_unique_board_id_t board_id;
pico_get_unique_board_id(&board_id);
memcpy(buf, &board_id.id[2], 6);
buf[0] &= (uint8_t)~0x1; // unicast
buf[0] |= 0x2; // locally administered
pico_unique_board_id_t pid;
pico_get_unique_board_id(&pid);
buf[0] = 0x02; // LAA range
buf[1] = (pid.id[7] << 4) | (pid.id[6] & 0xf);
buf[2] = (pid.id[5] << 4) | (pid.id[4] & 0xf);
buf[3] = (pid.id[3] << 4) | (pid.id[2] & 0xf);
buf[4] = pid.id[1];
buf[5] = (pid.id[0] << 2) | idx;
}

// Return mac address
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef _PICO_BTSTACK_CHIPSET_CYW43_H
#define _PICO_BTSTACK_CHIPSET_CYW43_H

#include "btstack_chipset.h"

/**
* \brief Return the singleton BTstack chipset CY43 API instance
* \ingroup pico_btstack
*/
const btstack_chipset_t * btstack_chipset_cyw43_instance(void);

#endif

0 comments on commit d9c88c6

Please sign in to comment.