From ba908c0b9319c3a12de98f806dd7007369c777cb Mon Sep 17 00:00:00 2001 From: Manuel Bleichenbacher Date: Mon, 26 Jul 2021 22:29:20 +0200 Subject: [PATCH] Configue sub-band --- include/TheThingsNetwork.h | 14 ++++++++++++++ include/ttn.h | 14 ++++++++++++++ src/ttn.c | 11 +++++++++++ 3 files changed, 39 insertions(+) diff --git a/include/TheThingsNetwork.h b/include/TheThingsNetwork.h index 64bdd22..1fb0038 100644 --- a/include/TheThingsNetwork.h +++ b/include/TheThingsNetwork.h @@ -193,6 +193,20 @@ public: ttn_configure_pins(spi_host, nss, rxtx, rst, dio0, dio1); } + /** + * @brief Sets the frequency sub-band to be used. + * + * For regions with sub-bands (USA, Australia), sets the sub-band to be used for uplink communication. + * For other regions, this function has no effect. + * + * The sub-band must be set before joining or sending the first message. + * + * If not set, it defaults to sub-band 2 as defined by TTN. + * + * @param band band (between 1 and 8) + */ + void setSubband(int band) { ttn_set_subband(band); } + /** * @brief Sets the credentials needed to activate the device via OTAA, without activating it. * diff --git a/include/ttn.h b/include/ttn.h index d8b189d..b419ffa 100644 --- a/include/ttn.h +++ b/include/ttn.h @@ -188,6 +188,20 @@ void ttn_reset(void); */ void ttn_configure_pins(spi_host_device_t spi_host, uint8_t nss, uint8_t rxtx, uint8_t rst, uint8_t dio0, uint8_t dio1); +/** + * @brief Sets the frequency sub-band to be used. + * + * For regions with sub-bands (USA, Australia), sets the sub-band to be used for uplink communication. + * For other regions, this function has no effect. + * + * The sub-band must be set before joining or sending the first message. + * + * If not set, it defaults to sub-band 2 as defined by TTN. + * + * @param band band (between 1 and 8) + */ +void ttn_set_subband(int band); + /** * @brief Sets the credentials needed to activate the device via OTAA, without activating it. * diff --git a/src/ttn.c b/src/ttn.c index f78463a..4c017d2 100644 --- a/src/ttn.c +++ b/src/ttn.c @@ -60,6 +60,7 @@ static ttn_message_cb message_callback; static ttn_waiting_reason_t waiting_reason = TTN_WAITING_NONE; static ttn_rf_settings_t last_rf_settings[4]; static ttn_rx_tx_window_t current_rx_tx_window; +static int subband = 1; static bool join_core(void); static void event_callback(void* user_data, ev_t event); @@ -99,6 +100,11 @@ void ttn_configure_pins(spi_host_device_t spi_host, uint8_t nss, uint8_t rxtx, u hal_esp32_start_lmic_task(); } +void ttn_set_subband(int band) +{ + subband = band; +} + void ttn_reset(void) { hal_esp32_enter_critical_section(); @@ -203,6 +209,11 @@ bool join_core() hal_esp32_enter_critical_section(); xQueueReset(lmic_event_queue); waiting_reason = TTN_WAITING_FOR_JOIN; + +#if defined(CFG_us915) || defined(CFG_au915) + LMIC_selectSubBand(subband - 1); +#endif + LMIC_startJoining(); hal_esp32_wake_up(); hal_esp32_leave_critical_section();