I am working on a project in which I am using code from central and peripheral hr example. I only need the scanning and advertising part and not HRS so I removed the HRS code. I also want to connect my board with a phone to config some settings like ID, calibration, etc. For this, I have also added a code from the Peripheral UART example. After combining these codes, the code should work like this.
On startup, MCU will wait for a mobile to connect with it and when a disconnected() callback happens, MCU will run in scan and adv mode. But what I observe is that in main(), my timer stops working when I call a function bt_update_packet(). When I am commenting bt_update_packet(), the timer continues to run without a problem. This is only happening when I am combining the two examples mentioned above. When I am not including peripheral uart code in my project timer is working fine and bt_update_packet() also happens to run without a problem. Can anybody help? I am using nRF connect sdk 2.1.2 and nRF52832 dev kit.
Here is my code and project configuration. Any help would be very appreciated. Thanks in advance!
#include <zephyr/zephyr.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include "include/adc.h"
#include "include/application.h"
#include "include/temp_sensor.h"
#include "include/bt.h"
#include "include/board_config.h"
#include "include/slave_param.h"
#pragma region "FLASH Configuration"
#ifdef CONFIG_TRUSTED_EXECUTION_NONSECURE
#define FLASH_TEST_LABEL image_1_nonsecure
#else
#define FLASH_TEST_LABEL image_1
#endif
#pragma endregion
/************************
* Macros
************************/
#define RUN_STATUS_LED DK_LED1
#define SLEEP_TIME_MS 100
#define TIMER_INTERVAL_MSEC 1000
#define FLASH_TEST_OFFSET FLASH_AREA_OFFSET(FLASH_TEST_LABEL)
#define FLASH_TEST_DEVICE FLASH_AREA_DEVICE(FLASH_TEST_LABEL)
#define FLASH_PAGE_SIZE 4096
#define TEST_DATA_WORD_0 0x1234
#define TEST_DATA_WORD_1 0xabcd
#define TEST_DATA_WORD_2 0xa1b1
#define TEST_DATA_WORD_3 0xc1d1
#define FLASH_TEST_OFFSET2 0x41234
#define FLASH_TEST_PAGE_IDX 37
#define CUSTOM_MAC_ADDR_LEN 15 //13 (without ID number)
#pragma region "BT LE Configuration"
uint8_t advData[MAX_NUM_OF_BYTES] = {};
static struct bt_data ad[] = {
BT_DATA(BT_DATA_MANUFACTURER_DATA, advData, sizeof(advData)),
};
static struct bt_data sd[] = {
BT_DATA_BYTES(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME)
};
#pragma endregion
/************************
* Global Variables
************************/
char mac_address_str[CUSTOM_MAC_ADDR_LEN];
uint8_t sdData[MAX_NUM_OF_BYTES] = {};
uint8_t *pSdData = sdData;
volatile bool uartConnectFlag = false;
volatile bool configFlag = false;
uint8_t counter = 0;
volatile bool timerUARTFlag = false;
volatile bool advUpdateFlag = false;
volatile bool scanStopFlag = false;
volatile bool timerStartFlag = false;
/************************
* Handlers
************************/
#pragma region "Scanning"
static bool data_cb(struct bt_data *data, void *user_data)
{
*pSdData = user_data;
uint8_t len;
switch (data->type)
{
case BT_DATA_MANUFACTURER_DATA:
len = MIN(data->data_len, DATABYTES - 1);
memcpy(pSdData, data->data, len);
pSdData[len] = '\0';
return false;
default:
return true;
}
}
static void scan_recv(const struct bt_le_scan_recv_info *info, struct net_buf_simple *buf)
{
char le_addr[BT_ADDR_LE_STR_LEN];
uint8_t parseDataBytes[DATABYTES];
uint16_t data_len;
memset(parseDataBytes, 0, sizeof(parseDataBytes));
data_len = buf->len;
bt_data_parse(buf, data_cb, parseDataBytes);
bt_addr_le_to_str(info->addr, le_addr, sizeof(le_addr));
if (strncmp(le_addr, "E0:FE:52:79:8B:7F", 17) == 0)
{
// printk("Slave SCAN -> Master device: %s, AD data len: %u Databytes:\n"
// "%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n\n",
// le_addr, data_len,
// sdData[0], sdData[1], sdData[2], sdData[3], sdData[4], sdData[5], sdData[6], sdData[7], sdData[8],
// sdData[9], sdData[10], sdData[11], sdData[12], sdData[13], sdData[14], sdData[15], sdData[16], sdData[17],
// sdData[18], sdData[19], sdData[20], sdData[21], sdData[22], sdData[23], sdData[24], sdData[25], sdData[26],
// sdData[27], sdData[28]);
// static uint16_t scanCount = 0;
// printk("Scan count %d\n", scanCount++);
gpio_pin_toggle_dt(&leds_array[3]);
}
}
static struct bt_le_scan_cb scan_callbacks = {
.recv = scan_recv,
};
struct bt_le_scan_param scan_param = {
.type = BT_LE_SCAN_TYPE_PASSIVE,
.options = BT_LE_SCAN_OPT_NONE,
.interval = BT_GAP_SCAN_FAST_INTERVAL,
.window = BT_GAP_SCAN_FAST_INTERVAL, // changed from BT_GAP_SCAN_FAST_WINDOW
};
#pragma endregion
void timer_ISR(void)
{
// sysFlags.timer_10ms_flag = true;
sysFlags.timer_10ms_count++;
if ((sysFlags.timer_10ms_count % ADC_TIMER_COUNTS) == 0)
{
sysFlags.adc_sample_task = true;
}
if ((sysFlags.timer_10ms_count % BT_ADV_TIMER_COUNTS) == 0)
{
sysFlags.bt_adv_task = true;
}
if (sysFlags.timer_10ms_count == 100)
{
sysFlags.timer_10ms_count = 0;
sysFlags.timer_1000ms_flag = true;
}
}
void bt_update_packet(void)
{
int res;
res = bt_le_adv_stop();
if (res)
{
printk("************ADV STOP failed!************ bt_cb (err %d)\n", res);
return;
}
printk("************ADV STOP successful!************\n");
slave.soc_temp = LOBYTE(nrf52_chip_temperature());
advData[0] = read_slave_id();
advData[1] = slave.counter++;
advData[2] = slave.cellsVoltage[0].adc_m_16bits.adc_hiByte;
advData[3] = slave.cellsVoltage[0].adc_m_16bits.adc_loByte;
advData[4] = slave.cellsVoltage[1].adc_m_16bits.adc_hiByte;
advData[5] = slave.cellsVoltage[1].adc_m_16bits.adc_loByte;
advData[6] = slave.cellsVoltage[2].adc_m_16bits.adc_hiByte;
advData[7] = slave.cellsVoltage[2].adc_m_16bits.adc_loByte;
advData[8] = slave.cellsVoltage[3].adc_m_16bits.adc_hiByte;
advData[9] = slave.cellsVoltage[3].adc_m_16bits.adc_loByte;
advData[10] = slave.cellsTemperature[0].adc_m_16bits.adc_loByte;
advData[11] = slave.cellsTemperature[1].adc_m_16bits.adc_loByte;
advData[12] = slave.cellsTemperature[2].adc_m_16bits.adc_loByte;
advData[13] = slave.cellsTemperature[3].adc_m_16bits.adc_loByte;
advData[14] = slave.soc_temp;
advData[15] = slave.current[1];
advData[16] = slave.current[0];
advData[17] = slave.cell_soc[0];
advData[18] = slave.cell_soc[1];
advData[19] = slave.cell_soc[2];
advData[20] = slave.cell_soc[3];
advData[21] = slave.status;
for (uint8_t i = 22; i < sizeof(advData); i++)
{
advData[i] = 0xB0 + (i - 21); // RFU bytes
}
// updating adv data
ad[0].data = advData;
ad[0].data_len = sizeof(advData);
ad[0].type = BT_DATA_MANUFACTURER_DATA;
// updating sd data
get_mac_address(mac_address_str, sizeof(mac_address_str));
sd[0].data = mac_address_str;
sd[0].data_len = sizeof(mac_address_str);
sd[0].type = BT_DATA_NAME_COMPLETE;
res = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (res)
{
printk("Advertising failed to start bt_cb (err %d)\n", res);
return;
}
printk("ADV started again!\n");
// printk("Slave ADV = %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n\n",
// advData[0], advData[1], advData[2], advData[3], advData[4], advData[5], advData[6], advData[7], advData[8],
// advData[9], advData[10], advData[11], advData[12], advData[13], advData[14], advData[15], advData[16], advData[17],
// advData[18], advData[19], advData[20], advData[21], advData[22], advData[23], advData[24], advData[25], advData[26],
// advData[27], advData[28]);
}
K_TIMER_DEFINE(htimer, &timer_ISR, NULL);
#pragma region "BLE UART"
#include "uart_async_adapter.h"
#include <zephyr/types.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/usb/usb_device.h>
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <soc.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci.h>
#include <bluetooth/services/nus.h>
#include <dk_buttons_and_leds.h>
#include <zephyr/settings/settings.h>
#include <stdio.h>
#include <zephyr/logging/log.h>
#define LOG_MODULE_NAME peripheral_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE
#define PRIORITY 7
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
#define RUN_LED_BLINK_INTERVAL 1000
#define CON_STATUS_LED DK_LED1
#define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
#define KEY_PASSKEY_REJECT DK_BTN2_MSK
#define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE
#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
#define UART_WAIT_FOR_RX CONFIG_BT_NUS_UART_RX_WAIT_TIME
static K_SEM_DEFINE(ble_init_ok, 0, 1);
static struct bt_conn *current_conn;
static struct bt_conn *auth_conn;
static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
static struct k_work_delayable uart_work;
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);
#if CONFIG_BT_NUS_UART_ASYNC_ADAPTER
UART_ASYNC_ADAPTER_INST_DEFINE(async_adapter);
#else
static const struct device *const async_adapter;
#endif
static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
ARG_UNUSED(dev);
static size_t aborted_len;
struct uart_data_t *buf;
static uint8_t *aborted_buf;
static bool disable_req;
switch (evt->type) {
case UART_TX_DONE:
LOG_DBG("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:
LOG_DBG("UART_RX_RDY");
buf = CONTAINER_OF(evt->data.rx.buf, struct uart_data_t, data);
buf->len += evt->data.rx.len;
if (disable_req) {
return;
}
if ((evt->data.rx.buf[buf->len - 1] == '\n') ||
(evt->data.rx.buf[buf->len - 1] == '\r')) {
disable_req = true;
uart_rx_disable(uart);
}
break;
case UART_RX_DISABLED:
LOG_DBG("UART_RX_DISABLED");
disable_req = false;
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_WAIT_FOR_RX);
break;
case UART_RX_BUF_REQUEST:
LOG_DBG("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:
LOG_DBG("UART_RX_BUF_RELEASED");
buf = CONTAINER_OF(evt->data.rx_buf.buf, struct uart_data_t,
data);
if (buf->len > 0) {
k_fifo_put(&fifo_uart_rx_data, buf);
} else {
k_free(buf);
}
break;
case UART_TX_ABORTED:
LOG_DBG("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 {
printk("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_WAIT_FOR_RX);
}
static bool uart_test_async_api(const struct device *dev)
{
const struct uart_driver_api *api =
(const struct uart_driver_api *)dev->api;
return (api->callback_set != NULL);
}
static int uart_init(void)
{
int err;
int pos;
struct uart_data_t *rx;
struct uart_data_t *tx;
if (!device_is_ready(uart)) {
return -ENODEV;
}
if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
err = usb_enable(NULL);
if (err && (err != -EALREADY)) {
LOG_ERR("Failed to enable USB");
return err;
}
}
rx = k_malloc(sizeof(*rx));
if (rx) {
rx->len = 0;
} else {
return -ENOMEM;
}
k_work_init_delayable(&uart_work, uart_work_handler);
if (IS_ENABLED(CONFIG_BT_NUS_UART_ASYNC_ADAPTER) && !uart_test_async_api(uart)) {
/* Implement API adapter */
uart_async_adapter_init(async_adapter, uart);
uart = async_adapter;
}
err = uart_callback_set(uart, uart_cb, NULL);
if (err) {
LOG_ERR("Cannot initialize UART callback");
return err;
}
if (IS_ENABLED(CONFIG_UART_LINE_CTRL)) {
LOG_INF("Wait for DTR");
while (true) {
uint32_t dtr = 0;
uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &dtr);
if (dtr) {
break;
}
/* Give CPU resources to low priority threads. */
k_sleep(K_MSEC(100));
}
LOG_INF("DTR set");
err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DCD, 1);
if (err) {
LOG_WRN("Failed to set DCD, ret code %d", err);
}
err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DSR, 1);
if (err) {
LOG_WRN("Failed to set DSR, ret code %d", err);
}
}
tx = k_malloc(sizeof(*tx));
if (tx) {
pos = snprintf(tx->data, sizeof(tx->data),
"Starting Nordic UART service example\r\n");
if ((pos < 0) || (pos >= sizeof(tx->data))) {
k_free(tx);
LOG_ERR("snprintf returned %d", pos);
return -ENOMEM;
}
tx->len = pos;
} else {
return -ENOMEM;
}
err = uart_tx(uart, tx->data, tx->len, SYS_FOREVER_MS);
if (err) {
LOG_ERR("Cannot display welcome message (err: %d)", err);
return err;
}
return uart_rx_enable(uart, rx->data, sizeof(rx->data), 50);
}
static void connected(struct bt_conn *conn, uint8_t err)
{
char addr[BT_ADDR_LE_STR_LEN];
if (err) {
printk("Connection failed (err %u)", err);
return;
}
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Connected %s", addr);
current_conn = bt_conn_ref(conn);
dk_set_led_on(CON_STATUS_LED);
printk("\nconnected()\n");
advUpdateFlag = false;
timerStartFlag = false;
}
static void disconnected(struct bt_conn *conn, uint8_t reason)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Disconnected: %s (reason %u)", addr, reason);
if (auth_conn) {
bt_conn_unref(auth_conn);
auth_conn = NULL;
}
if (current_conn) {
bt_conn_unref(current_conn);
current_conn = NULL;
dk_set_led_off(CON_STATUS_LED);
}
timerStartFlag = true;
}
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
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", addr, level);
} else {
LOG_WRN("Security failed: %s level %u err %d", addr,
level, err);
}
}
#endif
BT_CONN_CB_DEFINE(conn_callbacks) = {
.connected = connected,
.disconnected = disconnected,
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
.security_changed = security_changed,
#endif
};
#if defined(CONFIG_BT_NUS_SECURITY_ENABLED)
static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Passkey for %s: %06u", addr, passkey);
}
static void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey)
{
char addr[BT_ADDR_LE_STR_LEN];
auth_conn = bt_conn_ref(conn);
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
LOG_INF("Passkey for %s: %06u", addr, passkey);
LOG_INF("Press Button 1 to confirm, Button 2 to reject.");
}
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", 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", 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_INF("Pairing failed conn: %s, reason %d", addr, reason);
}
static struct bt_conn_auth_cb conn_auth_callbacks = {
.passkey_display = auth_passkey_display,
.passkey_confirm = auth_passkey_confirm,
.cancel = auth_cancel,
};
static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
.pairing_complete = pairing_complete,
.pairing_failed = pairing_failed
};
#else
static struct bt_conn_auth_cb conn_auth_callbacks;
#endif
static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
uint16_t len)
{
int err;
char addr[BT_ADDR_LE_STR_LEN] = {0};
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));
printk("Received data from: %s", addr);
for (uint16_t pos = 0; pos != len;) {
struct uart_data_t *tx = k_malloc(sizeof(*tx));
if (!tx) {
printk("Not able to allocate UART send data buffer");
return;
}
/* 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);
}
}
}
static struct bt_nus_cb nus_cb = {
.received = bt_receive_cb,
};
void error(void)
{
dk_set_leds_state(DK_ALL_LEDS_MSK, DK_NO_LEDS_MSK);
while (true) {
/* Spin for ever */
k_sleep(K_MSEC(1000));
}
}
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
static void num_comp_reply(bool accept)
{
if (accept) {
bt_conn_auth_passkey_confirm(auth_conn);
LOG_INF("Numeric Match, conn %p", (void *)auth_conn);
} else {
bt_conn_auth_cancel(auth_conn);
LOG_INF("Numeric Reject, conn %p", (void *)auth_conn);
}
bt_conn_unref(auth_conn);
auth_conn = NULL;
}
void button_changed(uint32_t button_state, uint32_t has_changed)
{
uint32_t buttons = button_state & has_changed;
if (auth_conn) {
if (buttons & KEY_PASSKEY_ACCEPT) {
num_comp_reply(true);
}
if (buttons & KEY_PASSKEY_REJECT) {
num_comp_reply(false);
}
}
}
#endif /* CONFIG_BT_NUS_SECURITY_ENABLED */
static void configure_gpio(void)
{
int err;
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
err = dk_buttons_init(button_changed);
if (err) {
LOG_ERR("Cannot init buttons (err: %d)", err);
}
#endif /* CONFIG_BT_NUS_SECURITY_ENABLED */
err = dk_leds_init();
if (err) {
LOG_ERR("Cannot init LEDs (err: %d)", err);
}
}
#pragma endregion
void main(void)
{
int err;
int blink_status = 0;
err = uart_init();
if (err)
{
error();
}
if (IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED))
{
err = bt_conn_auth_cb_register(&conn_auth_callbacks);
if (err) {
printk("Failed to register authorization callbacks.\n");
return;
}
// err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
// if (err) {
// printk("Failed to register authorization info callbacks.\n");
// return;
// }
}
// DIP switches initialization
dip_switch_init();
// ADC initialization
err = adc_init();
if (err)
{
printk("ADC setup failed (error %d)\n", err);
return;
}
// Reset advData buffer
bt_data_bytes_reset(advData, sizeof(advData));
printk("Starting Peripheral\n");
// LEDs initialization (disable for ble uart)
err = leds_init();
if (err)
{
printk("LEDs init failed (err %d)\n", err);
return;
}
// BT initialization
err = bt_enable(NULL);
if (err)
{
return;
}
printk("\nBluetooth initialized\n");
k_sem_give(&ble_init_ok);
if (IS_ENABLED(CONFIG_SETTINGS))
{
settings_load();
}
err = bt_nus_init(&nus_cb);
if (err)
{
printk("\nFailed to initialize UART service (err: %d)\n", err);
return;
}
// Get slave ID based on dip switch position and MAC addr
get_mac_address(mac_address_str, sizeof(mac_address_str));
// Scanning initialization
bt_le_scan_cb_register(&scan_callbacks);
printk("Registered scan callbacks\n");
// Starting adv
err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err)
{
printk("Advertising failed to start (err %d)\n", err);
return;
}
printk("Advertising started\n");
// Timer initialization
k_timer_start(&htimer, K_MSEC(1000), K_MSEC(10));
static uint16_t countValue = 0;
while (1)
{
if(timerStartFlag == true)
{
timerStartFlag = false;
advUpdateFlag = true;
printk("Timer started\n");
err = bt_le_adv_stop();
if (err)
{
printk("Stop adv failed (err %d)\n", err);
return err;
}
printk("adv stopped\n");
k_timer_start(&htimer, K_MSEC(1000), K_MSEC(10));
err = bt_le_scan_start(&scan_param, data_cb);
if (err)
{
printk("Start scanning failed (err %d)\n", err);
return err;
}
printk("Scanning started\n");
}
if (sysFlags.timer_1000ms_flag == true)
{
sysFlags.timer_1000ms_flag = false;
gpio_pin_toggle_dt(&leds_array[1]);
}
runApplication();
if (sysFlags.bt_adv_task == true)
{
sysFlags.bt_adv_task = false;
if(advUpdateFlag == true)
{
bt_update_packet();
gpio_pin_toggle_dt(&leds_array[2]);
}
}
}
}
void ble_write_thread(void)
{
// Don't go any further until BLE is initialized
k_sem_take(&ble_init_ok, K_FOREVER);
for (;;)
{
// Wait indefinitely for data to be sent over bluetooth
struct uart_data_t *buf = k_fifo_get(&fifo_uart_rx_data,
K_FOREVER);
if (bt_nus_send(NULL, buf->data, buf->len)) {
LOG_WRN("Failed to send data over BLE connection");
}
k_free(buf);
}
}
K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
NULL, PRIORITY, 0, 0);
# # Copyright (c) 2021 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # CONFIG_NCS_SAMPLES_DEFAULTS=y CONFIG_BT=y CONFIG_BT_PERIPHERAL=y CONFIG_BT_CENTRAL=y CONFIG_BT_DEVICE_NAME="Slave" # -> SlaveXX_22476E CONFIG_BT_DEVICE_APPEARANCE=832 CONFIG_BT_MAX_CONN=2 CONFIG_BT_MAX_PAIRED=2 CONFIG_BT_SMP=y CONFIG_BT_SCAN=y CONFIG_BT_SCAN_FILTER_ENABLE=y CONFIG_BT_SCAN_UUID_CNT=1 CONFIG_BT_GATT_CLIENT=y CONFIG_BT_GATT_DM=y CONFIG_BT_HRS=y CONFIG_BT_HRS_CLIENT=y CONFIG_BT_SETTINGS=y CONFIG_FLASH=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_FLASH_MAP=y CONFIG_NVS=y CONFIG_SETTINGS=y CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_DK_LIBRARY=y CONFIG_SENSOR=y # CONFIG_STDOUT_CONSOLE=y # CONFIG_CBPRINTF_FP_SUPPORT=y # CONFIG_TEMP_NRF5=y # added for extended packet #CONFIG_BT_EXT_ADV=y # added for ADC CONFIG_GPIO=y CONFIG_ADC=y # CONFIG_ADC_ASYNC=y # CONFIG_CBPRINTF_FP_A_SUPPORT=y # CONFIG_USE_SEGGER_RTT=y # CONFIG_LOG=y # CONFIG_LOG_BACKEND_SHOW_COLOR=y # Enable multithreading # CONFIG_MULTITHREADING=y # TIMER 4 # CONFIG_NRFX_TIMER4=y # Sensor # CONFIG_SENSOR=y # Enable internal oscillator # CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y # Enable MPU # CONFIG_ARM_MPU=y # Enable hardware stack protection # CONFIG_HW_STACK_PROTECTION=y # Configure GPIO as pin RESET # CONFIG_GPIO_AS_PINRESET=y # FLASH configuration CONFIG_MPU_ALLOW_FLASH_WRITE=y CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y # Get MAC address CONFIG_BT_HCI_VS_EXT=y # ------------------------- For BLE uart ------------------------- # Enable the UART driver CONFIG_UART_ASYNC_API=y CONFIG_NRFX_UARTE0=y CONFIG_SERIAL=y # Make sure printk is printing to the UART console CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y # This example requires more workqueue stack # CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 by default = 2048 (no need to include) # Config logger CONFIG_LOG=n #y CONFIG_USE_SEGGER_RTT=y CONFIG_LOG_BACKEND_RTT=n #y CONFIG_LOG_BACKEND_UART=n CONFIG_ASSERT=y # Enable the NUS service CONFIG_BT_NUS=y