Skip to content

Commit

Permalink
cleanup examples
Browse files Browse the repository at this point in the history
4Mhz for FSK/OOK SPI communication
8Mhz for LoRa SPI
Use SPI2_HOST explicitly as HSPI_HOST was deprecated for some ESP32 chips
  • Loading branch information
dernasherbrezon committed Jul 18, 2024
1 parent 82fac14 commit ce12cc4
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion debug_registers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ for (int i = 0; i < sizeof(registers); i++) {
if (i != 0) {
printf(",");
}
printf("%d", registers[i]);
printf("0x%02x", registers[i]);
}
printf("\n");
```
Expand Down
8 changes: 4 additions & 4 deletions examples/receive_fsk/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down Expand Up @@ -120,5 +120,5 @@ void app_main() {
ESP_ERROR_CHECK(sx127x_set_opmod(SX127x_MODE_RX_CONT, SX127x_MODULATION_FSK, device));
while (1) {
vTaskDelay(10000 / portTICK_PERIOD_MS);
}
}
}
6 changes: 3 additions & 3 deletions examples/receive_fsk_beacon/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_fsk_filtered/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_fsk_fixed/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
4 changes: 2 additions & 2 deletions examples/receive_fsk_raspberrypi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define GPIO_DIO3_PIN 26
#define GPIO_DIO0_PIN 27
#define GPIO_RESET_PIN 6
#define GPIO_POLL_TIMEOUT -1
#define GPIO_POLL_TIMEOUT (-1)

#define LINUX_ERROR_CHECK(x) \
do { \
Expand Down Expand Up @@ -177,7 +177,7 @@ int main() {
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word));
int lsb_setting = 0; // MSB
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_LSB_FIRST, &lsb_setting));
int max_speed = 500000;
int max_speed = 4000000;
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_MAX_SPEED_HZ, &max_speed));

sx127x *device = NULL;
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_lora/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.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(HSPI_HOST, &dev_cfg, &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));
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_lora_deepsleep/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.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(HSPI_HOST, &dev_cfg, &spi_device));
ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &dev_cfg, &spi_device));
ESP_ERROR_CHECK(sx127x_create(spi_device, &device));

esp_sleep_wakeup_cause_t cpu0WakeupReason = esp_sleep_get_wakeup_cause();
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_lora_implicit_header/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.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(HSPI_HOST, &dev_cfg, &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));
Expand Down
2 changes: 1 addition & 1 deletion examples/receive_lora_raspberrypi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ int main() {
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word));
int lsb_setting = 0; // MSB
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_LSB_FIRST, &lsb_setting));
int max_speed = 500000;
int max_speed = 8000000;
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_MAX_SPEED_HZ, &max_speed));

sx127x *device = NULL;
Expand Down
6 changes: 3 additions & 3 deletions examples/receive_ook/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_OOK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
7 changes: 4 additions & 3 deletions examples/transmit_fsk/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void setup_gpio_interrupts(gpio_num_t gpio, sx127x *device, gpio_int_type_t type

void app_main() {
ESP_LOGI(TAG, "starting up");

spi_bus_config_t config = {
.mosi_io_num = MOSI,
.miso_io_num = MISO,
Expand All @@ -95,17 +96,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
8 changes: 4 additions & 4 deletions examples/transmit_fsk_beacon/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand All @@ -61,7 +61,7 @@ void app_main() {
ESP_ERROR_CHECK(sx127x_tx_set_pa_config(SX127x_PA_PIN_BOOST, 4, device));
ESP_ERROR_CHECK(sx127x_fsk_ook_set_crc(SX127X_CRC_CCITT, device));

uint8_t payload = {0xCA, 0xFE, 0x01, 0x02, 0xBE, 0xEF};
uint8_t payload[] = {0xCA, 0xFE, 0x01, 0x02, 0xBE, 0xEF};
ESP_ERROR_CHECK(sx127x_fsk_ook_set_packet_format(SX127X_FIXED, sizeof(payload), device));
ESP_ERROR_CHECK(sx127x_fsk_ook_tx_start_beacon(payload, sizeof(payload), 1000, device));
vTaskDelay(10000 / portTICK_PERIOD_MS);
Expand Down
6 changes: 3 additions & 3 deletions examples/transmit_fsk_fixed/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_FSK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down
9 changes: 6 additions & 3 deletions examples/transmit_lora/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#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";

Expand Down Expand Up @@ -71,17 +74,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.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(HSPI_HOST, &dev_cfg, &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));
Expand Down
6 changes: 3 additions & 3 deletions examples/transmit_lora_implicit_header/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.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(HSPI_HOST, &dev_cfg, &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));
Expand Down
2 changes: 1 addition & 1 deletion examples/transmit_lora_raspberrypi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int main() {
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_BITS_PER_WORD, &bits_per_word));
int lsb_setting = 0; // MSB
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_LSB_FIRST, &lsb_setting));
int max_speed = 500000;
int max_speed = 8000000;
LINUX_ERROR_CHECK(ioctl(spi_device_fd, SPI_IOC_WR_MAX_SPEED_HZ, &max_speed));

sx127x *device = NULL;
Expand Down
6 changes: 3 additions & 3 deletions examples/transmit_ook/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ void app_main() {
.quadhd_io_num = -1,
.max_transfer_sz = 0,
};
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &config, 1));
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &config, 1));
spi_device_interface_config_t dev_cfg = {
.clock_speed_hz = 3E6,
.clock_speed_hz = 4E6,
.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(HSPI_HOST, &dev_cfg, &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_OOK, device));
ESP_ERROR_CHECK(sx127x_set_frequency(437200012, device));
Expand Down

0 comments on commit ce12cc4

Please sign in to comment.