Skip to content

Commit 39f4ca4

Browse files
committed
subsys: normalize energy detection values
ZBOSS expects a 0-255 range. Signed-off-by: Eduardo Montoya <eduardo.montoya@nordicsemi.no>
1 parent 642e525 commit 39f4ca4

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

subsys/osif/zb_nrf_transceiver.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <zephyr/logging/log.h>
1010
#include <nrf_802154.h>
1111
#include <nrf_802154_const.h>
12+
#include <nrf_802154_nrfx_addons.h>
1213
#include <nrf_802154_types.h>
1314
#include <zboss_api.h>
1415
#include <zb_macll.h>
@@ -26,6 +27,23 @@
2627

2728
LOG_MODULE_DECLARE(zboss_osif, CONFIG_ZBOSS_OSIF_LOG_LEVEL);
2829

30+
/** Map ED in dBm to ZBOSS 0..255 scale. */
31+
static uint8_t zboss_normalize_ed_dbm(int8_t ed_dbm)
32+
{
33+
int32_t ed = ed_dbm;
34+
const int32_t min_dbm = ED_DBM_MIN;
35+
const int32_t max_dbm = ED_DBM_MAX;
36+
37+
if (ed <= min_dbm) {
38+
return 0U;
39+
}
40+
if (ed >= max_dbm) {
41+
return UINT8_MAX;
42+
}
43+
44+
return (uint8_t)(UINT8_MAX * (ed - min_dbm) / (max_dbm - min_dbm));
45+
}
46+
2947
enum zb_radio_state {
3048
ZB_RADIO_STATE_SLEEP,
3149
ZB_RADIO_STATE_RECEIVE,
@@ -60,7 +78,7 @@ struct nrf5_data {
6078

6179
struct {
6280
uint32_t time_us;
63-
int8_t value;
81+
uint8_t value;
6482
} energy_detection;
6583

6684
struct k_sem rssi_wait;
@@ -149,7 +167,7 @@ void zb_trans_get_rssi(zb_uint8_t *rssi_value_p)
149167
* or by nrf_802154_energy_detection_failed() after retry attempt.
150168
*/
151169
k_sem_take(&nrf5_data.rssi_wait, K_FOREVER);
152-
*rssi_value_p = (uint8_t)nrf5_data.energy_detection.value;
170+
*rssi_value_p = nrf5_data.energy_detection.value;
153171
LOG_DBG("Energy detected: %d", *rssi_value_p);
154172
}
155173

@@ -474,7 +492,7 @@ void nrf_802154_receive_failed(nrf_802154_rx_error_t error, uint32_t id)
474492

475493
void nrf_802154_energy_detected(const nrf_802154_energy_detected_t *p_result)
476494
{
477-
nrf5_data.energy_detection.value = p_result->ed_dbm;
495+
nrf5_data.energy_detection.value = zboss_normalize_ed_dbm(p_result->ed_dbm);
478496
k_sem_give(&nrf5_data.rssi_wait);
479497
}
480498

@@ -486,7 +504,7 @@ void nrf_802154_energy_detection_failed(nrf_802154_ed_error_t error)
486504

487505
if (err != 0) {
488506
LOG_ERR("Failed to restart energy detection after failure");
489-
nrf5_data.energy_detection.value = INT8_MAX;
507+
nrf5_data.energy_detection.value = UINT8_MAX;
490508
k_sem_give(&nrf5_data.rssi_wait);
491509
}
492510
}

0 commit comments

Comments
 (0)