Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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_5
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()
00245 {
00246     bands[0] = AS923_BAND0 ;
00247 
00248     // Default Channels are always enabled in the channel list,
00249     // rest will be added later
00250     channels[0] = AS923_LC1 ;
00251     channels[0].band = 0;
00252     channels[1] = AS923_LC2 ;
00253     channels[1].band = 0;
00254 
00255     // Initialize the default channel mask
00256     default_channel_mask[0] = LC(1) + LC(2);
00257 
00258     // Update the channel mask list
00259     copy_channel_mask(channel_mask, default_channel_mask, AS923_CHANNEL_MASK_SIZE);
00260 
00261     // set default channels
00262     phy_params.channels.channel_list = channels;
00263     phy_params.channels.channel_list_size = AS923_MAX_NB_CHANNELS;
00264     phy_params.channels.mask = channel_mask;
00265     phy_params.channels.default_mask = default_channel_mask;
00266     phy_params.channels.mask_size = AS923_CHANNEL_MASK_SIZE;
00267 
00268     // set bands for AS923 spectrum
00269     phy_params.bands.table = (void *) bands;
00270     phy_params.bands.size = AS923_MAX_NB_BANDS;
00271 
00272     // set bandwidths available in AS923 spectrum
00273     phy_params.bandwidths.table = (void *) bandwidths_AS923 ;
00274     phy_params.bandwidths.size = 8;
00275 
00276     // set data rates available in AS923 spectrum
00277     phy_params.datarates.table = (void *) datarates_AS923 ;
00278     phy_params.datarates.size = 8;
00279 
00280     // set payload sizes with respect to data rates
00281     phy_params.payloads.table = (void *) max_payload_table;
00282     phy_params.payloads.size = 8;
00283     phy_params.payloads_with_repeater.table = (void *) max_payload_table_with_repeater;
00284     phy_params.payloads_with_repeater.size = 8;
00285 
00286     // dwell time setting, 400 ms
00287     phy_params.ul_dwell_time_setting = 1;
00288     phy_params.dl_dwell_time_setting = 1;
00289     phy_params.dwell_limit_datarate = AS923_DWELL_LIMIT_DATARATE;
00290 
00291     phy_params.duty_cycle_enabled = AS923_DUTY_CYCLE_ENABLED;
00292     phy_params.accept_tx_param_setup_req = true;
00293     phy_params.fsk_supported = true;
00294     phy_params.cflist_supported = true;
00295     phy_params.dl_channel_req_supported = true;
00296     phy_params.custom_channelplans_supported = true;
00297     phy_params.default_channel_cnt = AS923_NUMB_DEFAULT_CHANNELS;
00298     phy_params.max_channel_cnt = AS923_MAX_NB_CHANNELS;
00299     phy_params.cflist_channel_cnt = AS923_NUMB_CHANNELS_CF_LIST;
00300     phy_params.min_tx_datarate = AS923_TX_MIN_DATARATE;
00301     phy_params.max_tx_datarate = AS923_TX_MAX_DATARATE;
00302     phy_params.min_rx_datarate = AS923_RX_MIN_DATARATE;
00303     phy_params.max_rx_datarate = AS923_RX_MAX_DATARATE;
00304     phy_params.default_datarate = AS923_DEFAULT_DATARATE;
00305     phy_params.default_max_datarate = AS923_DEFAULT_MAX_DATARATE;
00306     phy_params.min_rx1_dr_offset = AS923_MIN_RX1_DR_OFFSET;
00307     phy_params.max_rx1_dr_offset = AS923_MAX_RX1_DR_OFFSET;
00308     phy_params.default_rx1_dr_offset = AS923_DEFAULT_RX1_DR_OFFSET;
00309     phy_params.min_tx_power = AS923_MIN_TX_POWER;
00310     phy_params.max_tx_power = AS923_MAX_TX_POWER;
00311     phy_params.default_tx_power = AS923_DEFAULT_TX_POWER;
00312     phy_params.default_max_eirp = AS923_DEFAULT_MAX_EIRP;
00313     phy_params.default_antenna_gain = AS923_DEFAULT_ANTENNA_GAIN;
00314     phy_params.adr_ack_limit = AS923_ADR_ACK_LIMIT;
00315     phy_params.adr_ack_delay = AS923_ADR_ACK_DELAY;
00316     phy_params.max_rx_window = AS923_MAX_RX_WINDOW;
00317     phy_params.recv_delay1 = AS923_RECEIVE_DELAY1;
00318     phy_params.recv_delay2 = AS923_RECEIVE_DELAY2;
00319     phy_params.join_channel_mask = AS923_JOIN_CHANNELS;
00320     phy_params.join_accept_delay1 = AS923_JOIN_ACCEPT_DELAY1;
00321     phy_params.join_accept_delay2 = AS923_JOIN_ACCEPT_DELAY2;
00322     phy_params.max_fcnt_gap = AS923_MAX_FCNT_GAP;
00323     phy_params.ack_timeout = AS923_ACKTIMEOUT;
00324     phy_params.ack_timeout_rnd = AS923_ACK_TIMEOUT_RND;
00325     phy_params.rx_window2_datarate = AS923_RX_WND_2_DR;
00326     phy_params.rx_window2_frequency = AS923_RX_WND_2_FREQ;
00327 }
00328 
00329 LoRaPHYAS923::~LoRaPHYAS923()
00330 {
00331 }
00332 
00333 int8_t LoRaPHYAS923::get_alternate_DR(uint8_t nb_trials)
00334 {
00335     // Only AS923_DWELL_LIMIT_DATARATE is supported
00336     return AS923_DWELL_LIMIT_DATARATE;
00337 }
00338 
00339 lorawan_status_t LoRaPHYAS923::set_next_channel(channel_selection_params_t *next_channel_prams,
00340                                                 uint8_t *channel, lorawan_time_t *time,
00341                                                 lorawan_time_t *aggregate_timeoff)
00342 {
00343     uint8_t next_channel_idx = 0;
00344     uint8_t nb_enabled_channels = 0;
00345     uint8_t delay_tx = 0;
00346     uint8_t enabled_channels[AS923_MAX_NB_CHANNELS] = { 0 };
00347     lorawan_time_t next_tx_delay = 0;
00348 
00349     if (num_active_channels(channel_mask, 0, 1) == 0) {
00350         // Reactivate default channels
00351         channel_mask[0] |= LC(1) + LC(2);
00352     }
00353 
00354     if (next_channel_prams->aggregate_timeoff <= _lora_time->get_elapsed_time(next_channel_prams->last_aggregate_tx_time)) {
00355         // Reset Aggregated time off
00356         *aggregate_timeoff = 0;
00357 
00358         // Update bands Time OFF
00359         next_tx_delay = update_band_timeoff(next_channel_prams->joined,
00360                                             next_channel_prams->dc_enabled,
00361                                             bands, AS923_MAX_NB_BANDS);
00362 
00363         // Search how many channels are enabled
00364         nb_enabled_channels = enabled_channel_count(next_channel_prams->current_datarate,
00365                                                     channel_mask,
00366                                                     enabled_channels, &delay_tx);
00367     }  else {
00368         delay_tx++;
00369         next_tx_delay = next_channel_prams->aggregate_timeoff - _lora_time->get_elapsed_time(next_channel_prams->last_aggregate_tx_time);
00370     }
00371 
00372     if (nb_enabled_channels > 0) {
00373 
00374         _radio->lock();
00375 
00376         for (uint8_t  i = 0, j = get_random(0, nb_enabled_channels - 1); i < AS923_MAX_NB_CHANNELS; i++) {
00377             next_channel_idx = enabled_channels[j];
00378             j = (j + 1) % nb_enabled_channels;
00379 
00380             // Perform carrier sense for AS923_CARRIER_SENSE_TIME
00381             // If the channel is free, we can stop the LBT mechanism
00382 
00383             if (_radio->perform_carrier_sense(MODEM_LORA,
00384                                               channels[next_channel_idx].frequency,
00385                                               AS923_RSSI_FREE_TH,
00386                                               AS923_CARRIER_SENSE_TIME) == true) {
00387                 // Free channel found
00388                 _radio->unlock();
00389                 *channel = next_channel_idx;
00390                 *time = 0;
00391                 return LORAWAN_STATUS_OK;
00392             }
00393         }
00394         _radio->unlock();
00395         return LORAWAN_STATUS_NO_FREE_CHANNEL_FOUND;
00396     } else {
00397 
00398         if (delay_tx > 0) {
00399             // Delay transmission due to AggregatedTimeOff or to a band time off
00400             *time = next_tx_delay;
00401             return LORAWAN_STATUS_DUTYCYCLE_RESTRICTED;
00402         }
00403 
00404         // Datarate not supported by any channel, restore defaults
00405         channel_mask[0] |= LC(1) + LC(2);
00406         *time = 0;
00407         return LORAWAN_STATUS_NO_CHANNEL_FOUND;
00408     }
00409 }
00410 
00411 uint8_t LoRaPHYAS923::apply_DR_offset(int8_t dr, int8_t dr_offset)
00412 {
00413     // Initialize minDr for a downlink dwell time configuration of 0
00414     int8_t min_dr = DR_0;
00415 
00416     // Update the minDR for a downlink dwell time configuration of 1
00417     if (phy_params.dl_dwell_time_setting == 1) {
00418         min_dr = AS923_DWELL_LIMIT_DATARATE;
00419     }
00420 
00421     // Apply offset formula
00422     return MIN(DR_5, MAX(min_dr, dr - rx1_dr_offset_AS923 [dr_offset]));
00423 }
00424 
00425