Skip to content

Commit b24754c

Browse files
committed
stm/rfcore: Add stm.rfcore_ble_hci function for raw ble hci comms.
1 parent a16a330 commit b24754c

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

docs/library/stm.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,11 @@ the second CPU, the RF core.
102102
Execute a HCI command on the SYS channel. The execution is synchronous.
103103

104104
Returns a bytes object with the result of the SYS command.
105+
106+
.. function:: rfcore_ble_hci(ogf, ocf, data) or rfcore_ble_hci(buffer)
107+
108+
Execute a HCI command on the BLE channel. The execution is synchronous.
109+
Can either specify ogf and ocf as integers along with bytes data to assemble hci packet
110+
or pass a byte/bytearray with pre-formatted hci packet.
111+
112+
Returns a bytes object with the result of the BLE command.

ports/stm32/modstm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ STATIC const mp_rom_map_elem_t stm_module_globals_table[] = {
5050
{ MP_ROM_QSTR(MP_QSTR_rfcore_status), MP_ROM_PTR(&rfcore_status_obj) },
5151
{ MP_ROM_QSTR(MP_QSTR_rfcore_fw_version), MP_ROM_PTR(&rfcore_fw_version_obj) },
5252
{ MP_ROM_QSTR(MP_QSTR_rfcore_sys_hci), MP_ROM_PTR(&rfcore_sys_hci_obj) },
53+
{ MP_ROM_QSTR(MP_QSTR_rfcore_ble_hci), MP_ROM_PTR(&rfcore_ble_hci_obj) },
5354
#endif
5455
};
5556

ports/stm32/rfcore.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ STATIC ssize_t tl_sys_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t l
503503
return tl_sys_wait_ack(ipcc_membuf_sys_cmd_buf, timeout_ms);
504504
}
505505

506-
STATIC int tl_ble_wait_resp(void) {
506+
STATIC ssize_t tl_ble_wait_resp(parse_hci_info_t *parse) {
507507
uint32_t t0 = mp_hal_ticks_ms();
508508
while (!LL_C2_IPCC_IsActiveFlag_CHx(IPCC, IPCC_CH_BLE)) {
509509
if (mp_hal_ticks_ms() - t0 > BLE_ACK_TIMEOUT_MS) {
@@ -513,16 +513,15 @@ STATIC int tl_ble_wait_resp(void) {
513513
}
514514

515515
// C2 set IPCC flag -- process the data, clear the flag, and re-enable IRQs.
516-
tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, NULL);
517-
return 0;
516+
return tl_check_msg(&ipcc_mem_ble_evt_queue, IPCC_CH_BLE, parse);
518517
}
519518

520519
// Synchronously send a BLE command.
521-
STATIC void tl_ble_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) {
520+
STATIC ssize_t tl_ble_hci_cmd_resp(uint16_t opcode, const uint8_t *buf, size_t len) {
522521
// Poll for completion rather than wait for IRQ->scheduler.
523522
LL_C1_IPCC_DisableReceiveChannel(IPCC, IPCC_CH_BLE);
524523
tl_hci_cmd(ipcc_membuf_ble_cmd_buf, IPCC_CH_BLE, HCI_KIND_BT_CMD, opcode, buf, len);
525-
tl_ble_wait_resp();
524+
return tl_ble_wait_resp(NULL);
526525
}
527526

528527
/******************************************************************************/
@@ -762,4 +761,40 @@ STATIC mp_obj_t rfcore_sys_hci(size_t n_args, const mp_obj_t *args) {
762761
}
763762
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj, 3, 4, rfcore_sys_hci);
764763

764+
STATIC void rfcore_ble_hci_response_to_buffer(void *env, const uint8_t *buf, size_t len) {
765+
// mp_buffer_info_t *respbufinfo = (mp_buffer_info_t *)env;
766+
DEBUG_printf("rfcore_ble_hci_response_to_buffer len 0x%x\n", len);
767+
mp_obj_t *rsp = (mp_obj_t *)env;
768+
*rsp = mp_obj_new_bytes(buf, len);
769+
}
770+
771+
STATIC mp_obj_t rfcore_ble_hci(size_t n_args, const mp_obj_t *args) {
772+
if (ipcc_mem_dev_info_tab.fus.table_state == MAGIC_IPCC_MEM_INCORRECT) {
773+
mp_raise_OSError(MP_EINVAL);
774+
}
775+
mp_buffer_info_t bufinfo = {0};
776+
// Can accept either just a raw buffer to send, or (ogf, ocf, buffer, <timeout>) to assemble and send.
777+
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) {
778+
// Poll for completion rather than wait for IRQ->scheduler.
779+
LL_C1_IPCC_DisableReceiveChannel(IPCC, IPCC_CH_BLE);
780+
781+
rfcore_ble_hci_cmd(bufinfo.len, bufinfo.buf);
782+
783+
mp_obj_t rsp = mp_const_none;
784+
parse_hci_info_t parse = { rfcore_ble_hci_response_to_buffer, &rsp, false };
785+
tl_ble_wait_resp(&parse);
786+
return rsp;
787+
} else {
788+
mp_int_t ogf = mp_obj_get_int(args[0]);
789+
mp_int_t ocf = mp_obj_get_int(args[1]);
790+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
791+
size_t len = tl_ble_hci_cmd_resp(HCI_OPCODE(ogf, ocf), bufinfo.buf, bufinfo.len);
792+
if (len < 0) {
793+
mp_raise_OSError(-len);
794+
}
795+
return mp_obj_new_bytes(ipcc_membuf_ble_cmd_buf, len);
796+
}
797+
}
798+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_ble_hci_obj, 1, 4, rfcore_ble_hci);
799+
765800
#endif // defined(STM32WB)

ports/stm32/rfcore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ void rfcore_end_flash_erase(void);
4343
MP_DECLARE_CONST_FUN_OBJ_0(rfcore_status_obj);
4444
MP_DECLARE_CONST_FUN_OBJ_1(rfcore_fw_version_obj);
4545
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_sys_hci_obj);
46+
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(rfcore_ble_hci_obj);
4647

4748
#endif // MICROPY_INCLUDED_STM32_RFCORE_H

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy