前言:本文章主要记录 ESP32C3 如何设置发射功率,以及对BLE 设备发射功率功率级别、RSSI 以及接收机灵敏度之间的关系做简单整理
1、BLE 发射功率级别
在当前最新的蓝牙核心规范v5.3 中规定,BLE 发射设备的最大发射功率必须在 0.01 mW (-20 dBm) 和 100 mW (+20 dBm) 之间,并且根据 BLE 设备支持的最大输出功率 Pmax 分为以下四个功率级别
功率级别(Power Class) | Requirements |
功率级1 | 100 mW (+20 dBm) ≥ Pmax ˃ 10 mW (+10 dBm) |
功率级1.5 | 10 mW (+10 dBm) ≥ Pmax ˃ 2.5 mW (+4 dBm) |
功率级2 | 2.5 mW (+4 dBm) ≥ Pmax ˃ 1 mW (0 dBm) |
功率级3 | 1 mW (0 dBm) ≥ Pmax ≥ 0.01 mW (-20 dBm) |
2、RSSI 与灵敏度
RSSI 全称是Received Signal Strength Indication, 表示为接收的信号强度指示, 单位是 dBm, 是一个考征功率绝对值的值,计算公式为:10lgP(功率值/1mW),例如发射功率 P为1mW,折算为dBm后为 0dBm, 如果发射功率 P 为 20W, 按dBm单位进行折算后的值应为:10lg(20W/1mW)=10lg(20000)=10lg2+10lg10+10lg1000=43dBm
蓝牙扫描设备 (如手机,或者成为蓝牙接收机) 扫描或者显示的 RSSI 常为负值,原因有两点:一是蓝牙发射设备的发射功率小,有的是功率级3 的蓝牙设备,发射功率电平最高才为 0dbm; 二是路径损耗,主要指从发射机到接收机天线的能量消耗,和路径长度、周围环境、信号干扰都有很大关联,这个也是信号强度衰减的主要因素
对于接收机灵敏度,在蓝牙核心规范v5.3 中对不同 PHY 有如下规定
PHY | Sensitivity(dBm) |
LE Uncoded PHYs | ≤ -70 |
LE Coded PHY with S=2 coding | ≤ -75 |
LE Coded PHY with S=8 coding | ≤ -82 |
接收机灵敏度越小,收包的距离就越长,这可以作为评测蓝牙性能的一项指标,一般可以在蓝牙芯片的 datasheet 中找到,如 ESP32C3 LE Uncoded 1M PHY 灵敏度 @30.8% PER 典型值是 -97dbm, LE Uncoded 2M PHY 灵敏度 @30.8% PER 典型值是 -93dbm, LE Coded PHY with S=2 coding 灵敏度 @30.8% PER 典型值是 -100dbm, LE Coded PHY with S=8 coding 灵敏度 @30.8% PER 典型值是 -105dbm, 这也可以大致看出 LE Coded PHY 的传输距离要长。
灵敏度 @30.8% PER 的含义
这个和蓝牙测试规范有关,参考BT4.0-RF-PHY.TS.5.0.1规范协议以下一段解释
大致意思是 37字节的有效负载时 368bit ,然后每一个有效负载位都出错率小于0.001 的可能性是 0.692,反过来误码率就是 0.308
路径损耗与距离的非官方计算公式:path loss = 40 + 25log(d)
, path loss 标识路径损耗,d标识发射机和接收机的距离。数据统计如下:
路径损耗(path loss) | 距离(d) |
50dbm | 2.5m |
60dbm | 6.3m |
70dbm | 16m |
80dbm | 40m |
90dbm | 100m |
100dbm | 250m |
110dbm | 630m |
以上可能是空旷环境下的数据,每个蓝牙设备的硬件结构以及所处传输环境差异较大,当然也并没有统一的计算公式。
3、ESP32C3 上如何设置发射功率
在ESP32C3 datasheet 上,发射功率的可控范围是 –24 ~ 21dBm, 代码中可以通过如下 API 进行设置(esp_ble_tx_power_set
)和获取(esp_ble_tx_power_get
)
typedef enum {
ESP_BLE_PWR_TYPE_CONN_HDL0 = 0, /*!< For connection handle 0 */
ESP_BLE_PWR_TYPE_CONN_HDL1 = 1, /*!< For connection handle 1 */
ESP_BLE_PWR_TYPE_CONN_HDL2 = 2, /*!< For connection handle 2 */
ESP_BLE_PWR_TYPE_CONN_HDL3 = 3, /*!< For connection handle 3 */
ESP_BLE_PWR_TYPE_CONN_HDL4 = 4, /*!< For connection handle 4 */
ESP_BLE_PWR_TYPE_CONN_HDL5 = 5, /*!< For connection handle 5 */
ESP_BLE_PWR_TYPE_CONN_HDL6 = 6, /*!< For connection handle 6 */
ESP_BLE_PWR_TYPE_CONN_HDL7 = 7, /*!< For connection handle 7 */
ESP_BLE_PWR_TYPE_CONN_HDL8 = 8, /*!< For connection handle 8 */
ESP_BLE_PWR_TYPE_ADV = 9, /*!< For advertising */
ESP_BLE_PWR_TYPE_SCAN = 10, /*!< For scan */
ESP_BLE_PWR_TYPE_DEFAULT = 11, /*!< For default, if not set other, it will use default value */
ESP_BLE_PWR_TYPE_NUM = 12, /*!< TYPE numbers */
} esp_ble_power_type_t;
/**
* @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm).
*/
typedef enum {
ESP_PWR_LVL_N24 = 0, /*!< Corresponding to -24dbm */
ESP_PWR_LVL_N21 = 1, /*!< Corresponding to -21dbm */
ESP_PWR_LVL_N18 = 2, /*!< Corresponding to -18dbm */
ESP_PWR_LVL_N15 = 3, /*!< Corresponding to -15dbm */
ESP_PWR_LVL_N12 = 4, /*!< Corresponding to -12dbm */
ESP_PWR_LVL_N9 = 5, /*!< Corresponding to -9dbm */
ESP_PWR_LVL_N6 = 6, /*!< Corresponding to -6dbm */
ESP_PWR_LVL_N3 = 7, /*!< Corresponding to -3dbm */
ESP_PWR_LVL_N0 = 8, /*!< Corresponding to 0dbm */
ESP_PWR_LVL_P3 = 9, /*!< Corresponding to +3dbm */
ESP_PWR_LVL_P6 = 10, /*!< Corresponding to +6dbm */
ESP_PWR_LVL_P9 = 11, /*!< Corresponding to +9dbm */
ESP_PWR_LVL_P12 = 12, /*!< Corresponding to +12dbm */
ESP_PWR_LVL_P15 = 13, /*!< Corresponding to +15dbm */
ESP_PWR_LVL_P18 = 14, /*!< Corresponding to +18dbm */
ESP_PWR_LVL_P21 = 15, /*!< Corresponding to +21dbm */
ESP_PWR_LVL_INVALID = 0xFF, /*!< Indicates an invalid value */
} esp_power_level_t;
/**
* @brief Set BLE TX power
* Connection Tx power should only be set after connection created.
* @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
* @param power_level: Power level(index) corresponding to absolute value(dbm)
* @return ESP_OK - success, other - failed
*/
esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level);
/**
* @brief Get BLE TX power
* Connection Tx power should only be get after connection created.
* @param power_type : The type of which tx power, could set Advertising/Connection/Default and etc
* @return >= 0 - Power level, < 0 - Invalid
*/
esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type);
如果 ESP32C3 使用多广播,怎样对每个广播设置不同的发射功率?
使用 esp_ble_gap_ext_adv_set_params
函数设置, 扩展广播参数结构体 esp_ble_gap_ext_adv_params_t
中有 tx_power
选项
/**
* @brief ext adv parameters
*/
typedef struct {
esp_ble_ext_adv_type_mask_t type; /*!< ext adv type */
uint32_t interval_min; /*!< ext adv minimum interval */
uint32_t interval_max; /*!< ext adv maximum interval */
esp_ble_adv_channel_t channel_map; /*!< ext adv channel map */
esp_ble_addr_type_t own_addr_type; /*!< ext adv own address type */
esp_ble_addr_type_t peer_addr_type; /*!< ext adv peer address type */
esp_bd_addr_t peer_addr; /*!< ext adv peer address */
esp_ble_adv_filter_t filter_policy; /*!< ext adv filter policy */
int8_t tx_power; /*!< ext adv tx power */
esp_ble_gap_pri_phy_t primary_phy; /*!< ext adv primary phy */
uint8_t max_skip; /*!< ext adv maximum skip */
esp_ble_gap_phy_t secondary_phy; /*!< ext adv secondary phy */
uint8_t sid; /*!< ext adv sid */
bool scan_req_notif; /*!< ext adv scan request event notify */
} esp_ble_gap_ext_adv_params_t;
是通过如下 HCI 命令传输给 Controller 进行处理的
广播中的 tx power 结构数值是以补码形式表示的,比如 数值是 -21dBm, 补码就是 0xeb, 广播中的 tx power 结构数值是其声称的发射功率,也可能是虚假的。
参考: