This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf5340-dk

Hello Sirs

I have two questions about the nrf5340-dk evaluation board.

The first one is how can I get the scan data of an advertising device?

And the second is that how can I change the TX power? 

There is a sample program (hci_pwr_ctrl) for this purpose. I have built and ran this program on 

my evolution board but it runs with errors and could not do the job.  

Parents
  • Thank you very much for your immediate response. I have changed the CONFIG file as you said , but still I have received the same error

    (Set Tx Power err: -5 reason 0x00) message and the Tx power doesn't change .Bellow is my entire code:

    /* main.c - Application main entry point */

    /*
    * Copyright (c) 2015-2016 Intel Corporation
    *
    * SPDX-License-Identifier: Apache-2.0
    */

    #include <zephyr/types.h>
    #include <stddef.h>
    #include <sys/printk.h>
    #include <sys/util.h>

    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>


    #include <sys/byteorder.h>
    #include <bluetooth/hci_vs.h>
    //#include <bluetooth/conn.h>
    //#include <bluetooth/uuid.h>
    //#include <bluetooth/gatt.h>
    //#include <bluetooth/services/hrs.h>

    #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
    #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

    static uint16_t default_conn_handle;
    static const int8_t txp[8] = {4, 0, -3, -8,
    -15, -18, -23, -30};


    /*
    * Set Advertisement data. Based on the Eddystone specification:
    * github.com/.../protocol-specification.md
    * github.com/.../eddystone-url
    */
    #ifndef IBEACON_RSSI
    #define IBEACON_RSSI 0xc8
    #endif


    static const struct bt_data ad[] = { // bt_data ibeacon_ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
    BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA,
    0x00, 0x4c, /* Apple */
    0x02, 0x15, /* iBeacon */
    0x18, 0xee, 0x15, 0x16, /* UUID[15..12] */
    0x00, 0x01, /* UUID[11..10] */
    0x4b, 0xec, /* UUID[9..8] */
    0xad, 0x96, /* UUID[7..6] */
    0xbc, 0xb9, 0x6d, 0x16, 0x6e, 0x97, /* UUID[5..0] */
    0x00, 0x10, /* Major */
    0x00, 0xff, /* Minor */
    IBEACON_RSSI), /* Calibrated RSSI @ 1m */


    };

    /*
    static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL),
    BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa,0xfe),

    BT_DATA_BYTES(BT_DATA_SVC_DATA16,
    0xaa, 0xfe, // Eddystone UUID //
    0x10, // Eddystone-URL frame type //
    0x00, // Calibrated Tx power at 0m //
    0x00, // URL Scheme Prefix http://www. //
    'z', 'e', 'p', 'h', 'y', 'r',
    'p', 'r', 'o', 'j', 'e', 'c', 't',
    0x08), // .org //

    // BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE,"Nasser")
    };
    */
    /* Set Scan Response data */
    static const struct bt_data sd[] = {
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),

    };

    static void bt_ready(int err)
    {
    char addr_s[BT_ADDR_LE_STR_LEN];
    bt_addr_le_t addr = {0};

    printk("Display Addres Content == %d\n", addr);
    printk("Display Addres Content of err in bt-ready %d\n", err);
    size_t count = 1;

    if (err) {
    printk("Bluetooth init failed (err %d)\n", err);
    return;
    }

    printk("Bluetooth initialized By Mahvis\n");


    /* Start advertising */
    err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad), // BT_LE_ADV_NCONN
    sd, ARRAY_SIZE(sd));
    printk("Arrey Size %s\n", ARRAY_SIZE(sd));

    printk("ad = %d\n",ad );


    if (err) {
    printk("Advertising failed to start (err %d)\n", err);
    return;
    }


    /* For connectable advertising you would use
    * bt_le_oob_get_local(). For non-connectable non-identity
    * advertising an non-resolvable private address is used;
    * there is no API to retrieve that.
    */

    bt_id_get(&addr, &count);
    bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));

    printk("Beacon started, advertising as %s\n", addr_s);
    }


    static void set_tx_power(uint8_t handle_type, uint16_t handle, int8_t tx_pwr_lvl)
    {
    struct bt_hci_cp_vs_write_tx_power_level *cp;
    struct bt_hci_rp_vs_write_tx_power_level *rp;
    struct net_buf *buf, *rsp = NULL;
    int err;

    buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    sizeof(*cp));
    if (!buf) {
    printk("Unable to allocate command buffer\n");
    return;
    }

    cp = net_buf_add(buf, sizeof(*cp));
    cp->handle = sys_cpu_to_le16(handle);
    cp->handle_type = handle_type;
    cp->tx_power_level = tx_pwr_lvl;

    err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    buf, &rsp);
    if (err) {
    uint8_t reason = rsp ?
    ((struct bt_hci_rp_vs_write_tx_power_level *)
    rsp->data)->status : 0;
    printk("Set Tx power err: %d reason 0x%02x\n", err, reason);
    return;
    }

    rp = (void *)rsp->data;
    printk("Actual Tx Power: %d\n", rp->selected_tx_power);

    net_buf_unref(rsp);
    }


    void main(void)
    {
    int err;

    printk("Starting Beacon Demo By Mahvis\n");



    /* Initialize the Bluetooth Subsystem */
    err = bt_enable(bt_ready);
    if (err) {
    printk("Bluetooth init failed (err %d)\n", err);
    }

    set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV,
    0,-30 );


    k_sleep(K_SECONDS(5));

    }

Reply
  • Thank you very much for your immediate response. I have changed the CONFIG file as you said , but still I have received the same error

    (Set Tx Power err: -5 reason 0x00) message and the Tx power doesn't change .Bellow is my entire code:

    /* main.c - Application main entry point */

    /*
    * Copyright (c) 2015-2016 Intel Corporation
    *
    * SPDX-License-Identifier: Apache-2.0
    */

    #include <zephyr/types.h>
    #include <stddef.h>
    #include <sys/printk.h>
    #include <sys/util.h>

    #include <bluetooth/bluetooth.h>
    #include <bluetooth/hci.h>


    #include <sys/byteorder.h>
    #include <bluetooth/hci_vs.h>
    //#include <bluetooth/conn.h>
    //#include <bluetooth/uuid.h>
    //#include <bluetooth/gatt.h>
    //#include <bluetooth/services/hrs.h>

    #define DEVICE_NAME CONFIG_BT_DEVICE_NAME
    #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

    static uint16_t default_conn_handle;
    static const int8_t txp[8] = {4, 0, -3, -8,
    -15, -18, -23, -30};


    /*
    * Set Advertisement data. Based on the Eddystone specification:
    * github.com/.../protocol-specification.md
    * github.com/.../eddystone-url
    */
    #ifndef IBEACON_RSSI
    #define IBEACON_RSSI 0xc8
    #endif


    static const struct bt_data ad[] = { // bt_data ibeacon_ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR),
    BT_DATA_BYTES(BT_DATA_MANUFACTURER_DATA,
    0x00, 0x4c, /* Apple */
    0x02, 0x15, /* iBeacon */
    0x18, 0xee, 0x15, 0x16, /* UUID[15..12] */
    0x00, 0x01, /* UUID[11..10] */
    0x4b, 0xec, /* UUID[9..8] */
    0xad, 0x96, /* UUID[7..6] */
    0xbc, 0xb9, 0x6d, 0x16, 0x6e, 0x97, /* UUID[5..0] */
    0x00, 0x10, /* Major */
    0x00, 0xff, /* Minor */
    IBEACON_RSSI), /* Calibrated RSSI @ 1m */


    };

    /*
    static const struct bt_data ad[] = {
    BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL),
    BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0xaa,0xfe),

    BT_DATA_BYTES(BT_DATA_SVC_DATA16,
    0xaa, 0xfe, // Eddystone UUID //
    0x10, // Eddystone-URL frame type //
    0x00, // Calibrated Tx power at 0m //
    0x00, // URL Scheme Prefix http://www. //
    'z', 'e', 'p', 'h', 'y', 'r',
    'p', 'r', 'o', 'j', 'e', 'c', 't',
    0x08), // .org //

    // BT_DATA_BYTES(BT_DATA_GAP_APPEARANCE,"Nasser")
    };
    */
    /* Set Scan Response data */
    static const struct bt_data sd[] = {
    BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),

    };

    static void bt_ready(int err)
    {
    char addr_s[BT_ADDR_LE_STR_LEN];
    bt_addr_le_t addr = {0};

    printk("Display Addres Content == %d\n", addr);
    printk("Display Addres Content of err in bt-ready %d\n", err);
    size_t count = 1;

    if (err) {
    printk("Bluetooth init failed (err %d)\n", err);
    return;
    }

    printk("Bluetooth initialized By Mahvis\n");


    /* Start advertising */
    err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad, ARRAY_SIZE(ad), // BT_LE_ADV_NCONN
    sd, ARRAY_SIZE(sd));
    printk("Arrey Size %s\n", ARRAY_SIZE(sd));

    printk("ad = %d\n",ad );


    if (err) {
    printk("Advertising failed to start (err %d)\n", err);
    return;
    }


    /* For connectable advertising you would use
    * bt_le_oob_get_local(). For non-connectable non-identity
    * advertising an non-resolvable private address is used;
    * there is no API to retrieve that.
    */

    bt_id_get(&addr, &count);
    bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s));

    printk("Beacon started, advertising as %s\n", addr_s);
    }


    static void set_tx_power(uint8_t handle_type, uint16_t handle, int8_t tx_pwr_lvl)
    {
    struct bt_hci_cp_vs_write_tx_power_level *cp;
    struct bt_hci_rp_vs_write_tx_power_level *rp;
    struct net_buf *buf, *rsp = NULL;
    int err;

    buf = bt_hci_cmd_create(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    sizeof(*cp));
    if (!buf) {
    printk("Unable to allocate command buffer\n");
    return;
    }

    cp = net_buf_add(buf, sizeof(*cp));
    cp->handle = sys_cpu_to_le16(handle);
    cp->handle_type = handle_type;
    cp->tx_power_level = tx_pwr_lvl;

    err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL,
    buf, &rsp);
    if (err) {
    uint8_t reason = rsp ?
    ((struct bt_hci_rp_vs_write_tx_power_level *)
    rsp->data)->status : 0;
    printk("Set Tx power err: %d reason 0x%02x\n", err, reason);
    return;
    }

    rp = (void *)rsp->data;
    printk("Actual Tx Power: %d\n", rp->selected_tx_power);

    net_buf_unref(rsp);
    }


    void main(void)
    {
    int err;

    printk("Starting Beacon Demo By Mahvis\n");



    /* Initialize the Bluetooth Subsystem */
    err = bt_enable(bt_ready);
    if (err) {
    printk("Bluetooth init failed (err %d)\n", err);
    }

    set_tx_power(BT_HCI_VS_LL_HANDLE_TYPE_ADV,
    0,-30 );


    k_sleep(K_SECONDS(5));

    }

Children
  • Hi 

    Do you need dynamic power control, or is static power configuration sufficient?

    If static power configuration is enough I would recommend starting out with some other Bluetooth example more related to what you are trying to do, and then configure the power consumption from there. 

    It seems there are some issues with Bluetooth power control in Zephyr/NCS, and that setting this is not as straight forward as I expected. Unfortunately the month of July is peak holiday season here in Norway, so it might take some time to get to the bottom of this, but I will try to find some time to look into this next week. 

    Best regards
    Torbjørn

Related