Skip to content

Commit

Permalink
Bluetooth: drivers: Convert STM32 IPM driver to new API
Browse files Browse the repository at this point in the history
Convert the ipm_stm32wb.c HCI driver to the new HCI driver API.

Signed-off-by: Johan Hedberg <johan.hedberg@gmail.com>
  • Loading branch information
jhedberg authored and nashif committed Jun 11, 2024
1 parent b7b606b commit 501e715
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
2 changes: 2 additions & 0 deletions drivers/bluetooth/hci/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ config BT_SPI

config BT_STM32_IPM
bool "IPM HCI"
default y
depends on DT_HAS_ST_STM32WB_RF_ENABLED
select USE_STM32_HAL_CORTEX
select HAS_STM32LIB
help
Expand Down
48 changes: 33 additions & 15 deletions drivers/bluetooth/hci/ipm_stm32wb.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <zephyr/init.h>
#include <zephyr/sys/util.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include <zephyr/drivers/bluetooth.h>
#include <zephyr/bluetooth/addr.h>
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
#include <zephyr/irq.h>
Expand All @@ -21,7 +21,11 @@
#include "shci.h"
#include "shci_tl.h"

static const struct stm32_pclken clk_cfg[] = STM32_DT_CLOCKS(DT_NODELABEL(ble_rf));
struct hci_data {
bt_hci_recv_t recv;
};

static const struct stm32_pclken clk_cfg[] = STM32_DT_CLOCKS(DT_DRV_INST(0));

#define POOL_SIZE (CFG_TLBLE_EVT_QUEUE_LENGTH * 4 * \
DIVC((sizeof(TL_PacketHeader_t) + TL_BLE_EVENT_FRAME_SIZE), 4))
Expand Down Expand Up @@ -156,7 +160,9 @@ void TM_EvtReceivedCb(TL_EvtPacket_t *hcievt)

static void bt_ipm_rx_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
const struct device *dev = p1;
struct hci_data *hci = dev->data;

ARG_UNUSED(p2);
ARG_UNUSED(p3);

Expand Down Expand Up @@ -245,7 +251,7 @@ static void bt_ipm_rx_thread(void *p1, void *p2, void *p3)

TL_MM_EvtDone(hcievt);

bt_recv(buf);
hci->recv(dev, buf);
end_loop:
k_sem_give(&ipm_busy);
}
Expand Down Expand Up @@ -347,10 +353,12 @@ void transport_init(void)
TL_Enable();
}

static int bt_ipm_send(struct net_buf *buf)
static int bt_ipm_send(const struct device *dev, struct net_buf *buf)
{
TL_CmdPacket_t *ble_cmd_buff = &BleCmdBuffer;

ARG_UNUSED(dev);

k_sem_take(&ipm_busy, K_FOREVER);

switch (bt_buf_get_type(buf)) {
Expand Down Expand Up @@ -534,8 +542,9 @@ static int c2_reset(void)
return 0;
}

static int bt_ipm_open(void)
static int bt_ipm_open(const struct device *dev, bt_hci_recv_t recv)
{
struct hci_data *hci = dev->data;
int err;

if (!c2_started_flag) {
Expand All @@ -553,7 +562,7 @@ static int bt_ipm_open(void)
/* Start RX thread */
k_thread_create(&ipm_rx_thread_data, ipm_rx_stack,
K_KERNEL_STACK_SIZEOF(ipm_rx_stack),
bt_ipm_rx_thread, NULL, NULL, NULL,
bt_ipm_rx_thread, (void *)dev, NULL, NULL,
K_PRIO_COOP(CONFIG_BT_DRIVER_RX_HIGH_PRIO),
0, K_NO_WAIT);

Expand All @@ -564,14 +573,17 @@ static int bt_ipm_open(void)
}
#endif /* CONFIG_BT_HCI_HOST */

hci->recv = recv;

LOG_DBG("IPM Channel Open Completed");

return 0;
}

#ifdef CONFIG_BT_HCI_HOST
static int bt_ipm_close(void)
static int bt_ipm_close(const struct device *dev)
{
struct hci_data *hci = dev->data;
int err;
struct net_buf *rsp;

Expand All @@ -590,28 +602,27 @@ static int bt_ipm_close(void)

k_thread_abort(&ipm_rx_thread_data);

hci->recv = NULL;

LOG_DBG("IPM Channel Close Completed");

return err;
}
#endif /* CONFIG_BT_HCI_HOST */

static const struct bt_hci_driver drv = {
.name = "BT IPM",
.bus = BT_HCI_DRIVER_BUS_IPM,
static const struct bt_hci_driver_api drv = {
.open = bt_ipm_open,
#ifdef CONFIG_BT_HCI_HOST
.close = bt_ipm_close,
#endif
.send = bt_ipm_send,
};

static int _bt_ipm_init(void)
static int _bt_ipm_init(const struct device *dev)
{
int err;


bt_hci_driver_register(&drv);
ARG_UNUSED(dev);

err = c2_reset();
if (err) {
Expand All @@ -621,4 +632,11 @@ static int _bt_ipm_init(void)
return 0;
}

SYS_INIT(_bt_ipm_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
#define HCI_DEVICE_INIT(inst) \
static struct hci_data hci_data_##inst = { \
}; \
DEVICE_DT_INST_DEFINE(inst, _bt_ipm_init, NULL, &hci_data_##inst, NULL, \
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &drv)

/* Only one instance supported right now */
HCI_DEVICE_INIT(0)
1 change: 1 addition & 0 deletions dts/arm/st/wb/stm32wb.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
chosen {
zephyr,entropy = &rng;
zephyr,flash-controller = &flash;
zephyr,bt-hci = &ble_rf;
};

cpus {
Expand Down
10 changes: 7 additions & 3 deletions dts/bindings/bluetooth/st,stm32wb-ble-rf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ description: |
compatible: "st,stm32wb-rf"

include: base.yaml
include: bt-hci.yaml

properties:
clocks:
required: true
clocks:
required: true
bt-hci-name:
default: "BT IPM"
bt-hci-bus:
default: "BT_HCI_BUS_IPM"

0 comments on commit 501e715

Please sign in to comment.