Hello,
I want to communicate one card nrf5340dk with 2 same card in using 'central_uart' and 'peripheral_uart' samples.
I have succeeded to establish the connection and send data.
But there is a problem : when i want to send the data to the two receivers. I can send the data with succeed, but after the central card reboots...
I juste wonder why...
I give you here a part of my key code and in attached my file main.c
for (;;) {
/* Wait indefinitely for data to be sent over Bluetooth */
struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
K_FOREVER);
err = bt_nus_client_send(&nus_client[0], buf->data, 1);
if (err) {
LOG_WRN("Failed to send data over BLE connection"
"(err %d)", err);
}
err = k_sem_take(&nus_write_sem, NUS_WRITE_TIMEOUT);
if (err) {
LOG_WRN("NUS send timeout");
}
k_sleep(K_SECONDS(1));
err = bt_nus_client_send(&nus_client[1], buf->data, 1);
if (err) {
LOG_WRN("Failed to send data over BLE connection"
"(err %d)", err);
}
err = k_sem_take(&nus_write_sem2, NUS_WRITE_TIMEOUT);
if (err) {
LOG_WRN("NUS send timeout");
}
}
here the two send can be done, but after the system of the central card reboot...
Thanks for all responses,
best regards
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
/** @file
* @brief Nordic UART Service Client sample
*/
#include <errno.h>
#include <zephyr.h>
#include <sys/byteorder.h>
#include <sys/printk.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/gap.h>
#include <bluetooth/services/nus.h>
#include <bluetooth/services/nus_client.h>
#include <bluetooth/gatt_dm.h>
#include <bluetooth/scan.h>
#include <settings/settings.h>
#include <drivers/uart.h>
#include <logging/log.h>
#define LOG_MODULE_NAME central_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
/* UART payload buffer element size. */
#define UART_BUF_SIZE 240
#define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
#define KEY_PASSKEY_REJECT DK_BTN2_MSK
#define NUS_WRITE_TIMEOUT K_MSEC(150)
#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
#define UART_RX_TIMEOUT 50
#define INTERVAL_MIN 0x140 /* 0x140 : 320 units, 400 ms -> 0x014 : 20 units, ///0x006 : 6 units*/
#define INTERVAL_MAX 0x140 /* 320 units, 400 ms */
static struct bt_le_conn_param *conn_param =
BT_LE_CONN_PARAM(INTERVAL_MIN, INTERVAL_MAX, 0, 400);
static const struct device *uart;
static struct k_work_delayable uart_work;
static int cpt1=0;
static struct bt_conn_info info = {0};//SL
static uint8_t volatile conn_count = 0;
static uint8_t volatile nus_count = 0;
K_SEM_DEFINE(nus_write_sem, 0, 1);
K_SEM_DEFINE(nus_write_sem2, 0, 1);
struct uart_data_t {
void *fifo_reserved;
uint8_t data[UART_BUF_SIZE];
uint16_t len;
};
static K_FIFO_DEFINE(fifo_uart_tx_data);
static K_FIFO_DEFINE(fifo_uart_rx_data);
static struct bt_conn *default_conn;
static struct bt_nus_client nus_client[2];
static void ble_data_sent(uint8_t err, const uint8_t *const data, uint16_t len)
{
struct uart_data_t *buf;
/* Retrieve buffer context. */
buf = CONTAINER_OF(data, struct uart_data_t, data);
k_free(buf);
k_sem_give(&nus_write_sem);
if (err) {
LOG_WRN("ATT error code: 0x%02X", err);
}
}
static void ble_data_sent2(uint8_t err, const uint8_t *const data, uint16_t len)
{
struct uart_data_t *buf;
/* Retrieve buffer context. */
buf = CONTAINER_OF(data, struct uart_data_t, data);
k_free(buf);
k_sem_give(&nus_write_sem2);
if (err) {
LOG_WRN("ATT error code: 0x%02X", err);
}
}
static uint8_t ble_data_received(const uint8_t *const data, uint16_t len)
{
int err;
for (uint16_t pos = 0; pos != len;) {
struct uart_data_t *tx = k_malloc(sizeof(*tx));
if (!tx) {
LOG_WRN("Not able to allocate UART send data buffer");
return BT_GATT_ITER_CONTINUE;
}
/* Keep the last byte of TX buffer for potential LF char. */
size_t tx_data_size = sizeof(tx->data) - 1;
if ((len - pos) > tx_data_size) {
tx->len = tx_data_size;
} else {
tx->len = (len - pos);
}
memcpy(tx->data, &data[pos], tx->len);
pos += tx->len;
/* Append the LF character when the CR character triggered
* transmission from the peer.
*/
/* if ((pos == len) && (data[len - 1] == '\r')) {
tx->data[tx->len] = '\n';
tx->len++;
}
*/
err = uart_tx(uart, tx->data, tx->len, SYS_FOREVER_MS);
if (err) {
k_fifo_put(&fifo_uart_tx_data, tx);
}
}
/*
//**********SL : for testing throughput*****************
cpt1++;
if(cpt1==100){
printk("=");
cpt1=0;
}//
*/
return BT_GATT_ITER_CONTINUE;
}
static uint8_t ble_data_received2(const uint8_t *const data, uint16_t len)
{
int err;
for (uint16_t pos = 0; pos != len;) {
struct uart_data_t *tx = k_malloc(sizeof(*tx));
if (!tx) {
LOG_WRN("Not able to allocate UART send data buffer");
return BT_GATT_ITER_CONTINUE;
}
/* Keep the last byte of TX buffer for potential LF char. */
size_t tx_data_size = sizeof(tx->data) - 1;
if ((len - pos) > tx_data_size) {
tx->len = tx_data_size;
} else {
tx->len = (len - pos);
}
memcpy(tx->data, &data[pos], tx->len);
pos += tx->len;
/* Append the LF character when the CR character triggered
* transmission from the peer.
*/
/* if ((pos == len) && (data[len - 1] == '\r')) {
tx->data[tx->len] = '\n';
tx->len++;
}
*/
err = uart_tx(uart, tx->data, tx->len, SYS_FOREVER_MS);
if (err) {
k_fifo_put(&fifo_uart_tx_data, tx);
}
}
/*
//**********SL : for testing throughput*****************
cpt1++;
if(cpt1==100){
printk("=");
cpt1=0;
}//
*/
return BT_GATT_ITER_CONTINUE;
}
static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
ARG_UNUSED(dev);
static uint8_t *current_buf;
static size_t aborted_len;
static bool buf_release;
struct uart_data_t *buf;
static uint8_t *aborted_buf;
switch (evt->type) {
case UART_TX_DONE:
if ((evt->data.tx.len == 0) ||
(!evt->data.tx.buf)) {
return;
}
if (aborted_buf) {
buf = CONTAINER_OF(aborted_buf, struct uart_data_t,
data);
aborted_buf = NULL;
aborted_len = 0;
} else {
buf = CONTAINER_OF(evt->data.tx.buf,
struct uart_data_t,
data);
}
k_free(buf);
buf = k_fifo_get(&fifo_uart_tx_data, K_NO_WAIT);
if (!buf) {
return;
}
if (uart_tx(uart, buf->data, buf->len, SYS_FOREVER_MS)) {
LOG_WRN("Failed to send data over UART");
}
break;
case UART_RX_RDY:
buf = CONTAINER_OF(evt->data.rx.buf, struct uart_data_t, data);
buf->len += evt->data.rx.len;
buf_release = false;
if (buf->len == UART_BUF_SIZE) {
k_fifo_put(&fifo_uart_rx_data, buf);
} else if ((evt->data.rx.buf[buf->len - 1] == '\n') ||
(evt->data.rx.buf[buf->len - 1] == '\r')) {
k_fifo_put(&fifo_uart_rx_data, buf);
current_buf = evt->data.rx.buf;
buf_release = true;
uart_rx_disable(uart);
}
break;
case UART_RX_DISABLED:
buf = k_malloc(sizeof(*buf));
if (buf) {
buf->len = 0;
} else {
LOG_WRN("Not able to allocate UART receive buffer");
k_work_reschedule(&uart_work, UART_WAIT_FOR_BUF_DELAY);
return;
}
uart_rx_enable(uart, buf->data, sizeof(buf->data),
UART_RX_TIMEOUT);
break;
case UART_RX_BUF_REQUEST:
buf = k_malloc(sizeof(*buf));
if (buf) {
buf->len = 0;
uart_rx_buf_rsp(uart, buf->data, sizeof(buf->data));
} else {
LOG_WRN("Not able to allocate UART receive buffer");
}
break;
case UART_RX_BUF_RELEASED:
buf = CONTAINER_OF(evt->data.rx_buf.buf, struct uart_data_t,
data);
if (buf_release && (current_buf != evt->data.rx_buf.buf)) {
k_free(buf);
buf_release = false;
current_buf = NULL;
}
break;
case UART_TX_ABORTED:
if (!aborted_buf) {
aborted_buf = (uint8_t *)evt->data.tx.buf;
}
aborted_len += evt->data.tx.len;
buf = CONTAINER_OF(aborted_buf, struct uart_data_t,
data);
uart_tx(uart, &buf->data[aborted_len],
buf->len - aborted_len, SYS_FOREVER_MS);
break;
default:
break;
}
}
static void uart_work_handler(struct k_work *item)
{
struct uart_data_t *buf;
buf = k_malloc(sizeof(*buf));
if (buf) {
buf->len = 0;
} else {
LOG_WRN("Not able to allocate UART receive buffer");
k_work_reschedule(&uart_work, UART_WAIT_FOR_BUF_DELAY);
return;
}
uart_rx_enable(uart, buf->data, sizeof(buf->data), UART_RX_TIMEOUT);
}
static int uart_init(void)
{
int err;
struct uart_data_t *rx;
struct uart_config *uart_conf; //SL: to configure Baudrate
//SL
uart_conf = k_malloc(sizeof(*uart_conf));
uart_conf->baudrate = 1000000;
uart_conf->data_bits = UART_CFG_DATA_BITS_8;
uart_conf->flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
uart_conf->parity = UART_CFG_PARITY_NONE;
uart_conf->stop_bits = UART_CFG_STOP_BITS_1;
uart = device_get_binding(DT_LABEL(DT_NODELABEL(uart0)));
if (!uart) {
LOG_ERR("UART binding failed");
return -ENXIO;
}
//SL
int verif;
verif = z_impl_uart_configure(uart, uart_conf);
if(verif != 0) {
printk("uart config don't pass\n");
}
rx = k_malloc(sizeof(*rx));
if (rx) {
rx->len = 0;
} else {
return -ENOMEM;
}
k_work_init_delayable(&uart_work, uart_work_handler);
err = uart_callback_set(uart, uart_cb, NULL);
if (err) {
return err;
}
return uart_rx_enable(uart, rx->data, sizeof(rx->data),
UART_RX_TIMEOUT);
}
static void discovery_complete(struct bt_gatt_dm *dm,
void *context)
{
struct bt_nus_client *nus = context;
LOG_INF("Service discovery completed");
bt_gatt_dm_data_print(dm);
bt_nus_handles_assign(dm, nus);
bt_nus_subscribe_receive(nus);
bt_gatt_dm_data_release(dm);
}
static void discovery_service_not_found(struct bt_conn *conn,
void *context)
{
LOG_INF("Service not found");
}
static void discovery_error(struct bt_conn *conn,
int err,
void *context)
{
LOG_WRN("Error while discovering GATT database: (%d)", err);
}
struct bt_gatt_dm_cb discovery_cb = {
.completed = discovery_complete,
.service_not_found = discovery_service_not_found,
.error_found = discovery_error,
};
static void gatt_discover(struct bt_conn *conn)
{
int err;
if (conn != default_conn) {
return;
}
err = bt_gatt_dm_start(conn,
BT_UUID_NUS_SERVICE,
&discovery_cb,
&nus_client[nus_count++]);
if (err) {
LOG_ERR("could not start the discovery procedure, error "
"code: %d", err);
}
}
static void exchange_func(struct bt_conn *conn, uint8_t err, struct bt_gatt_exchange_params *params)
{
if (!err) {
LOG_INF("MTU exchange done");
} else {
LOG_WRN("MTU exchange failed (err %" PRIu8 ")", err);
}
}
static void connected(struct bt_conn *conn, uint8_t conn_err)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (conn_err) {
LOG_INF("Failed to connect to %s (%d)", log_strdup(addr),
conn_err);
if (default_conn == conn) {
bt_conn_unref(default_conn);
default_conn = NULL;
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
LOG_ERR("Scanning failed to start (err %d)",
err);
}
}
return;
}
conn_count++;
if (conn_count < CONFIG_BT_MAX_CONN) {
bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
}
printk("Connected (%u): %s\n", conn_count, addr);
LOG_INF("Connected: %s", log_strdup(addr));
//******************SL : configure PHY to 2M******************************************
struct bt_conn_le_phy_param *phy;
struct bt_conn_le_data_len_param *data_length;
//data_length = BT_LE_DATA_LEN_PARAM_MAX;
phy = BT_CONN_LE_PHY_PARAM_2M;
err = bt_conn_le_phy_update(conn, phy);
if (err) {
printk("\nFailed to configure PHY param\n");
}
//
static struct bt_gatt_exchange_params exchange_params;
exchange_params.func = exchange_func;
err = bt_gatt_exchange_mtu(conn, &exchange_params);
if (err) {
LOG_WRN("MTU exchange failed (err %d)", err);
}
err = bt_conn_set_security(conn, BT_SECURITY_L2);
if (err) {
LOG_WRN("Failed to set security: %d", err);
gatt_discover(conn);
}
/*
err = bt_scan_stop();
if ((!err) && (err != -EALREADY)) {
LOG_ERR("Stop LE scan failed (err %d)", err);
}
*/
/*
//******************SL : configure conn param******************************************
err = bt_conn_le_param_update(conn, conn_param);
if (err) {
printk("\nFailed to configure conn param\n");
}
//
//SL : configure data length
err = bt_conn_le_data_len_update(conn, data_length);
if (err) {
printk("\nFailed to configure data length\n");
}
//
*/
/* //SL: get conn info
err = bt_conn_get_info(conn, &info);
if (err) {
printk("\nfail to get conn info\n");
//return err;
}
printk("The LE tx PHY is : %u \n", info.le.phy->tx_phy);
printk("The LE rx PHY is : %u \n", info.le.phy->rx_phy);
printk("The LE conn is : %u \n", info.le.interval);
printk("The LE tx data length is : %u \n", info.le.data_len->tx_max_len);
printk("The LE rx data length is : %u \n", info.le.data_len->rx_max_len);
printk("The LE data payload time : %u us\n\n", info.le.data_len->tx_max_time);
//*/
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
int err;
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Disconnected: %s (reason %u)", log_strdup(addr),
reason);
if (default_conn != conn) {
return;
}
bt_conn_unref(default_conn);
default_conn = NULL;
conn_count--;
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
LOG_ERR("Scanning failed to start (err %d)",
err);
}
}
static void security_changed(struct bt_conn *conn, bt_security_t level,
enum bt_security_err err)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
if (!err) {
LOG_INF("Security changed: %s level %u", log_strdup(addr),
level);
} else {
LOG_WRN("Security failed: %s level %u err %d", log_strdup(addr),
level, err);
}
gatt_discover(conn);
}
static struct bt_conn_cb conn_callbacks = {
.connected = connected,
.disconnected = disconnected,
.security_changed = security_changed
};
static void scan_filter_match(struct bt_scan_device_info *device_info,
struct bt_scan_filter_match *filter_match,
bool connectable)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(device_info->recv_info->addr, addr, sizeof(addr));
LOG_INF("Filters matched. Address: %s connectable: %d",
log_strdup(addr), connectable);
}
static void scan_connecting_error(struct bt_scan_device_info *device_info)
{
LOG_WRN("Connecting failed");
}
static void scan_connecting(struct bt_scan_device_info *device_info,
struct bt_conn *conn)
{
default_conn = bt_conn_ref(conn);
}
static int nus_client_init(void)
{
int err;
struct bt_nus_client_init_param init = {
.cb = {
.received = ble_data_received,
.sent = ble_data_sent,
}
};
struct bt_nus_client_init_param init2 = {
.cb = {
.received = ble_data_received2,
.sent = ble_data_sent2,
}
};
err = bt_nus_client_init(&nus_client[0], &init);
if (err) {
LOG_ERR("NUS Client initialization failed (err %d)", err);
return err;
}
err = bt_nus_client_init(&nus_client[1], &init2);
if (err) {
LOG_ERR("NUS Client initialization failed (err %d)", err);
return err;
}
/*
for (uint32_t i = 0; i < CONFIG_BT_MAX_CONN; i++)
{
err = bt_nus_client_init(&nus_client[i], &init);
if (err) {
LOG_ERR("NUS Client initialization failed (err %d)", err);
return err;
}
}
*/
LOG_INF("NUS Client module initialized");
return err;
}
BT_SCAN_CB_INIT(scan_cb, scan_filter_match, NULL,
scan_connecting_error, scan_connecting);
static int scan_init(void)
{
int err;
struct bt_scan_init_param scan_init = {
.connect_if_match = 1,
};
bt_scan_init(&scan_init);
bt_scan_cb_register(&scan_cb);
err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_NUS_SERVICE);
if (err) {
LOG_ERR("Scanning filters cannot be set (err %d)", err);
return err;
}
err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
if (err) {
LOG_ERR("Filters cannot be turned on (err %d)", err);
return err;
}
LOG_INF("Scan module initialized");
return err;
}
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Pairing cancelled: %s", log_strdup(addr));
}
static void pairing_confirm(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
bt_conn_auth_pairing_confirm(conn);
LOG_INF("Pairing confirmed: %s", log_strdup(addr));
}
static void pairing_complete(struct bt_conn *conn, bool bonded)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Pairing completed: %s, bonded: %d", log_strdup(addr),
bonded);
}
static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_WRN("Pairing failed conn: %s, reason %d", log_strdup(addr),
reason);
}
static struct bt_conn_auth_cb conn_auth_callbacks = {
.cancel = auth_cancel,
.pairing_confirm = pairing_confirm,
.pairing_complete = pairing_complete,
.pairing_failed = pairing_failed
};
void main(void)
{
int err;
err = bt_conn_auth_cb_register(&conn_auth_callbacks);
if (err) {
LOG_ERR("Failed to register authorization callbacks.");
return;
}
err = bt_enable(NULL);
if (err) {
LOG_ERR("Bluetooth init failed (err %d)", err);
return;
}
LOG_INF("Bluetooth initialized");
if (IS_ENABLED(CONFIG_SETTINGS)) {
settings_load();
}
bt_conn_cb_register(&conn_callbacks);
int (*module_init[])(void) = {uart_init, scan_init, nus_client_init};
for (size_t i = 0; i < ARRAY_SIZE(module_init); i++) {
err = (*module_init[i])();
if (err) {
return;
}
}
printk("Starting Bluetooth Central UART example\n");
err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);
if (err) {
LOG_ERR("Scanning failed to start (err %d)", err);
return;
}
LOG_INF("Scanning successfully started");
while (conn_count < CONFIG_BT_MAX_CONN) {
k_sleep(K_SECONDS(1));
}
for (;;) {
/* //SL: get conn info
err = bt_conn_get_info(default_conn, &info);
if (err) {
printk("\nfail to get conn info\n");
//return err;
}
printk("The LE tx PHY is : %u \n", info.le.phy->tx_phy);
printk("The LE rx PHY is : %u \n", info.le.phy->rx_phy);
printk("The LE conn is : %u \n", info.le.interval);
printk("The LE tx data length is : %u \n", info.le.data_len->tx_max_len);
printk("The LE rx data length is : %u \n", info.le.data_len->rx_max_len);
printk("The LE data payload time : %u us\n\n", info.le.data_len->tx_max_time);
k_sleep(K_MSEC(2000));
//*/
/* Wait indefinitely for data to be sent over Bluetooth */
struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
K_FOREVER);
//err = bt_nus_client_send(&nus_client[0], buf->data, buf->len);
err = bt_nus_client_send(&nus_client[0], buf->data, 1);
if (err) {
LOG_WRN("Failed to send data over BLE connection"
"(err %d)", err);
}
err = k_sem_take(&nus_write_sem, NUS_WRITE_TIMEOUT);
if (err) {
LOG_WRN("NUS send timeout");
}
//k_usleep(500000);
k_sleep(K_SECONDS(1));
err = bt_nus_client_send(&nus_client[1], buf->data, 1);
if (err) {
LOG_WRN("Failed to send data over BLE connection"
"(err %d)", err);
}
err = k_sem_take(&nus_write_sem2, NUS_WRITE_TIMEOUT);
if (err) {
LOG_WRN("NUS send timeout");
}
}
}