Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaPHYAS923.cpp Source File

LoRaPHYAS923.cpp

Go to the documentation of this file.
00001 /**
00002  *  @file LoRaPHYAS923.cpp
00003  *
00004  *  @brief Implements LoRaPHY for Asia-Pacific 923 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 "LoRaPHYAS923.h"
00033 #include "lora_phy_ds.h"
00034 
00035 /*!
00036  * Number of default channels
00037  */
00038 #define AS923_NUMB_DEFAULT_CHANNELS                 2
00039 
00040 /*!
00041  * Number of channels to apply for the CF list
00042  */
00043 #define AS923_NUMB_CHANNELS_CF_LIST                 5
00044 
00045 /*!
00046  * Minimal datarate that can be used by the node
00047  */
00048 #define AS923_TX_MIN_DATARATE                       DR_0
00049 
00050 /*!
00051  * Maximal datarate that can be used by the node
00052  */
00053 #define AS923_TX_MAX_DATARATE                       DR_7
00054 
00055 /*!
00056  * Minimal datarate that can be used by the node
00057  */
00058 #define AS923_RX_MIN_DATARATE                       DR_0
00059 
00060 /*!
00061  * Maximal datarate that can be used by the node
00062  */
00063 #define AS923_RX_MAX_DATARATE                       DR_7
00064 
00065 /*!
00066  * Default datarate used by the node
00067  */
00068 #define AS923_DEFAULT_DATARATE                      DR_2
00069 
00070 #define AS923_DEFAULT_MAX_DATARATE                  DR_7
00071 
00072 /*!
00073  * The minimum datarate which is used when the
00074  * dwell time is limited.
00075  */
00076 #define AS923_DWELL_LIMIT_DATARATE                  DR_2
00077 
00078 /*!
00079  * Minimal Rx1 receive datarate offset
00080  */
00081 #define AS923_MIN_RX1_DR_OFFSET                     0
00082 
00083 /*!
00084  * Maximal Rx1 receive datarate offset
00085  */
00086 #define AS923_MAX_RX1_DR_OFFSET                     7
00087 
00088 /*!
00089  * Default Rx1 receive datarate offset
00090  */
00091 #define AS923_DEFAULT_RX1_DR_OFFSET                 0
00092 
00093 /*!
00094  * Minimal Tx output power that can be used by the node
00095  */
00096 #define AS923_MIN_TX_POWER                          TX_POWER_7
00097 
00098 /*!
00099  * Maximal Tx output power that can be used by the node
00100  */
00101 #define AS923_MAX_TX_POWER                          TX_POWER_0
00102 
00103 /*!
00104  * Default Tx output power used by the node
00105  */
00106 #define AS923_DEFAULT_TX_POWER                      TX_POWER_0
00107 
00108 /*!
00109  * Default Max EIRP
00110  */
00111 #define AS923_DEFAULT_MAX_EIRP                      16.0f
00112 
00113 /*!
00114  * Default antenna gain
00115  */
00116 #define AS923_DEFAULT_ANTENNA_GAIN                  2.15f
00117 
00118 /*!
00119  * ADR Ack limit
00120  */
00121 #define AS923_ADR_ACK_LIMIT                         64
00122 
00123 /*!
00124  * ADR Ack delay
00125  */
00126 #define AS923_ADR_ACK_DELAY                         32
00127 
00128 /*!
00129  * Enabled or disabled the duty cycle
00130  */
00131 #define AS923_DUTY_CYCLE_ENABLED                    0
00132 
00133 /*!
00134  * Maximum RX window duration
00135  */
00136 #define AS923_MAX_RX_WINDOW                         3000
00137 
00138 /*!
00139  * Receive delay 1
00140  */
00141 #define AS923_RECEIVE_DELAY1                        1000
00142 
00143 /*!
00144  * Receive delay 2
00145  */
00146 #define AS923_RECEIVE_DELAY2                        2000
00147 
00148 /*!
00149  * Join accept delay 1
00150  */
00151 #define AS923_JOIN_ACCEPT_DELAY1                    5000
00152 
00153 /*!
00154  * Join accept delay 2
00155  */
00156 #define AS923_JOIN_ACCEPT_DELAY2                    6000
00157 
00158 /*!
00159  * Maximum frame counter gap
00160  */
00161 #define AS923_MAX_FCNT_GAP                          16384
00162 
00163 /*!
00164  * Ack timeout
00165  */
00166 #define AS923_ACKTIMEOUT                            2000
00167 
00168 /*!
00169  * Random ack timeout limits
00170  */
00171 #define AS923_ACK_TIMEOUT_RND                       1000
00172 
00173 #if ( AS923_DEFAULT_DATARATE > DR_5 )
00174 #error "A default DR higher than DR_5 may lead to connectivity loss."
00175 #endif
00176 
00177 /*!
00178  * Second reception window channel frequency definition.
00179  */
00180 #define AS923_RX_WND_2_FREQ                         923200000
00181 
00182 /*!
00183  * Second reception window channel datarate definition.
00184  */
00185 #define AS923_RX_WND_2_DR                           DR_2
00186 
00187 /*!
00188  * Band 0 definition
00189  * { DutyCycle, TxMaxPower, LastJoinTxDoneTime, LastTxDoneTime, TimeOff }
00190  */
00191 static const band_t  AS923_BAND0  = {100, AS923_MAX_TX_POWER, 0, 0, 0, 923000000, 928000000}; //  1.0 %
00192 
00193 /*!
00194  * LoRaMac default channel 1
00195  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
00196  */
00197 static const channel_params_t  AS923_LC1  = { 923200000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
00198 
00199 /*!
00200  * LoRaMac default channel 2
00201  * Channel = { Frequency [Hz], RX1 Frequency [Hz], { ( ( DrMax << 4 ) | DrMin ) }, Band }
00202  */
00203 static const channel_params_t  AS923_LC2  = { 923400000, 0, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 };
00204 
00205 /*!
00206  * LoRaMac channels which are allowed for the join procedure
00207  */
00208 #define AS923_JOIN_CHANNELS                         ( uint16_t )( LC( 1 ) | LC( 2 ) )
00209 
00210 /*!
00211  * RSSI threshold for a free channel [dBm]
00212  */
00213 #define AS923_RSSI_FREE_TH                          -85
00214 
00215 /*!
00216  * Specifies the time the node performs a carrier sense
00217  */
00218 #define AS923_CARRIER_SENSE_TIME                    6
00219 
00220 /*!
00221  * Data rates table definition
00222  */
00223 static const uint8_t datarates_AS923 []  = {12, 11, 10,  9,  8,  7, 7, 50};
00224 
00225 /*!
00226  * Bandwidths table definition in Hz
00227  */
00228 static const uint32_t bandwidths_AS923 [] = {125000, 125000, 125000, 125000, 125000, 125000, 250000, 0};
00229 
00230 #if (MBED_CONF_LORA_DWELL_TIME == 0)
00231     static  const uint8_t max_payload_table[] = {51, 51, 51, 115, 242, 242, 242, 242};
00232     static const uint8_t max_payload_table_with_repeater[] = {51, 51, 51, 115, 222, 222, 222, 222};
00233 #else
00234     // this is the default, i.e.,
00235     static const uint8_t max_payload_table[] = {0, 0, 11, 53, 125, 242, 242, 242};
00236     static const uint8_t max_payload_table_with_repeater[] =  {0, 0, 11, 53, 125, 242, 242, 242};
00237 #endif
00238 
00239 /*!
00240  * Effective datarate offsets for receive window 1.
00241  */
00242 static const int8_t rx1_dr_offset_AS923 [] = {0, 1, 2, 3, 4, 5, -1, -2};
00243 
00244 LoRaPHYAS923::LoRaPHYAS923(LoRaWANTimeHandler &lora_time)
00245     : LoRaPHY(lora_time)
00246 {
00247     bands[0] = AS923_BAND0 ;
00248 
00249     // Default Channels are always enabled in the channel list,
00250     // rest will be added later
00251     channels[0] = AS923_LC1 ;
00252     channels[1] = AS923_LC2 ;
00253 
00254     // Initialize the default channel mask
00255     default_channel_mask[0] = LC(1) + LC(2);
00256 
00257     // Update the channel mask list
00258     copy_channel_mask(channel_mask, default_channel_mask, AS923_CHANNEL_MASK_SIZE);
00259 
00260     // set default channels
00261     phy_params.channels.channel_list = channels;
00262     phy_params.channels.channel_list_size = AS923_MAX_NB_CHANNELS;
00263     phy_params.channels.mask = channel_mask;
00264     phy_params.channels.default_mask = default_channel_mask;
00265     phy_params.channels.mask_size = AS923_CHANNEL_MASK_SIZE;
00266 
00267     // set bands for AS923 spectrum
00268     phy_params.bands.table = (void *) bands;
00269     phy_params.bands.size = AS923_MAX_NB_BANDS;
00270 
00271     // set bandwidths available in AS923 spectrum
00272     phy_params.bandwidths.table = (void *) bandwidths_AS923 ;
00273     phy_params.bandwidths.size = 8;
00274 
00275     // set data rates available in AS923 spectrum
00276     phy_params.datarates.table = (void *) datarates_AS923 ;
00277     phy_params.datarates.size = 8;
00278 
00279     // set payload sizes with respect to data rates
00280     phy_params.payloads.table = (void *) max_payload_table;
00281     phy_params.payloads.size = 8;
00282     phy_params.payloads_with_repeater.table = (void *) max_payload_table_with_repeater;
00283     phy_params.payloads_with_repeater.size = 8;
00284 
00285     // dwell time setting, 400 ms
00286     phy_params.ul_dwell_time_setting = 1;
00287     phy_params.dl_dwell_time_setting = 1;
00288     phy_params.dwell_limit_datarate = AS923_DWELL_LIMIT_DATARATE;
00289 
00290     phy_params.duty_cycle_enabled = AS923_DUTY_CYCLE_ENABLED;
00291     phy_params.accept_tx_param_setup_req = true;
00292     phy_params.fsk_supported = true;
00293     phy_params.cflist_supported = true;
00294 
00295     phy_params.default_channel_cnt = AS923_NUMB_DEFAULT_CHANNELS;
00296     phy_params.max_channel_cnt = AS923_MAX_NB_CHANNELS;
00297     phy_params.cflist_channel_cnt = AS923_NUMB_CHANNELS_CF_LIST;
00298     phy_params.min_tx_datarate = AS923_TX_MIN_DATARATE;
00299     phy_params.max_tx_datarate = AS923_TX_MAX_DATARATE;
00300     phy_params.min_rx_datarate = AS923_RX_MIN_DATARATE;
00301     phy_params.max_rx_datarate = AS923_RX_MAX_DATARATE;
00302     phy_params.default_datarate = AS923_DEFAULT_DATARATE;
00303     phy_params.default_max_datarate = AS923_DEFAULT_MAX_DATARATE;
00304     phy_params.min_rx1_dr_offset = AS923_MIN_RX1_DR_OFFSET;
00305     phy_params.max_rx1_dr_offset = AS923_MAX_RX1_DR_OFFSET;
00306     phy_params.default_rx1_dr_offset = AS923_DEFAULT_RX1_DR_OFFSET;
00307     phy_params.min_tx_power = AS923_MIN_TX_POWER;
00308     phy_params.max_tx_power = AS923_MAX_TX_POWER;
00309     phy_params.default_tx_power = AS923_DEFAULT_TX_POWER;
00310     phy_params.default_max_eirp = AS923_DEFAULT_MAX_EIRP;
00311     phy_params.default_antenna_gain = AS923_DEFAULT_ANTENNA_GAIN;
00312     phy_params.adr_ack_limit = AS923_ADR_ACK_LIMIT;
00313     phy_params.adr_ack_delay = AS923_ADR_ACK_DELAY;
00314     phy_params.max_rx_window = AS923_MAX_RX_WINDOW;
00315     phy_params.recv_delay1 = AS923_RECEIVE_DELAY1;
00316     phy_params.recv_delay2 = AS923_RECEIVE_DELAY2;
00317     phy_params.join_channel_mask = AS923_JOIN_CHANNELS;
00318     phy_params.join_accept_delay1 = AS923_JOIN_ACCEPT_DELAY1;
00319     phy_params.join_accept_delay2 = AS923_JOIN_ACCEPT_DELAY2;
00320     phy_params.max_fcnt_gap = AS923_MAX_FCNT_GAP;
00321     phy_params.ack_timeout = AS923_ACKTIMEOUT;
00322     phy_params.ack_timeout_rnd = AS923_ACK_TIMEOUT_RND;
00323     phy_params.rx_window2_datarate = AS923_RX_WND_2_DR;
00324     phy_params.rx_window2_frequency = AS923_RX_WND_2_FREQ;
00325 }
00326 
00327 LoRaPHYAS923::~LoRaPHYAS923()
00328 {
00329 }
00330 
00331 int8_t LoRaPHYAS923::get_alternate_DR(uint8_t nb_trials)
00332 {
00333     // Only AS923_DWELL_LIMIT_DATARATE is supported
00334     return AS923_DWELL_LIMIT_DATARATE;
00335 }
00336 
00337 bool LoRaPHYAS923::set_next_channel(channel_selection_params_t* next_channel_prams,
00338                                     uint8_t* channel, lorawan_time_t* time,
00339                                     lorawan_time_t* aggregate_timeoff)
00340 {
00341     uint8_t next_channel_idx = 0;
00342     uint8_t nb_enabled_channels = 0;
00343     uint8_t delay_tx = 0;
00344     uint8_t enabled_channels[AS923_MAX_NB_CHANNELS] = { 0 };
00345     lorawan_time_t next_tx_delay = 0;
00346 
00347     if (num_active_channels(channel_mask, 0, 1) == 0) {
00348         // Reactivate default channels
00349         channel_mask[0] |= LC(1) + LC(2);
00350     }
00351 
00352     if (next_channel_prams->aggregate_timeoff <= _lora_time.get_elapsed_time(next_channel_prams->last_aggregate_tx_time)) {
00353         // Reset Aggregated time off
00354         *aggregate_timeoff = 0;
00355 
00356         // Update bands Time OFF
00357         next_tx_delay = update_band_timeoff(next_channel_prams->joined,
00358                                             next_channel_prams->dc_enabled,
00359                                             bands, AS923_MAX_NB_BANDS);
00360 
00361         // Search how many channels are enabled
00362         nb_enabled_channels = enabled_channel_count(next_channel_prams->joined,
00363                                                     next_channel_prams->current_datarate,
00364                                                     channel_mask,
00365                                                     enabled_channels, &delay_tx);
00366     }  else {
00367         delay_tx++;
00368         next_tx_delay = next_channel_prams->aggregate_timeoff - _lora_time.get_elapsed_time(next_channel_prams->last_aggregate_tx_time);
00369     }
00370 
00371     if (nb_enabled_channels > 0) {
00372 
00373         _radio->lock();
00374 
00375         for (uint8_t  i = 0, j = get_random(0, nb_enabled_channels - 1); i < AS923_MAX_NB_CHANNELS; i++) {
00376             next_channel_idx = enabled_channels[j];
00377             j = ( j + 1 ) % nb_enabled_channels;
00378 
00379             // Perform carrier sense for AS923_CARRIER_SENSE_TIME
00380             // If the channel is free, we can stop the LBT mechanism
00381 
00382             if (_radio->perform_carrier_sense(MODEM_LORA,
00383                                               channels[next_channel_idx].frequency,
00384                                               AS923_RSSI_FREE_TH,
00385                                               AS923_CARRIER_SENSE_TIME) == true) {
00386                 // Free channel found
00387                 _radio->unlock();
00388                 *channel = next_channel_idx;
00389                 *time = 0;
00390                 return true;
00391             }
00392         }
00393         _radio->unlock();
00394         return false;
00395     } else {
00396 
00397         if (delay_tx > 0) {
00398             // Delay transmission due to AggregatedTimeOff or to a band time off
00399             *time = next_tx_delay;
00400             return true;
00401         }
00402 
00403         // Datarate not supported by any channel, restore defaults
00404         channel_mask[0] |= LC( 1 ) + LC( 2 );
00405         *time = 0;
00406         return false;
00407     }
00408 }
00409 
00410 uint8_t LoRaPHYAS923::apply_DR_offset(int8_t dr, int8_t dr_offset)
00411 {
00412     // Initialize minDr for a downlink dwell time configuration of 0
00413     int8_t min_dr = DR_0;
00414 
00415     // Update the minDR for a downlink dwell time configuration of 1
00416     if (phy_params.dl_dwell_time_setting == 1) {
00417         min_dr = AS923_DWELL_LIMIT_DATARATE;
00418     }
00419 
00420     // Apply offset formula
00421     return MIN(DR_5, MAX(min_dr, dr - rx1_dr_offset_AS923 [dr_offset]));
00422 }
00423 
00424