Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
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)
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     return true;
00413 }
00414 
00415 bool LoRaPHYUS915::tx_config(tx_config_params_t* config, int8_t* tx_power,
00416                              lorawan_time_t* tx_toa)
00417 {
00418     int8_t phy_dr = datarates_US915[config->datarate];
00419     int8_t tx_power_limited = limit_tx_power(config->tx_power,
00420                                              bands[channels[config->channel].band].max_tx_pwr,
00421                                              config->datarate);
00422 
00423     uint32_t bandwidth = get_bandwidth(config->datarate);
00424     int8_t phy_tx_power = 0;
00425 
00426     // Calculate physical TX power
00427     phy_tx_power = compute_tx_power( tx_power_limited, US915_DEFAULT_MAX_ERP, 0 );
00428 
00429     _radio->lock();
00430 
00431     _radio->set_channel(channels[config->channel].frequency);
00432 
00433     _radio->set_tx_config(MODEM_LORA, phy_tx_power, 0, bandwidth, phy_dr, 1, 8,
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         next_idx = parse_link_ADR_req(&(params->payload [bytes_processed]),
00467                                       &adr_settings);
00468 
00469         if (next_idx == 0) {
00470             break; // break loop, since no more request has been found
00471         }
00472 
00473         // Update bytes processed
00474         bytes_processed += next_idx;
00475 
00476         // Revert status, as we only check the last ADR request for the channel mask KO
00477         status = 0x07;
00478 
00479         if (adr_settings.ch_mask_ctrl == 6) {
00480 
00481             // Enable all 125 kHz channels
00482             temp_channel_masks[0] = 0xFFFF;
00483             temp_channel_masks[1] = 0xFFFF;
00484             temp_channel_masks[2] = 0xFFFF;
00485             temp_channel_masks[3] = 0xFFFF;
00486             // Apply chMask to channels 64 to 71
00487             temp_channel_masks[4] = adr_settings.channel_mask;
00488 
00489         } else if (adr_settings.ch_mask_ctrl == 7) {
00490 
00491             // Disable all 125 kHz channels
00492             temp_channel_masks[0] = 0x0000;
00493             temp_channel_masks[1] = 0x0000;
00494             temp_channel_masks[2] = 0x0000;
00495             temp_channel_masks[3] = 0x0000;
00496             // Apply chMask to channels 64 to 71
00497             temp_channel_masks[4] = adr_settings.channel_mask;
00498 
00499         } else if (adr_settings.ch_mask_ctrl == 5) {
00500             // RFU
00501             status &= 0xFE; // Channel mask KO
00502 
00503         } else {
00504             temp_channel_masks[adr_settings.ch_mask_ctrl] = adr_settings.channel_mask;
00505         }
00506     }
00507 
00508     // FCC 15.247 paragraph F mandates to hop on at least 2 125 kHz channels
00509     if ((adr_settings.datarate < DR_4) &&
00510         (num_active_channels(temp_channel_masks, 0, 4) < 2)) {
00511 
00512         status &= 0xFE; // Channel mask KO
00513 
00514     }
00515 
00516     verify_params.status  = status;
00517     verify_params.adr_enabled  = params->adr_enabled ;
00518     verify_params.datarate  = adr_settings.datarate;
00519     verify_params.tx_power  = adr_settings.tx_power;
00520     verify_params.nb_rep  = adr_settings.nb_rep;
00521     verify_params.current_datarate  = params->current_datarate ;
00522     verify_params.current_tx_power  = params->current_tx_power ;
00523     verify_params.current_nb_rep  = params->current_nb_rep ;
00524     verify_params.channel_mask  = temp_channel_masks;
00525 
00526     // Verify the parameters and update, if necessary
00527     status = verify_link_ADR_req(&verify_params, &adr_settings.datarate,
00528                                  &adr_settings.tx_power, &adr_settings.nb_rep);
00529 
00530     // Update channelsMask if everything is correct
00531     if (status == 0x07) {
00532         // Copy Mask
00533         copy_channel_mask(channel_mask, temp_channel_masks, US915_CHANNEL_MASK_SIZE);
00534 
00535         current_channel_mask[0] &= channel_mask[0];
00536         current_channel_mask[1] &= channel_mask[1];
00537         current_channel_mask[2] &= channel_mask[2];
00538         current_channel_mask[3] &= channel_mask[3];
00539         current_channel_mask[4] = channel_mask[4];
00540     }
00541 
00542     // Update status variables
00543     *dr_out = adr_settings.datarate;
00544     *tx_power_out = adr_settings.tx_power;
00545     *nb_rep_out = adr_settings.nb_rep;
00546     *nb_bytes_parsed = bytes_processed;
00547 
00548     return status;
00549 }
00550 
00551 uint8_t LoRaPHYUS915::accept_rx_param_setup_req(rx_param_setup_req_t* params)
00552 {
00553     uint8_t status = 0x07;
00554     uint32_t freq = params->frequency;
00555 
00556     // Verify radio frequency
00557     if ((_radio->check_rf_frequency( freq ) == false)
00558             || (freq < US915_FIRST_RX1_CHANNEL)
00559             || (freq > US915_LAST_RX1_CHANNEL)
00560             || (((freq - (uint32_t) US915_FIRST_RX1_CHANNEL) % (uint32_t) US915_STEPWIDTH_RX1_CHANNEL) != 0)) {
00561 
00562         status &= 0xFE; // Channel frequency KO
00563 
00564     }
00565 
00566     // Verify datarate
00567     if (val_in_range(params->datarate, US915_RX_MIN_DATARATE, US915_RX_MAX_DATARATE) == 0) {
00568 
00569         status &= 0xFD; // Datarate KO
00570 
00571     }
00572 
00573     if ((val_in_range(params->datarate, DR_5, DR_7)) || (params->datarate > DR_13)) {
00574 
00575         status &= 0xFD; // Datarate KO
00576 
00577     }
00578 
00579     // Verify datarate offset
00580     if (val_in_range( params->dr_offset, US915_MIN_RX1_DR_OFFSET, US915_MAX_RX1_DR_OFFSET ) == 0 )
00581     {
00582         status &= 0xFB; // Rx1DrOffset range KO
00583     }
00584 
00585     return status;
00586 }
00587 
00588 int8_t LoRaPHYUS915::get_alternate_DR(uint8_t nb_trials)
00589 {
00590     int8_t datarate = 0;
00591 
00592     // Re-enable 500 kHz default channels
00593     channel_mask[4] = 0x00FF;
00594 
00595     if ((nb_trials & 0x01) == 0x01) {
00596         datarate = DR_4;
00597     } else {
00598         datarate = DR_0;
00599     }
00600 
00601     return datarate;
00602 }
00603 
00604 lorawan_status_t LoRaPHYUS915::set_next_channel(channel_selection_params_t* params,
00605                                                 uint8_t* channel, lorawan_time_t* time,
00606                                                 lorawan_time_t* aggregate_timeOff)
00607 {
00608     uint8_t nb_enabled_channels = 0;
00609     uint8_t delay_tx = 0;
00610     uint8_t enabled_channels[US915_MAX_NB_CHANNELS] = {0};
00611     lorawan_time_t next_tx_delay = 0;
00612 
00613     // Count 125kHz channels
00614     if (num_active_channels(current_channel_mask, 0, 4) == 0) {
00615         // If none of the 125 kHz Upstream channel found,
00616         // Reactivate default channels
00617         copy_channel_mask(current_channel_mask, channel_mask, 4);
00618     }
00619 
00620     // Check other channels
00621     if (params->current_datarate >= DR_4) {
00622         if ((current_channel_mask[4] & 0x00FF ) == 0) {
00623             current_channel_mask[4] = channel_mask[4];
00624         }
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->joined,
00636                                                     params->current_datarate,
00637                                                     current_channel_mask,
00638                                                     enabled_channels, &delay_tx);
00639     } else {
00640         delay_tx++;
00641         next_tx_delay = params->aggregate_timeoff - _lora_time.get_elapsed_time(params->last_aggregate_tx_time);
00642     }
00643 
00644     if (nb_enabled_channels > 0) {
00645         // We found a valid channel
00646         *channel = enabled_channels[get_random( 0, nb_enabled_channels - 1 )];
00647         // Disable the channel in the mask
00648         disable_channel(current_channel_mask, *channel, US915_MAX_NB_CHANNELS - 8);
00649 
00650         *time = 0;
00651         return LORAWAN_STATUS_OK;
00652 
00653     } else {
00654 
00655         if (delay_tx > 0) {
00656             // Delay transmission due to AggregatedTimeOff or to a band time off
00657             *time = next_tx_delay;
00658             return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
00659         }
00660 
00661         // Datarate not supported by any channel
00662         *time = 0;
00663         return LORAWAN_STATUS_NO_CHANNEL_FOUND;
00664     }
00665 }
00666 
00667 void LoRaPHYUS915::set_tx_cont_mode(cw_mode_params_t * params, uint32_t given_frequency)
00668 {
00669     (void)given_frequency;
00670 
00671     int8_t tx_power_limited = limit_tx_power(params->tx_power ,
00672                                            bands[channels[params->channel ].band].max_tx_pwr,
00673                                            params->datarate );
00674     int8_t phyTxPower = 0;
00675     uint32_t frequency = channels[params->channel ].frequency;
00676 
00677     // Calculate physical TX power
00678     phyTxPower = compute_tx_power(tx_power_limited, US915_DEFAULT_MAX_ERP, 0);
00679 
00680     _radio->lock();
00681 
00682     _radio->set_tx_continuous_wave(frequency, phyTxPower, params->timeout );
00683 
00684     _radio->unlock();
00685 }
00686 
00687 uint8_t LoRaPHYUS915::apply_DR_offset(int8_t dr, int8_t dr_offset)
00688 {
00689     int8_t datarate = datarate_offsets_US915[dr][dr_offset];
00690 
00691     if (datarate < 0) {
00692         datarate = DR_0;
00693     }
00694 
00695     return datarate;
00696 }