Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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     { DR_10, DR_9, DR_8, DR_8  },   // DR_0
00197     { DR_11, DR_10, DR_9, DR_8  },  // DR_1
00198     { DR_12, DR_11, DR_10, DR_9  }, // DR_2
00199     { DR_13, DR_12, DR_11, DR_10 }, // DR_3
00200     { DR_13, DR_13, DR_12, DR_11 }, // DR_4
00201 };
00202 
00203 /*!
00204  * Maximum payload with respect to the datarate index. Cannot operate with repeater.
00205  */
00206 static const uint8_t max_payloads_US915[] = {11, 53, 125, 242, 242, 0, 0, 0, 53, 129, 242, 242, 242, 242, 0, 0};
00207 
00208 /*!
00209  * Maximum payload with respect to the datarate index. Can operate with repeater.
00210  */
00211 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};
00212 
00213 static const uint16_t fsb_mask[] = MBED_CONF_LORA_FSB_MASK;
00214 static const uint16_t full_channel_mask [] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0x00FF};
00215 
00216 LoRaPHYUS915::LoRaPHYUS915()
00217 {
00218     bands[0] = US915_BAND0;
00219 
00220     // Channels
00221     // 125 kHz channels - Upstream
00222     for (uint8_t i = 0; i < US915_MAX_NB_CHANNELS - 8; i++) {
00223         channels[i].frequency = 902300000 + i * 200000;
00224         channels[i].dr_range.value = (DR_3 << 4) | DR_0;
00225         channels[i].band = 0;
00226     }
00227     // 500 kHz channels - Upstream
00228     for (uint8_t i = US915_MAX_NB_CHANNELS - 8; i < US915_MAX_NB_CHANNELS; i++) {
00229         channels[i].frequency = 903000000 + (i - (US915_MAX_NB_CHANNELS - 8)) * 1600000;
00230         channels[i].dr_range.value = (DR_4 << 4) | DR_4;
00231         channels[i].band = 0;
00232     }
00233 
00234     // Fill-up default channel mask and apply FSB mask too
00235     fill_channel_mask_with_fsb(full_channel_mask, fsb_mask,
00236                                default_channel_mask, US915_CHANNEL_MASK_SIZE);
00237 
00238     memset(channel_mask, 0, sizeof(channel_mask));
00239     memset(current_channel_mask, 0, sizeof(current_channel_mask));
00240 
00241     // Copy channels default mask
00242     copy_channel_mask(channel_mask, default_channel_mask, US915_CHANNEL_MASK_SIZE);
00243 
00244     // current channel masks keep track of the
00245     // channels previously used, i.e., which channels should be avoided in
00246     // next transmission
00247     copy_channel_mask(current_channel_mask, channel_mask, US915_CHANNEL_MASK_SIZE);
00248 
00249     // set default channels
00250     phy_params.channels.channel_list = channels;
00251     phy_params.channels.channel_list_size = US915_MAX_NB_CHANNELS;
00252     phy_params.channels.mask = channel_mask;
00253     phy_params.channels.default_mask = default_channel_mask;
00254     phy_params.channels.mask_size = US915_CHANNEL_MASK_SIZE;
00255 
00256     // set bands for US915 spectrum
00257     phy_params.bands.table = (void *) bands;
00258     phy_params.bands.size = US915_MAX_NB_BANDS;
00259 
00260     // set bandwidths available in US915 spectrum
00261     phy_params.bandwidths.table = (void *) bandwidths_US915;
00262     phy_params.bandwidths.size = 16;
00263 
00264     // set data rates available in US915 spectrum
00265     phy_params.datarates.table = (void *) datarates_US915;
00266     phy_params.datarates.size = 16;
00267 
00268     // set payload sizes with respect to data rates
00269     phy_params.payloads.table = (void *) max_payloads_US915;
00270     phy_params.payloads.size = 16;
00271     phy_params.payloads_with_repeater.table = (void *) max_payloads_with_repeater_US915;
00272     phy_params.payloads_with_repeater.size = 16;
00273 
00274     // dwell time setting
00275     phy_params.ul_dwell_time_setting = 0;
00276     phy_params.dl_dwell_time_setting = 0;
00277 
00278     // set initial and default parameters
00279     phy_params.duty_cycle_enabled = US915_DUTY_CYCLE_ENABLED;
00280     phy_params.accept_tx_param_setup_req = false;
00281     phy_params.fsk_supported = false;
00282     phy_params.cflist_supported = false;
00283     phy_params.dl_channel_req_supported = false;
00284     phy_params.custom_channelplans_supported = false;
00285     phy_params.default_channel_cnt = US915_MAX_NB_CHANNELS;
00286     phy_params.max_channel_cnt = US915_MAX_NB_CHANNELS;
00287     phy_params.cflist_channel_cnt = 0;
00288     phy_params.min_tx_datarate = US915_TX_MIN_DATARATE;
00289     phy_params.max_tx_datarate = US915_TX_MAX_DATARATE;
00290     phy_params.min_rx_datarate = US915_RX_MIN_DATARATE;
00291     phy_params.max_rx_datarate = US915_RX_MAX_DATARATE;
00292     phy_params.default_datarate = US915_DEFAULT_DATARATE;
00293     phy_params.default_max_datarate = US915_TX_MAX_DATARATE;
00294     phy_params.min_rx1_dr_offset = US915_MIN_RX1_DR_OFFSET;
00295     phy_params.max_rx1_dr_offset = US915_MAX_RX1_DR_OFFSET;
00296     phy_params.default_rx1_dr_offset = US915_DEFAULT_RX1_DR_OFFSET;
00297     phy_params.min_tx_power = US915_MIN_TX_POWER;
00298     phy_params.max_tx_power = US915_MAX_TX_POWER;
00299     phy_params.default_tx_power = US915_DEFAULT_TX_POWER;
00300     phy_params.default_max_eirp = 0;
00301     phy_params.default_antenna_gain = 0;
00302     phy_params.adr_ack_limit = US915_ADR_ACK_LIMIT;
00303     phy_params.adr_ack_delay = US915_ADR_ACK_DELAY;
00304     phy_params.max_rx_window = US915_MAX_RX_WINDOW;
00305     phy_params.recv_delay1 = US915_RECEIVE_DELAY1;
00306     phy_params.recv_delay2 = US915_RECEIVE_DELAY2;
00307 
00308     phy_params.join_accept_delay1 = US915_JOIN_ACCEPT_DELAY1;
00309     phy_params.join_accept_delay2 = US915_JOIN_ACCEPT_DELAY2;
00310     phy_params.max_fcnt_gap = US915_MAX_FCNT_GAP;
00311     phy_params.ack_timeout = US915_ACKTIMEOUT;
00312     phy_params.ack_timeout_rnd = US915_ACK_TIMEOUT_RND;
00313     phy_params.rx_window2_datarate = US915_RX_WND_2_DR;
00314     phy_params.rx_window2_frequency = US915_RX_WND_2_FREQ;
00315 }
00316 
00317 LoRaPHYUS915::~LoRaPHYUS915()
00318 {
00319 }
00320 
00321 int8_t LoRaPHYUS915::limit_tx_power(int8_t tx_power, int8_t max_band_tx_power,
00322                                     int8_t datarate)
00323 {
00324     int8_t tx_power_out = tx_power;
00325 
00326     // Limit tx power to the band max
00327     tx_power_out =  MAX(tx_power, max_band_tx_power);
00328 
00329     if (datarate == DR_4) {
00330         // Limit tx power to max 26dBm
00331         tx_power_out = MAX(tx_power, TX_POWER_2);
00332     } else {
00333 
00334         if (num_active_channels(channel_mask, 0, 4) < 50) {
00335             // Limit tx power to max 21dBm
00336             tx_power_out = MAX(tx_power, TX_POWER_5);
00337         }
00338     }
00339 
00340     return tx_power_out;
00341 }
00342 
00343 void LoRaPHYUS915::restore_default_channels()
00344 {
00345     // Copy channels default mask
00346     copy_channel_mask(channel_mask, default_channel_mask, US915_CHANNEL_MASK_SIZE);
00347 
00348     // Update running channel mask
00349     intersect_channel_mask(channel_mask, current_channel_mask, US915_CHANNEL_MASK_SIZE);
00350 }
00351 
00352 bool LoRaPHYUS915::rx_config(rx_config_params_t  *config)
00353 {
00354     int8_t dr = config->datarate ;
00355     uint8_t max_payload = 0;
00356     int8_t phy_dr = 0;
00357     uint32_t frequency = config->frequency ;
00358 
00359     _radio->lock();
00360 
00361     if (_radio->get_status() != RF_IDLE) {
00362 
00363         _radio->unlock();
00364         return false;
00365 
00366     }
00367 
00368     _radio->unlock();
00369 
00370     // For US915 spectrum, we have 8 Downstream channels, MAC would have
00371     // selected a channel randomly from 72 Upstream channels, that index is
00372     // passed in rx_config_params_t. Based on that channel index, we choose the
00373     // frequency for first RX slot
00374     if (config->rx_slot  == RX_SLOT_WIN_1 ) {
00375         // Apply window 1 frequency
00376         frequency = US915_FIRST_RX1_CHANNEL + (config->channel  % 8) * US915_STEPWIDTH_RX1_CHANNEL;
00377         // Caller may print the frequency to log so update it to match actual frequency
00378         config->frequency  = frequency;
00379     }
00380 
00381     // Read the physical datarate from the datarates table
00382     phy_dr = datarates_US915[dr];
00383 
00384     _radio->lock();
00385 
00386     _radio->set_channel(frequency);
00387 
00388     // Radio configuration
00389     _radio->set_rx_config(MODEM_LORA, config->bandwidth , phy_dr, 1, 0,
00390                           MBED_CONF_LORA_DOWNLINK_PREAMBLE_LENGTH,
00391                           config->window_timeout , false, 0, false, 0, 0, true,
00392                           config->is_rx_continuous );
00393     _radio->unlock();
00394 
00395     if (config->is_repeater_supported  == true) {
00396 
00397         max_payload = max_payloads_with_repeater_US915[dr];
00398 
00399     } else {
00400 
00401         max_payload = max_payloads_US915[dr];
00402 
00403     }
00404 
00405     _radio->lock();
00406 
00407     _radio->set_max_payload_length(MODEM_LORA, max_payload + LORA_MAC_FRMPAYLOAD_OVERHEAD);
00408 
00409     _radio->unlock();
00410 
00411     return true;
00412 }
00413 
00414 bool LoRaPHYUS915::tx_config(tx_config_params_t *config, int8_t *tx_power,
00415                              lorawan_time_t *tx_toa)
00416 {
00417     int8_t phy_dr = datarates_US915[config->datarate];
00418     int8_t tx_power_limited = limit_tx_power(config->tx_power,
00419                                              bands[channels[config->channel].band].max_tx_pwr,
00420                                              config->datarate);
00421 
00422     uint32_t bandwidth = get_bandwidth(config->datarate);
00423     int8_t phy_tx_power = 0;
00424 
00425     // Calculate physical TX power
00426     phy_tx_power = compute_tx_power(tx_power_limited, US915_DEFAULT_MAX_ERP, 0);
00427 
00428     _radio->lock();
00429 
00430     _radio->set_channel(channels[config->channel].frequency);
00431 
00432     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1,
00433                           MBED_CONF_LORA_UPLINK_PREAMBLE_LENGTH,
00434                           false, true, 0, 0, false, 3000);
00435 
00436     // Setup maximum payload lenght of the radio driver
00437     _radio->set_max_payload_length(MODEM_LORA, config->pkt_len);
00438 
00439     // Get the time-on-air of the next tx frame
00440     *tx_toa = _radio->time_on_air(MODEM_LORA, config->pkt_len);
00441 
00442     _radio->unlock();
00443 
00444     *tx_power = tx_power_limited;
00445 
00446     return true;
00447 }
00448 
00449 uint8_t LoRaPHYUS915::link_ADR_request(adr_req_params_t *params,
00450                                        int8_t *dr_out, int8_t *tx_power_out,
00451                                        uint8_t *nb_rep_out, uint8_t *nb_bytes_parsed)
00452 {
00453     uint8_t status = 0x07;
00454 
00455     link_adr_params_t adr_settings;
00456     uint8_t next_idx = 0;
00457     uint8_t bytes_processed = 0;
00458     uint16_t temp_channel_masks[US915_CHANNEL_MASK_SIZE] = {0, 0, 0, 0, 0};
00459 
00460     verify_adr_params_t verify_params;
00461 
00462     // Initialize local copy of channels mask
00463     copy_channel_mask(temp_channel_masks, channel_mask, US915_CHANNEL_MASK_SIZE);
00464 
00465     while (bytes_processed < params->payload_size &&
00466             params->payload [bytes_processed] == SRV_MAC_LINK_ADR_REQ ) {
00467         next_idx = parse_link_ADR_req(&(params->payload [bytes_processed]),
00468                                       params->payload_size  - bytes_processed,
00469                                       &adr_settings);
00470 
00471         if (next_idx == 0) {
00472             bytes_processed = 0;
00473             // break loop, malformed packet
00474             break;
00475         }
00476 
00477         // Update bytes processed
00478         bytes_processed += next_idx;
00479 
00480         // Revert status, as we only check the last ADR request for the channel mask KO
00481         status = 0x07;
00482 
00483         if (adr_settings.ch_mask_ctrl == 6) {
00484 
00485             // Enable all 125 kHz channels
00486             fill_channel_mask_with_value(temp_channel_masks, 0xFFFF,
00487                                          US915_CHANNEL_MASK_SIZE - 1);
00488 
00489             // Apply chMask to channels 64 to 71
00490             temp_channel_masks[4] = adr_settings.channel_mask;
00491 
00492         } else if (adr_settings.ch_mask_ctrl == 7) {
00493 
00494             // Disable all 125 kHz channels
00495             fill_channel_mask_with_value(temp_channel_masks, 0x0000,
00496                                          US915_CHANNEL_MASK_SIZE - 1);
00497 
00498             // Apply chMask to channels 64 to 71
00499             temp_channel_masks[4] = adr_settings.channel_mask;
00500 
00501         } else if (adr_settings.ch_mask_ctrl == 5) {
00502             // RFU
00503             status &= 0xFE; // Channel mask KO
00504 
00505         } else {
00506             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00507         }
00508     }
00509 
00510     if (bytes_processed == 0) {
00511         *nb_bytes_parsed = 0;
00512         return status;
00513     }
00514 
00515     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00516     if ((adr_settings.datarate < DR_4) &&
00517             (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00518 
00519         status &= 0xFE; // Channel mask KO
00520 
00521     }
00522 
00523     verify_params.status  = status;
00524     verify_params.adr_enabled  = params->adr_enabled ;
00525     verify_params.datarate  = adr_settings.datarate;
00526     verify_params.tx_power  = adr_settings.tx_power;
00527     verify_params.nb_rep  = adr_settings.nb_rep;
00528     verify_params.current_datarate  = params->current_datarate ;
00529     verify_params.current_tx_power  = params->current_tx_power ;
00530     verify_params.current_nb_rep  = params->current_nb_trans ;
00531     verify_params.channel_mask  = temp_channel_masks;
00532 
00533     // Verify the parameters and update, if necessary
00534     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00535                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00536 
00537     // Update channelsMask if everything is correct
00538     if (status == 0x07) {
00539         // Copy Mask
00540         copy_channel_mask(channel_mask, temp_channel_masks, US915_CHANNEL_MASK_SIZE);
00541 
00542         // update running channel mask
00543         intersect_channel_mask(channel_mask, current_channel_mask,
00544                                US915_CHANNEL_MASK_SIZE);
00545     }
00546 
00547     // Update status variables
00548     *dr_out = adr_settings.datarate;
00549     *tx_power_out = adr_settings.tx_power;
00550     *nb_rep_out = adr_settings.nb_rep;
00551     *nb_bytes_parsed = bytes_processed;
00552 
00553     return status;
00554 }
00555 
00556 uint8_t LoRaPHYUS915::accept_rx_param_setup_req(rx_param_setup_req_t *params)
00557 {
00558     uint8_t status = 0x07;
00559     uint32_t freq = params->frequency;
00560 
00561     // Verify radio frequency
00562     if ((_radio->check_rf_frequency(freq) == false)
00563             || (freq < US915_FIRST_RX1_CHANNEL)
00564             || (freq > US915_LAST_RX1_CHANNEL)
00565             || (((freq - (uint32_t) US915_FIRST_RX1_CHANNEL) % (uint32_t) US915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00566 
00567         status &= 0xFE; // Channel frequency KO
00568 
00569     }
00570 
00571     // Verify datarate
00572     if (val_in_range(params->datarate, US915_RX_MIN_DATARATE, US915_RX_MAX_DATARATE) == 0) {
00573 
00574         status &= 0xFD; // Datarate KO
00575 
00576     }
00577 
00578     if ((val_in_range(params->datarate, DR_5, DR_7)) || (params->datarate > DR_13)) {
00579 
00580         status &= 0xFD; // Datarate KO
00581 
00582     }
00583 
00584     // Verify datarate offset
00585     if (val_in_range(params->dr_offset, US915_MIN_RX1_DR_OFFSET, US915_MAX_RX1_DR_OFFSET) == 0) {
00586         status &= 0xFB; // Rx1DrOffset range KO
00587     }
00588 
00589     return status;
00590 }
00591 
00592 int8_t LoRaPHYUS915::get_alternate_DR(uint8_t nb_trials)
00593 {
00594     int8_t datarate = 0;
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 lorawan_status_t 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     // Update the 500 kHz channels in the running mask
00622     if ((params->current_datarate >= DR_4)
00623             && (current_channel_mask[4] & 0x00FF) == 0) {
00624         current_channel_mask[4] = channel_mask[4];
00625     }
00626 
00627     if (params->aggregate_timeoff <= _lora_time->get_elapsed_time(params->last_aggregate_tx_time)) {
00628         // Reset Aggregated time off
00629         *aggregate_timeOff = 0;
00630 
00631         // Update bands Time OFF
00632         next_tx_delay = update_band_timeoff(params->joined, params->dc_enabled, bands, US915_MAX_NB_BANDS);
00633 
00634         // Search how many channels are enabled
00635         nb_enabled_channels = enabled_channel_count(params->current_datarate,
00636                                                     current_channel_mask,
00637                                                     enabled_channels, &delay_tx);
00638     } else {
00639         delay_tx++;
00640         next_tx_delay = params->aggregate_timeoff - _lora_time->get_elapsed_time(params->last_aggregate_tx_time);
00641     }
00642 
00643     if (nb_enabled_channels > 0) {
00644         // We found a valid channel
00645         *channel = enabled_channels[get_random(0, nb_enabled_channels - 1)];
00646         // Disable the channel in the mask
00647         disable_channel(current_channel_mask, *channel, US915_MAX_NB_CHANNELS);
00648 
00649         *time = 0;
00650         return LORAWAN_STATUS_OK;
00651 
00652     } else {
00653 
00654         if (delay_tx > 0) {
00655             // Delay transmission due to AggregatedTimeOff or to a band time off
00656             *time = next_tx_delay;
00657             return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
00658         }
00659 
00660         // Datarate not supported by any channel
00661         *time = 0;
00662         return LORAWAN_STATUS_NO_CHANNEL_FOUND;
00663     }
00664 }
00665 
00666 void LoRaPHYUS915::set_tx_cont_mode(cw_mode_params_t  *params, uint32_t given_frequency)
00667 {
00668     (void)given_frequency;
00669 
00670     int8_t tx_power_limited = limit_tx_power(params->tx_power ,
00671                                              bands[channels[params->channel ].band].max_tx_pwr,
00672                                              params->datarate );
00673     int8_t phyTxPower = 0;
00674     uint32_t frequency = channels[params->channel ].frequency;
00675 
00676     // Calculate physical TX power
00677     phyTxPower = compute_tx_power(tx_power_limited, US915_DEFAULT_MAX_ERP, 0);
00678 
00679     _radio->lock();
00680 
00681     _radio->set_tx_continuous_wave(frequency, phyTxPower, params->timeout );
00682 
00683     _radio->unlock();
00684 }
00685 
00686 uint8_t LoRaPHYUS915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00687 {
00688     return datarate_offsets_US915[dr][dr_offset];
00689 }
00690 
00691 
00692 void LoRaPHYUS915::intersect_channel_mask(const uint16_t *source,
00693                                           uint16_t *destination, uint8_t size)
00694 {
00695     for (uint8_t i = 0; i < size; i++) {
00696         destination[i] &= source[i];
00697     }
00698 }
00699 
00700 void LoRaPHYUS915::fill_channel_mask_with_fsb(const uint16_t *expectation,
00701                                               const uint16_t *fsb_mask,
00702                                               uint16_t *destination,
00703                                               uint8_t size)
00704 {
00705     for (uint8_t i = 0; i < size; i++) {
00706         destination[i] = expectation[i] & fsb_mask[i];
00707     }
00708 
00709 }
00710 
00711 void LoRaPHYUS915::fill_channel_mask_with_value(uint16_t *channel_mask,
00712                                                 uint16_t value, uint8_t size)
00713 {
00714     for (uint8_t i = 0; i < size; i++) {
00715         channel_mask[i] = value;
00716     }
00717 }