Methods for setting data rate and TX power

This commit is contained in:
Manuel Bleichenbacher 2021-07-28 23:48:54 +02:00
parent 62e829b0d3
commit 37d7f8d517
5 changed files with 628 additions and 14 deletions

View File

@ -99,6 +99,10 @@ extern "C" void app_main(void)
// Register callback for received messages
ttn.onMessage(messageReceived);
// ttn.setAdrEnabled(false);
// ttn.setDataRate(kTTNDataRate_US915_SF7);
// ttn.setMaxTxPower(14);
printf("Joining...\n");
if (ttn.join())
{

View File

@ -100,6 +100,10 @@ void app_main(void)
// Register callback for received messages
ttn_on_message(messageReceived);
// ttn_set_adr_enabled(false);
// ttn_set_data_rate(TTN_DR_US915_SF7);
// ttn_set_max_tx_pow(14);
printf("Joining...\n");
if (ttn_join_provisioned())
{

View File

@ -105,15 +105,272 @@ enum TTNBandwidth
/**
* @brief Bandwidth of 125 kHz
*/
kTTNBW125 = TTN_BW125,
kTTNBW125 = TTN_BW_125,
/**
* @brief Bandwidth of 250 kHz
*/
kTTNBW250 = TTN_BW250,
kTTNBW250 = TTN_BW_250,
/**
* @brief Bandwidth of 500 kHz
*/
kTTNBW500 = TTN_BW500
kTTNBW500 = TTN_BW_500
};
/**
* @brief Data Rate
*
* Note that the spreading factor, bandwidth, bit rate and maximum message
* size associated with each data rate depends on the region.
*/
enum TTNDataRate
{
/**
* @brief Data rate for region AS923 using SF12 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF12 = 0,
/**
* @brief Data rate for region AS923 using SF11 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF11 = 1,
/**
* @brief Data rate for region AS923 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF10 = 2,
/**
* @brief Data rate for region AS923 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF9 = 3,
/**
* @brief Data rate for region AS923 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF8 = 4,
/**
* @brief Data rate for region AS923 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_AS923_SF7_BW125 = 5,
/**
* @brief Data rate for region AS923 using SF7 and 250 kHz bandwidth.
*/
kTTNDataRate_AS923_SF7_BW250 = 6,
/**
* @brief Data rate for region AS923 using FSK and 50 kpbs.
*/
kTTNDataRate_AS923_FSK = 7,
/**
* @brief Data rate for region AU915 using SF12 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF12 = 0,
/**
* @brief Data rate for region AU915 using SF11 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF11 = 1,
/**
* @brief Data rate for region AU915 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF10 = 2,
/**
* @brief Data rate for region AU915 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF9 = 3,
/**
* @brief Data rate for region AU915 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF8 = 4,
/**
* @brief Data rate for region AU915 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_AU915_SF7 = 5,
/**
* @brief Data rate for region AU915 using SF8 and 500 kHz bandwidth.
*/
kTTNDataRate_AU915_SF8_BW500 = 6,
/**
* @brief Data rate for region AU915 using SF12 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF12_BW500 = 8,
/**
* @brief Data rate for region AU915 using SF11 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF11_BW500 = 9,
/**
* @brief Data rate for region AU915 using SF10 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF10_BW500 = 10,
/**
* @brief Data rate for region AU915 using SF9 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF9_BW500 = 11,
/**
* @brief Data rate for region AU915 using SF8 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF8_BW500_DR12 = 12,
/**
* @brief Data rate for region AU915 using SF7 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_AU915_SF7_BW500 = 13,
/**
* @brief Data rate for region EU868 using SF12 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF12 = 0,
/**
* @brief Data rate for region EU868 using SF11 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF11 = 1,
/**
* @brief Data rate for region EU868 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF10 = 2,
/**
* @brief Data rate for region EU868 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF9 = 3,
/**
* @brief Data rate for region EU868 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF8 = 4,
/**
* @brief Data rate for region EU868 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_EU868_SF7_BW125 = 5,
/**
* @brief Data rate for region EU868 using SF7 and 250 kHz bandwidth.
*/
kTTNDataRate_EU868_SF7_BW250 = 6,
/**
* @brief Data rate for region EU868 using FSK and 50 kpbs.
*/
kTTNDataRate_EU868_FSK = 7,
/**
* @brief Data rate for region IN866 using SF12 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF12 = 0,
/**
* @brief Data rate for region IN866 using SF11 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF11 = 1,
/**
* @brief Data rate for region IN866 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF10 = 2,
/**
* @brief Data rate for region IN866 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF9 = 3,
/**
* @brief Data rate for region IN866 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF8 = 4,
/**
* @brief Data rate for region IN866 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_IN866_SF7 = 5,
/**
* @brief Data rate for region IN866 using FSK and 50 kpbs.
*/
kTTNDataRate_IN866_FSK = 7,
/**
* @brief Data rate for region KR920 using SF12 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF12 = 0,
/**
* @brief Data rate for region KR920 using SF11 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF11 = 1,
/**
* @brief Data rate for region KR920 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF10 = 2,
/**
* @brief Data rate for region KR920 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF9 = 3,
/**
* @brief Data rate for region KR920 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF8 = 4,
/**
* @brief Data rate for region KR920 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_KR920_SF7 = 5,
/**
* @brief Data rate for region US915 using SF10 and 125 kHz bandwidth.
*/
kTTNDataRate_US915_SF10 = 0,
/**
* @brief Data rate for region US915 using SF9 and 125 kHz bandwidth.
*/
kTTNDataRate_US915_SF9 = 1,
/**
* @brief Data rate for region US915 using SF8 and 125 kHz bandwidth.
*/
kTTNDataRate_US915_SF8 = 2,
/**
* @brief Data rate for region US915 using SF7 and 125 kHz bandwidth.
*/
kTTNDataRate_US915_SF7 = 3,
/**
* @brief Data rate for region US915 using SF8 and 500 kHz bandwidth.
*/
kTTNDataRate_US915_SF8_BW500 = 4,
/**
* @brief Data rate for region US915 using SF12 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF12_BW500 = 8,
/**
* @brief Data rate for region US915 using SF11 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF11_BW500 = 9,
/**
* @brief Data rate for region US915 using SF10 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF10_BW500 = 10,
/**
* @brief Data rate for region US915 using SF9 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF9_BW500 = 11,
/**
* @brief Data rate for region US915 using SF8 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF8_BW500_DR12 = 12,
/**
* @brief Data rate for region US915 using SF7 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
kTTNDataRate_US915_SF7_BW500 = 13,
/**
* @brief Default data rate for joining
*/
kTTNDRJoinDdefault
};
@ -359,6 +616,27 @@ public:
*/
void setAdrEnabled(bool enabled) { ttn_set_adr_enabled(enabled); }
/**
* @brief Sets the transmission data rate (i.e. the data rate for uplink messages).
*
* If ADR is enabled, it's is used as the initial data rate and later adjusted depending
* on the RF conditions. If ADR is disabled, it is used for all uplink messages.
*
* @param data_rate data rate (use constants kTTNDataRate_xx)
*/
void setDataRate(TTNDataRate data_rate) { ttn_set_data_rate(static_cast<ttn_data_rate_t>(data_rate)); }
/**
* @brief Sets the maximum power for transmission
*
* The power is specified in dBm and sets the power emitted by the radio.
* If the antenna has a gain, it must be substracted fro the specified value to
* achieve the correct transmission power.
*
* @param tx_pow power, in dBm
*/
void setMaxTxPower(int tx_pow) { ttn_set_max_tx_pow(tx_pow); }
/**
* @brief Stops all activies and shuts down the RF module and the background tasks.
*

View File

@ -118,18 +118,275 @@ typedef enum
/**
* @brief Bandwidth of 125 kHz
*/
TTN_BW125 = 1,
TTN_BW_125 = 1,
/**
* @brief Bandwidth of 250 kHz
*/
TTN_BW250 = 2,
TTN_BW_250 = 2,
/**
* @brief Bandwidth of 500 kHz
*/
TTN_BW500 = 3
TTN_BW_500 = 3
} ttn_bandwidth_t;
/**
* @brief Data Rate
*
* Note that the spreading factor, bandwidth, bit rate and maximum message
* size associated with each data rate depends on the region.
*/
typedef enum
{
/**
* @brief Data rate for region AS923 using SF12 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF12 = 0,
/**
* @brief Data rate for region AS923 using SF11 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF11 = 1,
/**
* @brief Data rate for region AS923 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF10 = 2,
/**
* @brief Data rate for region AS923 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF9 = 3,
/**
* @brief Data rate for region AS923 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF8 = 4,
/**
* @brief Data rate for region AS923 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_AS923_SF7_BW125 = 5,
/**
* @brief Data rate for region AS923 using SF7 and 250 kHz bandwidth.
*/
TTN_DR_AS923_SF7_BW250 = 6,
/**
* @brief Data rate for region AS923 using FSK and 50 kpbs.
*/
TTN_DR_AS923_FSK = 7,
/**
* @brief Data rate for region AU915 using SF12 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF12 = 0,
/**
* @brief Data rate for region AU915 using SF11 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF11 = 1,
/**
* @brief Data rate for region AU915 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF10 = 2,
/**
* @brief Data rate for region AU915 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF9 = 3,
/**
* @brief Data rate for region AU915 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF8 = 4,
/**
* @brief Data rate for region AU915 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_AU915_SF7 = 5,
/**
* @brief Data rate for region AU915 using SF8 and 500 kHz bandwidth.
*/
TTN_DR_AU915_SF8_BW500 = 6,
/**
* @brief Data rate for region AU915 using SF12 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF12_BW500 = 8,
/**
* @brief Data rate for region AU915 using SF11 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF11_BW500 = 9,
/**
* @brief Data rate for region AU915 using SF10 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF10_BW500 = 10,
/**
* @brief Data rate for region AU915 using SF9 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF9_BW500 = 11,
/**
* @brief Data rate for region AU915 using SF8 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF8_BW500_DR12 = 12,
/**
* @brief Data rate for region AU915 using SF7 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_AU915_SF7_BW500 = 13,
/**
* @brief Data rate for region EU868 using SF12 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF12 = 0,
/**
* @brief Data rate for region EU868 using SF11 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF11 = 1,
/**
* @brief Data rate for region EU868 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF10 = 2,
/**
* @brief Data rate for region EU868 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF9 = 3,
/**
* @brief Data rate for region EU868 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF8 = 4,
/**
* @brief Data rate for region EU868 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_EU868_SF7_BW125 = 5,
/**
* @brief Data rate for region EU868 using SF7 and 250 kHz bandwidth.
*/
TTN_DR_EU868_SF7_BW250 = 6,
/**
* @brief Data rate for region EU868 using FSK and 50 kpbs.
*/
TTN_DR_EU868_FSK = 7,
/**
* @brief Data rate for region IN866 using SF12 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF12 = 0,
/**
* @brief Data rate for region IN866 using SF11 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF11 = 1,
/**
* @brief Data rate for region IN866 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF10 = 2,
/**
* @brief Data rate for region IN866 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF9 = 3,
/**
* @brief Data rate for region IN866 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF8 = 4,
/**
* @brief Data rate for region IN866 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_IN866_SF7 = 5,
/**
* @brief Data rate for region IN866 using FSK and 50 kpbs.
*/
TTN_DR_IN866_FSK = 7,
/**
* @brief Data rate for region KR920 using SF12 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF12 = 0,
/**
* @brief Data rate for region KR920 using SF11 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF11 = 1,
/**
* @brief Data rate for region KR920 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF10 = 2,
/**
* @brief Data rate for region KR920 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF9 = 3,
/**
* @brief Data rate for region KR920 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF8 = 4,
/**
* @brief Data rate for region KR920 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_KR920_SF7 = 5,
/**
* @brief Data rate for region US915 using SF10 and 125 kHz bandwidth.
*/
TTN_DR_US915_SF10 = 0,
/**
* @brief Data rate for region US915 using SF9 and 125 kHz bandwidth.
*/
TTN_DR_US915_SF9 = 1,
/**
* @brief Data rate for region US915 using SF8 and 125 kHz bandwidth.
*/
TTN_DR_US915_SF8 = 2,
/**
* @brief Data rate for region US915 using SF7 and 125 kHz bandwidth.
*/
TTN_DR_US915_SF7 = 3,
/**
* @brief Data rate for region US915 using SF8 and 500 kHz bandwidth.
*/
TTN_DR_US915_SF8_BW500 = 4,
/**
* @brief Data rate for region US915 using SF12 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF12_BW500 = 8,
/**
* @brief Data rate for region US915 using SF11 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF11_BW500 = 9,
/**
* @brief Data rate for region US915 using SF10 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF10_BW500 = 10,
/**
* @brief Data rate for region US915 using SF9 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF9_BW500 = 11,
/**
* @brief Data rate for region US915 using SF8 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF8_BW500_DR12 = 12,
/**
* @brief Data rate for region US915 using SF7 and 500 kHz bandwidth.
*
* Reserved for future applications.
*/
TTN_DR_US915_SF7_BW500 = 13,
/**
* @brief Default data rate for joining.
*/
TTN_DR_JOIN_DEFAULT = 255
} ttn_data_rate_t;
/**
* @brief RF settings for TX or RX
*/
@ -351,6 +608,27 @@ bool ttn_adr_enabled(void);
*/
void ttn_set_adr_enabled(bool enabled);
/**
* @brief Sets the transmission data rate (i.e. the data rate for uplink messages).
*
* If ADR is enabled, it's is used as the initial data rate and later adjusted depending
* on the RF conditions. If ADR is disabled, it is used for all uplink messages.
*
* @param data_rate data rate (use constants TTN_DR_xx)
*/
void ttn_set_data_rate(ttn_data_rate_t data_rate);
/**
* @brief Sets the maximum power for transmission
*
* The power is specified in dBm and sets the power emitted by the radio.
* If the antenna has a gain, it must be substracted fro the specified value to
* achieve the correct transmission power.
*
* @param tx_pow power, in dBm
*/
void ttn_set_max_tx_pow(int tx_pow);
/**
* @brief Stops all activies and shuts down the RF module and the background tasks.
*

View File

@ -22,6 +22,7 @@
#define TAG "ttn"
#define DEFAULT_MAX_TX_POWER -1000
/**
* @brief Reason the user code is waiting
@ -55,14 +56,18 @@ typedef struct {
size_t message_size;
} ttn_lmic_event_t;
static bool is_started;
static QueueHandle_t lmic_event_queue;
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 ttn_data_rate_t join_data_rate = TTN_DR_JOIN_DEFAULT;
static int max_tx_power = DEFAULT_MAX_TX_POWER;
static bool join_core(void);
static void config_rf_params(void);
static void event_callback(void* user_data, ev_t event);
static void message_received_callback(void *user_data, uint8_t port, const uint8_t *message, size_t message_size);
static void message_transmitted_callback(void *user_data, int success);
@ -198,7 +203,23 @@ bool ttn_join_provisioned(void)
return join_core();
}
bool join_core()
// Called immediately before sending join request message
void config_rf_params(void)
{
#if defined(CFG_us915) || defined(CFG_au915)
if (subband != 0)
LMIC_selectSubBand(subband - 1);
#endif
if (join_data_rate != TTN_DR_JOIN_DEFAULT || max_tx_power != DEFAULT_MAX_TX_POWER)
{
dr_t dr = join_data_rate == TTN_DR_JOIN_DEFAULT ? LMIC.datarate : (dr_t)join_data_rate;
s1_t txpow = max_tx_power == DEFAULT_MAX_TX_POWER ? LMIC.adrTxPow : max_tx_power;
LMIC_setDrTxpow(dr, txpow);
}
}
bool join_core(void)
{
if (!ttn_provisioning_have_keys())
{
@ -206,22 +227,21 @@ bool join_core()
return false;
}
is_started = true;
hal_esp32_enter_critical_section();
xQueueReset(lmic_event_queue);
waiting_reason = TTN_WAITING_FOR_JOIN;
#if defined(CFG_us915) || defined(CFG_au915)
if (subband != 0)
LMIC_selectSubBand(subband - 1);
#endif
LMIC_startJoining();
config_rf_params();
hal_esp32_wake_up();
hal_esp32_leave_critical_section();
ttn_lmic_event_t event;
xQueueReceive(lmic_event_queue, &event, portMAX_DELAY);
return event.event == TTN_EVNT_JOIN_COMPLETED;
is_started = event.event == TTN_EVNT_JOIN_COMPLETED;
return is_started;
}
ttn_response_code_t ttn_transmit_message(const uint8_t *payload, size_t length, ttn_port_t port, bool confirm)
@ -290,9 +310,39 @@ bool ttn_adr_enabled(void)
return LMIC.adrEnabled != 0;
}
void ttn_set_adr_nabled(bool enabled)
void ttn_set_adr_enabled(bool enabled)
{
hal_esp32_enter_critical_section();
LMIC_setAdrMode(enabled);
hal_esp32_leave_critical_section();
}
void ttn_set_data_rate(ttn_data_rate_t data_rate)
{
if (is_started)
{
hal_esp32_enter_critical_section();
LMIC_setDrTxpow(data_rate, LMIC.adrTxPow);
hal_esp32_leave_critical_section();
}
else
{
join_data_rate = data_rate;
}
}
void ttn_set_max_tx_pow(int tx_pow)
{
if (is_started)
{
hal_esp32_enter_critical_section();
LMIC_setDrTxpow(LMIC.datarate, tx_pow);
hal_esp32_leave_critical_section();
}
else
{
max_tx_power = tx_pow;
}
}
ttn_rf_settings_t ttn_getrf_settings(ttn_rx_tx_window_t window)