Skip to content

Commit e18f36e

Browse files
add tx power to cordio hci driver
1 parent c7759fe commit e18f36e

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ void CordioHCIDriver::on_host_stack_inactivity()
287287
{
288288
}
289289

290+
ble_error_t CordioHCIDriver::set_tx_power(int8_t level_db) {
291+
return BLE_ERROR_NOT_IMPLEMENTED;
292+
}
293+
290294
} // namespace cordio
291295
} // namespace vendor
292296
} // namespace ble

features/FEATURE_BLE/targets/TARGET_CORDIO/driver/CordioHCIDriver.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <BLETypes.h>
2323
#include "wsf_buf.h"
2424
#include "CordioHCITransportDriver.h"
25+
#include "ble/blecommon.h"
2526

2627
namespace ble {
2728
namespace vendor {
@@ -144,6 +145,16 @@ class CordioHCIDriver {
144145
*/
145146
virtual void on_host_stack_inactivity();
146147

148+
/**
149+
* Set desired transmit power. Value equal or bigger will be used from available levels.
150+
* Consult chip documentation for available values. Actual TX power is not guaranteed
151+
* and is down to the implementation.
152+
*
153+
* @param level_db Signal level in dBm.
154+
* @return BLE_ERROR_NONE on success.
155+
*/
156+
virtual ble_error_t set_tx_power(int8_t level_db);
157+
147158
protected:
148159
/**
149160
* Return a default set of memory pool that the Cordio stack can use.

features/FEATURE_BLE/targets/TARGET_CORDIO/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
"help": "Where the CBC MAC calculatio is performed. Valid values are 0 (host) and 1 (controller through HCI).",
6464
"value": 1,
6565
"macro_name": "SEC_CCM_CFG"
66+
},
67+
"preferred-tx-power": {
68+
"help": "Preferred value of tx power in dbm (-128,127). This value is not guaranteed and relies on existing support in the HCI driver.",
69+
"value": 0
6670
}
6771
}
6872
}

features/FEATURE_BLE/targets/TARGET_STM/TARGET_STM32WB/HCIDriver.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include <stdio.h>
20+
#include "ble/blecommon.h"
2021
#include "CordioBLE.h"
2122
#include "CordioHCIDriver.h"
2223
#include "CordioHCITransportDriver.h"
@@ -131,6 +132,33 @@ class HCIDriver : public cordio::CordioHCIDriver {
131132
HciResetCmd();
132133
}
133134

135+
static uint8_t convert_db_to_tx_power_index(int8_t level_db) {
136+
const int8_t conversion[] = {
137+
-40, -21 - 10 - 19, -18, -16, -15, -14, -13, -12, -11, -10, -9, -8,
138+
-7, -6, -5, -4, -3, -2, -2, -1, -1, 0, 0, 0, 1, 2, 3, 4, 5, 6,
139+
};
140+
141+
uint8_t index;
142+
for (index = 0; index < sizeof(conversion); ++index) {
143+
if (level_db <= conversion[index]) {
144+
break;
145+
}
146+
}
147+
return index;
148+
}
149+
150+
virtual ble_error_t set_tx_power(int8_t level_db) {
151+
152+
153+
uint8_t buf[2];
154+
buf[0] = 0x1; // Enable high power mode - deprecated and ignored on STM32WB
155+
buf[1] = convert_db_to_tx_power_index(level_db);
156+
157+
HciVendorSpecificCmd(ACI_HAL_SET_TX_POWER_LEVEL, 2, buf);
158+
159+
return BLE_ERROR_NONE;
160+
}
161+
134162
/**
135163
* @see CordioHCIDriver::handle_reset_sequence
136164
*/
@@ -177,15 +205,15 @@ class HCIDriver : public cordio::CordioHCIDriver {
177205
} else {
178206
tr_info("could not find BDaddr");
179207
/* Skip to next step */
180-
aciSetTxPowerLevel();
208+
set_tx_power(MBED_CONF_CORDIO_PREFERRED_TX_POWER);
181209
}
182210
break;
183211

184212
case ACI_WRITE_CONFIG_DATA_OPCODE:
185213
tr_debug("Bluetooth Device address set");
186214
/* set the event mask to control which events are generated by the
187215
* controller for the host */
188-
aciSetTxPowerLevel();
216+
set_tx_power(MBED_CONF_CORDIO_PREFERRED_TX_POWER);
189217
break;
190218

191219

@@ -359,16 +387,6 @@ class HCIDriver : public cordio::CordioHCIDriver {
359387

360388
private:
361389
uint8_t bd_addr[6];
362-
void aciSetTxPowerLevel()
363-
{
364-
uint8_t *pBuf = hciCmdAlloc(ACI_HAL_SET_TX_POWER_LEVEL, 2);
365-
if (!pBuf) {
366-
return;
367-
}
368-
pBuf[HCI_CMD_HDR_LEN] = 0x1;
369-
pBuf[HCI_CMD_HDR_LEN + 1] = 0x18;
370-
hciCmdSend(pBuf);
371-
}
372390

373391
void aciReadConfigParameter(uint8_t offset)
374392
{

0 commit comments

Comments
 (0)