2019-04-17 18:48:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Nordic Semiconductor ASA.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-06-26 03:53:52 +08:00
|
|
|
#include <drivers/gpio.h>
|
2019-06-26 03:54:01 +08:00
|
|
|
#include <drivers/uart.h>
|
2019-04-17 18:48:39 +08:00
|
|
|
#include <device.h>
|
|
|
|
|
|
|
|
#define RESET_PIN CONFIG_BOARD_NRF52840_GPIO_RESET_PIN
|
|
|
|
|
|
|
|
/* Must be a pin from 17 to 23.
|
|
|
|
* Only those can be connected to the nRF52840.
|
|
|
|
*/
|
2020-03-12 23:16:00 +08:00
|
|
|
BUILD_ASSERT(RESET_PIN > 16 && RESET_PIN < 24,
|
|
|
|
"Selected pin is not connected to nRF52840");
|
2019-04-17 18:48:39 +08:00
|
|
|
|
|
|
|
int bt_hci_transport_setup(struct device *h4)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char c;
|
|
|
|
struct device *port;
|
|
|
|
|
2020-04-17 19:21:46 +08:00
|
|
|
port = device_get_binding(DT_LABEL(DT_NODELABEL(gpio0)));
|
2019-04-17 18:48:39 +08:00
|
|
|
if (!port) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2019-08-06 06:56:05 +08:00
|
|
|
/* Configure pin as output and initialize it to low. */
|
|
|
|
err = gpio_pin_configure(port, RESET_PIN, GPIO_OUTPUT_LOW);
|
2019-04-17 18:48:39 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the nRF52840 and let it wait until the pin is
|
|
|
|
* pulled low 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-08-06 06:56:05 +08:00
|
|
|
err = gpio_pin_set(port, RESET_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 */
|
|
|
|
while (uart_fifo_read(h4, &c, 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are ready, let the nRF52840 run to main */
|
2019-08-06 06:56:05 +08:00
|
|
|
err = gpio_pin_set(port, RESET_PIN, 0);
|
2019-04-17 18:48:39 +08:00
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|