2019-04-17 18:48:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Nordic Semiconductor ASA.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 16:56:39 +08:00
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/drivers/uart.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/devicetree.h>
|
2022-11-07 16:12:25 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2019-04-17 18:48:39 +08:00
|
|
|
|
2021-01-26 17:36:35 +08:00
|
|
|
#define RESET_NODE DT_NODELABEL(nrf52840_reset)
|
2019-04-17 18:48:39 +08:00
|
|
|
|
2024-09-20 12:47:40 +08:00
|
|
|
#if DT_NODE_HAS_STATUS_OKAY(RESET_NODE)
|
2021-01-26 17:36:35 +08:00
|
|
|
|
|
|
|
#define RESET_GPIO_CTRL DT_GPIO_CTLR(RESET_NODE, gpios)
|
|
|
|
#define RESET_GPIO_PIN DT_GPIO_PIN(RESET_NODE, gpios)
|
|
|
|
#define RESET_GPIO_FLAGS DT_GPIO_FLAGS(RESET_NODE, gpios)
|
2019-04-17 18:48:39 +08:00
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
int bt_hci_transport_setup(const struct device *h4)
|
2019-04-17 18:48:39 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char c;
|
2022-08-22 16:36:10 +08:00
|
|
|
const struct device *const port = DEVICE_DT_GET(RESET_GPIO_CTRL);
|
2019-04-17 18:48:39 +08:00
|
|
|
|
2021-01-26 17:36:35 +08:00
|
|
|
if (!device_is_ready(port)) {
|
2019-04-17 18:48:39 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:36:35 +08:00
|
|
|
/* Configure pin as output and initialize it to inactive state. */
|
|
|
|
err = gpio_pin_configure(port, RESET_GPIO_PIN,
|
|
|
|
RESET_GPIO_FLAGS | GPIO_OUTPUT_INACTIVE);
|
2019-04-17 18:48:39 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:36:35 +08:00
|
|
|
/* Reset the nRF52840 and let it wait until the pin is inactive again
|
|
|
|
* before running to main to ensure that it won't send any data until
|
|
|
|
* the H4 device is setup and ready to receive.
|
2019-04-17 18:48:39 +08:00
|
|
|
*/
|
2021-01-26 17:36:35 +08:00
|
|
|
err = gpio_pin_set(port, RESET_GPIO_PIN, 1);
|
2019-04-17 18:48:39 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the nRF52840 peripheral to stop sending data.
|
|
|
|
*
|
|
|
|
* It is critical (!) to wait here, so that all bytes
|
|
|
|
* on the lines are received and drained correctly.
|
|
|
|
*/
|
2019-11-04 18:24:53 +08:00
|
|
|
k_sleep(K_MSEC(10));
|
2019-04-17 18:48:39 +08:00
|
|
|
|
|
|
|
/* Drain bytes */
|
2021-06-17 00:25:15 +08:00
|
|
|
while (h4 && uart_fifo_read(h4, &c, 1)) {
|
2019-04-17 18:48:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are ready, let the nRF52840 run to main */
|
2021-01-26 17:36:35 +08:00
|
|
|
err = gpio_pin_set(port, RESET_GPIO_PIN, 0);
|
2019-04-17 18:48:39 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-26 17:36:35 +08:00
|
|
|
|
2024-09-20 12:47:40 +08:00
|
|
|
#endif /* DT_NODE_HAS_STATUS_OKAY(RESET_NODE) */
|