|
|
|
@ -3,15 +3,17 @@
@@ -3,15 +3,17 @@
|
|
|
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_read_register(sx127x_t *hndl, uint8_t reg, uint8_t *value) { |
|
|
|
|
esp_err_t sx127x_read_register(sx127x_t *hndl, sx127x_reg_t reg, |
|
|
|
|
uint8_t *value) { |
|
|
|
|
return sx127x_single_transfer(hndl, reg & 0x7f, 0x00, value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_write_register(sx127x_t *hndl, uint8_t reg, uint8_t value) { |
|
|
|
|
esp_err_t sx127x_write_register(sx127x_t *hndl, sx127x_reg_t reg, |
|
|
|
|
uint8_t value) { |
|
|
|
|
return sx127x_single_transfer(hndl, reg | 0x80, value, NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_single_transfer(sx127x_t *hndl, uint8_t addr, |
|
|
|
|
esp_err_t sx127x_single_transfer(sx127x_t *hndl, sx127x_reg_t addr, |
|
|
|
|
uint8_t to_slave, uint8_t *from_slave) { |
|
|
|
|
spi_transaction_t trans; |
|
|
|
|
memset(&trans, 0, sizeof(trans)); |
|
|
|
@ -32,12 +34,12 @@ esp_err_t sx127x_single_transfer(sx127x_t *hndl, uint8_t addr,
@@ -32,12 +34,12 @@ esp_err_t sx127x_single_transfer(sx127x_t *hndl, uint8_t addr,
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_sleep(sx127x_t *hndl) { |
|
|
|
|
return sx127x_write_register(hndl, REG_OP_MODE, |
|
|
|
|
return sx127x_write_register(hndl, SX127X_REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_SLEEP); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_standby(sx127x_t *hndl) { |
|
|
|
|
return sx127x_write_register(hndl, REG_OP_MODE, |
|
|
|
|
return sx127x_write_register(hndl, SX127X_REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_STDBY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -45,13 +47,13 @@ esp_err_t sx127x_set_frequency(sx127x_t *hndl, uint64_t frequency) {
@@ -45,13 +47,13 @@ esp_err_t sx127x_set_frequency(sx127x_t *hndl, uint64_t frequency) {
|
|
|
|
|
uint64_t frf = ((uint64_t)frequency << 19) / 32000000; |
|
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
ret = sx127x_write_register(hndl, REG_FRF_MSB, (uint8_t)(frf)); |
|
|
|
|
ret = sx127x_write_register(hndl, SX127X_REG_FRF_MSB, (uint8_t)(frf)); |
|
|
|
|
SX127X_ERROR_CHECK(ret); |
|
|
|
|
frf >>= 8; |
|
|
|
|
ret = sx127x_write_register(hndl, REG_FRF_MID, (uint8_t)(frf)); |
|
|
|
|
ret = sx127x_write_register(hndl, SX127X_REG_FRF_MID, (uint8_t)(frf)); |
|
|
|
|
SX127X_ERROR_CHECK(ret); |
|
|
|
|
frf >>= 8; |
|
|
|
|
ret = sx127x_write_register(hndl, REG_FRF_LSB, (uint8_t)(frf)); |
|
|
|
|
ret = sx127x_write_register(hndl, SX127X_REG_FRF_LSB, (uint8_t)(frf)); |
|
|
|
|
SX127X_ERROR_CHECK(ret); |
|
|
|
|
|
|
|
|
|
hndl->config.frequency = frequency; |
|
|
|
@ -65,13 +67,12 @@ esp_err_t sx127x_set_tx_power(sx127x_t *hndl, uint8_t tx_power,
@@ -65,13 +67,12 @@ esp_err_t sx127x_set_tx_power(sx127x_t *hndl, uint8_t tx_power,
|
|
|
|
|
// PA BOOST
|
|
|
|
|
SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", |
|
|
|
|
ESP_ERR_INVALID_ARG, tx_power); |
|
|
|
|
ret = |
|
|
|
|
sx127x_write_register(hndl, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); |
|
|
|
|
ret = sx127x_write_register(hndl, SX127X_REG_PA_CONFIG, SX127X_PA_BOOST | (tx_power - 2)); |
|
|
|
|
} else { |
|
|
|
|
// RFO
|
|
|
|
|
SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, |
|
|
|
|
tx_power); |
|
|
|
|
ret = sx127x_write_register(hndl, REG_PA_CONFIG, 0x70 | tx_power); |
|
|
|
|
ret = sx127x_write_register(hndl, SX127X_REG_PA_CONFIG, 0x70 | tx_power); |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK(ret); |
|
|
|
|
|
|
|
|
@ -92,17 +93,17 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *hndl,
@@ -92,17 +93,17 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *hndl,
|
|
|
|
|
detection_optimize = 0xc3; |
|
|
|
|
detection_threshold = 0x0a; |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_DETECTION_OPTIMIZE, |
|
|
|
|
detection_optimize)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_DETECTION_THRESHOLD, |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_DETECTION_OPTIMIZE, detection_optimize)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(hndl, SX127X_REG_DETECTION_THRESHOLD, |
|
|
|
|
detection_threshold)); |
|
|
|
|
|
|
|
|
|
uint8_t modem_config_3; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
sx127x_read_register(hndl, SX127X_REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
modem_config_3 = (modem_config_3 & 0x03) | ((spreading_factor << 4) & 0xf0); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
|
|
|
|
|
hndl->config.spreading_factor = spreading_factor; |
|
|
|
|
return ESP_OK; |
|
|
|
@ -113,10 +114,10 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *hndl,
@@ -113,10 +114,10 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *hndl,
|
|
|
|
|
uint8_t bw_reg = sx127x_bw_to_reg(signal_bandwidth); |
|
|
|
|
uint8_t modem_config_1; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_MODEM_CONFIG_1, &modem_config_1)); |
|
|
|
|
sx127x_read_register(hndl, SX127X_REG_MODEM_CONFIG_1, &modem_config_1)); |
|
|
|
|
modem_config_1 = (modem_config_1 & 0x0f) | (bw_reg << 4); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, REG_MODEM_CONFIG_1, modem_config_1)); |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_MODEM_CONFIG_1, modem_config_1)); |
|
|
|
|
hndl->config.signal_bandwidth = signal_bandwidth; |
|
|
|
|
|
|
|
|
|
// set low data rate optimization flag
|
|
|
|
@ -131,14 +132,14 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *hndl,
@@ -131,14 +132,14 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *hndl,
|
|
|
|
|
|
|
|
|
|
uint8_t modem_config_3; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
sx127x_read_register(hndl, SX127X_REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
if (ldo) { |
|
|
|
|
modem_config_3 |= (1 << 3); |
|
|
|
|
} else { |
|
|
|
|
modem_config_3 &= ~(1 << 3); |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
|
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
@ -184,7 +185,7 @@ uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg) {
@@ -184,7 +185,7 @@ uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_sync_word(sx127x_t *hndl, uint8_t sync_word) { |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(hndl, REG_SYNC_WORD, sync_word)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(hndl, SX127X_REG_SYNC_WORD, sync_word)); |
|
|
|
|
hndl->config.sync_word = sync_word; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
@ -192,14 +193,14 @@ esp_err_t sx127x_set_sync_word(sx127x_t *hndl, uint8_t sync_word) {
@@ -192,14 +193,14 @@ esp_err_t sx127x_set_sync_word(sx127x_t *hndl, uint8_t sync_word) {
|
|
|
|
|
esp_err_t sx127x_set_crc(sx127x_t *hndl, sx127x_crc_t crc) { |
|
|
|
|
uint8_t modem_config_2; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_MODEM_CONFIG_2, &modem_config_2)); |
|
|
|
|
sx127x_read_register(hndl, SX127X_REG_MODEM_CONFIG_2, &modem_config_2)); |
|
|
|
|
if (crc == SX127X_CRC_ENABLED) { |
|
|
|
|
modem_config_2 |= CONFIG2_CRC; |
|
|
|
|
modem_config_2 |= SX127X_CONFIG2_CRC; |
|
|
|
|
} else { |
|
|
|
|
modem_config_2 &= ~CONFIG2_CRC; |
|
|
|
|
modem_config_2 &= ~SX127X_CONFIG2_CRC; |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, REG_MODEM_CONFIG_2, modem_config_2)); |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_MODEM_CONFIG_2, modem_config_2)); |
|
|
|
|
hndl->config.crc = crc; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
@ -209,8 +210,7 @@ esp_err_t sx127x_read_pkt_rssi(sx127x_t *hndl, int32_t *rssi) {
@@ -209,8 +210,7 @@ esp_err_t sx127x_read_pkt_rssi(sx127x_t *hndl, int32_t *rssi) {
|
|
|
|
|
uint8_t rssi_val; |
|
|
|
|
uint64_t freq = hndl->config.frequency; |
|
|
|
|
int32_t min_rssi = (freq < 868E6) ? -164 : -157; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_PKT_RSSI_VALUE, &rssi_val)) |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(hndl, SX127X_REG_PKT_RSSI_VALUE, &rssi_val)) |
|
|
|
|
*rssi = min_rssi + rssi_val; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
@ -219,17 +219,16 @@ esp_err_t sx127x_read_pkt_snr(sx127x_t *hndl, int8_t *snr) {
@@ -219,17 +219,16 @@ esp_err_t sx127x_read_pkt_snr(sx127x_t *hndl, int8_t *snr) {
|
|
|
|
|
SX127X_CHECK(snr != NULL, "rssi can not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
|
int8_t snr_val; |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(hndl, REG_PKT_SNR_VALUE, (uint8_t *)&snr_val)) |
|
|
|
|
sx127x_read_register(hndl, SX127X_REG_PKT_SNR_VALUE, (uint8_t *)&snr_val)) |
|
|
|
|
*snr = snr_val; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_write_fifo(sx127x_t *hndl, const char *data, |
|
|
|
|
size_t data_len) { |
|
|
|
|
esp_err_t sx127x_write_fifo(sx127x_t *hndl, const char *data, size_t data_len) { |
|
|
|
|
spi_transaction_t trans; |
|
|
|
|
memset(&trans, 0, sizeof(trans)); |
|
|
|
|
trans.flags = 0; |
|
|
|
|
trans.addr = REG_FIFO; |
|
|
|
|
trans.addr = SX127X_REG_FIFO; |
|
|
|
|
trans.length = 8 * data_len; |
|
|
|
|
trans.tx_buffer = data; |
|
|
|
|
trans.rxlength = 0; |
|
|
|
@ -245,7 +244,7 @@ esp_err_t sx127x_read_fifo(sx127x_t *hndl, char *data_out, size_t data_len) {
@@ -245,7 +244,7 @@ esp_err_t sx127x_read_fifo(sx127x_t *hndl, char *data_out, size_t data_len) {
|
|
|
|
|
spi_transaction_t trans; |
|
|
|
|
memset(&trans, 0, sizeof(trans)); |
|
|
|
|
trans.flags = 0; |
|
|
|
|
trans.addr = REG_FIFO; |
|
|
|
|
trans.addr = SX127X_REG_FIFO; |
|
|
|
|
trans.length = 8 * data_len; |
|
|
|
|
trans.tx_buffer = NULL; |
|
|
|
|
trans.rxlength = 0; // match length
|
|
|
|
|