|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Nordic Semiconductor ASA. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <gpio.h> |
| 8 | +#include <uart.h> |
| 9 | +#include <device.h> |
| 10 | + |
| 11 | +#define RESET_PIN CONFIG_BOARD_NRF52840_GPIO_RESET_PIN |
| 12 | + |
| 13 | +/* Must be a pin from 17 to 23. |
| 14 | + * Only those can be connected to the nRF52840. |
| 15 | + */ |
| 16 | +BUILD_ASSERT_MSG(RESET_PIN > 16 && RESET_PIN < 24, |
| 17 | + "Selected pin is not connected to nRF52840"); |
| 18 | + |
| 19 | +int bt_hci_transport_setup(struct device *h4) |
| 20 | +{ |
| 21 | + int err; |
| 22 | + char c; |
| 23 | + struct device *port; |
| 24 | + |
| 25 | + port = device_get_binding(DT_GPIO_P0_DEV_NAME); |
| 26 | + if (!port) { |
| 27 | + return -EIO; |
| 28 | + } |
| 29 | + |
| 30 | + /* Pull the pin low before configuring it as output, to ensure that |
| 31 | + * it is driven to the correct level as soon as it is configured. |
| 32 | + */ |
| 33 | + err = gpio_pin_write(port, RESET_PIN, 0); |
| 34 | + if (err) { |
| 35 | + return err; |
| 36 | + } |
| 37 | + |
| 38 | + err = gpio_pin_configure(port, RESET_PIN, GPIO_DIR_OUT); |
| 39 | + if (err) { |
| 40 | + return err; |
| 41 | + } |
| 42 | + |
| 43 | + /* Reset the nRF52840 and let it wait until the pin is |
| 44 | + * pulled low again before running to main to ensure |
| 45 | + * that it won't send any data until the H4 device |
| 46 | + * is setup and ready to receive. |
| 47 | + */ |
| 48 | + err = gpio_pin_write(port, RESET_PIN, 1); |
| 49 | + if (err) { |
| 50 | + return err; |
| 51 | + } |
| 52 | + |
| 53 | + /* Wait for the nRF52840 peripheral to stop sending data. |
| 54 | + * |
| 55 | + * It is critical (!) to wait here, so that all bytes |
| 56 | + * on the lines are received and drained correctly. |
| 57 | + */ |
| 58 | + k_sleep(1); |
| 59 | + |
| 60 | + /* Drain bytes */ |
| 61 | + while (uart_fifo_read(h4, &c, 1)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + /* We are ready, let the nRF52840 run to main */ |
| 66 | + err = gpio_pin_write(port, RESET_PIN, 0); |
| 67 | + if (err) { |
| 68 | + return err; |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments