2016-01-13 17:14:39 +02:00
|
|
|
/* uart.c - Nordic BLE UART based Bluetooth driver */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-01-13 17:14:39 +02:00
|
|
|
*/
|
|
|
|
|
2016-03-09 15:22:04 -03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-10-27 11:40:43 +02:00
|
|
|
#include <zephyr.h>
|
2016-01-27 10:08:09 +02:00
|
|
|
#include <sections.h>
|
2016-01-13 17:14:39 +02:00
|
|
|
|
|
|
|
#include <board.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <uart.h>
|
|
|
|
#include <string.h>
|
2016-04-01 10:38:50 +03:00
|
|
|
#include <gpio.h>
|
2016-01-13 17:14:39 +02:00
|
|
|
|
|
|
|
#include <net/buf.h>
|
|
|
|
|
2017-01-17 11:01:47 +02:00
|
|
|
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLUETOOTH_DEBUG_HCI_DRIVER)
|
2016-01-13 17:14:39 +02:00
|
|
|
#include <bluetooth/log.h>
|
|
|
|
|
2016-12-08 11:56:49 -06:00
|
|
|
#include "../util.h"
|
2016-01-21 15:36:47 +02:00
|
|
|
#include "rpc.h"
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-04-05 14:51:34 +03:00
|
|
|
#if defined(CONFIG_BLUETOOTH_NRF51_PM)
|
2016-12-08 11:56:49 -06:00
|
|
|
#include "../nrf51_pm.h"
|
2016-04-05 14:51:34 +03:00
|
|
|
#endif
|
|
|
|
|
2016-02-11 11:35:03 +02:00
|
|
|
/**
|
|
|
|
* @note this structure must be self-aligned and self-packed
|
|
|
|
*/
|
|
|
|
struct ipc_uart_header {
|
2017-04-20 12:00:29 -05:00
|
|
|
u16_t len; /**< Length of IPC message. */
|
|
|
|
u8_t channel; /**< Channel number of IPC message. */
|
|
|
|
u8_t src_cpu_id; /**< CPU id of IPC sender. */
|
2016-02-11 11:35:03 +02:00
|
|
|
} __packed;
|
|
|
|
|
2016-01-13 17:14:39 +02:00
|
|
|
/* TODO: check size */
|
2016-02-05 13:46:08 +02:00
|
|
|
#define NBLE_TX_BUF_COUNT 2
|
2016-05-09 12:14:19 +03:00
|
|
|
#define NBLE_RX_BUF_COUNT 10
|
2016-02-05 13:46:08 +02:00
|
|
|
#define NBLE_BUF_SIZE 384
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-10-18 23:24:51 +03:00
|
|
|
NET_BUF_POOL_DEFINE(rx_pool, NBLE_RX_BUF_COUNT, NBLE_BUF_SIZE, 0, NULL);
|
|
|
|
NET_BUF_POOL_DEFINE(tx_pool, NBLE_TX_BUF_COUNT, NBLE_BUF_SIZE, 0, NULL);
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-11-10 22:01:50 +02:00
|
|
|
static BT_STACK_NOINIT(rx_thread_stack, CONFIG_BLUETOOTH_RX_STACK_SIZE);
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2017-05-09 12:00:09 -07:00
|
|
|
static struct k_thread rx_thread_data;
|
|
|
|
|
2016-01-21 15:36:47 +02:00
|
|
|
static struct device *nble_dev;
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-12-15 09:56:11 +02:00
|
|
|
static K_FIFO_DEFINE(rx_queue);
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-11-10 22:01:50 +02:00
|
|
|
static void rx_thread(void)
|
2016-01-21 15:36:47 +02:00
|
|
|
{
|
|
|
|
BT_DBG("Started");
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
struct net_buf *buf;
|
|
|
|
|
2016-10-18 23:24:51 +03:00
|
|
|
buf = net_buf_get(&rx_queue, K_FOREVER);
|
2016-01-21 15:36:47 +02:00
|
|
|
BT_DBG("Got buf %p", buf);
|
|
|
|
|
2016-02-12 14:24:41 +02:00
|
|
|
rpc_deserialize(buf);
|
2016-01-21 15:36:47 +02:00
|
|
|
|
|
|
|
net_buf_unref(buf);
|
2016-06-07 21:28:07 +03:00
|
|
|
|
|
|
|
/* Make sure we don't hog the CPU if the rx_queue never
|
|
|
|
* gets empty.
|
|
|
|
*/
|
2016-11-10 21:53:12 +02:00
|
|
|
k_yield();
|
2016-01-21 15:36:47 +02:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2017-04-20 12:00:29 -05:00
|
|
|
struct net_buf *rpc_alloc_cb(u16_t length)
|
2016-01-21 12:05:08 +02:00
|
|
|
{
|
|
|
|
struct net_buf *buf;
|
|
|
|
|
|
|
|
BT_DBG("length %u", length);
|
|
|
|
|
2016-10-18 23:24:51 +03:00
|
|
|
buf = net_buf_alloc(&tx_pool, K_FOREVER);
|
2016-01-21 12:05:08 +02:00
|
|
|
if (!buf) {
|
|
|
|
BT_ERR("Unable to get tx buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-18 23:24:51 +03:00
|
|
|
net_buf_reserve(buf, sizeof(struct ipc_uart_header));
|
|
|
|
|
2016-01-21 12:05:08 +02:00
|
|
|
if (length > net_buf_tailroom(buf)) {
|
|
|
|
BT_ERR("Too big tx buffer requested");
|
|
|
|
net_buf_unref(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-11 11:07:25 +02:00
|
|
|
return buf;
|
2016-01-21 12:05:08 +02:00
|
|
|
}
|
|
|
|
|
2016-02-11 11:07:25 +02:00
|
|
|
void rpc_transmit_cb(struct net_buf *buf)
|
2016-01-21 12:05:08 +02:00
|
|
|
{
|
2016-02-11 11:35:03 +02:00
|
|
|
struct ipc_uart_header *hdr;
|
2016-01-21 12:05:08 +02:00
|
|
|
|
2016-02-11 11:07:25 +02:00
|
|
|
BT_DBG("buf %p length %u", buf, buf->len);
|
2016-01-21 12:05:08 +02:00
|
|
|
|
2016-02-11 11:35:03 +02:00
|
|
|
hdr = net_buf_push(buf, sizeof(*hdr));
|
|
|
|
hdr->len = buf->len - sizeof(*hdr);
|
|
|
|
hdr->channel = 0;
|
|
|
|
hdr->src_cpu_id = 0;
|
2016-08-23 16:15:56 +02:00
|
|
|
#if defined(CONFIG_BLUETOOTH_NRF51_PM)
|
|
|
|
/* Wake-up nble */
|
2016-08-26 11:38:59 +03:00
|
|
|
nrf51_wakeup();
|
2016-08-23 16:15:56 +02:00
|
|
|
#endif
|
2016-02-11 11:35:03 +02:00
|
|
|
while (buf->len) {
|
2016-02-12 14:58:02 +02:00
|
|
|
uart_poll_out(nble_dev, net_buf_pull_u8(buf));
|
2016-02-11 11:35:03 +02:00
|
|
|
}
|
2016-01-21 12:05:08 +02:00
|
|
|
|
|
|
|
net_buf_unref(buf);
|
2016-08-23 16:15:56 +02:00
|
|
|
#if defined(CONFIG_BLUETOOTH_NRF51_PM)
|
|
|
|
/* TODO check if FIFO is empty */
|
|
|
|
/* Allow nble to go to deep sleep */
|
2016-08-26 11:38:59 +03:00
|
|
|
nrf51_allow_sleep();
|
2016-08-23 16:15:56 +02:00
|
|
|
#endif
|
2016-01-21 12:05:08 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 17:14:39 +02:00
|
|
|
static size_t nble_discard(struct device *uart, size_t len)
|
|
|
|
{
|
|
|
|
/* FIXME: correct size for nble */
|
2017-04-20 12:00:29 -05:00
|
|
|
u8_t buf[33];
|
2016-01-13 17:14:39 +02:00
|
|
|
|
|
|
|
return uart_fifo_read(uart, buf, min(len, sizeof(buf)));
|
|
|
|
}
|
|
|
|
|
2016-03-03 10:14:50 -08:00
|
|
|
static void bt_uart_isr(struct device *unused)
|
2016-01-13 17:14:39 +02:00
|
|
|
{
|
|
|
|
static struct net_buf *buf;
|
|
|
|
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
|
|
|
while (uart_irq_update(nble_dev) && uart_irq_is_pending(nble_dev)) {
|
2016-02-12 12:06:08 +02:00
|
|
|
static struct ipc_uart_header hdr;
|
2017-04-20 12:00:29 -05:00
|
|
|
static u8_t hdr_bytes;
|
2016-01-13 17:14:39 +02:00
|
|
|
int read;
|
|
|
|
|
|
|
|
if (!uart_irq_rx_ready(nble_dev)) {
|
|
|
|
if (uart_irq_tx_ready(nble_dev)) {
|
|
|
|
BT_DBG("transmit ready");
|
|
|
|
/*
|
|
|
|
* Implementing ISR based transmit requires
|
|
|
|
* extra API for uart such as
|
|
|
|
* uart_line_status(), etc. The support was
|
|
|
|
* removed from the recent code, using polling
|
|
|
|
* for transmit for now.
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
BT_DBG("spurious interrupt");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-12 12:06:08 +02:00
|
|
|
if (hdr_bytes < sizeof(hdr)) {
|
2016-01-13 17:14:39 +02:00
|
|
|
/* Get packet type */
|
2016-02-12 12:06:08 +02:00
|
|
|
hdr_bytes += uart_fifo_read(nble_dev,
|
2017-04-20 12:00:29 -05:00
|
|
|
(u8_t *)&hdr + hdr_bytes,
|
2016-02-12 12:06:08 +02:00
|
|
|
sizeof(hdr) - hdr_bytes);
|
|
|
|
if (hdr_bytes < sizeof(hdr)) {
|
2016-01-13 17:14:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-12 12:06:08 +02:00
|
|
|
if (hdr.len > NBLE_BUF_SIZE) {
|
|
|
|
BT_ERR("Too much data to fit buffer");
|
2016-01-13 17:14:39 +02:00
|
|
|
buf = NULL;
|
2016-02-12 12:06:08 +02:00
|
|
|
} else {
|
2016-10-18 23:24:51 +03:00
|
|
|
buf = net_buf_alloc(&rx_pool, K_NO_WAIT);
|
2016-02-12 12:06:08 +02:00
|
|
|
if (!buf) {
|
|
|
|
BT_ERR("No available IPC buffers");
|
|
|
|
}
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buf) {
|
2016-02-12 12:06:08 +02:00
|
|
|
hdr.len -= nble_discard(nble_dev, hdr.len);
|
|
|
|
if (!hdr.len) {
|
|
|
|
hdr_bytes = 0;
|
|
|
|
}
|
2016-01-13 17:14:39 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-12 12:06:08 +02:00
|
|
|
read = uart_fifo_read(nble_dev, net_buf_tail(buf), hdr.len);
|
2016-01-13 17:14:39 +02:00
|
|
|
|
|
|
|
buf->len += read;
|
2016-02-12 12:06:08 +02:00
|
|
|
hdr.len -= read;
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-02-12 12:06:08 +02:00
|
|
|
if (!hdr.len) {
|
2016-01-13 17:14:39 +02:00
|
|
|
BT_DBG("full packet received");
|
2016-02-12 12:06:08 +02:00
|
|
|
hdr_bytes = 0;
|
2016-01-13 17:14:39 +02:00
|
|
|
/* Pass buffer to the stack */
|
2016-06-05 17:36:46 +03:00
|
|
|
net_buf_put(&rx_queue, buf);
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int nble_open(void)
|
|
|
|
{
|
|
|
|
BT_DBG("");
|
|
|
|
|
2016-11-10 22:01:50 +02:00
|
|
|
/* Initialize receive queue and start rx_thread */
|
2017-05-09 12:00:09 -07:00
|
|
|
k_thread_create(&rx_thread_data, rx_thread_stack,
|
|
|
|
sizeof(rx_thread_stack), (k_thread_entry_t)rx_thread,
|
|
|
|
NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
|
2016-01-21 15:36:47 +02:00
|
|
|
|
2016-01-13 17:14:39 +02:00
|
|
|
uart_irq_rx_disable(nble_dev);
|
|
|
|
uart_irq_tx_disable(nble_dev);
|
|
|
|
|
2016-05-04 15:12:16 +03:00
|
|
|
#if defined(CONFIG_BLUETOOTH_NRF51_PM)
|
|
|
|
if (nrf51_init(nble_dev) < 0) {
|
|
|
|
return -EIO;
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
2016-05-04 15:12:16 +03:00
|
|
|
#else
|
|
|
|
bt_uart_drain(nble_dev);
|
|
|
|
#endif
|
2016-01-13 17:14:39 +02:00
|
|
|
|
2016-03-03 10:14:50 -08:00
|
|
|
uart_irq_callback_set(nble_dev, bt_uart_isr);
|
|
|
|
|
2016-01-13 17:14:39 +02:00
|
|
|
uart_irq_rx_enable(nble_dev);
|
|
|
|
|
2016-04-08 13:50:41 +03:00
|
|
|
return 0;
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _bt_nble_init(struct device *unused)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
|
|
|
nble_dev = device_get_binding(CONFIG_NBLE_UART_ON_DEV_NAME);
|
|
|
|
if (!nble_dev) {
|
2016-03-09 15:22:04 -03:00
|
|
|
return -EINVAL;
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 14:01:20 -03:00
|
|
|
return 0;
|
2016-01-13 17:14:39 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 09:50:47 +02:00
|
|
|
DEVICE_INIT(bt_nble, "", _bt_nble_init, NULL, NULL, POST_KERNEL,
|
2016-01-13 17:14:39 +02:00
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
|