diff --git a/drivers/include/sx127x.h b/drivers/include/sx127x.h index 3ebd2f641e..9d200d0a64 100644 --- a/drivers/include/sx127x.h +++ b/drivers/include/sx127x.h @@ -281,6 +281,26 @@ uint32_t sx127x_random(sx127x_t *dev); */ void sx127x_start_cad(sx127x_t *dev); +/** + * @brief Checks that channel is free with specified RSSI threshold. + * + * @param[in] dev The sx127x device structure pointer + * @param[in] freq channel RF frequency + * @param[in] rssi_threshold RSSI threshold + * + * @return true if channel is free, false otherwise + */ +bool sx127x_is_channel_free(sx127x_t *dev, uint32_t freq, int16_t rssi_threshold); + +/** + * @brief Reads the current RSSI value. + * + * @param[in] dev The sx127x device structure pointer + * + * @return the current value of RSSI (in dBm) + */ +int16_t sx127x_read_rssi(const sx127x_t *dev); + /** * @brief Gets current state of transceiver. * diff --git a/drivers/sx127x/sx127x_getset.c b/drivers/sx127x/sx127x_getset.c index aad2ee2f97..6f74f9d23b 100644 --- a/drivers/sx127x/sx127x_getset.c +++ b/drivers/sx127x/sx127x_getset.c @@ -109,7 +109,7 @@ uint8_t sx127x_get_syncword(const sx127x_t *dev) void sx127x_set_syncword(sx127x_t *dev, uint8_t syncword) { - DEBUG("[DEBUG] Set syncword: %d\n", syncword); + DEBUG("[DEBUG] Set syncword: %02x\n", syncword); sx127x_reg_write(dev, SX127X_REG_LR_SYNCWORD, syncword); } diff --git a/drivers/sx127x/sx127x_internal.c b/drivers/sx127x/sx127x_internal.c index d8be969727..5889e41d0c 100644 --- a/drivers/sx127x/sx127x_internal.c +++ b/drivers/sx127x/sx127x_internal.c @@ -224,3 +224,18 @@ void sx127x_start_cad(sx127x_t *dev) break; } } + +bool sx127x_is_channel_free(sx127x_t *dev, uint32_t freq, int16_t rssi_threshold) +{ + int16_t rssi = 0; + + sx127x_set_channel(dev, freq); + sx127x_set_op_mode(dev, SX127X_RF_OPMODE_RECEIVER); + + xtimer_usleep(1000); /* wait 1 millisecond */ + + rssi = sx127x_read_rssi(dev); + sx127x_set_sleep(dev); + + return (rssi <= rssi_threshold); +}