|
|
|
@ -23,7 +23,13 @@ esp_err_t sx127x_single_transfer(sx127x_t *hndl, sx127x_reg_t addr,
@@ -23,7 +23,13 @@ esp_err_t sx127x_single_transfer(sx127x_t *hndl, sx127x_reg_t addr,
|
|
|
|
|
trans.rxlength = 8; |
|
|
|
|
trans.tx_data[0] = to_slave; |
|
|
|
|
|
|
|
|
|
BaseType_t pdRet = xSemaphoreTake(hndl->spi_mutex, SX127X_MUTEX_TIMOUT); |
|
|
|
|
if (pdRet != pdTRUE) { |
|
|
|
|
ESP_LOGE(SX127X_TAG, "timeout on spi_mutex"); |
|
|
|
|
return ESP_ERR_TIMEOUT; |
|
|
|
|
} |
|
|
|
|
esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); |
|
|
|
|
xSemaphoreGive(hndl->spi_mutex); |
|
|
|
|
SX127X_ERROR_CHECK2(ret, spi_device_transmit); |
|
|
|
|
if (from_slave) { |
|
|
|
|
*from_slave = trans.rx_data[0]; |
|
|
|
@ -67,7 +73,8 @@ esp_err_t sx127x_set_tx_power(sx127x_t *hndl, uint8_t tx_power,
@@ -67,7 +73,8 @@ 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, SX127X_REG_PA_CONFIG, SX127X_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, |
|
|
|
@ -93,8 +100,8 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *hndl,
@@ -93,8 +100,8 @@ esp_err_t sx127x_set_spreading_factor(sx127x_t *hndl,
|
|
|
|
|
detection_optimize = 0xc3; |
|
|
|
|
detection_threshold = 0x0a; |
|
|
|
|
} |
|
|
|
|
SX127X_ERROR_CHECK( |
|
|
|
|
sx127x_write_register(hndl, SX127X_REG_DETECTION_OPTIMIZE, detection_optimize)); |
|
|
|
|
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)); |
|
|
|
|
|
|
|
|
@ -185,7 +192,8 @@ uint64_t sx127x_reg_to_bw(uint8_t bandwidth_reg) {
@@ -185,7 +192,8 @@ 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, SX127X_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; |
|
|
|
|
} |
|
|
|
@ -210,7 +218,8 @@ esp_err_t sx127x_read_pkt_rssi(sx127x_t *hndl, int32_t *rssi) {
@@ -210,7 +218,8 @@ 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, SX127X_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; |
|
|
|
|
} |
|
|
|
@ -224,6 +233,25 @@ esp_err_t sx127x_read_pkt_snr(sx127x_t *hndl, int8_t *snr) {
@@ -224,6 +233,25 @@ esp_err_t sx127x_read_pkt_snr(sx127x_t *hndl, int8_t *snr) {
|
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_read_rssi(sx127x_t *hndl, int32_t *rssi) { |
|
|
|
|
SX127X_CHECK(rssi != NULL, "rssi can not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
|
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, SX127X_REG_RSSI_VALUE, &rssi_val)) |
|
|
|
|
*rssi = min_rssi + rssi_val; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
esp_err_t sx127x_read_lna_gain(sx127x_t *hndl, uint8_t *lna_gain) { |
|
|
|
|
SX127X_CHECK(lna_gain != NULL, "rssi can not be NULL", ESP_ERR_INVALID_ARG); |
|
|
|
|
uint8_t lna_val; |
|
|
|
|
SX127X_ERROR_CHECK(sx127x_read_register(hndl, SX127X_REG_LNA, &lna_val)) |
|
|
|
|
*lna_gain = (lna_val >> 4) & 0x07; |
|
|
|
|
return ESP_OK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
@ -234,7 +262,13 @@ esp_err_t sx127x_write_fifo(sx127x_t *hndl, const char *data, size_t data_len) {
@@ -234,7 +262,13 @@ esp_err_t sx127x_write_fifo(sx127x_t *hndl, const char *data, size_t data_len) {
|
|
|
|
|
trans.rxlength = 0; |
|
|
|
|
trans.rx_buffer = NULL; |
|
|
|
|
|
|
|
|
|
BaseType_t pdRet = xSemaphoreTake(hndl->spi_mutex, SX127X_MUTEX_TIMOUT); |
|
|
|
|
if (pdRet != pdTRUE) { |
|
|
|
|
ESP_LOGE(SX127X_TAG, "timeout on spi_mutex"); |
|
|
|
|
return ESP_ERR_TIMEOUT; |
|
|
|
|
} |
|
|
|
|
esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); |
|
|
|
|
xSemaphoreGive(hndl->spi_mutex); |
|
|
|
|
SX127X_ERROR_CHECK2(ret, spi_device_transmit); |
|
|
|
|
ESP_LOGV(SX127X_TAG, "sx127x_write_fifo(%.*s)", data_len, data); |
|
|
|
|
return ESP_OK; |
|
|
|
@ -250,7 +284,13 @@ esp_err_t sx127x_read_fifo(sx127x_t *hndl, char *data_out, size_t data_len) {
@@ -250,7 +284,13 @@ esp_err_t sx127x_read_fifo(sx127x_t *hndl, char *data_out, size_t data_len) {
|
|
|
|
|
trans.rxlength = 0; // match length
|
|
|
|
|
trans.rx_buffer = data_out; |
|
|
|
|
|
|
|
|
|
BaseType_t pdRet = xSemaphoreTake(hndl->spi_mutex, SX127X_MUTEX_TIMOUT); |
|
|
|
|
if (pdRet != pdTRUE) { |
|
|
|
|
ESP_LOGE(SX127X_TAG, "timeout on spi_mutex"); |
|
|
|
|
return ESP_ERR_TIMEOUT; |
|
|
|
|
} |
|
|
|
|
esp_err_t ret = spi_device_transmit(hndl->device_handle, &trans); |
|
|
|
|
xSemaphoreGive(hndl->spi_mutex); |
|
|
|
|
SX127X_ERROR_CHECK2(ret, spi_device_transmit); |
|
|
|
|
ESP_LOGV(SX127X_TAG, "sx127x_read_fifo(%.*s)", data_len, data_out); |
|
|
|
|
return ESP_OK; |
|
|
|
|