|
|
|
@ -11,7 +11,8 @@ esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value) {
@@ -11,7 +11,8 @@ esp_err_t sx127x_write_register(sx127x_t *handle, uint8_t reg, uint8_t value) {
|
|
|
|
|
return sx127x_single_transfer(handle, reg | 0x80, value, NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slave, uint8_t *from_slave) { |
|
|
|
|
esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, |
|
|
|
|
uint8_t to_slave, uint8_t *from_slave) { |
|
|
|
|
spi_transaction_t trans; |
|
|
|
|
memset(&trans, 0, sizeof(trans)); |
|
|
|
|
trans.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA; |
|
|
|
@ -25,16 +26,19 @@ esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slav
@@ -25,16 +26,19 @@ esp_err_t sx127x_single_transfer(sx127x_t *handle, uint8_t addr, uint8_t to_slav
|
|
|
|
|
if (from_slave) { |
|
|
|
|
*from_slave = trans.rx_data[0]; |
|
|
|
|
} |
|
|
|
|
ESP_LOGV(SX127X_TAG, "sx127x_single_transfer(%#x, %#x, %#x)", addr, trans.tx_data[0], trans.rx_data[0]); |
|
|
|
|
ESP_LOGV(SX127X_TAG, "sx127x_single_transfer(%#x, %#x, %#x)", addr, |
|
|
|
|
trans.tx_data[0], trans.rx_data[0]); |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_sleep(sx127x_t *handle) { |
|
|
|
|
return sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_SLEEP); |
|
|
|
|
return sx127x_write_register(handle, REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_SLEEP); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_standby(sx127x_t *handle) { |
|
|
|
|
return sx127x_write_register(handle, REG_OP_MODE, SX127X_LONG_RANGE | SX127X_MODE_STDBY); |
|
|
|
|
return sx127x_write_register(handle, REG_OP_MODE, |
|
|
|
|
SX127X_LONG_RANGE | SX127X_MODE_STDBY); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) { |
|
|
|
@ -54,15 +58,19 @@ esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) {
@@ -54,15 +58,19 @@ esp_err_t sx127x_set_frequency(sx127x_t *handle, uint64_t frequency) {
|
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boost_t pa_boost) { |
|
|
|
|
esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, |
|
|
|
|
sx127x_pa_boost_t pa_boost) { |
|
|
|
|
esp_err_t ret; |
|
|
|
|
if (pa_boost == SX127X_PA_BOOST_ENABLED) { |
|
|
|
|
// PA BOOST
|
|
|
|
|
SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); |
|
|
|
|
ret = sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); |
|
|
|
|
SX127X_CHECK(tx_power >= 2 && tx_power <= 20, "invalid tx_power: %d", |
|
|
|
|
ESP_ERR_INVALID_ARG, tx_power); |
|
|
|
|
ret = |
|
|
|
|
sx127x_write_register(handle, REG_PA_CONFIG, PA_BOOST | (tx_power - 2)); |
|
|
|
|
} else { |
|
|
|
|
// RFO
|
|
|
|
|
SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, tx_power); |
|
|
|
|
SX127X_CHECK(tx_power <= 14, "invalid tx_power: %d", ESP_ERR_INVALID_ARG, |
|
|
|
|
tx_power); |
|
|
|
|
ret = sx127x_write_register(handle, REG_PA_CONFIG, 0x70 | tx_power); |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK(ret); |
|
|
|
@ -71,8 +79,10 @@ esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boos
@@ -71,8 +79,10 @@ esp_err_t sx127x_set_tx_power(sx127x_t *handle, uint8_t tx_power, sx127x_pa_boos
|
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor) { |
|
|
|
|
SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, "invalid spreading_factor", ESP_ERR_INVALID_ARG); |
|
|
|
|
esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, |
|
|
|
|
uint8_t spreading_factor) { |
|
|
|
|
SX127X_CHECK(spreading_factor >= 6 && spreading_factor <= 12, |
|
|
|
|
"invalid spreading_factor", ESP_ERR_INVALID_ARG); |
|
|
|
|
// section 4.1.1.2 in SX1276 datasheet
|
|
|
|
|
uint8_t detection_optimize, detection_threshold; |
|
|
|
|
if (spreading_factor == 6) { |
|
|
|
@ -82,24 +92,31 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor
@@ -82,24 +92,31 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *handle, uint8_t spreading_factor
|
|
|
|
|
detection_optimize = 0xc3; |
|
|
|
|
detection_threshold = 0x0a; |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, detection_optimize)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, detection_threshold)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_OPTIMIZE, |
|
|
|
|
detection_optimize)); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_DETECTION_THRESHOLD, |
|
|
|
|
detection_threshold)); |
|
|
|
|
|
|
|
|
|
uint8_t modem_config_3; |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
modem_config_3 = (modem_config_3 & 0x03) | ((spreading_factor << 4) & 0xf0); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
|
|
|
|
|
handle->config.spreading_factor = spreading_factor; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidth) { |
|
|
|
|
esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, |
|
|
|
|
uint64_t signal_bandwidth) { |
|
|
|
|
uint8_t bw_reg = sx127x_bw_to_reg(signal_bandwidth); |
|
|
|
|
uint8_t modem_config_1; |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(handle, REG_MODEM_CONFIG_1, &modem_config_1)); |
|
|
|
|
modem_config_1 = (modem_config_1 & 0x0f) | (bw_reg << 4); |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(handle, REG_MODEM_CONFIG_1, modem_config_1)); |
|
|
|
|
handle->config.signal_bandwidth = signal_bandwidth; |
|
|
|
|
|
|
|
|
|
// set low data rate optimization flag
|
|
|
|
@ -113,13 +130,15 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidt
@@ -113,13 +130,15 @@ esp_err_t sx127x_set_signal_bandwidth(sx127x_t *handle, uint64_t signal_bandwidt
|
|
|
|
|
bool ldo = must_have_ldo || (handle->config.ldo == SX127X_LDO_ENABLED); |
|
|
|
|
|
|
|
|
|
uint8_t modem_config_3; |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_3, &modem_config_3)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(handle, 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(handle, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(handle, REG_MODEM_CONFIG_3, modem_config_3)); |
|
|
|
|
|
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
@ -172,13 +191,15 @@ esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word) {
@@ -172,13 +191,15 @@ esp_err_t sx127x_set_sync_word(sx127x_t *handle, uint8_t sync_word) {
|
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_set_crc(sx127x_t *handle, sx127x_crc_t crc) { |
|
|
|
|
uint8_t modem_config_2; |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_read_register(handle, REG_MODEM_CONFIG_2, &modem_config_2)); |
|
|
|
|
if (crc == SX127X_CRC_ENABLED) { |
|
|
|
|
modem_config_2 |= CONFIG2_CRC; |
|
|
|
|
} else { |
|
|
|
|
modem_config_2 &= ~CONFIG2_CRC; |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2)); |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(handle, REG_MODEM_CONFIG_2, modem_config_2)); |
|
|
|
|
handle->config.crc = crc; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|