Skip to content

Commit

Permalink
use sequencer to send FSK messages
Browse files Browse the repository at this point in the history
Sequencer can quickly start carrier, modulate data (tx) and stop carrier. This happens much faster than stopping manually in the chip using sx127x_set_opmod

make sure chip is reset before starting the test
  • Loading branch information
dernasherbrezon committed Jul 23, 2024
1 parent b0bfe13 commit 88995ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
11 changes: 9 additions & 2 deletions examples/transmit_fsk/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void tx_callback(sx127x *device) {
return;
}
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_FSK, device));
ESP_LOGI(TAG, "transmitting");
ESP_LOGI(TAG, "transmitting: %d", messages_sent);
messages_sent++;
}

Expand All @@ -87,6 +87,13 @@ void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type

void app_main() {
ESP_LOGI(TAG, "starting up");
ESP_ERROR_CHECK(gpio_set_direction((gpio_num_t) RST, GPIO_MODE_INPUT_OUTPUT));
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 0));
vTaskDelay(pdMS_TO_TICKS(5));
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 1));
vTaskDelay(pdMS_TO_TICKS(10));
ESP_LOGI(TAG, "sx127x was reset");
ESP_ERROR_CHECK(gpio_reset_pin((gpio_num_t) RST));

spi_bus_config_t config = {
.mosi_io_num = MOSI,
Expand All @@ -113,7 +120,7 @@ void app_main() {
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_STANDBY, SX127x_MODULATION_FSK, device));
ESP_ERROR_CHECK(sx127x_fsk_ook_set_bitrate(4800.0, device));
ESP_ERROR_CHECK(sx127x_fsk_set_fdev(5000.0, device));
ESP_ERROR_CHECK(sx127x_set_preamble_length(4, device)); // FIXME?
ESP_ERROR_CHECK(sx127x_set_preamble_length(4, device));
uint8_t syncWord[] = {0x12, 0xAD};
ESP_ERROR_CHECK(sx127x_fsk_ook_set_syncword(syncWord, 2, device));
ESP_ERROR_CHECK(sx127x_fsk_ook_set_address_filtering(SX127X_FILTER_NONE, 0, 0, device));
Expand Down
25 changes: 19 additions & 6 deletions src/sx127x.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,32 @@ void sx127x_fsk_ook_handle_interrupt(sx127x *device) {
return;
}
if ((irq & SX127X_FSK_IRQ_PACKET_SENT) != 0) {
// turn off TX mode as soon as possible to reduce power consumption
// FSK modem by default keep sending preamble after manual shutdown
// ignore status code
sx127x_set_opmod(SX127x_MODE_STANDBY, device->active_modem, device);
sx127x_fsk_ook_reset_state(device);
if (device->tx_callback != NULL) {
device->tx_callback(device);
}
sx127x_fsk_ook_reset_state(device);
return;
}
if (device->opmod == SX127x_MODE_TX) {
if ((irq & SX127X_FSK_IRQ_FIFO_EMPTY) != 0) {
// TX sequencer clears PACKET_SENT IRQ so only FIFO_EMPTY interrupt can be used to detect if message was actually sent
sx127x_fsk_ook_reset_state(device);
if (device->tx_callback != NULL) {
device->tx_callback(device);
}
return;
}
// FIFO_LEVEL == 0 - below level
if ((irq & SX127X_FSK_IRQ_FIFO_LEVEL) == 0 && (irq & SX127X_FSK_IRQ_FIFO_FULL) == 0) {
uint8_t to_send;
if (device->expected_packet_length - device->fsk_ook_packet_sent_received > (HALF_MAX_FIFO_THRESHOLD - 1)) {
to_send = HALF_MAX_FIFO_THRESHOLD - 1;
} else {
to_send = (uint8_t) (device->expected_packet_length - device->fsk_ook_packet_sent_received);
}
// safe check
// tx still sending the data
// ignore interrupt
// this can happen when exactly 63 bytes sent and FIFO_LEVEL is ~30bytes
if (to_send == 0) {
return;
}
Expand Down Expand Up @@ -558,6 +565,12 @@ int sx127x_set_opmod(sx127x_mode_t opmod, sx127x_modulation_t modulation, sx127x
// start tx as soon as first byte in FIFO available
uint8_t data = (TX_START_CONDITION_FIFO_EMPTY | HALF_MAX_FIFO_THRESHOLD);
ERROR_CHECK(sx127x_spi_write_register(REG_FIFO_THRESH, &data, 1, device->spi_device));
// use sequencer to send single packet and stop carrier
uint8_t value = 0b10010000;
ERROR_CHECK(sx127x_spi_write_register(REG_SEQ_CONFIG1, &value, 1, device->spi_device));
device->active_modem = modulation;
device->opmod = opmod;
return SX127X_OK;
}
} else {
return SX127X_ERR_INVALID_ARG;
Expand Down

0 comments on commit 88995ff

Please sign in to comment.