Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaPHYUS915.cpp Source File

LoRaPHYUS915.cpp

00001 /**
00002  *  @file LoRaPHUS915.cpp
00003  *
00004  *  @brief Implements LoRaPHY for US 915 MHz band
00005  *
00006  *  \code
00007  *   ______                              _
00008  *  / _____)             _              | |
00009  * ( (____  _____ ____ _| |_ _____  ____| |__
00010  *  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00011  *  _____) ) ____| | | || |_| ____( (___| | | |
00012  * (______/|_____)_|_|_| \__)_____)\____)_| |_|
00013  *   (C)2013 Semtech
00014  *  ___ _____ _   ___ _  _____ ___  ___  ___ ___
00015  * / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
00016  * \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
00017  * |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
00018  * embedded.connectivity.solutions===============
00019  *
00020  * \endcode
00021  *
00022  *
00023  * License: Revised BSD License, see LICENSE.TXT file include in the project
00024  *
00025  * Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
00026  *
00027  * Copyright (c) 2017, Arm Limited and affiliates.
00028  * SPDX-License-Identifier: BSD-3-Clause
00029  *
00030  */
00031 
00032 #include "LoRaPHYUS915.h"
00033 #include "lora_phy_ds.h"
00034 
00035 
00036 /*!
00037  * Minimal datarate that can be used by the node
00038  */
00039 #define US915_TX_MIN_DATARATE                       DR_0
00040 
00041 /*!
00042  * Maximal datarate that can be used by the node
00043  */
00044 #define US915_TX_MAX_DATARATE                       DR_4
00045 
00046 /*!
00047  * Minimal datarate that can be used by the node
00048  */
00049 #define US915_RX_MIN_DATARATE                       DR_8
00050 
00051 /*!
00052  * Maximal datarate that can be used by the node
00053  */
00054 #define US915_RX_MAX_DATARATE                       DR_13
00055 
00056 /*!
00057  * Default datarate used by the node
00058  */
00059 #define US915_DEFAULT_DATARATE                      DR_0
00060 
00061 /*!
00062  * Minimal Rx1 receive datarate offset
00063  */
00064 #define US915_MIN_RX1_DR_OFFSET                     0
00065 
00066 /*!
00067  * Maximal Rx1 receive datarate offset
00068  */
00069 #define US915_MAX_RX1_DR_OFFSET                     3
00070 
00071 /*!
00072  * Default Rx1 receive datarate offset
00073  */
00074 #define US915_DEFAULT_RX1_DR_OFFSET                 0
00075 
00076 /*!
00077  * Minimal Tx output power that can be used by the node
00078  */
00079 #define US915_MIN_TX_POWER                          TX_POWER_10
00080 
00081 /*!
00082  * Maximal Tx output power that can be used by the node
00083  */
00084 #define US915_MAX_TX_POWER                          TX_POWER_0
00085 
00086 /*!
00087  * Default Tx output power used by the node
00088  */
00089 #define US915_DEFAULT_TX_POWER                      TX_POWER_0
00090 
00091 /*!
00092  * Default Max ERP
00093  */
00094 #define US915_DEFAULT_MAX_ERP                      30.0f
00095 
00096 /*!
00097  * ADR Ack limit
00098  */
00099 #define US915_ADR_ACK_LIMIT                         64
00100 
00101 /*!
00102  * ADR Ack delay
00103  */
00104 #define US915_ADR_ACK_DELAY                         32
00105 
00106 /*!
00107  * Enabled or disabled the duty cycle
00108  */
00109 #define US915_DUTY_CYCLE_ENABLED                    0
00110 
00111 /*!
00112  * Maximum RX window duration
00113  */
00114 #define US915_MAX_RX_WINDOW                         3000
00115 
00116 /*!
00117  * Receive delay 1
00118  */
00119 #define US915_RECEIVE_DELAY1                        1000
00120 
00121 /*!
00122  * Receive delay 2
00123  */
00124 #define US915_RECEIVE_DELAY2                        2000
00125 
00126 /*!
00127  * Join accept delay 1
00128  */
00129 #define US915_JOIN_ACCEPT_DELAY1                    5000
00130 
00131 /*!
00132  * Join accept delay 2
00133  */
00134 #define US915_JOIN_ACCEPT_DELAY2                    6000
00135 
00136 /*!
00137  * Maximum frame counter gap
00138  */
00139 #define US915_MAX_FCNT_GAP                          16384
00140 
00141 /*!
00142  * Ack timeout
00143  */
00144 #define US915_ACKTIMEOUT                            2000
00145 
00146 /*!
00147  * Random ack timeout limits
00148  */
00149 #define US915_ACK_TIMEOUT_RND                       1000
00150 
00151 /*!
00152  * Second reception window channel frequency definition.
00153  */
00154 #define US915_RX_WND_2_FREQ                         923300000
00155 
00156 /*!
00157  * Second reception window channel datarate definition.
00158  */
00159 #define US915_RX_WND_2_DR                           DR_8
00160 
00161 /*!
00162  * Band 0 definition
00163  * { DutyCycle, TxMaxPower, LastJoinTxDoneTime, LastTxDoneTime, TimeOff }
00164  */
00165 static const band_t  US915_BAND0 = { 1, US915_MAX_TX_POWER, 0, 0, 0 }; //  100.0 %
00166 
00167 /*!
00168  * Defines the first channel for RX window 1 for US band
00169  */
00170 #define US915_FIRST_RX1_CHANNEL                     ( (uint32_t) 923300000 )
00171 
00172 /*!
00173  * Defines the last channel for RX window 1 for US band
00174  */
00175 #define US915_LAST_RX1_CHANNEL                      ( (uint32_t) 927500000 )
00176 
00177 /*!
00178  * Defines the step width of the channels for RX window 1
00179  */
00180 #define US915_STEPWIDTH_RX1_CHANNEL                 ( (uint32_t) 600000 )
00181 
00182 /*!
00183  * Data rates table definition
00184  */
00185 static const uint8_t datarates_US915[]  = {10, 9, 8,  7,  8,  0,  0, 0, 12, 11, 10, 9, 8, 7, 0, 0};
00186 
00187 /*!
00188  * Bandwidths table definition in Hz
00189  */
00190 static const uint32_t bandwidths_US915[] = {125000, 125000, 125000, 125000, 500000, 0, 0, 0, 500000, 500000, 500000, 500000, 500000, 500000, 0, 0};
00191 
00192 /*!
00193  * Up/Down link data rates offset definition
00194  */
00195 static const int8_t datarate_offsets_US915[5][4] =
00196 {
00197     { DR_10, DR_9 , DR_8 , DR_8  }, // DR_0
00198     { DR_11, DR_10, DR_9 , DR_8  }, // DR_1
00199     { DR_12, DR_11, DR_10, DR_9  }, // DR_2
00200     { DR_13, DR_12, DR_11, DR_10 }, // DR_3
00201     { DR_13, DR_13, DR_12, DR_11 }, // DR_4
00202 };
00203 
00204 /*!
00205  * Maximum payload with respect to the datarate index. Cannot operate with repeater.
00206  */
00207 static const uint8_t max_payloads_US915[] = { 11, 53, 125, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0 };
00208 
00209 /*!
00210  * Maximum payload with respect to the datarate index. Can operate with repeater.
00211  */
00212 static const uint8_t max_payloads_with_repeater_US915[] = {11, 53, 125, 242, 242, 0, 0, 0, 33, 109, 222, 222, 222, 222, 0, 0};
00213 
00214 LoRaPHYUS915::LoRaPHYUS915(LoRaWANTimeHandler &lora_time)
00215         : LoRaPHY(lora_time)
00216 {
00217     bands[0] = US915_BAND0;
00218 
00219     // Channels
00220     // 125 kHz channels
00221     for (uint8_t i = 0; i < US915_MAX_NB_CHANNELS - 8; i++) {
00222         channels[i].frequency = 902300000 + i * 200000;
00223         channels[i].dr_range.value = ( DR_3 << 4) | DR_0;
00224         channels[i].band = 0;
00225     }
00226     // 500 kHz channels
00227     for (uint8_t i = US915_MAX_NB_CHANNELS - 8; i < US915_MAX_NB_CHANNELS; i++) {
00228         channels[i].frequency = 903000000 + (i - ( US915_MAX_NB_CHANNELS - 8)) * 1600000;
00229         channels[i].dr_range.value = ( DR_4 << 4) | DR_4;
00230         channels[i].band = 0;
00231     }
00232 
00233     // ChannelsMask
00234     default_channel_mask[0] = 0xFFFF;
00235     default_channel_mask[1] = 0xFFFF;
00236     default_channel_mask[2] = 0xFFFF;
00237     default_channel_mask[3] = 0xFFFF;
00238     default_channel_mask[4] = 0x00FF;
00239 
00240     memset(channel_mask, 0, sizeof(channel_mask));
00241     memset(current_channel_mask, 0, sizeof(current_channel_mask));
00242 
00243     // Copy channels default mask
00244     copy_channel_mask(channel_mask, default_channel_mask, US915_CHANNEL_MASK_SIZE);
00245 
00246     // current channel masks keep track what of the
00247     // channels previously used, i.e., which channels should be avoided in
00248     // next transmission
00249     copy_channel_mask(current_channel_mask, channel_mask, US915_CHANNEL_MASK_SIZE);
00250 
00251     // set default channels
00252     phy_params.channels.channel_list = channels;
00253     phy_params.channels.channel_list_size = US915_MAX_NB_CHANNELS;
00254     phy_params.channels.mask = channel_mask;
00255     phy_params.channels.default_mask = default_channel_mask;
00256     phy_params.channels.mask_size = US915_CHANNEL_MASK_SIZE;
00257 
00258     // set bands for US915 spectrum
00259     phy_params.bands.table = (void *) bands;
00260     phy_params.bands.size = US915_MAX_NB_BANDS;
00261 
00262     // set bandwidths available in US915 spectrum
00263     phy_params.bandwidths.table = (void *) bandwidths_US915;
00264     phy_params.bandwidths.size = 16;
00265 
00266     // set data rates available in US915 spectrum
00267     phy_params.datarates.table = (void *) datarates_US915;
00268     phy_params.datarates.size = 16;
00269 
00270     // set payload sizes with respect to data rates
00271     phy_params.payloads.table = (void *) max_payloads_US915;
00272     phy_params.payloads.size = 16;
00273     phy_params.payloads_with_repeater.table = (void *) max_payloads_with_repeater_US915;
00274     phy_params.payloads_with_repeater.size = 16;
00275 
00276     // dwell time setting
00277     phy_params.ul_dwell_time_setting = 0;
00278     phy_params.dl_dwell_time_setting = 0;
00279 
00280     // set initial and default parameters
00281     phy_params.duty_cycle_enabled = US915_DUTY_CYCLE_ENABLED;
00282     phy_params.accept_tx_param_setup_req = false;
00283     phy_params.fsk_supported = false;
00284     phy_params.cflist_supported = false;
00285     phy_params.dl_channel_req_supported = false;
00286     phy_params.custom_channelplans_supported = false;
00287     phy_params.default_channel_cnt = US915_MAX_NB_CHANNELS;
00288     phy_params.max_channel_cnt = US915_MAX_NB_CHANNELS;
00289     phy_params.cflist_channel_cnt = 0;
00290     phy_params.min_tx_datarate = US915_TX_MIN_DATARATE;
00291     phy_params.max_tx_datarate = US915_TX_MAX_DATARATE;
00292     phy_params.min_rx_datarate = US915_RX_MIN_DATARATE;
00293     phy_params.max_rx_datarate = US915_RX_MAX_DATARATE;
00294     phy_params.default_datarate = US915_DEFAULT_DATARATE;
00295     phy_params.default_max_datarate = US915_TX_MAX_DATARATE;
00296     phy_params.min_rx1_dr_offset = US915_MIN_RX1_DR_OFFSET;
00297     phy_params.max_rx1_dr_offset = US915_MAX_RX1_DR_OFFSET;
00298     phy_params.default_rx1_dr_offset = US915_DEFAULT_RX1_DR_OFFSET;
00299     phy_params.min_tx_power = US915_MIN_TX_POWER;
00300     phy_params.max_tx_power = US915_MAX_TX_POWER;
00301     phy_params.default_tx_power = US915_DEFAULT_TX_POWER;
00302     phy_params.default_max_eirp = 0;
00303     phy_params.default_antenna_gain = 0;
00304     phy_params.adr_ack_limit = US915_ADR_ACK_LIMIT;
00305     phy_params.adr_ack_delay = US915_ADR_ACK_DELAY;
00306     phy_params.max_rx_window = US915_MAX_RX_WINDOW;
00307     phy_params.recv_delay1 = US915_RECEIVE_DELAY1;
00308     phy_params.recv_delay2 = US915_RECEIVE_DELAY2;
00309 
00310     phy_params.join_accept_delay1 = US915_JOIN_ACCEPT_DELAY1;
00311     phy_params.join_accept_delay2 = US915_JOIN_ACCEPT_DELAY2;
00312     phy_params.max_fcnt_gap = US915_MAX_FCNT_GAP;
00313     phy_params.ack_timeout = US915_ACKTIMEOUT;
00314     phy_params.ack_timeout_rnd = US915_ACK_TIMEOUT_RND;
00315     phy_params.rx_window2_datarate = US915_RX_WND_2_DR;
00316     phy_params.rx_window2_frequency = US915_RX_WND_2_FREQ;
00317 }
00318 
00319 LoRaPHYUS915::~LoRaPHYUS915()
00320 {
00321 }
00322 
00323 int8_t LoRaPHYUS915::limit_tx_power(int8_t tx_power, int8_t max_band_tx_power,
00324                                     int8_t datarate)
00325 {
00326     int8_t tx_power_out = tx_power;
00327 
00328     // Limit tx power to the band max
00329     tx_power_out =  MAX (tx_power, max_band_tx_power);
00330 
00331     if (datarate == DR_4) {
00332         // Limit tx power to max 26dBm
00333         tx_power_out = MAX (tx_power, TX_POWER_2);
00334     } else {
00335 
00336         if (num_active_channels(channel_mask, 0, 4) < 50) {
00337             // Limit tx power to max 21dBm
00338             tx_power_out = MAX (tx_power, TX_POWER_5);
00339         }
00340     }
00341 
00342     return tx_power_out;
00343 }
00344 
00345 void LoRaPHYUS915::restore_default_channels()
00346 {
00347     // Copy channels default mask
00348     copy_channel_mask(channel_mask, default_channel_mask, US915_CHANNEL_MASK_SIZE);
00349 
00350     for ( uint8_t i = 0; i < US915_CHANNEL_MASK_SIZE; i++ ) {
00351         // Copy-And the channels mask
00352         current_channel_mask[i] &= channel_mask[i];
00353     }
00354 }
00355 
00356 bool LoRaPHYUS915::rx_config(rx_config_params_t * config, int8_t* datarate)
00357 {
00358     int8_t dr = config->datarate ;
00359     uint8_t max_payload = 0;
00360     int8_t phy_dr = 0;
00361     uint32_t frequency = config->frequency ;
00362 
00363     _radio->lock();
00364 
00365     if (_radio->get_status() != RF_IDLE) {
00366 
00367         _radio->unlock();
00368         return false;
00369 
00370     }
00371 
00372     _radio->unlock();
00373 
00374     // For US915 spectrum, we have 8 Downstream channels, MAC would have
00375     // selected a channel randomly from 72 Upstream channels, that index is
00376     // passed in rx_config_params_t. Based on that channel index, we choose the
00377     // frequency for first RX slot
00378     if (config->rx_slot  == RX_SLOT_WIN_1 ) {
00379         // Apply window 1 frequency
00380         frequency = US915_FIRST_RX1_CHANNEL + (config->channel  % 8) * US915_STEPWIDTH_RX1_CHANNEL;
00381     }
00382 
00383     // Read the physical datarate from the datarates table
00384     phy_dr = datarates_US915[dr];
00385 
00386     _radio->lock();
00387 
00388     _radio->set_channel( frequency );
00389 
00390     // Radio configuration
00391     _radio->set_rx_config(MODEM_LORA, config->bandwidth , phy_dr, 1, 0, 8,
00392                           config->window_timeout , false, 0, false, 0, 0, true,
00393                           config->is_rx_continuous );
00394     _radio->unlock();
00395 
00396     if (config->is_repeater_supported  == true) {
00397 
00398         max_payload = max_payloads_with_repeater_US915[dr];
00399 
00400     } else {
00401 
00402         max_payload = max_payloads_US915[dr];
00403 
00404     }
00405 
00406     _radio->lock();
00407 
00408     _radio->set_max_payload_length(MODEM_LORA, max_payload + LORA_MAC_FRMPAYLOAD_OVERHEAD);
00409 
00410     _radio->unlock();
00411 
00412     *datarate = (uint8_t) dr;
00413     return true;
00414 }
00415 
00416 bool LoRaPHYUS915::tx_config(tx_config_params_t* config, int8_t* tx_power,
00417                              lorawan_time_t* tx_toa)
00418 {
00419     int8_t phy_dr = datarates_US915[config->datarate];
00420     int8_t tx_power_limited = limit_tx_power(config->tx_power,
00421                                              bands[channels[config->channel].band].max_tx_pwr,
00422                                              config->datarate);
00423 
00424     uint32_t bandwidth = get_bandwidth(config->datarate);
00425     int8_t phy_tx_power = 0;
00426 
00427     // Calculate physical TX power
00428     phy_tx_power = compute_tx_power( tx_power_limited, US915_DEFAULT_MAX_ERP, 0 );
00429 
00430     _radio->lock();
00431 
00432     _radio->set_channel(channels[config->channel].frequency);
00433 
00434     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1, 8,
00435                           false, true, 0, 0, false, 3000);
00436 
00437     // Setup maximum payload lenght of the radio driver
00438     _radio->set_max_payload_length(MODEM_LORA, config->pkt_len);
00439 
00440     // Get the time-on-air of the next tx frame
00441     *tx_toa = _radio->time_on_air(MODEM_LORA, config->pkt_len);
00442 
00443     _radio->unlock();
00444 
00445     *tx_power = tx_power_limited;
00446 
00447     return true;
00448 }
00449 
00450 uint8_t LoRaPHYUS915::link_ADR_request(adr_req_params_t* params,
00451                                        int8_t* dr_out, int8_t* tx_power_out,
00452                                        uint8_t* nb_rep_out, uint8_t* nb_bytes_parsed)
00453 {
00454     uint8_t status = 0x07;
00455 
00456     link_adr_params_t adr_settings;
00457     uint8_t next_idx = 0;
00458     uint8_t bytes_processed = 0;
00459     uint16_t temp_channel_masks[US915_CHANNEL_MASK_SIZE] = {0, 0, 0, 0, 0};
00460 
00461     verify_adr_params_t verify_params;
00462 
00463     // Initialize local copy of channels mask
00464     copy_channel_mask(temp_channel_masks, channel_mask, US915_CHANNEL_MASK_SIZE);
00465 
00466     while (bytes_processed < params->payload_size) {
00467         next_idx = parse_link_ADR_req(&(params->payload [bytes_processed]),
00468                                       &adr_settings);
00469 
00470         if (next_idx == 0) {
00471             break; // break loop, since no more request has been found
00472         }
00473 
00474         // Update bytes processed
00475         bytes_processed += next_idx;
00476 
00477         // Revert status, as we only check the last ADR request for the channel mask KO
00478         status = 0x07;
00479 
00480         if (adr_settings.ch_mask_ctrl == 6) {
00481 
00482             // Enable all 125 kHz channels
00483             temp_channel_masks[0] = 0xFFFF;
00484             temp_channel_masks[1] = 0xFFFF;
00485             temp_channel_masks[2] = 0xFFFF;
00486             temp_channel_masks[3] = 0xFFFF;
00487             // Apply chMask to channels 64 to 71
00488             temp_channel_masks[4] = adr_settings.channel_mask;
00489 
00490         } else if (adr_settings.ch_mask_ctrl == 7) {
00491 
00492             // Disable all 125 kHz channels
00493             temp_channel_masks[0] = 0x0000;
00494             temp_channel_masks[1] = 0x0000;
00495             temp_channel_masks[2] = 0x0000;
00496             temp_channel_masks[3] = 0x0000;
00497             // Apply chMask to channels 64 to 71
00498             temp_channel_masks[4] = adr_settings.channel_mask;
00499 
00500         } else if (adr_settings.ch_mask_ctrl == 5) {
00501             // RFU
00502             status &= 0xFE; // Channel mask KO
00503 
00504         } else {
00505             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00506         }
00507     }
00508 
00509     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00510     if ((adr_settings.datarate < DR_4) &&
00511         (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00512 
00513         status &= 0xFE; // Channel mask KO
00514 
00515     }
00516 
00517     verify_params.status  = status;
00518     verify_params.adr_enabled  = params->adr_enabled ;
00519     verify_params.datarate  = adr_settings.datarate;
00520     verify_params.tx_power  = adr_settings.tx_power;
00521     verify_params.nb_rep  = adr_settings.nb_rep;
00522     verify_params.current_datarate  = params->current_datarate ;
00523     verify_params.current_tx_power  = params->current_tx_power ;
00524     verify_params.current_nb_rep  = params->current_nb_rep ;
00525     verify_params.channel_mask  = temp_channel_masks;
00526 
00527     // Verify the parameters and update, if necessary
00528     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00529                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00530 
00531     // Update channelsMask if everything is correct
00532     if (status == 0x07) {
00533         // Copy Mask
00534         copy_channel_mask(channel_mask, temp_channel_masks, US915_CHANNEL_MASK_SIZE);
00535 
00536         current_channel_mask[0] &= channel_mask[0];
00537         current_channel_mask[1] &= channel_mask[1];
00538         current_channel_mask[2] &= channel_mask[2];
00539         current_channel_mask[3] &= channel_mask[3];
00540         current_channel_mask[4] = channel_mask[4];
00541     }
00542 
00543     // Update status variables
00544     *dr_out = adr_settings.datarate;
00545     *tx_power_out = adr_settings.tx_power;
00546     *nb_rep_out = adr_settings.nb_rep;
00547     *nb_bytes_parsed = bytes_processed;
00548 
00549     return status;
00550 }
00551 
00552 uint8_t LoRaPHYUS915::accept_rx_param_setup_req(rx_param_setup_req_t* params)
00553 {
00554     uint8_t status = 0x07;
00555     uint32_t freq = params->frequency;
00556 
00557     // Verify radio frequency
00558     if ((_radio->check_rf_frequency( freq ) == false)
00559             || (freq < US915_FIRST_RX1_CHANNEL)
00560             || (freq > US915_LAST_RX1_CHANNEL)
00561             || (((freq - (uint32_t) US915_FIRST_RX1_CHANNEL) % (uint32_t) US915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00562 
00563         status &= 0xFE; // Channel frequency KO
00564 
00565     }
00566 
00567     // Verify datarate
00568     if (val_in_range(params->datarate, US915_RX_MIN_DATARATE, US915_RX_MAX_DATARATE) == 0) {
00569 
00570         status &= 0xFD; // Datarate KO
00571 
00572     }
00573 
00574     if ((val_in_range(params->datarate, DR_5, DR_7)) || (params->datarate > DR_13)) {
00575 
00576         status &= 0xFD; // Datarate KO
00577 
00578     }
00579 
00580     // Verify datarate offset
00581     if (val_in_range( params->dr_offset, US915_MIN_RX1_DR_OFFSET, US915_MAX_RX1_DR_OFFSET ) == 0 )
00582     {
00583         status &= 0xFB; // Rx1DrOffset range KO
00584     }
00585 
00586     return status;
00587 }
00588 
00589 int8_t LoRaPHYUS915::get_alternate_DR(uint8_t nb_trials)
00590 {
00591     int8_t datarate = 0;
00592 
00593     // Re-enable 500 kHz default channels
00594     channel_mask[4] = 0x00FF;
00595 
00596     if ((nb_trials & 0x01) == 0x01) {
00597         datarate = DR_4;
00598     } else {
00599         datarate = DR_0;
00600     }
00601 
00602     return datarate;
00603 }
00604 
00605 bool LoRaPHYUS915::set_next_channel(channel_selection_params_t* params,
00606                                     uint8_t* channel, lorawan_time_t* time,
00607                                     lorawan_time_t* aggregate_timeOff)
00608 {
00609     uint8_t nb_enabled_channels = 0;
00610     uint8_t delay_tx = 0;
00611     uint8_t enabled_channels[US915_MAX_NB_CHANNELS] = {0};
00612     lorawan_time_t next_tx_delay = 0;
00613 
00614     // Count 125kHz channels
00615     if (num_active_channels(current_channel_mask, 0, 4) == 0) {
00616         // If none of the 125 kHz Upstream channel found,
00617         // Reactivate default channels
00618         copy_channel_mask(current_channel_mask, channel_mask, 4);
00619     }
00620 
00621     // Check other channels
00622     if (params->current_datarate >= DR_4) {
00623         if ((current_channel_mask[4] & 0x00FF ) == 0) {
00624             current_channel_mask[4] = channel_mask[4];
00625         }
00626     }
00627 
00628     if (params->aggregate_timeoff <= _lora_time.get_elapsed_time(params->last_aggregate_tx_time)) {
00629         // Reset Aggregated time off
00630         *aggregate_timeOff = 0;
00631 
00632         // Update bands Time OFF
00633         next_tx_delay = update_band_timeoff(params->joined, params->dc_enabled, bands, US915_MAX_NB_BANDS);
00634 
00635         // Search how many channels are enabled
00636         nb_enabled_channels = enabled_channel_count(params->joined,
00637                                                     params->current_datarate,
00638                                                     current_channel_mask,
00639                                                     enabled_channels, &delay_tx);
00640     } else {
00641         delay_tx++;
00642         next_tx_delay = params->aggregate_timeoff - _lora_time.get_elapsed_time(params->last_aggregate_tx_time);
00643     }
00644 
00645     if (nb_enabled_channels > 0) {
00646         // We found a valid channel
00647         *channel = enabled_channels[get_random( 0, nb_enabled_channels - 1 )];
00648         // Disable the channel in the mask
00649         disable_channel(current_channel_mask, *channel, US915_MAX_NB_CHANNELS - 8);
00650 
00651         *time = 0;
00652         return true;
00653 
00654     } else {
00655 
00656         if (delay_tx > 0) {
00657             // Delay transmission due to AggregatedTimeOff or to a band time off
00658             *time = next_tx_delay;
00659             return true;
00660         }
00661 
00662         // Datarate not supported by any channel
00663         *time = 0;
00664         return false;
00665     }
00666 }
00667 
00668 void LoRaPHYUS915::set_tx_cont_mode(cw_mode_params_t * params, uint32_t given_frequency)
00669 {
00670     (void)given_frequency;
00671 
00672     int8_t tx_power_limited = limit_tx_power(params->tx_power ,
00673                                            bands[channels[params->channel ].band].max_tx_pwr,
00674                                            params->datarate );
00675     int8_t phyTxPower = 0;
00676     uint32_t frequency = channels[params->channel ].frequency;
00677 
00678     // Calculate physical TX power
00679     phyTxPower = compute_tx_power(tx_power_limited, US915_DEFAULT_MAX_ERP, 0);
00680 
00681     _radio->lock();
00682 
00683     _radio->set_tx_continuous_wave(frequency, phyTxPower, params->timeout );
00684 
00685     _radio->unlock();
00686 }
00687 
00688 uint8_t LoRaPHYUS915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00689 {
00690     int8_t datarate = datarate_offsets_US915[dr][dr_offset];
00691 
00692     if (datarate < 0) {
00693         datarate = DR_0;
00694     }
00695 
00696     return datarate;
00697 }