-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
352 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
set(EXTRA_COMPONENT_DIRS "../../") | ||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(sx127x_receive_lora_fhss) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
idf_component_register(SRCS "main.c" INCLUDE_DIRS "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#include <driver/gpio.h> | ||
#include <driver/spi_common.h> | ||
#include <driver/spi_master.h> | ||
#include <esp_intr_alloc.h> | ||
#include <esp_log.h> | ||
#include <freertos/task.h> | ||
#include <sx127x.h> | ||
#include <inttypes.h> | ||
|
||
#define SCK 5 | ||
#define MISO 19 | ||
#define MOSI 27 | ||
#define SS 18 | ||
#define RST 23 | ||
#define DIO0 26 | ||
// older versions of TTGO require manual wiring of pins below | ||
#define DIO1 33 | ||
#define DIO2 32 | ||
|
||
sx127x *device = NULL; | ||
TaskHandle_t handle_interrupt; | ||
int total_packets_received = 0; | ||
static const char *TAG = "sx127x"; | ||
uint64_t frequencies[] = {437700000, 438200000, 437200012}; | ||
|
||
void IRAM_ATTR handle_interrupt_fromisr(void *arg) { | ||
xTaskResumeFromISR(handle_interrupt); | ||
} | ||
|
||
void handle_interrupt_task(void *arg) { | ||
while (1) { | ||
vTaskSuspend(NULL); | ||
sx127x_handle_interrupt((sx127x *)arg); | ||
} | ||
} | ||
|
||
void rx_callback(sx127x *device, uint8_t *data, uint16_t data_length) { | ||
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device)); | ||
uint8_t payload[514]; | ||
const char SYMBOLS[] = "0123456789ABCDEF"; | ||
for (size_t i = 0; i < data_length; i++) { | ||
uint8_t cur = data[i]; | ||
payload[2 * i] = SYMBOLS[cur >> 4]; | ||
payload[2 * i + 1] = SYMBOLS[cur & 0x0F]; | ||
} | ||
payload[data_length * 2] = '\0'; | ||
|
||
int16_t rssi; | ||
ESP_ERROR_CHECK(sx127x_rx_get_packet_rssi(device, &rssi)); | ||
float snr; | ||
ESP_ERROR_CHECK(sx127x_lora_rx_get_packet_snr(device, &snr)); | ||
int32_t frequency_error; | ||
ESP_ERROR_CHECK(sx127x_rx_get_frequency_error(device, &frequency_error)); | ||
ESP_LOGI(TAG, "received: %d %s rssi: %d snr: %f freq_error: %" PRId32, data_length, payload, rssi, snr, frequency_error); | ||
total_packets_received++; | ||
} | ||
|
||
void cad_callback(sx127x *device, int cad_detected) { | ||
if (cad_detected == 0) { | ||
ESP_LOGI(TAG, "cad not detected"); | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_CAD, SX127x_MODULATION_LORA, device)); | ||
return; | ||
} | ||
// put into RX mode first to handle interrupt as soon as possible | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_RX_CONT, SX127x_MODULATION_LORA, device)); | ||
ESP_LOGI(TAG, "cad detected\n"); | ||
} | ||
|
||
void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type) { | ||
gpio_set_direction(gpio, GPIO_MODE_INPUT); | ||
gpio_pulldown_en(gpio); | ||
gpio_pullup_dis(gpio); | ||
gpio_set_intr_type(gpio, type); | ||
gpio_isr_handler_add(gpio, handle_interrupt_fromisr, (void *)device); | ||
} | ||
|
||
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, | ||
.miso_io_num = MISO, | ||
.sclk_io_num = SCK, | ||
.quadwp_io_num = -1, | ||
.quadhd_io_num = -1, | ||
.max_transfer_sz = 0, | ||
}; | ||
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1)); | ||
spi_device_interface_config_t dev_cfg = { | ||
.clock_speed_hz = 8E6, | ||
.spics_io_num = SS, | ||
.queue_size = 16, | ||
.command_bits = 0, | ||
.address_bits = 8, | ||
.dummy_bits = 0, | ||
.mode = 0}; | ||
spi_device_handle_t spi_device; | ||
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device)); | ||
ESP_ERROR_CHECK(sx127x_create(spi_device, &device)); | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_LORA, device)); | ||
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_frequency_hopping(5, frequencies, sizeof(frequencies) / sizeof(uint64_t), device)); | ||
ESP_ERROR_CHECK(sx127x_lora_reset_fifo(device)); | ||
ESP_ERROR_CHECK(sx127x_rx_set_lna_boost_hf(true, device)); | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_STANDBY, SX127x_MODULATION_LORA, device)); | ||
ESP_ERROR_CHECK(sx127x_rx_set_lna_gain(SX127x_LNA_GAIN_G4, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_bandwidth(SX127x_BW_125000, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_implicit_header(NULL, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_modem_config_2(SX127x_SF_9, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_syncword(18, device)); | ||
ESP_ERROR_CHECK(sx127x_set_preamble_length(8, device)); | ||
sx127x_rx_set_callback(rx_callback, device); | ||
sx127x_lora_cad_set_callback(cad_callback, device); | ||
|
||
BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, device, 2, &handle_interrupt, xPortGetCoreID()); | ||
if (task_code != pdPASS) { | ||
ESP_LOGE(TAG, "can't create task %d", task_code); | ||
sx127x_destroy(device); | ||
return; | ||
} | ||
|
||
gpio_install_isr_service(0); | ||
setup_gpio_interrupts((gpio_num_t)DIO0, device, GPIO_INTR_POSEDGE); | ||
setup_gpio_interrupts((gpio_num_t)DIO1, device, GPIO_INTR_POSEDGE); | ||
setup_gpio_interrupts((gpio_num_t)DIO2, device, GPIO_INTR_POSEDGE); | ||
|
||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_RX_CONT, SX127x_MODULATION_LORA, device)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
set(EXTRA_COMPONENT_DIRS "../../") | ||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(sx127x_transmit_lora_fhss) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
idf_component_register(SRCS "main.c" INCLUDE_DIRS "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
#include <driver/gpio.h> | ||
#include <driver/spi_common.h> | ||
#include <driver/spi_master.h> | ||
#include <esp_intr_alloc.h> | ||
#include <esp_log.h> | ||
#include <freertos/task.h> | ||
#include <sx127x.h> | ||
|
||
#define SCK 5 | ||
#define MISO 19 | ||
#define MOSI 27 | ||
#define SS 18 | ||
#define RST 23 | ||
#define DIO0 26 | ||
// older versions of TTGO require manual wiring of pins below | ||
#define DIO1 33 | ||
#define DIO2 32 | ||
|
||
static const char *TAG = "sx127x"; | ||
|
||
sx127x *device = NULL; | ||
int messages_sent = 0; | ||
TaskHandle_t handle_interrupt; | ||
uint64_t frequencies[] = {437700000, 438200000, 437200012}; | ||
|
||
void IRAM_ATTR handle_interrupt_fromisr(void *arg) { | ||
xTaskResumeFromISR(handle_interrupt); | ||
} | ||
|
||
void handle_interrupt_task(void *arg) { | ||
while (1) { | ||
vTaskSuspend(NULL); | ||
sx127x_handle_interrupt((sx127x *) arg); | ||
} | ||
} | ||
|
||
void tx_callback(sx127x *device) { | ||
if (messages_sent > 0) { | ||
ESP_LOGI(TAG, "transmitted"); | ||
} | ||
if (messages_sent == 0) { | ||
uint8_t data[] = {0xCA, 0xFE}; | ||
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device)); | ||
} else if (messages_sent == 1) { | ||
// 200 bytes | ||
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, | ||
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, | ||
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, | ||
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, | ||
0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7}; | ||
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device)); | ||
} else if (messages_sent == 2) { | ||
// 255 bytes | ||
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, | ||
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, | ||
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, | ||
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, | ||
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, | ||
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}; | ||
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_tx_set_for_transmission(data, sizeof(data), device)); | ||
} else { | ||
return; | ||
} | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_TX, SX127x_MODULATION_LORA, device)); | ||
ESP_LOGI(TAG, "transmitting"); | ||
messages_sent++; | ||
} | ||
|
||
void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type) { | ||
gpio_set_direction(gpio, GPIO_MODE_INPUT); | ||
gpio_pulldown_en(gpio); | ||
gpio_pullup_dis(gpio); | ||
gpio_set_intr_type(gpio, type); | ||
gpio_isr_handler_add(gpio, handle_interrupt_fromisr, (void *)device); | ||
} | ||
|
||
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, | ||
.miso_io_num = MISO, | ||
.sclk_io_num = SCK, | ||
.quadwp_io_num = -1, | ||
.quadhd_io_num = -1, | ||
.max_transfer_sz = 0, | ||
}; | ||
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1)); | ||
spi_device_interface_config_t dev_cfg = { | ||
.clock_speed_hz = 8E6, | ||
.spics_io_num = SS, | ||
.queue_size = 16, | ||
.command_bits = 0, | ||
.address_bits = 8, | ||
.dummy_bits = 0, | ||
.mode = 0}; | ||
spi_device_handle_t spi_device; | ||
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device)); | ||
ESP_ERROR_CHECK(sx127x_create(spi_device, &device)); | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_SLEEP, SX127x_MODULATION_LORA, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_frequency_hopping(5, frequencies, sizeof(frequencies) / sizeof(uint64_t), device)); | ||
ESP_ERROR_CHECK(sx127x_lora_reset_fifo(device)); | ||
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_STANDBY, SX127x_MODULATION_LORA, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_bandwidth(SX127x_BW_125000, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_implicit_header(NULL, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_modem_config_2(SX127x_SF_9, device)); | ||
ESP_ERROR_CHECK(sx127x_lora_set_syncword(18, device)); | ||
ESP_ERROR_CHECK(sx127x_set_preamble_length(8, device)); | ||
sx127x_tx_set_callback(tx_callback, device); | ||
|
||
BaseType_t task_code = xTaskCreatePinnedToCore(handle_interrupt_task, "handle interrupt", 8196, device, 2, &handle_interrupt, xPortGetCoreID()); | ||
if (task_code != pdPASS) { | ||
ESP_LOGE(TAG, "can't create task %d", task_code); | ||
sx127x_destroy(device); | ||
return; | ||
} | ||
|
||
gpio_install_isr_service(0); | ||
setup_gpio_interrupts((gpio_num_t)DIO0, device, GPIO_INTR_POSEDGE); | ||
setup_gpio_interrupts((gpio_num_t)DIO1, device, GPIO_INTR_POSEDGE); | ||
setup_gpio_interrupts((gpio_num_t)DIO2, device, GPIO_INTR_POSEDGE); | ||
|
||
ESP_ERROR_CHECK(sx127x_tx_set_pa_config(SX127x_PA_PIN_BOOST, 4, device)); | ||
sx127x_tx_header_t header = { | ||
.enable_crc = true, | ||
.coding_rate = SX127x_CR_4_5}; | ||
ESP_ERROR_CHECK(sx127x_lora_tx_set_explicit_header(&header, device)); | ||
|
||
tx_callback(device); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.