Skip to content

Commit 8f87735

Browse files
authored
Merge pull request #13040 from paul-szczepanek-arm/cordio-tx-power
BLE: Add tx power control to cordio HCI driver
2 parents 3333f41 + fe98309 commit 8f87735

File tree

5 files changed

+61
-13
lines changed

5 files changed

+61
-13
lines changed

features/FEATURE_BLE/targets/TARGET_CORDIO/doc/HCIAbstraction.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ specific to the controller used.
4646
The driver also provides an interface to perform RF testing on the BLE trasnmitter.
4747
This is done using the LE Receiver/Transmitter Test command and LE Test End command
4848
as described in the Bluetooth Core spec in Vol.2, Part E, 7.8.28-30.
49+
50+
The driver allows to set preferred TX power. This is an optional feature
51+
(check return code) and the support for it and the extent of control is
52+
down to the BLE chip. Chips may have different allowed values, although the
53+
command will pick the closest value no smaller than requested. Please refer
54+
to the controller code or BLE chip documentation.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ ble_error_t CordioHCIDriver::rf_test_end()
364364
return BLE_ERROR_NO_MEM;
365365
}
366366

367+
ble_error_t CordioHCIDriver::set_tx_power(int8_t level_db) {
368+
return BLE_ERROR_NOT_IMPLEMENTED;
369+
}
370+
367371
} // namespace cordio
368372
} // namespace vendor
369373
} // namespace ble

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ class CordioHCIDriver {
187187
*/
188188
ble_error_t rf_test_end();
189189

190+
/**
191+
* Set desired transmit power. Value equal or bigger will be used from available levels.
192+
* Consult chip documentation for available values. Actual TX power is not guaranteed
193+
* and is down to the implementation.
194+
*
195+
* @param level_db Signal level in dBm.
196+
* @return BLE_ERROR_NONE on success.
197+
*/
198+
virtual ble_error_t set_tx_power(int8_t level_db);
199+
190200
protected:
191201
/**
192202
* Return a default set of memory pool that the Cordio stack can use.

features/FEATURE_BLE/targets/TARGET_CORDIO/mbed_lib.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
"route_unhandled_command_complete_events": {
6868
"help": "If enabled the stack will forward to the user all HCI events not handled by the stack.",
6969
"value": 1
70+
},
71+
"preferred-tx-power": {
72+
"help": "Preferred value of tx power in dbm (-128,127). This value is not guaranteed and relies on existing support in the HCI driver.",
73+
"value": 0
7074
}
7175
}
72-
}
76+
}

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

Lines changed: 36 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,39 @@ 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, -20, -19,
138+
-18, -16, -15, -14,
139+
-13, -12, -11, -10,
140+
-9, -8, -7, -6,
141+
-5, -4, -3, -2,
142+
-1, -1, -1, -1,
143+
0, 0, 1, 2,
144+
3, 4, 5, 6
145+
};
146+
147+
uint8_t index;
148+
for (index = 0; index < sizeof(conversion); ++index) {
149+
if (level_db <= conversion[index]) {
150+
break;
151+
}
152+
}
153+
return index;
154+
}
155+
156+
virtual ble_error_t set_tx_power(int8_t level_db) {
157+
158+
159+
uint8_t buf[2];
160+
buf[0] = 0x1; // Enable high power mode - deprecated and ignored on STM32WB
161+
buf[1] = convert_db_to_tx_power_index(level_db);
162+
163+
HciVendorSpecificCmd(ACI_HAL_SET_TX_POWER_LEVEL, 2, buf);
164+
165+
return BLE_ERROR_NONE;
166+
}
167+
134168
/**
135169
* @see CordioHCIDriver::handle_reset_sequence
136170
*/
@@ -177,15 +211,15 @@ class HCIDriver : public cordio::CordioHCIDriver {
177211
} else {
178212
tr_info("could not find BDaddr");
179213
/* Skip to next step */
180-
aciSetTxPowerLevel();
214+
set_tx_power(MBED_CONF_CORDIO_PREFERRED_TX_POWER);
181215
}
182216
break;
183217

184218
case ACI_WRITE_CONFIG_DATA_OPCODE:
185219
tr_debug("Bluetooth Device address set");
186220
/* set the event mask to control which events are generated by the
187221
* controller for the host */
188-
aciSetTxPowerLevel();
222+
set_tx_power(MBED_CONF_CORDIO_PREFERRED_TX_POWER);
189223
break;
190224

191225

@@ -359,16 +393,6 @@ class HCIDriver : public cordio::CordioHCIDriver {
359393

360394
private:
361395
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-
}
372396

373397
void aciReadConfigParameter(uint8_t offset)
374398
{

0 commit comments

Comments
 (0)