Skip to content

Commit d9c88c6

Browse files
Make sure BT device address is set. (#1284)
* Make sure BT device address is set. * Change cyw43_hal_generate_laa_mac to match MicroPython
1 parent b1d4ba5 commit d9c88c6

File tree

6 files changed

+65
-7
lines changed

6 files changed

+65
-7
lines changed

src/rp2_common/pico_cyw43_driver/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE})
6161
pico_add_library(pico_btstack_hci_transport_cyw43 NOFLAG)
6262
target_sources(pico_btstack_hci_transport_cyw43 INTERFACE
6363
${CMAKE_CURRENT_LIST_DIR}/btstack_hci_transport_cyw43.c
64+
${CMAKE_CURRENT_LIST_DIR}/btstack_chipset_cyw43.c
6465
)
6566
target_include_directories(pico_btstack_hci_transport_cyw43_headers INTERFACE
6667
${CMAKE_CURRENT_LIST_DIR}/include
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "pico/btstack_chipset_cyw43.h"
8+
9+
static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer) {
10+
hci_cmd_buffer[0] = 0x01;
11+
hci_cmd_buffer[1] = 0xfc;
12+
hci_cmd_buffer[2] = 0x06;
13+
reverse_bd_addr(addr, &hci_cmd_buffer[3]);
14+
}
15+
16+
static const btstack_chipset_t btstack_chipset_cyw43 = {
17+
.name = "CYW43",
18+
.init = NULL,
19+
.next_command = NULL,
20+
.set_baudrate_command = NULL,
21+
.set_bd_addr_command = chipset_set_bd_addr_command,
22+
};
23+
24+
const btstack_chipset_t * btstack_chipset_cyw43_instance(void) {
25+
return &btstack_chipset_cyw43;
26+
}

src/rp2_common/pico_cyw43_driver/btstack_cyw43.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ bool btstack_cyw43_init(async_context_t *context) {
5959
#endif
6060
#endif
6161

62-
hci_init(hci_transport_cyw43_instance(), NULL);
62+
hci_init(hci_transport_cyw43_instance(), NULL);
6363

6464
// setup TLV storage
6565
setup_tlv();

src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "hci_transport.h"
1010
#include "hci.h"
1111
#include "pico/btstack_hci_transport_cyw43.h"
12+
#include "pico/btstack_chipset_cyw43.h"
1213

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

63+
// OTP should be set in which case BT gets an address of wifi mac + 1
64+
// If OTP is not set for some reason BT gets set to 43:43:A2:12:1F:AC.
65+
// So for safety, set the bluetooth device address here.
66+
bd_addr_t addr;
67+
cyw43_hal_get_mac(0, (uint8_t*)&addr);
68+
addr[BD_ADDR_LEN - 1]++;
69+
hci_set_chipset(btstack_chipset_cyw43_instance());
70+
hci_set_bd_addr(addr);
71+
6272
btstack_run_loop_set_data_source_handler(&transport_data_source, &hci_transport_data_source_process);
6373
btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
6474
btstack_run_loop_add_data_source(&transport_data_source);

src/rp2_common/pico_cyw43_driver/cyw43_driver.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num
129129
}
130130

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

141144
// Return mac address
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#ifndef _PICO_BTSTACK_CHIPSET_CYW43_H
8+
#define _PICO_BTSTACK_CHIPSET_CYW43_H
9+
10+
#include "btstack_chipset.h"
11+
12+
/**
13+
* \brief Return the singleton BTstack chipset CY43 API instance
14+
* \ingroup pico_btstack
15+
*/
16+
const btstack_chipset_t * btstack_chipset_cyw43_instance(void);
17+
18+
#endif

0 commit comments

Comments
 (0)