|
| 1 | +.. _bluetooth_automated_power_control: |
| 2 | + |
| 3 | +Bluetooth: Automated power control |
| 4 | +################################## |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 2 |
| 9 | + |
| 10 | +The Automated power control sample demonstrates how to monitor the Bluetooth® Low Energy signal strength (RSSI) and dynamically adjust the transmit (TX) power of a connected Peripheral device. |
| 11 | +There are two versions of this sample: One for the central device (in the :file:`rssi_power_control/central` folder) and one for the peripheral device (in the :file:`rssi_power_control/peripheral` folder) |
| 12 | + |
| 13 | +The sample for the central device collects RSSI data and sends power control commands to optimize the connection quality, which in turn optimizes the current consumption. |
| 14 | + |
| 15 | +The sample for the peripheral device is simply advertising its existence, allowing the central device to establish a connection. |
| 16 | + |
| 17 | +Requirements |
| 18 | +************ |
| 19 | + |
| 20 | +The sample supports the following development kits: |
| 21 | + |
| 22 | +.. table-from-sample-yaml:: |
| 23 | + |
| 24 | +You must use two compatible development kits to run this sample, one running the version for the central device (:file:`rssi_power_control/central`) and one running the version for the peripheral device (:file:`rssi_power_control/peripheral`). |
| 25 | + |
| 26 | +Overview |
| 27 | +******** |
| 28 | + |
| 29 | +This sample illustrates Bluetooth LE power control by having the central device read the RSSI of an active connection and use it to adjust the transmit power of the peripheral version. |
| 30 | +The central version directly invokes SoftDevice Controller (SDC) APIs to configure power control behavior based on RSSI measurements. |
| 31 | +This approach only works on single-core platforms where the Bluetooth Controller and application run on the same core. |
| 32 | + |
| 33 | +There are two ways to monitor the connection parameters: |
| 34 | + |
| 35 | +* Terminal output - Connects to the Central device's virtual serial port to view RSSI and TX power logs in real time. |
| 36 | +* Graphical plotting - Uses the provided script to visualize signal strength over time: |
| 37 | + |
| 38 | + .. code-block:: console |
| 39 | +
|
| 40 | + python3 tools/plot_path_loss.py --comport COMPORT |
| 41 | +
|
| 42 | +.. note:: |
| 43 | + Make sure no terminal is open on the same serial port when using the Python plotting script, as it requires exclusive access. |
| 44 | + Example of serial port - ``/dev/ttyACM1``. |
| 45 | + |
| 46 | +This sample works only on single-core SoCs because it directly calls ``sdc_*`` functions (such as :c:func:`sdc_hci_cmd_vs_set_power_control_request_params`) that are only accessible on the core running the Bluetooth Controller. |
| 47 | +On dual-core platforms such as nRF5340 or nRF54H20 SoCs, the Bluetooth Controller runs on the network core, making these calls inaccessible from the application core and resulting in linker errors. |
| 48 | + |
| 49 | +Workaround for dual-core SoCs |
| 50 | +============================= |
| 51 | + |
| 52 | +To adapt this sample for dual-core SoCs: |
| 53 | + |
| 54 | +1. Remove all ``sdc_*`` function calls from the application. |
| 55 | +#. Use Zephyr HCI commands to interface with the controller: |
| 56 | + |
| 57 | + .. code-block:: c |
| 58 | +
|
| 59 | + struct net_buf *buf = bt_hci_cmd_create(...); |
| 60 | + bt_hci_cmd_send_sync(...); |
| 61 | +
|
| 62 | +Refer to the :zephyr:code-sample:`bluetooth_hci_pwr_ctrl` sample or Nordic DevZone for reference implementations. |
| 63 | + |
| 64 | +User interface |
| 65 | +************** |
| 66 | + |
| 67 | +The following is the user interface used by the two versions of this sample: |
| 68 | + |
| 69 | +Central device: |
| 70 | + |
| 71 | +* Virtual serial output displays live RSSI and TX power data. |
| 72 | +* Optional plotting provides a visual graph of signal variation over time. |
| 73 | + |
| 74 | +Peripheral device: |
| 75 | + |
| 76 | +* No direct user interface. |
| 77 | +* Automatically accepts connections and responds to TX power control commands. |
| 78 | + |
| 79 | +Configuration |
| 80 | +************* |
| 81 | + |
| 82 | +|config| |
| 83 | + |
| 84 | +Additional configuration |
| 85 | +======================== |
| 86 | + |
| 87 | +Check and configure the following Kconfig options: |
| 88 | + |
| 89 | +* :kconfig:option:`CONFIG_BT_CENTRAL` - Enables the Bluetooth LE Central role for the Central application. |
| 90 | +* :kconfig:option:`CONFIG_BT_PERIPHERAL` - Enables the Bluetooth LE Peripheral role for the Peripheral application. |
| 91 | +* :kconfig:option:`CONFIG_BT_DEVICE_NAME` - Sets the advertised name of the Peripheral (default: ``power_control_peripheral``). |
| 92 | +* :kconfig:option:`CONFIG_BT_SCAN` - Enables scanning for Bluetooth LE advertisements (Central only). |
| 93 | +* :kconfig:option:`CONFIG_BT_SCAN_FILTER_ENABLE` - Enables name-based filtering for Bluetooth scan results (Central only). |
| 94 | +* :kconfig:option:`CONFIG_BT_CTLR_CONN_RSSI` - Enables controller-side RSSI reporting for active connections. |
| 95 | +* :kconfig:option:`CONFIG_BT_TRANSMIT_POWER_CONTROL` - Enables support for Bluetooth LE transmit power control. |
| 96 | +* :kconfig:option:`CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL` - Allows dynamic adjustment of transmit power based on RSSI feedback. |
| 97 | +* :kconfig:option:`CONFIG_BT_HCI_RAW` - Enables raw HCI interface for communication between the application and controller cores (used in IPC radio configurations). |
| 98 | +* :kconfig:option:`CONFIG_LOG` - Enables logging through Zephyr's logging subsystem (optional but useful for debugging). |
| 99 | + |
| 100 | +Building and running |
| 101 | +******************** |
| 102 | + |
| 103 | +.. |sample path| replace:: :file:`samples/bluetooth/power_control_demo` |
| 104 | + |
| 105 | +.. include:: /includes/build_and_run.txt |
| 106 | + |
| 107 | +.. |sample_or_app| replace:: sample |
| 108 | + |
| 109 | +.. |ipc_radio_dir| replace:: :file:`sysbuild/ipc_radio` |
| 110 | + |
| 111 | +.. include:: /includes/ipc_radio_conf.txt |
| 112 | + |
| 113 | +Testing |
| 114 | +======= |
| 115 | + |
| 116 | +|test_sample| |
| 117 | + |
| 118 | +1. Program one development kit with the power control firmware for the peripheral device. |
| 119 | +#. Program another development kit with the power control firmware for the central device. |
| 120 | +#. Open a terminal on the Central's virtual serial port or run the plotting script. |
| 121 | +#. Power on both devices. |
| 122 | +#. The Central will scan and connect to the Peripheral automatically. |
| 123 | +#. Observe RSSI and TX power data printed or plotted. |
| 124 | +#. Move the devices or add obstructions to see dynamic power adjustment. |
| 125 | + |
| 126 | +Sample output |
| 127 | +============= |
| 128 | + |
| 129 | +The following output is logged from the central device: |
| 130 | + |
| 131 | +.. code-block:: console |
| 132 | +
|
| 133 | + [00:00:01.000,000] I: Starting BLE Central |
| 134 | + [00:00:01.500,000] I: Scanning for peripherals... |
| 135 | + [00:00:03.020,000] I: Found peripheral: FF:EE:DD:CC:BB:AA (random) |
| 136 | + [00:00:03.050,000] I: Connection to FF:EE:DD:CC:BB:AA (random) |
| 137 | + [00:00:03.800,000] I: Connected to FF:EE:DD:CC:BB:AA (random) |
| 138 | + [00:00:05.010,000] I: Successful initialization of power control |
| 139 | + [00:00:06.010,000] I: Tx: 0 dBm, RSSI: -33 dBm |
| 140 | + [00:00:07.010,000] I: Tx: -9 dBm, RSSI: -45 dBm |
| 141 | +
|
| 142 | +Dependencies |
| 143 | +************ |
| 144 | + |
| 145 | +This sample uses the following |NCS| libraries: |
| 146 | + |
| 147 | +* :ref:`nrf_bt_scan_readme` |
| 148 | + |
| 149 | +It also relies on the following Zephyr's Bluetooth and HCI layers: |
| 150 | + |
| 151 | +* :ref:`zephyr:kernel_api`: |
| 152 | + |
| 153 | + * :file:`include/zephyr/kernel.h` |
| 154 | + |
| 155 | +* :ref:`zephyr:bluetooth_api`: |
| 156 | + |
| 157 | + * :file:`include/zephyr/bluetooth/addr.h` |
| 158 | + * :file:`include/zephyr/bluetooth/bluetooth.h` |
| 159 | + * :file:`include/zephyr/bluetooth/conn.h` |
| 160 | + |
| 161 | +* :ref:`zephyr:logging_api`: |
| 162 | + |
| 163 | + * :file:`include/zephyr/logging/log.h` |
| 164 | + |
| 165 | +If using the optional Python plotting script, the following dependencies are required: |
| 166 | + |
| 167 | +* Python 3.6 or newer |
| 168 | +* The following Python modules: |
| 169 | + |
| 170 | + .. code-block:: console |
| 171 | +
|
| 172 | + pip install pyserial matplotlib |
0 commit comments