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