Skip to content

Commit 1385702

Browse files
add support for FHSS
optimize working with shadow registers
1 parent a64dfc5 commit 1385702

File tree

8 files changed

+352
-18
lines changed

8 files changed

+352
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
set(EXTRA_COMPONENT_DIRS "../../")
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
project(sx127x_receive_lora_fhss)
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
idf_component_register(SRCS "main.c" INCLUDE_DIRS "")
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <driver/gpio.h>
2+
#include <driver/spi_common.h>
3+
#include <driver/spi_master.h>
4+
#include <esp_intr_alloc.h>
5+
#include <esp_log.h>
6+
#include <freertos/task.h>
7+
#include <sx127x.h>
8+
#include <inttypes.h>
9+
10+
#define SCK 5
11+
#define MISO 19
12+
#define MOSI 27
13+
#define SS 18
14+
#define RST 23
15+
#define DIO0 26
16+
// older versions of TTGO require manual wiring of pins below
17+
#define DIO1 33
18+
#define DIO2 32
19+
20+
sx127x *device = NULL;
21+
TaskHandle_t handle_interrupt;
22+
int total_packets_received = 0;
23+
static const char *TAG = "sx127x";
24+
uint64_t frequencies[] = {437700000, 438200000, 437200012};
25+
26+
void IRAM_ATTR handle_interrupt_fromisr(void *arg) {
27+
xTaskResumeFromISR(handle_interrupt);
28+
}
29+
30+
void handle_interrupt_task(void *arg) {
31+
while (1) {
32+
vTaskSuspend(NULL);
33+
sx127x_handle_interrupt((sx127x *)arg);
34+
}
35+
}
36+
37+
void rx_callback(sx127x *device, uint8_t *data, uint16_t data_length) {
38+
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
39+
uint8_t payload[514];
40+
const char SYMBOLS[] = "0123456789ABCDEF";
41+
for (size_t i = 0; i < data_length; i++) {
42+
uint8_t cur = data[i];
43+
payload[2 * i] = SYMBOLS[cur >> 4];
44+
payload[2 * i + 1] = SYMBOLS[cur & 0x0F];
45+
}
46+
payload[data_length * 2] = '\0';
47+
48+
int16_t rssi;
49+
ESP_ERROR_CHECK(sx127x_rx_get_packet_rssi(device, &rssi));
50+
float snr;
51+
ESP_ERROR_CHECK(sx127x_lora_rx_get_packet_snr(device, &snr));
52+
int32_t frequency_error;
53+
ESP_ERROR_CHECK(sx127x_rx_get_frequency_error(device, &frequency_error));
54+
ESP_LOGI(TAG, "received: %d %s rssi: %d snr: %f freq_error: %" PRId32, data_length, payload, rssi, snr, frequency_error);
55+
total_packets_received++;
56+
}
57+
58+
void cad_callback(sx127x *device, int cad_detected) {
59+
if (cad_detected == 0) {
60+
ESP_LOGI(TAG, "cad not detected");
61+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_CAD, SX127x_MODULATION_LORA, device));
62+
return;
63+
}
64+
// put into RX mode first to handle interrupt as soon as possible
65+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_RX_CONT, SX127x_MODULATION_LORA, device));
66+
ESP_LOGI(TAG, "cad detected\n");
67+
}
68+
69+
void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type) {
70+
gpio_set_direction(gpio, GPIO_MODE_INPUT);
71+
gpio_pulldown_en(gpio);
72+
gpio_pullup_dis(gpio);
73+
gpio_set_intr_type(gpio, type);
74+
gpio_isr_handler_add(gpio, handle_interrupt_fromisr, (void *)device);
75+
}
76+
77+
void app_main() {
78+
ESP_LOGI(TAG, "starting up");
79+
ESP_ERROR_CHECK(gpio_set_direction((gpio_num_t) RST, GPIO_MODE_INPUT_OUTPUT));
80+
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 0));
81+
vTaskDelay(pdMS_TO_TICKS(5));
82+
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 1));
83+
vTaskDelay(pdMS_TO_TICKS(10));
84+
ESP_LOGI(TAG, "sx127x was reset");
85+
ESP_ERROR_CHECK(gpio_reset_pin((gpio_num_t) RST));
86+
87+
spi_bus_config_t config = {
88+
.mosi_io_num = MOSI,
89+
.miso_io_num = MISO,
90+
.sclk_io_num = SCK,
91+
.quadwp_io_num = -1,
92+
.quadhd_io_num = -1,
93+
.max_transfer_sz = 0,
94+
};
95+
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
96+
spi_device_interface_config_t dev_cfg = {
97+
.clock_speed_hz = 8E6,
98+
.spics_io_num = SS,
99+
.queue_size = 16,
100+
.command_bits = 0,
101+
.address_bits = 8,
102+
.dummy_bits = 0,
103+
.mode = 0};
104+
spi_device_handle_t spi_device;
105+
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device));
106+
ESP_ERROR_CHECK(sx127x_create(spi_device, &device));
107+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_LORA, device));
108+
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
109+
ESP_ERROR_CHECK(sx127x_lora_set_frequency_hopping(5, frequencies, sizeof(frequencies) / sizeof(uint64_t), device));
110+
ESP_ERROR_CHECK(sx127x_lora_reset_fifo(device));
111+
ESP_ERROR_CHECK(sx127x_rx_set_lna_boost_hf(true, device));
112+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_STANDBY, SX127x_MODULATION_LORA, device));
113+
ESP_ERROR_CHECK(sx127x_rx_set_lna_gain(SX127x_LNA_GAIN_G4, device));
114+
ESP_ERROR_CHECK(sx127x_lora_set_bandwidth(SX127x_BW_125000, device));
115+
ESP_ERROR_CHECK(sx127x_lora_set_implicit_header(NULL, device));
116+
ESP_ERROR_CHECK(sx127x_lora_set_modem_config_2(SX127x_SF_9, device));
117+
ESP_ERROR_CHECK(sx127x_lora_set_syncword(18, device));
118+
ESP_ERROR_CHECK(sx127x_set_preamble_length(8, device));
119+
sx127x_rx_set_callback(rx_callback, device);
120+
sx127x_lora_cad_set_callback(cad_callback, device);
121+
122+
BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, device, 2, &handle_interrupt, xPortGetCoreID());
123+
if (task_code != pdPASS) {
124+
ESP_LOGE(TAG, "can't create task %d", task_code);
125+
sx127x_destroy(device);
126+
return;
127+
}
128+
129+
gpio_install_isr_service(0);
130+
setup_gpio_interrupts((gpio_num_t)DIO0, device, GPIO_INTR_POSEDGE);
131+
setup_gpio_interrupts((gpio_num_t)DIO1, device, GPIO_INTR_POSEDGE);
132+
setup_gpio_interrupts((gpio_num_t)DIO2, device, GPIO_INTR_POSEDGE);
133+
134+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_RX_CONT, SX127x_MODULATION_LORA, device));
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
set(EXTRA_COMPONENT_DIRS "../../")
4+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
5+
project(sx127x_transmit_lora_fhss)
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
idf_component_register(SRCS "main.c" INCLUDE_DIRS "")
+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include <driver/gpio.h>
2+
#include <driver/spi_common.h>
3+
#include <driver/spi_master.h>
4+
#include <esp_intr_alloc.h>
5+
#include <esp_log.h>
6+
#include <freertos/task.h>
7+
#include <sx127x.h>
8+
9+
#define SCK 5
10+
#define MISO 19
11+
#define MOSI 27
12+
#define SS 18
13+
#define RST 23
14+
#define DIO0 26
15+
// older versions of TTGO require manual wiring of pins below
16+
#define DIO1 33
17+
#define DIO2 32
18+
19+
static const char *TAG = "sx127x";
20+
21+
sx127x *device = NULL;
22+
int messages_sent = 0;
23+
TaskHandle_t handle_interrupt;
24+
uint64_t frequencies[] = {437700000, 438200000, 437200012};
25+
26+
void IRAM_ATTR handle_interrupt_fromisr(void *arg) {
27+
xTaskResumeFromISR(handle_interrupt);
28+
}
29+
30+
void handle_interrupt_task(void *arg) {
31+
while (1) {
32+
vTaskSuspend(NULL);
33+
sx127x_handle_interrupt((sx127x *) arg);
34+
}
35+
}
36+
37+
void tx_callback(sx127x *device) {
38+
if (messages_sent > 0) {
39+
ESP_LOGI(TAG, "transmitted");
40+
}
41+
if (messages_sent == 0) {
42+
uint8_t data[] = {0xCA, 0xFE};
43+
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
44+
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device));
45+
} else if (messages_sent == 1) {
46+
// 200 bytes
47+
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
48+
0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
49+
0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
50+
0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
51+
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7};
52+
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
53+
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device));
54+
} else if (messages_sent == 2) {
55+
// 255 bytes
56+
uint8_t data[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
57+
0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b,
58+
0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
59+
0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
60+
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5,
61+
0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe};
62+
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
63+
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device));
64+
} else {
65+
return;
66+
}
67+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_LORA, device));
68+
ESP_LOGI(TAG, "transmitting");
69+
messages_sent++;
70+
}
71+
72+
void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type) {
73+
gpio_set_direction(gpio, GPIO_MODE_INPUT);
74+
gpio_pulldown_en(gpio);
75+
gpio_pullup_dis(gpio);
76+
gpio_set_intr_type(gpio, type);
77+
gpio_isr_handler_add(gpio, handle_interrupt_fromisr, (void *)device);
78+
}
79+
80+
void app_main() {
81+
ESP_LOGI(TAG, "starting up");
82+
ESP_ERROR_CHECK(gpio_set_direction((gpio_num_t) RST, GPIO_MODE_INPUT_OUTPUT));
83+
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 0));
84+
vTaskDelay(pdMS_TO_TICKS(5));
85+
ESP_ERROR_CHECK(gpio_set_level((gpio_num_t) RST, 1));
86+
vTaskDelay(pdMS_TO_TICKS(10));
87+
ESP_LOGI(TAG, "sx127x was reset");
88+
ESP_ERROR_CHECK(gpio_reset_pin((gpio_num_t) RST));
89+
spi_bus_config_t config = {
90+
.mosi_io_num = MOSI,
91+
.miso_io_num = MISO,
92+
.sclk_io_num = SCK,
93+
.quadwp_io_num = -1,
94+
.quadhd_io_num = -1,
95+
.max_transfer_sz = 0,
96+
};
97+
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
98+
spi_device_interface_config_t dev_cfg = {
99+
.clock_speed_hz = 8E6,
100+
.spics_io_num = SS,
101+
.queue_size = 16,
102+
.command_bits = 0,
103+
.address_bits = 8,
104+
.dummy_bits = 0,
105+
.mode = 0};
106+
spi_device_handle_t spi_device;
107+
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device));
108+
ESP_ERROR_CHECK(sx127x_create(spi_device, &device));
109+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_LORA, device));
110+
ESP_ERROR_CHECK(sx127x_lora_set_frequency_hopping(5, frequencies, sizeof(frequencies) / sizeof(uint64_t), device));
111+
ESP_ERROR_CHECK(sx127x_lora_reset_fifo(device));
112+
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_STANDBY, SX127x_MODULATION_LORA, device));
113+
ESP_ERROR_CHECK(sx127x_lora_set_bandwidth(SX127x_BW_125000, device));
114+
ESP_ERROR_CHECK(sx127x_lora_set_implicit_header(NULL, device));
115+
ESP_ERROR_CHECK(sx127x_lora_set_modem_config_2(SX127x_SF_9, device));
116+
ESP_ERROR_CHECK(sx127x_lora_set_syncword(18, device));
117+
ESP_ERROR_CHECK(sx127x_set_preamble_length(8, device));
118+
sx127x_tx_set_callback(tx_callback, device);
119+
120+
BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, device, 2, &handle_interrupt, xPortGetCoreID());
121+
if (task_code != pdPASS) {
122+
ESP_LOGE(TAG, "can't create task %d", task_code);
123+
sx127x_destroy(device);
124+
return;
125+
}
126+
127+
gpio_install_isr_service(0);
128+
setup_gpio_interrupts((gpio_num_t)DIO0, device, GPIO_INTR_POSEDGE);
129+
setup_gpio_interrupts((gpio_num_t)DIO1, device, GPIO_INTR_POSEDGE);
130+
setup_gpio_interrupts((gpio_num_t)DIO2, device, GPIO_INTR_POSEDGE);
131+
132+
ESP_ERROR_CHECK(sx127x_tx_set_pa_config(SX127x_PA_PIN_BOOST, 4, device));
133+
sx127x_tx_header_t header = {
134+
.enable_crc = true,
135+
.coding_rate = SX127x_CR_4_5};
136+
ESP_ERROR_CHECK(sx127x_lora_tx_set_explicit_header(&header, device));
137+
138+
tx_callback(device);
139+
}

include/sx127x.h

+14
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,20 @@ int sx127x_set_preamble_length(uint16_t value, sx127x *device);
458458
*/
459459
int sx127x_lora_set_implicit_header(sx127x_implicit_header_t *header, sx127x *device);
460460

461+
/**
462+
* @brief Configure frequency hopping for TX and RX. Frequency hopping spread spectrum (FHSS) is typically employed when the duration of a single packet could exceed
463+
regulatory requirements relating to the maximum permissible channel dwell time.
464+
*
465+
* @param period Symbol periods between frequency hops.
466+
* @param frequencies Set of predefined frequencies. Should be the same on RX and TX
467+
* @param frequencies_length Size of predefined frequencies
468+
* @param device Pointer to variable to hold the device handle
469+
* @return
470+
* - SX127X_ERR_INVALID_ARG if parameter is invalid
471+
* - SX127X_OK on success
472+
*/
473+
int sx127x_lora_set_frequency_hopping(uint8_t period, uint64_t *frequencies, uint8_t frequencies_length, sx127x *device);
474+
461475
/**
462476
* @brief Output internal registers
463477
*

0 commit comments

Comments
 (0)