diff --git a/components/sx127x_driver/sx127x_driver.c b/components/sx127x_driver/sx127x_driver.c index 4b94758..2f5fe08 100644 --- a/components/sx127x_driver/sx127x_driver.c +++ b/components/sx127x_driver/sx127x_driver.c @@ -12,7 +12,56 @@ typedef struct sx127x_packet { size_t data_len; } sx127x_packet_t; -esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t **handle_ptr) { +sx127x_config_t sx127x_config_default() { + sx127x_config_t config = SX127X_CONFIG_DEFAULT; + return config; +} + +static esp_err_t sx127x_write_config(sx127x_t *handle) { + esp_err_t ret; + const sx127x_config_t *config = &handle->config; + + ret = sx127x_sleep(handle); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_set_frequency(handle, config->frequency); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_write_register(handle, REG_FIFO_TX_BASE_ADDR, 0); + SX127X_ERROR_CHECK(ret); + sx127x_write_register(handle, REG_FIFO_RX_BASE_ADDR, 0); + SX127X_ERROR_CHECK(ret); + + uint8_t reg_lna; + ret = sx127x_read_register(handle, REG_LNA, ®_lna); + SX127X_ERROR_CHECK(ret); + reg_lna |= 0x03; // set LNA boost + ret = sx127x_write_register(handle, REG_LNA, reg_lna); + SX127X_ERROR_CHECK(ret); + + // set auto AGC + ret = sx127x_write_register(handle, REG_MODEM_CONFIG_3, CONFIG3_AUTO_AGC); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_set_tx_power(handle, config->tx_power, true); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_set_spreading_factor(handle, config->spreading_factor); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_set_sync_word(handle, config->sync_word); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_set_crc(handle, config->crc); + SX127X_ERROR_CHECK(ret); + + ret = sx127x_standby(handle); + ESP_ERROR_CHECK(ret); + + return ESP_OK; +} + +esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr) { esp_err_t ret; sx127x_t *handle = malloc(sizeof(sx127x_t)); SX127X_CHECK(handle != NULL, "malloc error", ESP_ERR_NO_MEM); @@ -67,43 +116,9 @@ esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t **handle_ptr) { SX127X_ERROR_CHECK2(ret, sx127x_read_register); SX127X_CHECK(version == 0x12, "unsupported version %#x", ESP_ERR_INVALID_VERSION, version); - ret = sx127x_sleep(handle); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_set_frequency(handle, config->frequency); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_write_register(handle, REG_FIFO_TX_BASE_ADDR, 0); - SX127X_ERROR_CHECK(ret); - sx127x_write_register(handle, REG_FIFO_RX_BASE_ADDR, 0); - SX127X_ERROR_CHECK(ret); - - uint8_t reg_lna; - ret = sx127x_read_register(handle, REG_LNA, ®_lna); - SX127X_ERROR_CHECK(ret); - reg_lna |= 0x03; // set LNA boost - ret = sx127x_write_register(handle, REG_LNA, reg_lna); + ret = sx127x_write_config(handle); SX127X_ERROR_CHECK(ret); - // set auto AGC - ret = sx127x_write_register(handle, REG_MODEM_CONFIG_3, CONFIG3_AUTO_AGC); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_set_tx_power(handle, config->tx_power, true); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_set_spreading_factor(handle, config->spreading_factor); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_set_sync_word(handle, config->sync_word); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_set_crc(handle, config->crc); - SX127X_ERROR_CHECK(ret); - - ret = sx127x_standby(handle); - ESP_ERROR_CHECK(ret); - *handle_ptr = handle; return ESP_OK; } @@ -280,7 +295,8 @@ esp_err_t sx127x_stop(sx127x_t *handle) { return ESP_OK; } -esp_err_t sx127x_send_packet(sx127x_t *handle, char *data, size_t data_len, TickType_t ticks_to_wait) { +esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, size_t data_len, TickType_t ticks_to_wait) { + SX127X_CHECK(handle->task_state == SX127X_TASK_RUNNING, "task not running", ESP_FAIL); SX127X_CHECK(data_len < SX127_MAX_PACKET_LEN, "packet len too long: %d", ESP_FAIL, data_len); sx127x_packet_t packet; packet.data_len = data_len; diff --git a/components/sx127x_driver/sx127x_driver.h b/components/sx127x_driver/sx127x_driver.h index 27a3142..b8f8fd7 100644 --- a/components/sx127x_driver/sx127x_driver.h +++ b/components/sx127x_driver/sx127x_driver.h @@ -72,7 +72,9 @@ typedef struct sx127x_config { typedef struct sx127x sx127x_t; -esp_err_t sx127x_init(sx127x_config_t *config, sx127x_t **handle_ptr); +sx127x_config_t sx127x_config_default(); + +esp_err_t sx127x_init(const sx127x_config_t *config, sx127x_t **handle_ptr); esp_err_t sx127x_free(sx127x_t *handle); @@ -80,4 +82,4 @@ esp_err_t sx127x_start(sx127x_t *handle); esp_err_t sx127x_stop(sx127x_t *handle); -esp_err_t sx127x_send_packet(sx127x_t *handle, char *data, size_t data_len, TickType_t ticks_to_wait); +esp_err_t sx127x_send_packet(sx127x_t *handle, const char *data, size_t data_len, TickType_t ticks_to_wait);