p2p with rssi value

Dependents:   Lora_SX1272_serial_apr29-rssi

Committer:
afzalsamira
Date:
Thu Jul 08 20:12:14 2021 +0000
Revision:
7:e99757b7c421
Parent:
6:d7c7e731da3a
rssi is added and it works correctly

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cdupaty 0:d974bcee4f69 1 /*
cdupaty 0:d974bcee4f69 2 * Library for LoRa 868 / 915MHz SX1272 LoRa module
cdupaty 0:d974bcee4f69 3 *
cdupaty 0:d974bcee4f69 4 * Copyright (C) Libelium Comunicaciones Distribuidas S.L.
cdupaty 0:d974bcee4f69 5 * http://www.libelium.com
cdupaty 0:d974bcee4f69 6 *
cdupaty 0:d974bcee4f69 7 * This program is free software: you can redistribute it and/or modify
cdupaty 0:d974bcee4f69 8 * it under the terms of the GNU General Public License as published by
cdupaty 0:d974bcee4f69 9 * the Free Software Foundation, either version 3 of the License, or
cdupaty 0:d974bcee4f69 10 * (at your option) any later version.
cdupaty 0:d974bcee4f69 11 *
cdupaty 0:d974bcee4f69 12 * This program is distributed in the hope that it will be useful,
cdupaty 0:d974bcee4f69 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cdupaty 0:d974bcee4f69 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cdupaty 0:d974bcee4f69 15 * GNU General Public License for more details.
cdupaty 0:d974bcee4f69 16 *
cdupaty 0:d974bcee4f69 17 * You should have received a copy of the GNU General Public License
cdupaty 0:d974bcee4f69 18 * along with this program. If not, see http://www.gnu.org/licenses/.
cdupaty 0:d974bcee4f69 19 *
cdupaty 0:d974bcee4f69 20 * Version: 1.1
cdupaty 0:d974bcee4f69 21 * Design: David Gascón
cdupaty 0:d974bcee4f69 22 * Implementation: Covadonga Albiñana & Victor Boria
cdupaty 0:d974bcee4f69 23 */
cdupaty 0:d974bcee4f69 24
cdupaty 0:d974bcee4f69 25 //**********************************************************************/
cdupaty 0:d974bcee4f69 26 // Includes
cdupaty 0:d974bcee4f69 27 //**********************************************************************/
cdupaty 0:d974bcee4f69 28 #include "mbed.h"
cdupaty 0:d974bcee4f69 29 #include "SX1272.h"
cdupaty 0:d974bcee4f69 30 #include <SPI.h>
afzalsamira 7:e99757b7c421 31 #include "serial.h"
afzalsamira 7:e99757b7c421 32
cdupaty 0:d974bcee4f69 33
cdupaty 0:d974bcee4f69 34 /* CHANGE LOGS by C. Pham
cdupaty 0:d974bcee4f69 35 * June, 22th, 2017
cdupaty 0:d974bcee4f69 36 * - setPowerDBM(uint8_t dbm) calls setPower('X') when dbm is set to 20
cdupaty 0:d974bcee4f69 37 * Apr, 21th, 2017
cdupaty 0:d974bcee4f69 38 * - change the way timeout are detected: exitTime=millis()+(unsigned long)wait; then millis() < exitTime;
cdupaty 0:d974bcee4f69 39 * Mar, 26th, 2017
cdupaty 0:d974bcee4f69 40 * - insert delay(100) before setting radio module to sleep mode. Remove unstability issue
cdupaty 0:d974bcee4f69 41 * - (proposed by escyes - https://github.com/CongducPham/LowCostLoRaGw/issues/53#issuecomment-289237532)
cdupaty 0:d974bcee4f69 42 * Jan, 11th, 2017
cdupaty 0:d974bcee4f69 43 * - fix bug in getRSSIpacket() when SNR < 0 thanks to John Rohde from Aarhus University
cdupaty 0:d974bcee4f69 44 * Dec, 17th, 2016
cdupaty 0:d974bcee4f69 45 * - fix bug making -DPABOOST in radio.makefile inoperant
cdupaty 0:d974bcee4f69 46 * Dec, 1st, 2016
cdupaty 0:d974bcee4f69 47 * - add RSSI computation while performing CAD with doCAD()
cdupaty 0:d974bcee4f69 48 * - WARNING: the SX1272 lib for gateway (Raspberry) does not have this functionality
cdupaty 0:d974bcee4f69 49 * Nov, 26th, 2016
cdupaty 0:d974bcee4f69 50 * - add preliminary support for ToA limitation
cdupaty 0:d974bcee4f69 51 * - when in "production" mode, uncomment #define LIMIT_TOA
cdupaty 0:d974bcee4f69 52 * Nov, 16th, 2016
cdupaty 0:d974bcee4f69 53 * - provide better power management mechanisms
cdupaty 0:d974bcee4f69 54 * - manage PA_BOOST and dBm setting
cdupaty 0:d974bcee4f69 55 * Jan, 23rd, 2016
cdupaty 0:d974bcee4f69 56 * - the packet format at transmission does not use the original Libelium format anymore
cdupaty 0:d974bcee4f69 57 * * the retry field is removed therefore all operations using retry will probably not work well, not tested though
cdupaty 0:d974bcee4f69 58 * - therefore DO NOT use sendPacketTimeoutACKRetries()
cdupaty 0:d974bcee4f69 59 * - the reason is that we do not want to have a reserved byte after the payload
cdupaty 0:d974bcee4f69 60 * * the length field is removed because it is much better to get the packet length at the reception side
cdupaty 0:d974bcee4f69 61 * * after the dst field, we inserted a packet type field to better identify the packet type: DATA, ACK, encryption, app key,...
cdupaty 0:d974bcee4f69 62 * - the format is now dst(1B) ptype(1B) src(1B) seq(1B) payload(xB)
cdupaty 0:d974bcee4f69 63 * - ptype is decomposed in 2 parts type(4bits) flags(4bits)
cdupaty 0:d974bcee4f69 64 * - type can take current value of DATA=0001 and ACK=0010
cdupaty 0:d974bcee4f69 65 * - the flags are from left to right: ack_requested|encrypted|with_appkey|is_binary
cdupaty 0:d974bcee4f69 66 * - ptype can be set with setPacketType(), see constant defined in SX1272.h
cdupaty 0:d974bcee4f69 67 * - the header length is then 4 instead of 5
cdupaty 0:d974bcee4f69 68 * Jan, 16th, 2016
cdupaty 0:d974bcee4f69 69 * - add support for SX1276, automatic detect
cdupaty 0:d974bcee4f69 70 * - add LF/HF calibaration copied from LoRaMAC-Node. Don't know if it is really necessary though
cdupaty 0:d974bcee4f69 71 * - change various radio settings
cdupaty 0:d974bcee4f69 72 * Dec, 10th, 2015
cdupaty 0:d974bcee4f69 73 * - add SyncWord for test with simple LoRaWAN
cdupaty 0:d974bcee4f69 74 * - add mode 11 that have BW=125, CR=4/5, SF=7 on channel 868.1MHz
cdupaty 0:d974bcee4f69 75 * - use following in your code if (loraMode==11) { e = sx1272.setChannel(CH_18_868); }
cdupaty 0:d974bcee4f69 76 * Nov, 13th, 2015
cdupaty 0:d974bcee4f69 77 * - add CarrierSense() to perform some Listen Before Talk procedure
cdupaty 0:d974bcee4f69 78 * - add dynamic ACK suport
cdupaty 0:d974bcee4f69 79 * - compile with W_REQUESTED_ACK, retry field is used to indicate at the receiver
cdupaty 0:d974bcee4f69 80 * that an ACK should be sent
cdupaty 0:d974bcee4f69 81 * - receiveWithTimeout() has been modified to send an ACK if retry is 1
cdupaty 0:d974bcee4f69 82 * - at sender side, sendPacketTimeoutACK() has been modified to indicate
cdupaty 0:d974bcee4f69 83 * whether retry should be set to 1 or not in setPacket()
cdupaty 0:d974bcee4f69 84 * - receiver should always use receiveWithTimeout() while sender decides to use
cdupaty 0:d974bcee4f69 85 * sendPacketTimeout() or sendPacketTimeoutACK()
cdupaty 0:d974bcee4f69 86 * Jun, 2015
cdupaty 0:d974bcee4f69 87 * - Add time on air computation and CAD features
cdupaty 0:d974bcee4f69 88 */
cdupaty 0:d974bcee4f69 89
cdupaty 0:d974bcee4f69 90 // Added by C. Pham
cdupaty 0:d974bcee4f69 91 // based on SIFS=3CAD
cdupaty 0:d974bcee4f69 92 uint8_t sx1272_SIFS_value[11]={0, 183, 94, 44, 47, 23, 24, 12, 12, 7, 4};
cdupaty 0:d974bcee4f69 93 uint8_t sx1272_CAD_value[11]={0, 62, 31, 16, 16, 8, 9, 5, 3, 1, 1};
cdupaty 0:d974bcee4f69 94
cdupaty 0:d974bcee4f69 95 //#define LIMIT_TOA
cdupaty 0:d974bcee4f69 96 // 0.1% for testing
cdupaty 0:d974bcee4f69 97 //#define MAX_DUTY_CYCLE_PER_HOUR 3600L
cdupaty 0:d974bcee4f69 98 // 1%, regular mode
cdupaty 0:d974bcee4f69 99 #define MAX_DUTY_CYCLE_PER_HOUR 36000L
cdupaty 0:d974bcee4f69 100 // normally 1 hour, set to smaller value for testing
cdupaty 0:d974bcee4f69 101 #define DUTYCYCLE_DURATION 3600000L
cdupaty 0:d974bcee4f69 102 // 4 min for testing
cdupaty 0:d974bcee4f69 103 //#define DUTYCYCLE_DURATION 240000L
cdupaty 0:d974bcee4f69 104
cdupaty 0:d974bcee4f69 105 // end
cdupaty 0:d974bcee4f69 106
cdupaty 0:d974bcee4f69 107 //ajoute par C.DUPATY
cdupaty 0:d974bcee4f69 108 //Serial pc(USBTX, USBRX); // tx, rx
cdupaty 1:5d57c6a92509 109 // config pour SX1272MB2xAS sur NUCLEO-L073RZ
marcoantonioara 3:82630593359c 110
marcoantonioara 3:82630593359c 111
marcoantonioara 3:82630593359c 112 #define SX1272_debug_mode 0
marcoantonioara 3:82630593359c 113
marcoantonioara 3:82630593359c 114
marcoantonioara 3:82630593359c 115 //Coragem pins
marcoantonioara 3:82630593359c 116 SPI spi(P0_4,P0_6,P0_8);; // PA_7, PA_6, PA_5
marcoantonioara 3:82630593359c 117 DigitalOut ss(P0_26); //(PB_6)
marcoantonioara 3:82630593359c 118 DigitalOut rst(P1_15);
marcoantonioara 3:82630593359c 119
cdupaty 0:d974bcee4f69 120
cdupaty 0:d974bcee4f69 121 //**********************************************************************/
cdupaty 0:d974bcee4f69 122 // Public functions.
cdupaty 0:d974bcee4f69 123 //**********************************************************************/
cdupaty 0:d974bcee4f69 124
cdupaty 0:d974bcee4f69 125 SX1272::SX1272()
cdupaty 0:d974bcee4f69 126 {
cdupaty 0:d974bcee4f69 127 //ajoute par C.DUPATY
cdupaty 0:d974bcee4f69 128 us_ticker_init();
cdupaty 0:d974bcee4f69 129 srand(time(NULL));
cdupaty 0:d974bcee4f69 130 // ajoute parC.Dupaty
cdupaty 0:d974bcee4f69 131 spi.format(8,0); // spi 8 bits mode 0
cdupaty 0:d974bcee4f69 132 spi.frequency(2000000); // spi clock 200KHz a l origine
cdupaty 0:d974bcee4f69 133
cdupaty 0:d974bcee4f69 134 // Initialize class variables
cdupaty 0:d974bcee4f69 135 _bandwidth = BW_125;
cdupaty 0:d974bcee4f69 136 _codingRate = CR_5;
cdupaty 0:d974bcee4f69 137 _spreadingFactor = SF_7;
afzalsamira 6:d7c7e731da3a 138 _channel = CH_07_900;
cdupaty 0:d974bcee4f69 139 _header = HEADER_ON;
cdupaty 0:d974bcee4f69 140 _CRC = CRC_OFF;
cdupaty 0:d974bcee4f69 141 _modem = FSK;
cdupaty 0:d974bcee4f69 142 _power = 15;
cdupaty 0:d974bcee4f69 143 _packetNumber = 0;
cdupaty 0:d974bcee4f69 144 _reception = CORRECT_PACKET;
cdupaty 0:d974bcee4f69 145 _retries = 0;
cdupaty 0:d974bcee4f69 146 // added by C. Pham
cdupaty 0:d974bcee4f69 147 _defaultSyncWord=0x12;
marcoantonioara 3:82630593359c 148 _rawFormat=true;//____________________________________________________
cdupaty 0:d974bcee4f69 149 _extendedIFS=true;
cdupaty 0:d974bcee4f69 150 _RSSIonSend=true;
cdupaty 0:d974bcee4f69 151 // disabled by default
cdupaty 0:d974bcee4f69 152 _enableCarrierSense=false;
cdupaty 0:d974bcee4f69 153 // DIFS by default
cdupaty 0:d974bcee4f69 154 _send_cad_number=9;
cdupaty 0:d974bcee4f69 155 #ifdef PABOOST
cdupaty 0:d974bcee4f69 156 _needPABOOST=true;
cdupaty 0:d974bcee4f69 157 #else
cdupaty 0:d974bcee4f69 158 _needPABOOST=false;
cdupaty 0:d974bcee4f69 159 #endif
cdupaty 0:d974bcee4f69 160 _limitToA=false;
cdupaty 0:d974bcee4f69 161 _startToAcycle=millis();
cdupaty 0:d974bcee4f69 162 _remainingToA=MAX_DUTY_CYCLE_PER_HOUR;
cdupaty 0:d974bcee4f69 163 _endToAcycle=_startToAcycle+DUTYCYCLE_DURATION;
cdupaty 0:d974bcee4f69 164 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 165 _requestACK = 0;
cdupaty 0:d974bcee4f69 166 #endif
cdupaty 0:d974bcee4f69 167 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 168 _my_netkey[0] = net_key_0;
cdupaty 0:d974bcee4f69 169 _my_netkey[1] = net_key_1;
cdupaty 0:d974bcee4f69 170 #endif
cdupaty 0:d974bcee4f69 171 // end
cdupaty 0:d974bcee4f69 172 _maxRetries = 3;
cdupaty 0:d974bcee4f69 173 packet_sent.retry = _retries;
cdupaty 0:d974bcee4f69 174 };
cdupaty 0:d974bcee4f69 175
cdupaty 0:d974bcee4f69 176 // added by C. Pham
cdupaty 0:d974bcee4f69 177 // copied from LoRaMAC-Node
cdupaty 0:d974bcee4f69 178 /*!
cdupaty 0:d974bcee4f69 179 * Performs the Rx chain calibration for LF and HF bands
cdupaty 0:d974bcee4f69 180 * \remark Must be called just after the reset so all registers are at their
cdupaty 0:d974bcee4f69 181 * default values
cdupaty 0:d974bcee4f69 182 */
cdupaty 0:d974bcee4f69 183 void SX1272::RxChainCalibration()
cdupaty 0:d974bcee4f69 184 {
cdupaty 0:d974bcee4f69 185 if (_board==SX1276Chip) {
cdupaty 0:d974bcee4f69 186
cdupaty 0:d974bcee4f69 187 printf("SX1276 LF/HF calibration\r");
cdupaty 0:d974bcee4f69 188
cdupaty 0:d974bcee4f69 189 // Cut the PA just in case, RFO output, power = -1 dBm
cdupaty 0:d974bcee4f69 190 writeRegister( REG_PA_CONFIG, 0x00 );
cdupaty 0:d974bcee4f69 191
cdupaty 0:d974bcee4f69 192 // Launch Rx chain calibration for LF band
cdupaty 0:d974bcee4f69 193 writeRegister( REG_IMAGE_CAL, ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START );
cdupaty 0:d974bcee4f69 194 while( ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
cdupaty 0:d974bcee4f69 195 {
cdupaty 0:d974bcee4f69 196 }
cdupaty 0:d974bcee4f69 197
cdupaty 0:d974bcee4f69 198 // Sets a Frequency in HF band
afzalsamira 6:d7c7e731da3a 199 setChannel(CH_07_900);
cdupaty 0:d974bcee4f69 200
cdupaty 0:d974bcee4f69 201 // Launch Rx chain calibration for HF band
cdupaty 0:d974bcee4f69 202 writeRegister( REG_IMAGE_CAL, ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START );
cdupaty 0:d974bcee4f69 203 while( ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
cdupaty 0:d974bcee4f69 204 {
cdupaty 0:d974bcee4f69 205 }
cdupaty 0:d974bcee4f69 206 }
cdupaty 0:d974bcee4f69 207 }
cdupaty 0:d974bcee4f69 208
cdupaty 0:d974bcee4f69 209
cdupaty 0:d974bcee4f69 210 /*
cdupaty 0:d974bcee4f69 211 Function: Sets the module ON.
cdupaty 0:d974bcee4f69 212 Returns: uint8_t setLORA state
cdupaty 0:d974bcee4f69 213 */
marcoantonioara 3:82630593359c 214 //uint8_t SX1272::Read()
marcoantonioara 3:82630593359c 215 //readRegister(
cdupaty 0:d974bcee4f69 216 uint8_t SX1272::ON()
cdupaty 0:d974bcee4f69 217 {
cdupaty 0:d974bcee4f69 218 uint8_t state = 2;
cdupaty 0:d974bcee4f69 219
cdupaty 0:d974bcee4f69 220 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 221 printf("\n");
cdupaty 0:d974bcee4f69 222 printf("Starting 'ON'\n");
cdupaty 0:d974bcee4f69 223 #endif
cdupaty 0:d974bcee4f69 224
marcoantonioara 3:82630593359c 225
cdupaty 0:d974bcee4f69 226 // Powering the module
cdupaty 0:d974bcee4f69 227 // pinMode(SX1272_SS,OUTPUT);
cdupaty 0:d974bcee4f69 228 // digitalWrite(SX1272_SS,HIGH);
cdupaty 0:d974bcee4f69 229 ss=1;
cdupaty 0:d974bcee4f69 230 wait_ms(100);
cdupaty 0:d974bcee4f69 231
cdupaty 0:d974bcee4f69 232 //#define USE_SPI_SETTINGS
cdupaty 0:d974bcee4f69 233 /*
cdupaty 0:d974bcee4f69 234 #ifdef USE_SPI_SETTINGS
cdupaty 0:d974bcee4f69 235 SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
cdupaty 0:d974bcee4f69 236 #else
cdupaty 0:d974bcee4f69 237 //Configure the MISO, MOSI, CS, SPCR.
cdupaty 0:d974bcee4f69 238 SPI.begin();
cdupaty 0:d974bcee4f69 239 //Set Most significant bit first
cdupaty 0:d974bcee4f69 240 SPI.setBitOrder(MSBFIRST);
cdupaty 0:d974bcee4f69 241 #ifdef _VARIANT_ARDUINO_DUE_X_
cdupaty 0:d974bcee4f69 242 // for the DUE, set to 4MHz
cdupaty 0:d974bcee4f69 243 SPI.setClockDivider(42);
cdupaty 0:d974bcee4f69 244 #else
cdupaty 0:d974bcee4f69 245 // for the MEGA, set to 2MHz
cdupaty 0:d974bcee4f69 246 SPI.setClockDivider(SPI_CLOCK_DIV8);
cdupaty 0:d974bcee4f69 247 #endif
cdupaty 0:d974bcee4f69 248 //Set data mode
cdupaty 0:d974bcee4f69 249 SPI.setDataMode(SPI_MODE0);
cdupaty 0:d974bcee4f69 250 #endif
cdupaty 0:d974bcee4f69 251 */
cdupaty 0:d974bcee4f69 252 wait_ms(100);
cdupaty 0:d974bcee4f69 253
cdupaty 0:d974bcee4f69 254 /* // added by C. Pham
cdupaty 0:d974bcee4f69 255 pinMode(SX1272_RST,OUTPUT);
cdupaty 0:d974bcee4f69 256 digitalWrite(SX1272_RST,HIGH);
cdupaty 0:d974bcee4f69 257 wait_ms(100);
cdupaty 0:d974bcee4f69 258 digitalWrite(SX1272_RST,LOW);
cdupaty 0:d974bcee4f69 259 wait_ms(100);
cdupaty 0:d974bcee4f69 260 */
cdupaty 0:d974bcee4f69 261
cdupaty 0:d974bcee4f69 262 rst=1;
cdupaty 0:d974bcee4f69 263 wait_ms(100);
cdupaty 0:d974bcee4f69 264 rst=0;
cdupaty 0:d974bcee4f69 265
cdupaty 0:d974bcee4f69 266 // from single_chan_pkt_fwd by Thomas Telkamp
marcoantonioara 3:82630593359c 267
marcoantonioara 3:82630593359c 268 //__________________________lendo todos os registradores________________________
marcoantonioara 3:82630593359c 269 // for(char i=0;i<100;i++){
marcoantonioara 3:82630593359c 270 // int value =readRegister(i);
marcoantonioara 3:82630593359c 271 //// printf("reg%d =%d ",i,value);
marcoantonioara 3:82630593359c 272 // }
marcoantonioara 3:82630593359c 273 readRegister(0);//evitar falsos positivos
cdupaty 0:d974bcee4f69 274 uint8_t version = readRegister(REG_VERSION);
cdupaty 0:d974bcee4f69 275
marcoantonioara 3:82630593359c 276 if (version == 0x22) { //sx1272 ?
cdupaty 0:d974bcee4f69 277 // sx1272
marcoantonioara 3:82630593359c 278 printf("\nSX1272 detected, starting\n");
cdupaty 0:d974bcee4f69 279 _board = SX1272Chip;
marcoantonioara 3:82630593359c 280 }
marcoantonioara 3:82630593359c 281 else if (version ==0x12) {// sx1276?
marcoantonioara 3:82630593359c 282 printf("SX1276 detected, starting\n");
marcoantonioara 3:82630593359c 283 _board = SX1276Chip;
marcoantonioara 3:82630593359c 284 }
marcoantonioara 3:82630593359c 285 else {
marcoantonioara 3:82630593359c 286 // rst=0;
marcoantonioara 3:82630593359c 287 // wait_ms(100);
marcoantonioara 3:82630593359c 288 // rst=1;
marcoantonioara 3:82630593359c 289 // wait_ms(100);
marcoantonioara 3:82630593359c 290
marcoantonioara 3:82630593359c 291 while(1){
marcoantonioara 3:82630593359c 292 printf("Unrecognized transceiver\n");
marcoantonioara 3:82630593359c 293 wait_ms(2000);
marcoantonioara 3:82630593359c 294 }
marcoantonioara 3:82630593359c 295 }
marcoantonioara 3:82630593359c 296
cdupaty 0:d974bcee4f69 297
cdupaty 0:d974bcee4f69 298 // added by C. Pham
cdupaty 0:d974bcee4f69 299 RxChainCalibration();
cdupaty 0:d974bcee4f69 300
cdupaty 0:d974bcee4f69 301 setMaxCurrent(0x1B);
cdupaty 0:d974bcee4f69 302 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 303 printf("## Setting ON with maximum current supply ##");
cdupaty 0:d974bcee4f69 304 printf("\n");
cdupaty 0:d974bcee4f69 305 #endif
cdupaty 0:d974bcee4f69 306
cdupaty 0:d974bcee4f69 307 // set LoRa mode
cdupaty 0:d974bcee4f69 308 state = setLORA();
cdupaty 0:d974bcee4f69 309
cdupaty 0:d974bcee4f69 310 // Added by C. Pham for ToA computation
cdupaty 0:d974bcee4f69 311 getPreambleLength();
cdupaty 0:d974bcee4f69 312 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 313 //#if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 314 printf("## SX1272 layer has net key##");
cdupaty 0:d974bcee4f69 315 //#endif
cdupaty 0:d974bcee4f69 316 #endif
cdupaty 0:d974bcee4f69 317
cdupaty 0:d974bcee4f69 318 #ifdef W_INITIALIZATION
cdupaty 0:d974bcee4f69 319 // CAUTION
cdupaty 0:d974bcee4f69 320 // doing initialization as proposed by Libelium seems not to work for the SX1276
cdupaty 0:d974bcee4f69 321 // so we decided to leave the default value of the SX127x, then configure the radio when
cdupaty 0:d974bcee4f69 322 // setting to LoRa mode
cdupaty 0:d974bcee4f69 323
cdupaty 0:d974bcee4f69 324 //Set initialization values
cdupaty 0:d974bcee4f69 325 writeRegister(0x0,0x0);
cdupaty 0:d974bcee4f69 326 // comment by C. Pham
cdupaty 0:d974bcee4f69 327 // still valid for SX1276
cdupaty 0:d974bcee4f69 328 writeRegister(0x1,0x81);
cdupaty 0:d974bcee4f69 329 // end
cdupaty 0:d974bcee4f69 330 writeRegister(0x2,0x1A);
cdupaty 0:d974bcee4f69 331 writeRegister(0x3,0xB);
cdupaty 0:d974bcee4f69 332 writeRegister(0x4,0x0);
cdupaty 0:d974bcee4f69 333 writeRegister(0x5,0x52);
cdupaty 0:d974bcee4f69 334 writeRegister(0x6,0xD8);
cdupaty 0:d974bcee4f69 335 writeRegister(0x7,0x99);
cdupaty 0:d974bcee4f69 336 writeRegister(0x8,0x99);
cdupaty 0:d974bcee4f69 337 // modified by C. Pham
cdupaty 0:d974bcee4f69 338 // added by C. Pham
cdupaty 0:d974bcee4f69 339 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 340 // RFIO_pin RFU OutputPower
cdupaty 0:d974bcee4f69 341 // 0 000 0000
marcoantonioara 3:82630593359c 342 writeRegister(0x9,0x8F);// was 0, 8F gives max outputpower
cdupaty 0:d974bcee4f69 343 else
cdupaty 0:d974bcee4f69 344 // RFO_pin MaxP OutputPower
cdupaty 0:d974bcee4f69 345 // 0 100 1111
cdupaty 0:d974bcee4f69 346 // set MaxPower to 0x4 and OutputPower to 0
cdupaty 0:d974bcee4f69 347 writeRegister(0x9,0x40);
cdupaty 0:d974bcee4f69 348
cdupaty 0:d974bcee4f69 349 writeRegister(0xA,0x9);
cdupaty 0:d974bcee4f69 350 writeRegister(0xB,0x3B);
cdupaty 0:d974bcee4f69 351
cdupaty 0:d974bcee4f69 352 // comment by C. Pham
cdupaty 0:d974bcee4f69 353 // still valid for SX1276
cdupaty 0:d974bcee4f69 354 writeRegister(0xC,0x23);
cdupaty 0:d974bcee4f69 355
cdupaty 0:d974bcee4f69 356 // REG_RX_CONFIG
cdupaty 0:d974bcee4f69 357 writeRegister(0xD,0x1);
cdupaty 0:d974bcee4f69 358
cdupaty 0:d974bcee4f69 359 writeRegister(0xE,0x80);
cdupaty 0:d974bcee4f69 360 writeRegister(0xF,0x0);
cdupaty 0:d974bcee4f69 361 writeRegister(0x10,0x0);
cdupaty 0:d974bcee4f69 362 writeRegister(0x11,0x0);
cdupaty 0:d974bcee4f69 363 writeRegister(0x12,0x0);
cdupaty 0:d974bcee4f69 364 writeRegister(0x13,0x0);
cdupaty 0:d974bcee4f69 365 writeRegister(0x14,0x0);
cdupaty 0:d974bcee4f69 366 writeRegister(0x15,0x0);
cdupaty 0:d974bcee4f69 367 writeRegister(0x16,0x0);
cdupaty 0:d974bcee4f69 368 writeRegister(0x17,0x0);
cdupaty 0:d974bcee4f69 369 writeRegister(0x18,0x10);
cdupaty 0:d974bcee4f69 370 writeRegister(0x19,0x0);
cdupaty 0:d974bcee4f69 371 writeRegister(0x1A,0x0);
cdupaty 0:d974bcee4f69 372 writeRegister(0x1B,0x0);
cdupaty 0:d974bcee4f69 373 writeRegister(0x1C,0x0);
cdupaty 0:d974bcee4f69 374
cdupaty 0:d974bcee4f69 375 // added by C. Pham
cdupaty 0:d974bcee4f69 376 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 377 // comment by C. Pham
cdupaty 0:d974bcee4f69 378 // 0x4A = 01 001 0 1 0
cdupaty 0:d974bcee4f69 379 // BW=250 CR=4/5 ImplicitH_off RxPayloadCrcOn_on LowDataRateOptimize_off
cdupaty 0:d974bcee4f69 380 writeRegister(0x1D,0x4A);
cdupaty 0:d974bcee4f69 381 // 1001 0 1 11
cdupaty 0:d974bcee4f69 382 // SF=9 TxContinuous_off AgcAutoOn SymbTimeOut
cdupaty 0:d974bcee4f69 383 writeRegister(0x1E,0x97);
cdupaty 0:d974bcee4f69 384 }
cdupaty 0:d974bcee4f69 385 else {
cdupaty 0:d974bcee4f69 386 // 1000 001 0
cdupaty 0:d974bcee4f69 387 // BW=250 CR=4/5 ImplicitH_off
cdupaty 0:d974bcee4f69 388 writeRegister(0x1D,0x82);
cdupaty 0:d974bcee4f69 389 // 1001 0 1 11
cdupaty 0:d974bcee4f69 390 // SF=9 TxContinuous_off RxPayloadCrcOn_on SymbTimeOut
cdupaty 0:d974bcee4f69 391 writeRegister(0x1E,0x97);
cdupaty 0:d974bcee4f69 392 }
cdupaty 0:d974bcee4f69 393 // end
cdupaty 0:d974bcee4f69 394
cdupaty 0:d974bcee4f69 395 writeRegister(0x1F,0xFF);
cdupaty 0:d974bcee4f69 396 writeRegister(0x20,0x0);
cdupaty 0:d974bcee4f69 397 writeRegister(0x21,0x8);
cdupaty 0:d974bcee4f69 398 writeRegister(0x22,0xFF);
cdupaty 0:d974bcee4f69 399 writeRegister(0x23,0xFF);
cdupaty 0:d974bcee4f69 400 writeRegister(0x24,0x0);
cdupaty 0:d974bcee4f69 401 writeRegister(0x25,0x0);
cdupaty 0:d974bcee4f69 402
cdupaty 0:d974bcee4f69 403 // added by C. Pham
cdupaty 0:d974bcee4f69 404 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 405 writeRegister(0x26,0x0);
cdupaty 0:d974bcee4f69 406 else
cdupaty 0:d974bcee4f69 407 // 0000 0 1 00
cdupaty 0:d974bcee4f69 408 // reserved LowDataRateOptimize_off AgcAutoOn reserved
cdupaty 0:d974bcee4f69 409 writeRegister(0x26,0x04);
cdupaty 0:d974bcee4f69 410
cdupaty 0:d974bcee4f69 411 // REG_SYNC_CONFIG
cdupaty 0:d974bcee4f69 412 writeRegister(0x27,0x0);
cdupaty 0:d974bcee4f69 413
cdupaty 0:d974bcee4f69 414 writeRegister(0x28,0x0);
cdupaty 0:d974bcee4f69 415 writeRegister(0x29,0x0);
cdupaty 0:d974bcee4f69 416 writeRegister(0x2A,0x0);
cdupaty 0:d974bcee4f69 417 writeRegister(0x2B,0x0);
cdupaty 0:d974bcee4f69 418 writeRegister(0x2C,0x0);
cdupaty 0:d974bcee4f69 419 writeRegister(0x2D,0x50);
cdupaty 0:d974bcee4f69 420 writeRegister(0x2E,0x14);
cdupaty 0:d974bcee4f69 421 writeRegister(0x2F,0x40);
cdupaty 0:d974bcee4f69 422 writeRegister(0x30,0x0);
cdupaty 0:d974bcee4f69 423 writeRegister(0x31,0x3);
cdupaty 0:d974bcee4f69 424 writeRegister(0x32,0x5);
cdupaty 0:d974bcee4f69 425 writeRegister(0x33,0x27);
cdupaty 0:d974bcee4f69 426 writeRegister(0x34,0x1C);
cdupaty 0:d974bcee4f69 427 writeRegister(0x35,0xA);
cdupaty 0:d974bcee4f69 428 writeRegister(0x36,0x0);
cdupaty 0:d974bcee4f69 429 writeRegister(0x37,0xA);
cdupaty 0:d974bcee4f69 430 writeRegister(0x38,0x42);
cdupaty 0:d974bcee4f69 431 writeRegister(0x39,0x12);
cdupaty 0:d974bcee4f69 432 //writeRegister(0x3A,0x65);
cdupaty 0:d974bcee4f69 433 //writeRegister(0x3B,0x1D);
cdupaty 0:d974bcee4f69 434 //writeRegister(0x3C,0x1);
cdupaty 0:d974bcee4f69 435 //writeRegister(0x3D,0xA1);
cdupaty 0:d974bcee4f69 436 //writeRegister(0x3E,0x0);
cdupaty 0:d974bcee4f69 437 //writeRegister(0x3F,0x0);
cdupaty 0:d974bcee4f69 438 //writeRegister(0x40,0x0);
cdupaty 0:d974bcee4f69 439 //writeRegister(0x41,0x0);
cdupaty 0:d974bcee4f69 440 // commented by C. Pham
cdupaty 0:d974bcee4f69 441 // since now we handle also the SX1276
cdupaty 0:d974bcee4f69 442 //writeRegister(0x42,0x22);
cdupaty 0:d974bcee4f69 443 #endif
cdupaty 0:d974bcee4f69 444 // added by C. Pham
cdupaty 0:d974bcee4f69 445 // default sync word for non-LoRaWAN
cdupaty 0:d974bcee4f69 446 setSyncWord(_defaultSyncWord);
cdupaty 0:d974bcee4f69 447 getSyncWord();
cdupaty 0:d974bcee4f69 448 _defaultSyncWord=_syncWord;
cdupaty 0:d974bcee4f69 449
cdupaty 0:d974bcee4f69 450 #ifdef LIMIT_TOA
cdupaty 0:d974bcee4f69 451 uint16_t remainingToA=limitToA();
cdupaty 0:d974bcee4f69 452 printf("## Limit ToA ON ##");
cdupaty 0:d974bcee4f69 453 printf("cycle begins at ");
cdupaty 0:d974bcee4f69 454 Serial.print(_startToAcycle);
cdupaty 0:d974bcee4f69 455 printf(" cycle ends at ");
cdupaty 0:d974bcee4f69 456 Serial.print(_endToAcycle);
cdupaty 0:d974bcee4f69 457 printf(" remaining ToA is ");
cdupaty 0:d974bcee4f69 458 Serial.print(remainingToA);
cdupaty 0:d974bcee4f69 459 printf("\n");
cdupaty 0:d974bcee4f69 460 #endif
cdupaty 0:d974bcee4f69 461 //end
marcoantonioara 3:82630593359c 462
marcoantonioara 3:82630593359c 463
marcoantonioara 3:82630593359c 464 // printf("reg1=%d \n",readRegister(1));
cdupaty 0:d974bcee4f69 465
cdupaty 0:d974bcee4f69 466 return state;
cdupaty 0:d974bcee4f69 467 }
cdupaty 0:d974bcee4f69 468
cdupaty 0:d974bcee4f69 469 /*
cdupaty 0:d974bcee4f69 470 Function: Sets the module OFF.
cdupaty 0:d974bcee4f69 471 Returns: Nothing
cdupaty 0:d974bcee4f69 472 */
cdupaty 0:d974bcee4f69 473 void SX1272::OFF()
cdupaty 0:d974bcee4f69 474 {
cdupaty 0:d974bcee4f69 475 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 476 printf("\n");
cdupaty 0:d974bcee4f69 477 printf("Starting 'OFF'\n");
cdupaty 0:d974bcee4f69 478 #endif
cdupaty 0:d974bcee4f69 479
cdupaty 0:d974bcee4f69 480 // ajoute par C.Dupaty
cdupaty 0:d974bcee4f69 481 //SPI.end();
cdupaty 0:d974bcee4f69 482
cdupaty 0:d974bcee4f69 483 // Powering the module
cdupaty 0:d974bcee4f69 484 // pinMode(SX1272_SS,OUTPUT);
cdupaty 0:d974bcee4f69 485 // digitalWrite(SX1272_SS,LOW);
cdupaty 0:d974bcee4f69 486 ss=0; // ????? !!!!!!!!!!!!!!!!!
cdupaty 0:d974bcee4f69 487 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 488 printf("## Setting OFF ##");
cdupaty 0:d974bcee4f69 489 printf("\n");
cdupaty 0:d974bcee4f69 490 #endif
cdupaty 0:d974bcee4f69 491 }
cdupaty 0:d974bcee4f69 492
cdupaty 0:d974bcee4f69 493 /*
cdupaty 0:d974bcee4f69 494 Function: Reads the indicated register.
cdupaty 0:d974bcee4f69 495 Returns: The content of the register
cdupaty 0:d974bcee4f69 496 Parameters:
cdupaty 0:d974bcee4f69 497 address: address register to read from
cdupaty 0:d974bcee4f69 498 */
cdupaty 0:d974bcee4f69 499 byte SX1272::readRegister(byte address)
cdupaty 0:d974bcee4f69 500 {
cdupaty 0:d974bcee4f69 501 byte value = 0x00;
cdupaty 0:d974bcee4f69 502
cdupaty 0:d974bcee4f69 503 //digitalWrite(SX1272_SS,LOW);
cdupaty 0:d974bcee4f69 504 ss=0;
cdupaty 0:d974bcee4f69 505 bitClear(address, 7); // Bit 7 cleared to write in registers
cdupaty 0:d974bcee4f69 506 // SPI.transfer(address);
cdupaty 0:d974bcee4f69 507 spi.write(address);
cdupaty 0:d974bcee4f69 508 //value = SPI.transfer(0x00);
cdupaty 0:d974bcee4f69 509 value = spi.write(0x00);
cdupaty 0:d974bcee4f69 510 //digitalWrite(SX1272_SS,HIGH);
cdupaty 0:d974bcee4f69 511 ss=1;
cdupaty 0:d974bcee4f69 512
cdupaty 0:d974bcee4f69 513 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 514 printf("## Reading: ");
cdupaty 0:d974bcee4f69 515 printf("Register 0x%02X : 0x%02X\n",address,value);
cdupaty 0:d974bcee4f69 516 // Serial.print(address, HEX);
cdupaty 0:d974bcee4f69 517 // printf(": ");
cdupaty 0:d974bcee4f69 518 // Serial.print(value, HEX);
cdupaty 0:d974bcee4f69 519 // printf("\n");
cdupaty 0:d974bcee4f69 520 #endif
cdupaty 0:d974bcee4f69 521
cdupaty 0:d974bcee4f69 522 return value;
cdupaty 0:d974bcee4f69 523 }
cdupaty 0:d974bcee4f69 524
cdupaty 0:d974bcee4f69 525 /*
cdupaty 0:d974bcee4f69 526 Function: Writes on the indicated register.
cdupaty 0:d974bcee4f69 527 Returns: Nothing
cdupaty 0:d974bcee4f69 528 Parameters:
cdupaty 0:d974bcee4f69 529 address: address register to write in
cdupaty 0:d974bcee4f69 530 data : value to write in the register
cdupaty 0:d974bcee4f69 531 */
cdupaty 0:d974bcee4f69 532 void SX1272::writeRegister(byte address, byte data)
cdupaty 0:d974bcee4f69 533 {
afzalsamira 6:d7c7e731da3a 534 // printf("Samira - writeRegister ( ) - ENTER..\n");
afzalsamira 6:d7c7e731da3a 535
cdupaty 0:d974bcee4f69 536 //digitalWrite(SX1272_SS,LOW);
cdupaty 0:d974bcee4f69 537 ss=0;
cdupaty 0:d974bcee4f69 538 bitSet(address, 7); // Bit 7 set to read from registers
cdupaty 0:d974bcee4f69 539 //SPI.transfer(address);
cdupaty 0:d974bcee4f69 540 //SPI.transfer(data);
cdupaty 0:d974bcee4f69 541 spi.write(address);
cdupaty 0:d974bcee4f69 542 spi.write(data);
cdupaty 0:d974bcee4f69 543 // digitalWrite(SX1272_SS,HIGH);
cdupaty 0:d974bcee4f69 544 ss=1;
cdupaty 0:d974bcee4f69 545
cdupaty 0:d974bcee4f69 546 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 547 printf("## Writing: Register ");
cdupaty 0:d974bcee4f69 548 bitClear(address, 7);
cdupaty 0:d974bcee4f69 549 printf("0x%02X : 0x%02X\n",address,data);
cdupaty 0:d974bcee4f69 550 // Serial.print(address, HEX);
cdupaty 0:d974bcee4f69 551 // printf(": ");
cdupaty 0:d974bcee4f69 552 // Serial.print(data, HEX);
cdupaty 0:d974bcee4f69 553 // printf("\n");
cdupaty 0:d974bcee4f69 554 #endif
cdupaty 0:d974bcee4f69 555
cdupaty 0:d974bcee4f69 556 }
cdupaty 0:d974bcee4f69 557
cdupaty 0:d974bcee4f69 558 /*
cdupaty 0:d974bcee4f69 559 Function: Clears the interruption flags
cdupaty 0:d974bcee4f69 560 Returns: Nothing
cdupaty 0:d974bcee4f69 561 */
cdupaty 0:d974bcee4f69 562 void SX1272::clearFlags()
cdupaty 0:d974bcee4f69 563 {
cdupaty 0:d974bcee4f69 564 byte st0;
cdupaty 0:d974bcee4f69 565
cdupaty 0:d974bcee4f69 566 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 567
cdupaty 0:d974bcee4f69 568 if( _modem == LORA )
cdupaty 0:d974bcee4f69 569 { // LoRa mode
cdupaty 0:d974bcee4f69 570 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby mode to write in registers
cdupaty 0:d974bcee4f69 571 writeRegister(REG_IRQ_FLAGS, 0xFF); // LoRa mode flags register
cdupaty 0:d974bcee4f69 572 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 573 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 574 printf("## LoRa flags cleared ##\n");
cdupaty 0:d974bcee4f69 575 #endif
cdupaty 0:d974bcee4f69 576 }
cdupaty 0:d974bcee4f69 577 else
cdupaty 0:d974bcee4f69 578 { // FSK mode
cdupaty 0:d974bcee4f69 579 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Stdby mode to write in registers
cdupaty 0:d974bcee4f69 580 writeRegister(REG_IRQ_FLAGS1, 0xFF); // FSK mode flags1 register
cdupaty 0:d974bcee4f69 581 writeRegister(REG_IRQ_FLAGS2, 0xFF); // FSK mode flags2 register
cdupaty 0:d974bcee4f69 582 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 583 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 584 printf("## FSK flags cleared ##");
cdupaty 0:d974bcee4f69 585 #endif
cdupaty 0:d974bcee4f69 586 }
cdupaty 0:d974bcee4f69 587 }
cdupaty 0:d974bcee4f69 588
cdupaty 0:d974bcee4f69 589 /*
cdupaty 0:d974bcee4f69 590 Function: Sets the module in LoRa mode.
cdupaty 0:d974bcee4f69 591 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 592 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 593 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 594 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 595 */
cdupaty 0:d974bcee4f69 596 uint8_t SX1272::setLORA()
cdupaty 0:d974bcee4f69 597 {
cdupaty 0:d974bcee4f69 598 uint8_t state = 2;
cdupaty 0:d974bcee4f69 599 byte st0;
cdupaty 0:d974bcee4f69 600
cdupaty 0:d974bcee4f69 601 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 602 printf("\n");
cdupaty 0:d974bcee4f69 603 printf("Starting 'setLORA'\n");
cdupaty 0:d974bcee4f69 604 #endif
cdupaty 0:d974bcee4f69 605
cdupaty 0:d974bcee4f69 606 // modified by C. Pham
cdupaty 0:d974bcee4f69 607 uint8_t retry=0;
cdupaty 0:d974bcee4f69 608
cdupaty 0:d974bcee4f69 609 do {
cdupaty 0:d974bcee4f69 610 wait_ms(200);
cdupaty 0:d974bcee4f69 611 writeRegister(REG_OP_MODE, FSK_SLEEP_MODE); // Sleep mode (mandatory to set LoRa mode)
cdupaty 0:d974bcee4f69 612 writeRegister(REG_OP_MODE, LORA_SLEEP_MODE); // LoRa sleep mode
cdupaty 0:d974bcee4f69 613 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 614 wait_ms(50+retry*10);
cdupaty 0:d974bcee4f69 615 st0 = readRegister(REG_OP_MODE);
cdupaty 0:d974bcee4f69 616 printf("...");
cdupaty 0:d974bcee4f69 617
cdupaty 0:d974bcee4f69 618 if ((retry % 2)==0)
cdupaty 0:d974bcee4f69 619 if (retry==20)
cdupaty 0:d974bcee4f69 620 retry=0;
cdupaty 0:d974bcee4f69 621 else
cdupaty 0:d974bcee4f69 622 retry++;
cdupaty 0:d974bcee4f69 623 /*
cdupaty 0:d974bcee4f69 624 if (st0!=LORA_STANDBY_MODE) {
cdupaty 0:d974bcee4f69 625 pinMode(SX1272_RST,OUTPUT);
cdupaty 0:d974bcee4f69 626 digitalWrite(SX1272_RST,HIGH);
cdupaty 0:d974bcee4f69 627 wait_ms(100);
cdupaty 0:d974bcee4f69 628 digitalWrite(SX1272_RST,LOW);
cdupaty 0:d974bcee4f69 629 }
cdupaty 0:d974bcee4f69 630 */
cdupaty 0:d974bcee4f69 631
cdupaty 0:d974bcee4f69 632 } while (st0!=LORA_STANDBY_MODE); // LoRa standby mode
cdupaty 0:d974bcee4f69 633
cdupaty 0:d974bcee4f69 634 if( st0 == LORA_STANDBY_MODE)
cdupaty 0:d974bcee4f69 635 { // LoRa mode
cdupaty 0:d974bcee4f69 636 _modem = LORA;
cdupaty 0:d974bcee4f69 637 state = 0;
cdupaty 0:d974bcee4f69 638 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 639 printf("## LoRa set with success ##");
cdupaty 0:d974bcee4f69 640 printf("\n");
cdupaty 0:d974bcee4f69 641 #endif
cdupaty 0:d974bcee4f69 642 }
cdupaty 0:d974bcee4f69 643 else
cdupaty 0:d974bcee4f69 644 { // FSK mode
cdupaty 0:d974bcee4f69 645 _modem = FSK;
cdupaty 0:d974bcee4f69 646 state = 1;
cdupaty 0:d974bcee4f69 647 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 648 printf("** There has been an error while setting LoRa **");
cdupaty 0:d974bcee4f69 649 printf("\n");
cdupaty 0:d974bcee4f69 650 #endif
cdupaty 0:d974bcee4f69 651 }
cdupaty 0:d974bcee4f69 652 return state;
cdupaty 0:d974bcee4f69 653 }
cdupaty 0:d974bcee4f69 654
cdupaty 0:d974bcee4f69 655 /*
cdupaty 0:d974bcee4f69 656 Function: Sets the module in FSK mode.
cdupaty 0:d974bcee4f69 657 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 658 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 659 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 660 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 661 */
cdupaty 0:d974bcee4f69 662 uint8_t SX1272::setFSK()
cdupaty 0:d974bcee4f69 663 {
cdupaty 0:d974bcee4f69 664 uint8_t state = 2;
cdupaty 0:d974bcee4f69 665 byte st0;
cdupaty 0:d974bcee4f69 666 byte config1;
cdupaty 0:d974bcee4f69 667
cdupaty 0:d974bcee4f69 668 if (_board==SX1276Chip)
cdupaty 0:d974bcee4f69 669 printf("Warning: FSK has not been tested on SX1276!");
cdupaty 0:d974bcee4f69 670
cdupaty 0:d974bcee4f69 671 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 672 printf("\n");
cdupaty 0:d974bcee4f69 673 printf("Starting 'setFSK'\n");
cdupaty 0:d974bcee4f69 674 #endif
cdupaty 0:d974bcee4f69 675
cdupaty 0:d974bcee4f69 676 writeRegister(REG_OP_MODE, FSK_SLEEP_MODE); // Sleep mode (mandatory to change mode)
cdupaty 0:d974bcee4f69 677 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // FSK standby mode
cdupaty 0:d974bcee4f69 678 config1 = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 679 config1 = config1 & 0B01111101; // clears bits 8 and 1 from REG_PACKET_CONFIG1
cdupaty 0:d974bcee4f69 680 config1 = config1 | 0B00000100; // sets bit 2 from REG_PACKET_CONFIG1
cdupaty 0:d974bcee4f69 681 writeRegister(REG_PACKET_CONFIG1,config1); // AddressFiltering = NodeAddress + BroadcastAddress
cdupaty 0:d974bcee4f69 682 writeRegister(REG_FIFO_THRESH, 0x80); // condition to start packet tx
cdupaty 0:d974bcee4f69 683 config1 = readRegister(REG_SYNC_CONFIG);
cdupaty 0:d974bcee4f69 684 config1 = config1 & 0B00111111;
cdupaty 0:d974bcee4f69 685 writeRegister(REG_SYNC_CONFIG,config1);
cdupaty 0:d974bcee4f69 686
cdupaty 0:d974bcee4f69 687 wait_ms(100);
cdupaty 0:d974bcee4f69 688
cdupaty 0:d974bcee4f69 689 st0 = readRegister(REG_OP_MODE); // Reading config mode
cdupaty 0:d974bcee4f69 690 if( st0 == FSK_STANDBY_MODE )
cdupaty 0:d974bcee4f69 691 { // FSK mode
cdupaty 0:d974bcee4f69 692 _modem = FSK;
cdupaty 0:d974bcee4f69 693 state = 0;
cdupaty 0:d974bcee4f69 694 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 695 printf("## FSK set with success ##");
cdupaty 0:d974bcee4f69 696 printf("\n");
cdupaty 0:d974bcee4f69 697 #endif
cdupaty 0:d974bcee4f69 698 }
cdupaty 0:d974bcee4f69 699 else
cdupaty 0:d974bcee4f69 700 { // LoRa mode
cdupaty 0:d974bcee4f69 701 _modem = LORA;
cdupaty 0:d974bcee4f69 702 state = 1;
cdupaty 0:d974bcee4f69 703 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 704 printf("** There has been an error while setting FSK **");
cdupaty 0:d974bcee4f69 705 printf("\n");
cdupaty 0:d974bcee4f69 706 #endif
cdupaty 0:d974bcee4f69 707 }
cdupaty 0:d974bcee4f69 708 return state;
cdupaty 0:d974bcee4f69 709 }
cdupaty 0:d974bcee4f69 710
cdupaty 0:d974bcee4f69 711 /*
cdupaty 0:d974bcee4f69 712 Function: Gets the bandwidth, coding rate and spreading factor of the LoRa modulation.
cdupaty 0:d974bcee4f69 713 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 714 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 715 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 716 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 717 */
cdupaty 0:d974bcee4f69 718 uint8_t SX1272::getMode()
cdupaty 0:d974bcee4f69 719 {
cdupaty 0:d974bcee4f69 720 byte st0;
cdupaty 0:d974bcee4f69 721 int8_t state = 2;
cdupaty 0:d974bcee4f69 722 byte value = 0x00;
cdupaty 0:d974bcee4f69 723
cdupaty 0:d974bcee4f69 724 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 725 printf("\n");
cdupaty 0:d974bcee4f69 726 printf("Starting 'getMode'\n");
cdupaty 0:d974bcee4f69 727 #endif
cdupaty 0:d974bcee4f69 728
cdupaty 0:d974bcee4f69 729 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 730 if( _modem == FSK )
cdupaty 0:d974bcee4f69 731 {
cdupaty 0:d974bcee4f69 732 setLORA(); // Setting LoRa mode
cdupaty 0:d974bcee4f69 733 }
cdupaty 0:d974bcee4f69 734 value = readRegister(REG_MODEM_CONFIG1);
cdupaty 0:d974bcee4f69 735 // added by C. Pham
cdupaty 0:d974bcee4f69 736 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 737 _bandwidth = (value >> 6); // Storing 2 MSB from REG_MODEM_CONFIG1 (=_bandwidth)
cdupaty 0:d974bcee4f69 738 // added by C. Pham
cdupaty 0:d974bcee4f69 739 // convert to common bandwidth values used by both SX1272 and SX1276
cdupaty 0:d974bcee4f69 740 _bandwidth += 7;
cdupaty 0:d974bcee4f69 741 }
cdupaty 0:d974bcee4f69 742 else
cdupaty 0:d974bcee4f69 743 _bandwidth = (value >> 4); // Storing 4 MSB from REG_MODEM_CONFIG1 (=_bandwidth)
cdupaty 0:d974bcee4f69 744
cdupaty 0:d974bcee4f69 745 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 746 _codingRate = (value >> 3) & 0x07; // Storing third, forth and fifth bits from
cdupaty 0:d974bcee4f69 747 else
cdupaty 0:d974bcee4f69 748 _codingRate = (value >> 1) & 0x07; // Storing 3-1 bits REG_MODEM_CONFIG1 (=_codingRate)
cdupaty 0:d974bcee4f69 749
cdupaty 0:d974bcee4f69 750 value = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 751 _spreadingFactor = (value >> 4) & 0x0F; // Storing 4 MSB from REG_MODEM_CONFIG2 (=_spreadingFactor)
cdupaty 0:d974bcee4f69 752 state = 1;
cdupaty 0:d974bcee4f69 753
cdupaty 0:d974bcee4f69 754 if( isBW(_bandwidth) ) // Checking available values for:
cdupaty 0:d974bcee4f69 755 { // _bandwidth
cdupaty 0:d974bcee4f69 756 if( isCR(_codingRate) ) // _codingRate
cdupaty 0:d974bcee4f69 757 { // _spreadingFactor
cdupaty 0:d974bcee4f69 758 if( isSF(_spreadingFactor) )
cdupaty 0:d974bcee4f69 759 {
cdupaty 0:d974bcee4f69 760 state = 0;
cdupaty 0:d974bcee4f69 761 }
cdupaty 0:d974bcee4f69 762 }
cdupaty 0:d974bcee4f69 763 }
cdupaty 0:d974bcee4f69 764
cdupaty 0:d974bcee4f69 765 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 766 printf("## Parameters from configuration mode are:");
cdupaty 0:d974bcee4f69 767 printf("Bandwidth: %X\n",_bandwidth);
cdupaty 0:d974bcee4f69 768 // Serial.print(_bandwidth, HEX);
cdupaty 0:d974bcee4f69 769 // printf("\n");
cdupaty 0:d974bcee4f69 770 printf("\t Coding Rate: %X\n",_codingRate);
cdupaty 0:d974bcee4f69 771 // Serial.print(_codingRate, HEX);
cdupaty 0:d974bcee4f69 772 // printf("\n");
cdupaty 0:d974bcee4f69 773 printf("\t Spreading Factor: %X\n",_spreadingFactor);
cdupaty 0:d974bcee4f69 774 // Serial.print(_spreadingFactor, HEX);
cdupaty 0:d974bcee4f69 775 printf(" ##");
cdupaty 0:d974bcee4f69 776 printf("\n");
cdupaty 0:d974bcee4f69 777 #endif
cdupaty 0:d974bcee4f69 778
cdupaty 0:d974bcee4f69 779 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 780 wait_ms(100);
cdupaty 0:d974bcee4f69 781 return state;
cdupaty 0:d974bcee4f69 782 }
cdupaty 0:d974bcee4f69 783
cdupaty 0:d974bcee4f69 784 /*
cdupaty 0:d974bcee4f69 785 Function: Sets the bandwidth, coding rate and spreading factor of the LoRa modulation.
cdupaty 0:d974bcee4f69 786 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 787 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 788 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 789 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 790 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 791 Parameters:
cdupaty 0:d974bcee4f69 792 mode: mode number to set the required BW, SF and CR of LoRa modem.
cdupaty 0:d974bcee4f69 793 */
cdupaty 0:d974bcee4f69 794 int8_t SX1272::setMode(uint8_t mode)
cdupaty 0:d974bcee4f69 795 {
cdupaty 0:d974bcee4f69 796 int8_t state = 2;
cdupaty 0:d974bcee4f69 797 byte st0;
cdupaty 0:d974bcee4f69 798 byte config1 = 0x00;
cdupaty 0:d974bcee4f69 799 byte config2 = 0x00;
cdupaty 0:d974bcee4f69 800
cdupaty 0:d974bcee4f69 801 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 802 printf("\n");
cdupaty 0:d974bcee4f69 803 printf("Starting 'setMode'\n");
cdupaty 0:d974bcee4f69 804 #endif
cdupaty 0:d974bcee4f69 805
cdupaty 0:d974bcee4f69 806 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 807
cdupaty 0:d974bcee4f69 808 if( _modem == FSK )
cdupaty 0:d974bcee4f69 809 {
cdupaty 0:d974bcee4f69 810 setLORA();
cdupaty 0:d974bcee4f69 811 }
cdupaty 0:d974bcee4f69 812 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // LoRa standby mode
cdupaty 0:d974bcee4f69 813
cdupaty 0:d974bcee4f69 814 switch (mode)
cdupaty 0:d974bcee4f69 815 {
cdupaty 0:d974bcee4f69 816 // mode 1 (better reach, medium time on air)
cdupaty 0:d974bcee4f69 817 case 1:
cdupaty 0:d974bcee4f69 818 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 819 setSF(SF_12); // SF = 12
cdupaty 0:d974bcee4f69 820 setBW(BW_125); // BW = 125 KHz
cdupaty 0:d974bcee4f69 821 break;
cdupaty 0:d974bcee4f69 822
cdupaty 0:d974bcee4f69 823 // mode 2 (medium reach, less time on air)
cdupaty 0:d974bcee4f69 824 case 2:
cdupaty 0:d974bcee4f69 825 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 826 setSF(SF_12); // SF = 12
cdupaty 0:d974bcee4f69 827 setBW(BW_250); // BW = 250 KHz
cdupaty 0:d974bcee4f69 828 break;
cdupaty 0:d974bcee4f69 829
cdupaty 0:d974bcee4f69 830 // mode 3 (worst reach, less time on air)
cdupaty 0:d974bcee4f69 831 case 3:
cdupaty 0:d974bcee4f69 832 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 833 setSF(SF_10); // SF = 10
cdupaty 0:d974bcee4f69 834 setBW(BW_125); // BW = 125 KHz
cdupaty 0:d974bcee4f69 835 break;
cdupaty 0:d974bcee4f69 836
cdupaty 0:d974bcee4f69 837 // mode 4 (better reach, low time on air)
cdupaty 0:d974bcee4f69 838 case 4:
cdupaty 0:d974bcee4f69 839 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 840 setSF(SF_12); // SF = 12
cdupaty 0:d974bcee4f69 841 setBW(BW_500); // BW = 500 KHz
cdupaty 0:d974bcee4f69 842 break;
cdupaty 0:d974bcee4f69 843
cdupaty 0:d974bcee4f69 844 // mode 5 (better reach, medium time on air)
cdupaty 0:d974bcee4f69 845 case 5:
cdupaty 0:d974bcee4f69 846 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 847 setSF(SF_10); // SF = 10
cdupaty 0:d974bcee4f69 848 setBW(BW_250); // BW = 250 KHz
cdupaty 0:d974bcee4f69 849 break;
cdupaty 0:d974bcee4f69 850
cdupaty 0:d974bcee4f69 851 // mode 6 (better reach, worst time-on-air)
cdupaty 0:d974bcee4f69 852 case 6:
cdupaty 0:d974bcee4f69 853 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 854 setSF(SF_11); // SF = 11
cdupaty 0:d974bcee4f69 855 setBW(BW_500); // BW = 500 KHz
cdupaty 0:d974bcee4f69 856 break;
cdupaty 0:d974bcee4f69 857
cdupaty 0:d974bcee4f69 858 // mode 7 (medium-high reach, medium-low time-on-air)
cdupaty 0:d974bcee4f69 859 case 7:
cdupaty 0:d974bcee4f69 860 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 861 setSF(SF_9); // SF = 9
cdupaty 0:d974bcee4f69 862 setBW(BW_250); // BW = 250 KHz
cdupaty 0:d974bcee4f69 863 break;
cdupaty 0:d974bcee4f69 864
cdupaty 0:d974bcee4f69 865 // mode 8 (medium reach, medium time-on-air)
cdupaty 0:d974bcee4f69 866 case 8:
cdupaty 0:d974bcee4f69 867 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 868 setSF(SF_9); // SF = 9
cdupaty 0:d974bcee4f69 869 setBW(BW_500); // BW = 500 KHz
cdupaty 0:d974bcee4f69 870 break;
cdupaty 0:d974bcee4f69 871
cdupaty 0:d974bcee4f69 872 // mode 9 (medium-low reach, medium-high time-on-air)
cdupaty 0:d974bcee4f69 873 case 9:
cdupaty 0:d974bcee4f69 874 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 875 setSF(SF_8); // SF = 8
cdupaty 0:d974bcee4f69 876 setBW(BW_500); // BW = 500 KHz
cdupaty 0:d974bcee4f69 877 break;
cdupaty 0:d974bcee4f69 878
cdupaty 0:d974bcee4f69 879 // mode 10 (worst reach, less time_on_air)
cdupaty 0:d974bcee4f69 880 case 10:
cdupaty 0:d974bcee4f69 881 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 882 setSF(SF_7); // SF = 7
cdupaty 0:d974bcee4f69 883 setBW(BW_500); // BW = 500 KHz
cdupaty 0:d974bcee4f69 884 break;
cdupaty 0:d974bcee4f69 885
cdupaty 0:d974bcee4f69 886 // added by C. Pham
cdupaty 0:d974bcee4f69 887 // test for LoRaWAN channel
cdupaty 0:d974bcee4f69 888 case 11:
cdupaty 0:d974bcee4f69 889 setCR(CR_5); // CR = 4/5
cdupaty 0:d974bcee4f69 890 setSF(SF_12); // SF = 12
cdupaty 0:d974bcee4f69 891 setBW(BW_125); // BW = 125 KHz
cdupaty 0:d974bcee4f69 892 // set the sync word to the LoRaWAN sync word which is 0x34
cdupaty 0:d974bcee4f69 893 setSyncWord(0x34);
cdupaty 0:d974bcee4f69 894 printf("** Using sync word of 0x%X\n",_syncWord);
cdupaty 0:d974bcee4f69 895 // Serial.println(_syncWord, HEX);
cdupaty 0:d974bcee4f69 896 break;
cdupaty 0:d974bcee4f69 897
cdupaty 0:d974bcee4f69 898 default: state = -1; // The indicated mode doesn't exist
cdupaty 0:d974bcee4f69 899
cdupaty 0:d974bcee4f69 900 };
cdupaty 0:d974bcee4f69 901
cdupaty 0:d974bcee4f69 902 if( state == -1 ) // if state = -1, don't change its value
cdupaty 0:d974bcee4f69 903 {
cdupaty 0:d974bcee4f69 904 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 905 printf("** The indicated mode doesn't exist, ");
cdupaty 0:d974bcee4f69 906 printf("please select from 1 to 10 **");
cdupaty 0:d974bcee4f69 907 #endif
cdupaty 0:d974bcee4f69 908 }
cdupaty 0:d974bcee4f69 909 else
cdupaty 0:d974bcee4f69 910 {
cdupaty 0:d974bcee4f69 911 state = 1;
cdupaty 0:d974bcee4f69 912 config1 = readRegister(REG_MODEM_CONFIG1);
cdupaty 0:d974bcee4f69 913 switch (mode)
cdupaty 0:d974bcee4f69 914 { // Different way to check for each mode:
cdupaty 0:d974bcee4f69 915 // (config1 >> 3) ---> take out bits 7-3 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 916 // (config2 >> 4) ---> take out bits 7-4 from REG_MODEM_CONFIG2 (=_spreadingFactor)
cdupaty 0:d974bcee4f69 917
cdupaty 0:d974bcee4f69 918 // mode 1: BW = 125 KHz, CR = 4/5, SF = 12.
cdupaty 0:d974bcee4f69 919 case 1:
cdupaty 0:d974bcee4f69 920
cdupaty 0:d974bcee4f69 921 //modified by C. Pham
cdupaty 0:d974bcee4f69 922 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 923 //////////////////////////////////////////////possible pb sur config1 qui vaut 0
cdupaty 0:d974bcee4f69 924 if( (config1 >> 3) == 0x01 )
cdupaty 0:d974bcee4f69 925 state=0;
cdupaty 0:d974bcee4f69 926 }
cdupaty 0:d974bcee4f69 927 else {
cdupaty 0:d974bcee4f69 928 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 929 if( (config1 >> 1) == 0x39 )
cdupaty 0:d974bcee4f69 930 state=0;
cdupaty 0:d974bcee4f69 931 }
cdupaty 0:d974bcee4f69 932
cdupaty 0:d974bcee4f69 933 if( state==0) {
cdupaty 0:d974bcee4f69 934 state = 1;
cdupaty 0:d974bcee4f69 935 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 936
cdupaty 0:d974bcee4f69 937 if( (config2 >> 4) == SF_12 )
cdupaty 0:d974bcee4f69 938 {
cdupaty 0:d974bcee4f69 939 state = 0;
cdupaty 0:d974bcee4f69 940 }
cdupaty 0:d974bcee4f69 941 }
cdupaty 0:d974bcee4f69 942 break;
cdupaty 0:d974bcee4f69 943
cdupaty 0:d974bcee4f69 944
cdupaty 0:d974bcee4f69 945 // mode 2: BW = 250 KHz, CR = 4/5, SF = 12.
cdupaty 0:d974bcee4f69 946 case 2:
cdupaty 0:d974bcee4f69 947
cdupaty 0:d974bcee4f69 948 //modified by C. Pham
cdupaty 0:d974bcee4f69 949 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 950 if( (config1 >> 3) == 0x09 )
cdupaty 0:d974bcee4f69 951 state=0;
cdupaty 0:d974bcee4f69 952 }
cdupaty 0:d974bcee4f69 953 else {
cdupaty 0:d974bcee4f69 954 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 955 if( (config1 >> 1) == 0x41 )
cdupaty 0:d974bcee4f69 956 state=0;
cdupaty 0:d974bcee4f69 957 }
cdupaty 0:d974bcee4f69 958
cdupaty 0:d974bcee4f69 959 if( state==0) {
cdupaty 0:d974bcee4f69 960 state = 1;
cdupaty 0:d974bcee4f69 961 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 962
cdupaty 0:d974bcee4f69 963 if( (config2 >> 4) == SF_12 )
cdupaty 0:d974bcee4f69 964 {
cdupaty 0:d974bcee4f69 965 state = 0;
cdupaty 0:d974bcee4f69 966 }
cdupaty 0:d974bcee4f69 967 }
cdupaty 0:d974bcee4f69 968 break;
cdupaty 0:d974bcee4f69 969
cdupaty 0:d974bcee4f69 970 // mode 3: BW = 125 KHz, CR = 4/5, SF = 10.
cdupaty 0:d974bcee4f69 971 case 3:
cdupaty 0:d974bcee4f69 972
cdupaty 0:d974bcee4f69 973 //modified by C. Pham
cdupaty 0:d974bcee4f69 974 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 975 if( (config1 >> 3) == 0x01 )
cdupaty 0:d974bcee4f69 976 state=0;
cdupaty 0:d974bcee4f69 977 }
cdupaty 0:d974bcee4f69 978 else {
cdupaty 0:d974bcee4f69 979 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 980 if( (config1 >> 1) == 0x39 )
cdupaty 0:d974bcee4f69 981 state=0;
cdupaty 0:d974bcee4f69 982 }
cdupaty 0:d974bcee4f69 983
cdupaty 0:d974bcee4f69 984 if( state==0) {
cdupaty 0:d974bcee4f69 985 state = 1;
cdupaty 0:d974bcee4f69 986 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 987
cdupaty 0:d974bcee4f69 988 if( (config2 >> 4) == SF_10 )
cdupaty 0:d974bcee4f69 989 {
cdupaty 0:d974bcee4f69 990 state = 0;
cdupaty 0:d974bcee4f69 991 }
cdupaty 0:d974bcee4f69 992 }
cdupaty 0:d974bcee4f69 993 break;
cdupaty 0:d974bcee4f69 994
cdupaty 0:d974bcee4f69 995 // mode 4: BW = 500 KHz, CR = 4/5, SF = 12.
cdupaty 0:d974bcee4f69 996 case 4:
cdupaty 0:d974bcee4f69 997
cdupaty 0:d974bcee4f69 998 //modified by C. Pham
cdupaty 0:d974bcee4f69 999 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1000 if( (config1 >> 3) == 0x11 )
cdupaty 0:d974bcee4f69 1001 state=0;
cdupaty 0:d974bcee4f69 1002 }
cdupaty 0:d974bcee4f69 1003 else {
cdupaty 0:d974bcee4f69 1004 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1005 if( (config1 >> 1) == 0x49 )
cdupaty 0:d974bcee4f69 1006 state=0;
cdupaty 0:d974bcee4f69 1007 }
cdupaty 0:d974bcee4f69 1008
cdupaty 0:d974bcee4f69 1009 if( state==0) {
cdupaty 0:d974bcee4f69 1010 state = 1;
cdupaty 0:d974bcee4f69 1011 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1012
cdupaty 0:d974bcee4f69 1013 if( (config2 >> 4) == SF_12 )
cdupaty 0:d974bcee4f69 1014 {
cdupaty 0:d974bcee4f69 1015 state = 0;
cdupaty 0:d974bcee4f69 1016 }
cdupaty 0:d974bcee4f69 1017 }
cdupaty 0:d974bcee4f69 1018 break;
cdupaty 0:d974bcee4f69 1019
cdupaty 0:d974bcee4f69 1020 // mode 5: BW = 250 KHz, CR = 4/5, SF = 10.
cdupaty 0:d974bcee4f69 1021 case 5:
cdupaty 0:d974bcee4f69 1022
cdupaty 0:d974bcee4f69 1023 //modified by C. Pham
cdupaty 0:d974bcee4f69 1024 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1025 if( (config1 >> 3) == 0x09 )
cdupaty 0:d974bcee4f69 1026 state=0;
cdupaty 0:d974bcee4f69 1027 }
cdupaty 0:d974bcee4f69 1028 else {
cdupaty 0:d974bcee4f69 1029 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1030 if( (config1 >> 1) == 0x41 )
cdupaty 0:d974bcee4f69 1031 state=0;
cdupaty 0:d974bcee4f69 1032 }
cdupaty 0:d974bcee4f69 1033
cdupaty 0:d974bcee4f69 1034 if( state==0) {
cdupaty 0:d974bcee4f69 1035 state = 1;
cdupaty 0:d974bcee4f69 1036 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1037
cdupaty 0:d974bcee4f69 1038 if( (config2 >> 4) == SF_10 )
cdupaty 0:d974bcee4f69 1039 {
cdupaty 0:d974bcee4f69 1040 state = 0;
cdupaty 0:d974bcee4f69 1041 }
cdupaty 0:d974bcee4f69 1042 }
cdupaty 0:d974bcee4f69 1043 break;
cdupaty 0:d974bcee4f69 1044
cdupaty 0:d974bcee4f69 1045 // mode 6: BW = 500 KHz, CR = 4/5, SF = 11.
cdupaty 0:d974bcee4f69 1046 case 6:
cdupaty 0:d974bcee4f69 1047
cdupaty 0:d974bcee4f69 1048 //modified by C. Pham
cdupaty 0:d974bcee4f69 1049 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1050 if( (config1 >> 3) == 0x11 )
cdupaty 0:d974bcee4f69 1051 state=0;
cdupaty 0:d974bcee4f69 1052 }
cdupaty 0:d974bcee4f69 1053 else {
cdupaty 0:d974bcee4f69 1054 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1055 if( (config1 >> 1) == 0x49 )
cdupaty 0:d974bcee4f69 1056 state=0;
cdupaty 0:d974bcee4f69 1057 }
cdupaty 0:d974bcee4f69 1058
cdupaty 0:d974bcee4f69 1059 if( state==0) {
cdupaty 0:d974bcee4f69 1060 state = 1;
cdupaty 0:d974bcee4f69 1061 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1062
cdupaty 0:d974bcee4f69 1063 if( (config2 >> 4) == SF_11 )
cdupaty 0:d974bcee4f69 1064 {
cdupaty 0:d974bcee4f69 1065 state = 0;
cdupaty 0:d974bcee4f69 1066 }
cdupaty 0:d974bcee4f69 1067 }
cdupaty 0:d974bcee4f69 1068 break;
cdupaty 0:d974bcee4f69 1069
cdupaty 0:d974bcee4f69 1070 // mode 7: BW = 250 KHz, CR = 4/5, SF = 9.
cdupaty 0:d974bcee4f69 1071 case 7:
cdupaty 0:d974bcee4f69 1072
cdupaty 0:d974bcee4f69 1073 //modified by C. Pham
cdupaty 0:d974bcee4f69 1074 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1075 if( (config1 >> 3) == 0x09 )
cdupaty 0:d974bcee4f69 1076 state=0;
cdupaty 0:d974bcee4f69 1077 }
cdupaty 0:d974bcee4f69 1078 else {
cdupaty 0:d974bcee4f69 1079 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1080 if( (config1 >> 1) == 0x41 )
cdupaty 0:d974bcee4f69 1081 state=0;
cdupaty 0:d974bcee4f69 1082 }
cdupaty 0:d974bcee4f69 1083
cdupaty 0:d974bcee4f69 1084 if( state==0) {
cdupaty 0:d974bcee4f69 1085 state = 1;
cdupaty 0:d974bcee4f69 1086 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1087
cdupaty 0:d974bcee4f69 1088 if( (config2 >> 4) == SF_9 )
cdupaty 0:d974bcee4f69 1089 {
cdupaty 0:d974bcee4f69 1090 state = 0;
cdupaty 0:d974bcee4f69 1091 }
cdupaty 0:d974bcee4f69 1092 }
cdupaty 0:d974bcee4f69 1093 break;
cdupaty 0:d974bcee4f69 1094
cdupaty 0:d974bcee4f69 1095 // mode 8: BW = 500 KHz, CR = 4/5, SF = 9.
cdupaty 0:d974bcee4f69 1096 case 8:
cdupaty 0:d974bcee4f69 1097
cdupaty 0:d974bcee4f69 1098 //modified by C. Pham
cdupaty 0:d974bcee4f69 1099 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1100 if( (config1 >> 3) == 0x11 )
cdupaty 0:d974bcee4f69 1101 state=0;
cdupaty 0:d974bcee4f69 1102 }
cdupaty 0:d974bcee4f69 1103 else {
cdupaty 0:d974bcee4f69 1104 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1105 if( (config1 >> 1) == 0x49 )
cdupaty 0:d974bcee4f69 1106 state=0;
cdupaty 0:d974bcee4f69 1107 }
cdupaty 0:d974bcee4f69 1108
cdupaty 0:d974bcee4f69 1109 if( state==0) {
cdupaty 0:d974bcee4f69 1110 state = 1;
cdupaty 0:d974bcee4f69 1111 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1112
cdupaty 0:d974bcee4f69 1113 if( (config2 >> 4) == SF_9 )
cdupaty 0:d974bcee4f69 1114 {
cdupaty 0:d974bcee4f69 1115 state = 0;
cdupaty 0:d974bcee4f69 1116 }
cdupaty 0:d974bcee4f69 1117 }
cdupaty 0:d974bcee4f69 1118 break;
cdupaty 0:d974bcee4f69 1119
cdupaty 0:d974bcee4f69 1120 // mode 9: BW = 500 KHz, CR = 4/5, SF = 8.
cdupaty 0:d974bcee4f69 1121 case 9:
cdupaty 0:d974bcee4f69 1122
cdupaty 0:d974bcee4f69 1123 //modified by C. Pham
cdupaty 0:d974bcee4f69 1124 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1125 if( (config1 >> 3) == 0x11 )
cdupaty 0:d974bcee4f69 1126 state=0;
cdupaty 0:d974bcee4f69 1127 }
cdupaty 0:d974bcee4f69 1128 else {
cdupaty 0:d974bcee4f69 1129 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1130 if( (config1 >> 1) == 0x49 )
cdupaty 0:d974bcee4f69 1131 state=0;
cdupaty 0:d974bcee4f69 1132 }
cdupaty 0:d974bcee4f69 1133
cdupaty 0:d974bcee4f69 1134 if( state==0) {
cdupaty 0:d974bcee4f69 1135 state = 1;
cdupaty 0:d974bcee4f69 1136 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1137
cdupaty 0:d974bcee4f69 1138 if( (config2 >> 4) == SF_8 )
cdupaty 0:d974bcee4f69 1139 {
cdupaty 0:d974bcee4f69 1140 state = 0;
cdupaty 0:d974bcee4f69 1141 }
cdupaty 0:d974bcee4f69 1142 }
cdupaty 0:d974bcee4f69 1143 break;
cdupaty 0:d974bcee4f69 1144
cdupaty 0:d974bcee4f69 1145 // mode 10: BW = 500 KHz, CR = 4/5, SF = 7.
cdupaty 0:d974bcee4f69 1146 case 10:
cdupaty 0:d974bcee4f69 1147
cdupaty 0:d974bcee4f69 1148 //modified by C. Pham
cdupaty 0:d974bcee4f69 1149 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1150 if( (config1 >> 3) == 0x11 )
cdupaty 0:d974bcee4f69 1151 state=0;
cdupaty 0:d974bcee4f69 1152 }
cdupaty 0:d974bcee4f69 1153 else {
cdupaty 0:d974bcee4f69 1154 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1155 if( (config1 >> 1) == 0x49 )
cdupaty 0:d974bcee4f69 1156 state=0;
cdupaty 0:d974bcee4f69 1157 }
cdupaty 0:d974bcee4f69 1158
cdupaty 0:d974bcee4f69 1159 if( state==0) {
cdupaty 0:d974bcee4f69 1160 state = 1;
cdupaty 0:d974bcee4f69 1161 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1162
cdupaty 0:d974bcee4f69 1163 if( (config2 >> 4) == SF_7 )
cdupaty 0:d974bcee4f69 1164 {
cdupaty 0:d974bcee4f69 1165 state = 0;
cdupaty 0:d974bcee4f69 1166 }
cdupaty 0:d974bcee4f69 1167 }
cdupaty 0:d974bcee4f69 1168 break;
cdupaty 0:d974bcee4f69 1169
cdupaty 0:d974bcee4f69 1170 // added by C. Pham
cdupaty 0:d974bcee4f69 1171 // test of LoRaWAN channel
cdupaty 0:d974bcee4f69 1172 // mode 11: BW = 125 KHz, CR = 4/5, SF = 12.
cdupaty 0:d974bcee4f69 1173 case 11:
cdupaty 0:d974bcee4f69 1174
cdupaty 0:d974bcee4f69 1175 //modified by C. Pham
cdupaty 0:d974bcee4f69 1176 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1177 if( (config1 >> 3) == 0x01 )
cdupaty 0:d974bcee4f69 1178 state=0;
cdupaty 0:d974bcee4f69 1179 }
cdupaty 0:d974bcee4f69 1180 else {
cdupaty 0:d974bcee4f69 1181 // (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
cdupaty 0:d974bcee4f69 1182 if( (config1 >> 1) == 0x39 )
cdupaty 0:d974bcee4f69 1183 state=0;
cdupaty 0:d974bcee4f69 1184 }
cdupaty 0:d974bcee4f69 1185
cdupaty 0:d974bcee4f69 1186 if( state==0) {
cdupaty 0:d974bcee4f69 1187 state = 1;
cdupaty 0:d974bcee4f69 1188 config2 = readRegister(REG_MODEM_CONFIG2);
cdupaty 0:d974bcee4f69 1189
cdupaty 0:d974bcee4f69 1190 if( (config2 >> 4) == SF_12 )
cdupaty 0:d974bcee4f69 1191 {
cdupaty 0:d974bcee4f69 1192 state = 0;
cdupaty 0:d974bcee4f69 1193 }
cdupaty 0:d974bcee4f69 1194 }
cdupaty 0:d974bcee4f69 1195 break;
cdupaty 0:d974bcee4f69 1196 }// end switch
cdupaty 0:d974bcee4f69 1197
cdupaty 0:d974bcee4f69 1198 if (mode!=11) {
cdupaty 0:d974bcee4f69 1199 setSyncWord(_defaultSyncWord);
cdupaty 0:d974bcee4f69 1200 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1201 printf("*** Using sync word of 0x%X\n",_defaultSyncWord);
cdupaty 0:d974bcee4f69 1202 // Serial.println(_defaultSyncWord, HEX);
cdupaty 0:d974bcee4f69 1203 #endif
cdupaty 0:d974bcee4f69 1204 }
cdupaty 0:d974bcee4f69 1205 }
cdupaty 0:d974bcee4f69 1206 // added by C. Pham
cdupaty 0:d974bcee4f69 1207 if (state == 0)
cdupaty 0:d974bcee4f69 1208 _loraMode=mode;
cdupaty 0:d974bcee4f69 1209
cdupaty 0:d974bcee4f69 1210 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1211
cdupaty 0:d974bcee4f69 1212 if( state == 0 )
cdupaty 0:d974bcee4f69 1213 {
cdupaty 0:d974bcee4f69 1214 printf("## Mode %d ",mode);
cdupaty 0:d974bcee4f69 1215 // Serial.print(mode, DEC);
cdupaty 0:d974bcee4f69 1216 printf(" configured with success ##");
cdupaty 0:d974bcee4f69 1217 }
cdupaty 0:d974bcee4f69 1218 else
cdupaty 0:d974bcee4f69 1219 {
cdupaty 0:d974bcee4f69 1220 printf("** There has been an error while configuring mode %d ",mode);
cdupaty 0:d974bcee4f69 1221 // Serial.print(mode, DEC);
cdupaty 0:d974bcee4f69 1222 printf(". **\n");
cdupaty 0:d974bcee4f69 1223 }
cdupaty 0:d974bcee4f69 1224 #endif
cdupaty 0:d974bcee4f69 1225
cdupaty 0:d974bcee4f69 1226 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 1227 wait_ms(100);
cdupaty 0:d974bcee4f69 1228 return state;
cdupaty 0:d974bcee4f69 1229 }
cdupaty 0:d974bcee4f69 1230
cdupaty 0:d974bcee4f69 1231 /*
cdupaty 0:d974bcee4f69 1232 Function: Indicates if module is configured in implicit or explicit header mode.
cdupaty 0:d974bcee4f69 1233 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1234 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1235 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1236 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1237 */
cdupaty 0:d974bcee4f69 1238 uint8_t SX1272::getHeader()
cdupaty 0:d974bcee4f69 1239 {
cdupaty 0:d974bcee4f69 1240 int8_t state = 2;
cdupaty 0:d974bcee4f69 1241
cdupaty 0:d974bcee4f69 1242 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1243 printf("\n");
cdupaty 0:d974bcee4f69 1244 printf("Starting 'getHeader'\n");
cdupaty 0:d974bcee4f69 1245 #endif
cdupaty 0:d974bcee4f69 1246
cdupaty 0:d974bcee4f69 1247 // added by C. Pham
cdupaty 0:d974bcee4f69 1248 uint8_t theHeaderBit;
cdupaty 0:d974bcee4f69 1249
cdupaty 0:d974bcee4f69 1250 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1251 theHeaderBit=2;
cdupaty 0:d974bcee4f69 1252 else
cdupaty 0:d974bcee4f69 1253 theHeaderBit=0;
cdupaty 0:d974bcee4f69 1254
cdupaty 0:d974bcee4f69 1255 // take out bit 2 from REG_MODEM_CONFIG1 indicates ImplicitHeaderModeOn
cdupaty 0:d974bcee4f69 1256 if( bitRead(REG_MODEM_CONFIG1, theHeaderBit) == 0 )
cdupaty 0:d974bcee4f69 1257 { // explicit header mode (ON)
cdupaty 0:d974bcee4f69 1258 _header = HEADER_ON;
cdupaty 0:d974bcee4f69 1259 state = 1;
cdupaty 0:d974bcee4f69 1260 }
cdupaty 0:d974bcee4f69 1261 else
cdupaty 0:d974bcee4f69 1262 { // implicit header mode (OFF)
cdupaty 0:d974bcee4f69 1263 _header = HEADER_OFF;
cdupaty 0:d974bcee4f69 1264 state = 1;
cdupaty 0:d974bcee4f69 1265 }
cdupaty 0:d974bcee4f69 1266
cdupaty 0:d974bcee4f69 1267 state = 0;
cdupaty 0:d974bcee4f69 1268
cdupaty 0:d974bcee4f69 1269 if( _modem == FSK )
cdupaty 0:d974bcee4f69 1270 { // header is not available in FSK mode
cdupaty 0:d974bcee4f69 1271 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1272 printf("## Notice that FSK mode packets hasn't header ##");
cdupaty 0:d974bcee4f69 1273 printf("\n");
cdupaty 0:d974bcee4f69 1274 #endif
cdupaty 0:d974bcee4f69 1275 }
cdupaty 0:d974bcee4f69 1276 else
cdupaty 0:d974bcee4f69 1277 { // header in LoRa mode
cdupaty 0:d974bcee4f69 1278 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1279 printf("## Header is ");
cdupaty 0:d974bcee4f69 1280 if( _header == HEADER_ON )
cdupaty 0:d974bcee4f69 1281 {
cdupaty 0:d974bcee4f69 1282 printf("in explicit header mode ##");
cdupaty 0:d974bcee4f69 1283 }
cdupaty 0:d974bcee4f69 1284 else
cdupaty 0:d974bcee4f69 1285 {
cdupaty 0:d974bcee4f69 1286 printf("in implicit header mode ##");
cdupaty 0:d974bcee4f69 1287 }
cdupaty 0:d974bcee4f69 1288 printf("\n");
cdupaty 0:d974bcee4f69 1289 #endif
cdupaty 0:d974bcee4f69 1290 }
cdupaty 0:d974bcee4f69 1291 return state;
cdupaty 0:d974bcee4f69 1292 }
cdupaty 0:d974bcee4f69 1293
cdupaty 0:d974bcee4f69 1294 /*
cdupaty 0:d974bcee4f69 1295 Function: Sets the module in explicit header mode (header is sent).
cdupaty 0:d974bcee4f69 1296 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1297 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1298 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1299 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1300 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 1301 */
cdupaty 0:d974bcee4f69 1302 int8_t SX1272::setHeaderON()
cdupaty 0:d974bcee4f69 1303 {
cdupaty 0:d974bcee4f69 1304 int8_t state = 2;
cdupaty 0:d974bcee4f69 1305 byte config1;
cdupaty 0:d974bcee4f69 1306
cdupaty 0:d974bcee4f69 1307 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1308 printf("\n");
cdupaty 0:d974bcee4f69 1309 printf("Starting 'setHeaderON'\n");
cdupaty 0:d974bcee4f69 1310 #endif
cdupaty 0:d974bcee4f69 1311
cdupaty 0:d974bcee4f69 1312 if( _modem == FSK )
cdupaty 0:d974bcee4f69 1313 {
cdupaty 0:d974bcee4f69 1314 state = -1; // header is not available in FSK mode
cdupaty 0:d974bcee4f69 1315 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1316 printf("## FSK mode packets hasn't header ##");
cdupaty 0:d974bcee4f69 1317 printf("\n");
cdupaty 0:d974bcee4f69 1318 #endif
cdupaty 0:d974bcee4f69 1319 }
cdupaty 0:d974bcee4f69 1320 else
cdupaty 0:d974bcee4f69 1321 {
cdupaty 0:d974bcee4f69 1322 config1 = readRegister(REG_MODEM_CONFIG1); // Save config1 to modify only the header bit
cdupaty 0:d974bcee4f69 1323 if( _spreadingFactor == 6 )
cdupaty 0:d974bcee4f69 1324 {
cdupaty 0:d974bcee4f69 1325 state = -1; // Mandatory headerOFF with SF = 6
cdupaty 0:d974bcee4f69 1326 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1327 printf("## Mandatory implicit header mode with spreading factor = 6 ##");
cdupaty 0:d974bcee4f69 1328 #endif
cdupaty 0:d974bcee4f69 1329 }
cdupaty 0:d974bcee4f69 1330 else
cdupaty 0:d974bcee4f69 1331 {
cdupaty 0:d974bcee4f69 1332 // added by C. Pham
cdupaty 0:d974bcee4f69 1333 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1334 config1 = config1 & 0B11111011; // clears bit 2 from config1 = headerON
cdupaty 0:d974bcee4f69 1335 else
cdupaty 0:d974bcee4f69 1336 config1 = config1 & 0B11111110; // clears bit 0 from config1 = headerON
cdupaty 0:d974bcee4f69 1337
cdupaty 0:d974bcee4f69 1338 writeRegister(REG_MODEM_CONFIG1,config1); // Update config1
cdupaty 0:d974bcee4f69 1339 }
cdupaty 0:d974bcee4f69 1340
cdupaty 0:d974bcee4f69 1341 // added by C. Pham
cdupaty 0:d974bcee4f69 1342 uint8_t theHeaderBit;
cdupaty 0:d974bcee4f69 1343
cdupaty 0:d974bcee4f69 1344 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1345 theHeaderBit=2;
cdupaty 0:d974bcee4f69 1346 else
cdupaty 0:d974bcee4f69 1347 theHeaderBit=0;
cdupaty 0:d974bcee4f69 1348
cdupaty 0:d974bcee4f69 1349 if( _spreadingFactor != 6 )
cdupaty 0:d974bcee4f69 1350 { // checking headerON taking out bit 2 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 1351 config1 = readRegister(REG_MODEM_CONFIG1);
cdupaty 0:d974bcee4f69 1352 // modified by C. Pham
cdupaty 0:d974bcee4f69 1353 if( bitRead(config1, theHeaderBit) == HEADER_ON )
cdupaty 0:d974bcee4f69 1354 {
cdupaty 0:d974bcee4f69 1355 state = 0;
cdupaty 0:d974bcee4f69 1356 _header = HEADER_ON;
cdupaty 0:d974bcee4f69 1357 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1358 printf("## Header has been activated ##");
cdupaty 0:d974bcee4f69 1359 printf("\n");
cdupaty 0:d974bcee4f69 1360 #endif
cdupaty 0:d974bcee4f69 1361 }
cdupaty 0:d974bcee4f69 1362 else
cdupaty 0:d974bcee4f69 1363 {
cdupaty 0:d974bcee4f69 1364 state = 1;
cdupaty 0:d974bcee4f69 1365 }
cdupaty 0:d974bcee4f69 1366 }
cdupaty 0:d974bcee4f69 1367 // modifie par C.Dupaty
cdupaty 0:d974bcee4f69 1368 //return state;
cdupaty 0:d974bcee4f69 1369 }
cdupaty 0:d974bcee4f69 1370 return state;
cdupaty 0:d974bcee4f69 1371 }
cdupaty 0:d974bcee4f69 1372
cdupaty 0:d974bcee4f69 1373 /*
cdupaty 0:d974bcee4f69 1374 Function: Sets the module in implicit header mode (header is not sent).
cdupaty 0:d974bcee4f69 1375 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1376 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1377 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1378 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1379 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 1380 */
cdupaty 0:d974bcee4f69 1381 int8_t SX1272::setHeaderOFF()
cdupaty 0:d974bcee4f69 1382 {
cdupaty 0:d974bcee4f69 1383 int8_t state = 2;
cdupaty 0:d974bcee4f69 1384 byte config1;
cdupaty 0:d974bcee4f69 1385
cdupaty 0:d974bcee4f69 1386 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1387 printf("\n");
cdupaty 0:d974bcee4f69 1388 printf("Starting 'setHeaderOFF'\n");
cdupaty 0:d974bcee4f69 1389 #endif
cdupaty 0:d974bcee4f69 1390
cdupaty 0:d974bcee4f69 1391 if( _modem == FSK )
cdupaty 0:d974bcee4f69 1392 { // header is not available in FSK mode
cdupaty 0:d974bcee4f69 1393 state = -1;
cdupaty 0:d974bcee4f69 1394 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1395 printf("## Notice that FSK mode packets hasn't header ##");
cdupaty 0:d974bcee4f69 1396 printf("\n");
cdupaty 0:d974bcee4f69 1397 #endif
cdupaty 0:d974bcee4f69 1398 }
cdupaty 0:d974bcee4f69 1399 else
cdupaty 0:d974bcee4f69 1400 {
cdupaty 0:d974bcee4f69 1401 config1 = readRegister(REG_MODEM_CONFIG1); // Save config1 to modify only the header bit
cdupaty 0:d974bcee4f69 1402
cdupaty 0:d974bcee4f69 1403 // modified by C. Pham
cdupaty 0:d974bcee4f69 1404 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1405 config1 = config1 | 0B00000100; // sets bit 2 from REG_MODEM_CONFIG1 = headerOFF
cdupaty 0:d974bcee4f69 1406 else
cdupaty 0:d974bcee4f69 1407 config1 = config1 | 0B00000001; // sets bit 0 from REG_MODEM_CONFIG1 = headerOFF
cdupaty 0:d974bcee4f69 1408
cdupaty 0:d974bcee4f69 1409 writeRegister(REG_MODEM_CONFIG1,config1); // Update config1
cdupaty 0:d974bcee4f69 1410
cdupaty 0:d974bcee4f69 1411 config1 = readRegister(REG_MODEM_CONFIG1);
cdupaty 0:d974bcee4f69 1412
cdupaty 0:d974bcee4f69 1413 // added by C. Pham
cdupaty 0:d974bcee4f69 1414 uint8_t theHeaderBit;
cdupaty 0:d974bcee4f69 1415
cdupaty 0:d974bcee4f69 1416 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1417 theHeaderBit=2;
cdupaty 0:d974bcee4f69 1418 else
cdupaty 0:d974bcee4f69 1419 theHeaderBit=0;
cdupaty 0:d974bcee4f69 1420
cdupaty 0:d974bcee4f69 1421 if( bitRead(config1, theHeaderBit) == HEADER_OFF )
cdupaty 0:d974bcee4f69 1422 { // checking headerOFF taking out bit 2 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 1423 state = 0;
cdupaty 0:d974bcee4f69 1424 _header = HEADER_OFF;
cdupaty 0:d974bcee4f69 1425
cdupaty 0:d974bcee4f69 1426 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1427 printf("## Header has been desactivated ##");
cdupaty 0:d974bcee4f69 1428 printf("\n");
cdupaty 0:d974bcee4f69 1429 #endif
cdupaty 0:d974bcee4f69 1430 }
cdupaty 0:d974bcee4f69 1431 else
cdupaty 0:d974bcee4f69 1432 {
cdupaty 0:d974bcee4f69 1433 state = 1;
cdupaty 0:d974bcee4f69 1434 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1435 printf("** Header hasn't been desactivated ##");
cdupaty 0:d974bcee4f69 1436 printf("\n");
cdupaty 0:d974bcee4f69 1437 #endif
cdupaty 0:d974bcee4f69 1438 }
cdupaty 0:d974bcee4f69 1439 }
cdupaty 0:d974bcee4f69 1440 return state;
cdupaty 0:d974bcee4f69 1441 }
cdupaty 0:d974bcee4f69 1442
cdupaty 0:d974bcee4f69 1443 /*
cdupaty 0:d974bcee4f69 1444 Function: Indicates if module is configured with or without checking CRC.
cdupaty 0:d974bcee4f69 1445 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1446 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1447 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1448 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1449 */
cdupaty 0:d974bcee4f69 1450 uint8_t SX1272::getCRC()
cdupaty 0:d974bcee4f69 1451 {
cdupaty 0:d974bcee4f69 1452 int8_t state = 2;
cdupaty 0:d974bcee4f69 1453 byte value;
cdupaty 0:d974bcee4f69 1454
cdupaty 0:d974bcee4f69 1455 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1456 printf("\n");
cdupaty 0:d974bcee4f69 1457 printf("Starting 'getCRC'\n");
cdupaty 0:d974bcee4f69 1458 #endif
cdupaty 0:d974bcee4f69 1459
cdupaty 0:d974bcee4f69 1460 if( _modem == LORA )
cdupaty 0:d974bcee4f69 1461 { // LoRa mode
cdupaty 0:d974bcee4f69 1462
cdupaty 0:d974bcee4f69 1463 // added by C. Pham
cdupaty 0:d974bcee4f69 1464 uint8_t theRegister;
cdupaty 0:d974bcee4f69 1465 uint8_t theCrcBit;
cdupaty 0:d974bcee4f69 1466
cdupaty 0:d974bcee4f69 1467 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1468 theRegister=REG_MODEM_CONFIG1;
cdupaty 0:d974bcee4f69 1469 theCrcBit=1;
cdupaty 0:d974bcee4f69 1470 }
cdupaty 0:d974bcee4f69 1471 else {
cdupaty 0:d974bcee4f69 1472 theRegister=REG_MODEM_CONFIG2;
cdupaty 0:d974bcee4f69 1473 theCrcBit=2;
cdupaty 0:d974bcee4f69 1474 }
cdupaty 0:d974bcee4f69 1475
cdupaty 0:d974bcee4f69 1476 // take out bit 1 from REG_MODEM_CONFIG1 indicates RxPayloadCrcOn
cdupaty 0:d974bcee4f69 1477 value = readRegister(theRegister);
cdupaty 0:d974bcee4f69 1478 if( bitRead(value, theCrcBit) == CRC_OFF )
cdupaty 0:d974bcee4f69 1479 { // CRCoff
cdupaty 0:d974bcee4f69 1480 _CRC = CRC_OFF;
cdupaty 0:d974bcee4f69 1481 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1482 printf("## CRC is desactivated ##");
cdupaty 0:d974bcee4f69 1483 printf("\n");
cdupaty 0:d974bcee4f69 1484 #endif
cdupaty 0:d974bcee4f69 1485 state = 0;
cdupaty 0:d974bcee4f69 1486 }
cdupaty 0:d974bcee4f69 1487 else
cdupaty 0:d974bcee4f69 1488 { // CRCon
cdupaty 0:d974bcee4f69 1489 _CRC = CRC_ON;
cdupaty 0:d974bcee4f69 1490 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1491 printf("## CRC is activated ##");
cdupaty 0:d974bcee4f69 1492 printf("\n");
cdupaty 0:d974bcee4f69 1493 #endif
cdupaty 0:d974bcee4f69 1494 state = 0;
cdupaty 0:d974bcee4f69 1495 }
cdupaty 0:d974bcee4f69 1496 }
cdupaty 0:d974bcee4f69 1497 else
cdupaty 0:d974bcee4f69 1498 { // FSK mode
cdupaty 0:d974bcee4f69 1499
cdupaty 0:d974bcee4f69 1500 // take out bit 2 from REG_PACKET_CONFIG1 indicates CrcOn
cdupaty 0:d974bcee4f69 1501 value = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 1502 if( bitRead(value, 4) == CRC_OFF )
cdupaty 0:d974bcee4f69 1503 { // CRCoff
cdupaty 0:d974bcee4f69 1504 _CRC = CRC_OFF;
cdupaty 0:d974bcee4f69 1505 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1506 printf("## CRC is desactivated ##");
cdupaty 0:d974bcee4f69 1507 printf("\n");
cdupaty 0:d974bcee4f69 1508 #endif
cdupaty 0:d974bcee4f69 1509 state = 0;
cdupaty 0:d974bcee4f69 1510 }
cdupaty 0:d974bcee4f69 1511 else
cdupaty 0:d974bcee4f69 1512 { // CRCon
cdupaty 0:d974bcee4f69 1513 _CRC = CRC_ON;
cdupaty 0:d974bcee4f69 1514 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1515 printf("## CRC is activated ##");
cdupaty 0:d974bcee4f69 1516 printf("\n");
cdupaty 0:d974bcee4f69 1517 #endif
cdupaty 0:d974bcee4f69 1518 state = 0;
cdupaty 0:d974bcee4f69 1519 }
cdupaty 0:d974bcee4f69 1520 }
cdupaty 0:d974bcee4f69 1521 if( state != 0 )
cdupaty 0:d974bcee4f69 1522 {
cdupaty 0:d974bcee4f69 1523 state = 1;
cdupaty 0:d974bcee4f69 1524 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1525 printf("** There has been an error while getting configured CRC **");
cdupaty 0:d974bcee4f69 1526 printf("\n");
cdupaty 0:d974bcee4f69 1527 #endif
cdupaty 0:d974bcee4f69 1528 }
cdupaty 0:d974bcee4f69 1529 return state;
cdupaty 0:d974bcee4f69 1530 }
cdupaty 0:d974bcee4f69 1531
cdupaty 0:d974bcee4f69 1532 /*
cdupaty 0:d974bcee4f69 1533 Function: Sets the module with CRC on.
cdupaty 0:d974bcee4f69 1534 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1535 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1536 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1537 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1538 */
cdupaty 0:d974bcee4f69 1539 uint8_t SX1272::setCRC_ON()
cdupaty 0:d974bcee4f69 1540 {
cdupaty 0:d974bcee4f69 1541 uint8_t state = 2;
cdupaty 0:d974bcee4f69 1542 byte config1;
cdupaty 0:d974bcee4f69 1543
cdupaty 0:d974bcee4f69 1544 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1545 printf("\n");
cdupaty 0:d974bcee4f69 1546 printf("Starting 'setCRC_ON'\n");
cdupaty 0:d974bcee4f69 1547 #endif
cdupaty 0:d974bcee4f69 1548
cdupaty 0:d974bcee4f69 1549 if( _modem == LORA )
cdupaty 0:d974bcee4f69 1550 { // LORA mode
cdupaty 0:d974bcee4f69 1551
cdupaty 0:d974bcee4f69 1552 // added by C. Pham
cdupaty 0:d974bcee4f69 1553 uint8_t theRegister;
cdupaty 0:d974bcee4f69 1554 uint8_t theCrcBit;
cdupaty 0:d974bcee4f69 1555
cdupaty 0:d974bcee4f69 1556 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1557 theRegister=REG_MODEM_CONFIG1;
cdupaty 0:d974bcee4f69 1558 theCrcBit=1;
cdupaty 0:d974bcee4f69 1559 }
cdupaty 0:d974bcee4f69 1560 else {
cdupaty 0:d974bcee4f69 1561 theRegister=REG_MODEM_CONFIG2;
cdupaty 0:d974bcee4f69 1562 theCrcBit=2;
cdupaty 0:d974bcee4f69 1563 }
cdupaty 0:d974bcee4f69 1564
cdupaty 0:d974bcee4f69 1565 config1 = readRegister(theRegister); // Save config1 to modify only the CRC bit
cdupaty 0:d974bcee4f69 1566
cdupaty 0:d974bcee4f69 1567 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1568 config1 = config1 | 0B00000010; // sets bit 1 from REG_MODEM_CONFIG1 = CRC_ON
cdupaty 0:d974bcee4f69 1569 else
cdupaty 0:d974bcee4f69 1570 config1 = config1 | 0B00000100; // sets bit 2 from REG_MODEM_CONFIG2 = CRC_ON
cdupaty 0:d974bcee4f69 1571
cdupaty 0:d974bcee4f69 1572 writeRegister(theRegister,config1);
cdupaty 0:d974bcee4f69 1573
cdupaty 0:d974bcee4f69 1574 state = 1;
cdupaty 0:d974bcee4f69 1575
cdupaty 0:d974bcee4f69 1576 config1 = readRegister(theRegister);
cdupaty 0:d974bcee4f69 1577
cdupaty 0:d974bcee4f69 1578 if( bitRead(config1, theCrcBit) == CRC_ON )
cdupaty 0:d974bcee4f69 1579 { // take out bit 1 from REG_MODEM_CONFIG1 indicates RxPayloadCrcOn
cdupaty 0:d974bcee4f69 1580 state = 0;
cdupaty 0:d974bcee4f69 1581 _CRC = CRC_ON;
cdupaty 0:d974bcee4f69 1582 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1583 printf("## CRC has been activated ##");
cdupaty 0:d974bcee4f69 1584 printf("\n");
cdupaty 0:d974bcee4f69 1585 #endif
cdupaty 0:d974bcee4f69 1586 }
cdupaty 0:d974bcee4f69 1587 }
cdupaty 0:d974bcee4f69 1588 else
cdupaty 0:d974bcee4f69 1589 { // FSK mode
cdupaty 0:d974bcee4f69 1590 config1 = readRegister(REG_PACKET_CONFIG1); // Save config1 to modify only the CRC bit
cdupaty 0:d974bcee4f69 1591 config1 = config1 | 0B00010000; // set bit 4 and 3 from REG_MODEM_CONFIG1 = CRC_ON
cdupaty 0:d974bcee4f69 1592 writeRegister(REG_PACKET_CONFIG1,config1);
cdupaty 0:d974bcee4f69 1593
cdupaty 0:d974bcee4f69 1594 state = 1;
cdupaty 0:d974bcee4f69 1595
cdupaty 0:d974bcee4f69 1596 config1 = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 1597 if( bitRead(config1, 4) == CRC_ON )
cdupaty 0:d974bcee4f69 1598 { // take out bit 4 from REG_PACKET_CONFIG1 indicates CrcOn
cdupaty 0:d974bcee4f69 1599 state = 0;
cdupaty 0:d974bcee4f69 1600 _CRC = CRC_ON;
cdupaty 0:d974bcee4f69 1601 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1602 printf("## CRC has been activated ##");
cdupaty 0:d974bcee4f69 1603 printf("\n");
cdupaty 0:d974bcee4f69 1604 #endif
cdupaty 0:d974bcee4f69 1605 }
cdupaty 0:d974bcee4f69 1606 }
cdupaty 0:d974bcee4f69 1607 if( state != 0 )
cdupaty 0:d974bcee4f69 1608 {
cdupaty 0:d974bcee4f69 1609 state = 1;
cdupaty 0:d974bcee4f69 1610 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1611 printf("** There has been an error while setting CRC ON **");
cdupaty 0:d974bcee4f69 1612 printf("\n");
cdupaty 0:d974bcee4f69 1613 #endif
cdupaty 0:d974bcee4f69 1614 }
cdupaty 0:d974bcee4f69 1615 return state;
cdupaty 0:d974bcee4f69 1616 }
cdupaty 0:d974bcee4f69 1617
cdupaty 0:d974bcee4f69 1618 /*
cdupaty 0:d974bcee4f69 1619 Function: Sets the module with CRC off.
cdupaty 0:d974bcee4f69 1620 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1621 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1622 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1623 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1624 */
cdupaty 0:d974bcee4f69 1625 uint8_t SX1272::setCRC_OFF()
cdupaty 0:d974bcee4f69 1626 {
cdupaty 0:d974bcee4f69 1627 int8_t state = 2;
cdupaty 0:d974bcee4f69 1628 byte config1;
cdupaty 0:d974bcee4f69 1629
cdupaty 0:d974bcee4f69 1630 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1631 printf("\n");
cdupaty 0:d974bcee4f69 1632 printf("Starting 'setCRC_OFF'\n");
cdupaty 0:d974bcee4f69 1633 #endif
cdupaty 0:d974bcee4f69 1634
cdupaty 0:d974bcee4f69 1635 if( _modem == LORA )
cdupaty 0:d974bcee4f69 1636 { // LORA mode
cdupaty 0:d974bcee4f69 1637
cdupaty 0:d974bcee4f69 1638 // added by C. Pham
cdupaty 0:d974bcee4f69 1639 uint8_t theRegister;
cdupaty 0:d974bcee4f69 1640 uint8_t theCrcBit;
cdupaty 0:d974bcee4f69 1641
cdupaty 0:d974bcee4f69 1642 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1643 theRegister=REG_MODEM_CONFIG1;
cdupaty 0:d974bcee4f69 1644 theCrcBit=1;
cdupaty 0:d974bcee4f69 1645 }
cdupaty 0:d974bcee4f69 1646 else {
cdupaty 0:d974bcee4f69 1647 theRegister=REG_MODEM_CONFIG2;
cdupaty 0:d974bcee4f69 1648 theCrcBit=2;
cdupaty 0:d974bcee4f69 1649 }
cdupaty 0:d974bcee4f69 1650
cdupaty 0:d974bcee4f69 1651 config1 = readRegister(theRegister); // Save config1 to modify only the CRC bit
cdupaty 0:d974bcee4f69 1652 if (_board==SX1272Chip)
cdupaty 0:d974bcee4f69 1653 config1 = config1 & 0B11111101; // clears bit 1 from config1 = CRC_OFF
cdupaty 0:d974bcee4f69 1654 else
cdupaty 0:d974bcee4f69 1655 config1 = config1 & 0B11111011; // clears bit 2 from config1 = CRC_OFF
cdupaty 0:d974bcee4f69 1656
cdupaty 0:d974bcee4f69 1657 writeRegister(theRegister,config1);
cdupaty 0:d974bcee4f69 1658
cdupaty 0:d974bcee4f69 1659 config1 = readRegister(theRegister);
cdupaty 0:d974bcee4f69 1660 if( (bitRead(config1, theCrcBit)) == CRC_OFF )
cdupaty 0:d974bcee4f69 1661 { // take out bit 1 from REG_MODEM_CONFIG1 indicates RxPayloadCrcOn
cdupaty 0:d974bcee4f69 1662 state = 0;
cdupaty 0:d974bcee4f69 1663 _CRC = CRC_OFF;
cdupaty 0:d974bcee4f69 1664 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1665 printf("## CRC has been desactivated ##");
cdupaty 0:d974bcee4f69 1666 printf("\n");
cdupaty 0:d974bcee4f69 1667 #endif
cdupaty 0:d974bcee4f69 1668 }
cdupaty 0:d974bcee4f69 1669 }
cdupaty 0:d974bcee4f69 1670 else
cdupaty 0:d974bcee4f69 1671 { // FSK mode
cdupaty 0:d974bcee4f69 1672 config1 = readRegister(REG_PACKET_CONFIG1); // Save config1 to modify only the CRC bit
cdupaty 0:d974bcee4f69 1673 config1 = config1 & 0B11101111; // clears bit 4 from config1 = CRC_OFF
cdupaty 0:d974bcee4f69 1674 writeRegister(REG_PACKET_CONFIG1,config1);
cdupaty 0:d974bcee4f69 1675
cdupaty 0:d974bcee4f69 1676 config1 = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 1677 if( bitRead(config1, 4) == CRC_OFF )
cdupaty 0:d974bcee4f69 1678 { // take out bit 4 from REG_PACKET_CONFIG1 indicates RxPayloadCrcOn
cdupaty 0:d974bcee4f69 1679 state = 0;
cdupaty 0:d974bcee4f69 1680 _CRC = CRC_OFF;
cdupaty 0:d974bcee4f69 1681 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1682 printf("## CRC has been desactivated ##");
cdupaty 0:d974bcee4f69 1683 printf("\n");
cdupaty 0:d974bcee4f69 1684 #endif
cdupaty 0:d974bcee4f69 1685 }
cdupaty 0:d974bcee4f69 1686 }
cdupaty 0:d974bcee4f69 1687 if( state != 0 )
cdupaty 0:d974bcee4f69 1688 {
cdupaty 0:d974bcee4f69 1689 state = 1;
cdupaty 0:d974bcee4f69 1690 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1691 printf("** There has been an error while setting CRC OFF **");
cdupaty 0:d974bcee4f69 1692 printf("\n");
cdupaty 0:d974bcee4f69 1693 #endif
cdupaty 0:d974bcee4f69 1694 }
cdupaty 0:d974bcee4f69 1695 return state;
cdupaty 0:d974bcee4f69 1696 }
cdupaty 0:d974bcee4f69 1697
cdupaty 0:d974bcee4f69 1698 /*
cdupaty 0:d974bcee4f69 1699 Function: Checks if SF is a valid value.
cdupaty 0:d974bcee4f69 1700 Returns: Boolean that's 'true' if the SF value exists and
cdupaty 0:d974bcee4f69 1701 it's 'false' if the SF value does not exist.
cdupaty 0:d974bcee4f69 1702 Parameters:
cdupaty 0:d974bcee4f69 1703 spr: spreading factor value to check.
cdupaty 0:d974bcee4f69 1704 */
cdupaty 0:d974bcee4f69 1705 boolean SX1272::isSF(uint8_t spr)
cdupaty 0:d974bcee4f69 1706 {
cdupaty 0:d974bcee4f69 1707 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1708 printf("\n");
cdupaty 0:d974bcee4f69 1709 printf("Starting 'isSF'\n");
cdupaty 0:d974bcee4f69 1710 #endif
cdupaty 0:d974bcee4f69 1711
cdupaty 0:d974bcee4f69 1712 // Checking available values for _spreadingFactor
cdupaty 0:d974bcee4f69 1713 switch(spr)
cdupaty 0:d974bcee4f69 1714 {
cdupaty 0:d974bcee4f69 1715 case SF_6:
cdupaty 0:d974bcee4f69 1716 case SF_7:
cdupaty 0:d974bcee4f69 1717 case SF_8:
cdupaty 0:d974bcee4f69 1718 case SF_9:
cdupaty 0:d974bcee4f69 1719 case SF_10:
cdupaty 0:d974bcee4f69 1720 case SF_11:
cdupaty 0:d974bcee4f69 1721 case SF_12:
cdupaty 0:d974bcee4f69 1722 return true;
cdupaty 0:d974bcee4f69 1723 //break;
cdupaty 0:d974bcee4f69 1724
cdupaty 0:d974bcee4f69 1725 default:
cdupaty 0:d974bcee4f69 1726 return false;
cdupaty 0:d974bcee4f69 1727 }
cdupaty 0:d974bcee4f69 1728 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1729 printf("## Finished 'isSF' ##");
cdupaty 0:d974bcee4f69 1730 printf("\n");
cdupaty 0:d974bcee4f69 1731 #endif
cdupaty 0:d974bcee4f69 1732 }
cdupaty 0:d974bcee4f69 1733
cdupaty 0:d974bcee4f69 1734 /*
cdupaty 0:d974bcee4f69 1735 Function: Gets the SF within the module is configured.
cdupaty 0:d974bcee4f69 1736 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1737 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1738 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1739 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1740 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 1741 */
cdupaty 0:d974bcee4f69 1742 int8_t SX1272::getSF()
cdupaty 0:d974bcee4f69 1743 {
cdupaty 0:d974bcee4f69 1744 int8_t state = 2;
cdupaty 0:d974bcee4f69 1745 byte config2;
cdupaty 0:d974bcee4f69 1746
cdupaty 0:d974bcee4f69 1747 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1748 printf("\n");
cdupaty 0:d974bcee4f69 1749 printf("Starting 'getSF'\n");
cdupaty 0:d974bcee4f69 1750 #endif
cdupaty 0:d974bcee4f69 1751
cdupaty 0:d974bcee4f69 1752 if( _modem == FSK )
cdupaty 0:d974bcee4f69 1753 {
cdupaty 0:d974bcee4f69 1754 state = -1; // SF is not available in FSK mode
cdupaty 0:d974bcee4f69 1755 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1756 printf("** FSK mode hasn't spreading factor **");
cdupaty 0:d974bcee4f69 1757 printf("\n");
cdupaty 0:d974bcee4f69 1758 #endif
cdupaty 0:d974bcee4f69 1759 }
cdupaty 0:d974bcee4f69 1760 else
cdupaty 0:d974bcee4f69 1761 {
cdupaty 0:d974bcee4f69 1762 // take out bits 7-4 from REG_MODEM_CONFIG2 indicates _spreadingFactor
cdupaty 0:d974bcee4f69 1763 config2 = (readRegister(REG_MODEM_CONFIG2)) >> 4;
cdupaty 0:d974bcee4f69 1764 _spreadingFactor = config2;
cdupaty 0:d974bcee4f69 1765 state = 1;
cdupaty 0:d974bcee4f69 1766
cdupaty 0:d974bcee4f69 1767 if( (config2 == _spreadingFactor) && isSF(_spreadingFactor) )
cdupaty 0:d974bcee4f69 1768 {
cdupaty 0:d974bcee4f69 1769 state = 0;
cdupaty 0:d974bcee4f69 1770 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1771 printf("## Spreading factor is %X",_spreadingFactor);
cdupaty 0:d974bcee4f69 1772 // Serial.print(_spreadingFactor,HEX);
cdupaty 0:d974bcee4f69 1773 printf(" ##");
cdupaty 0:d974bcee4f69 1774 printf("\n");
cdupaty 0:d974bcee4f69 1775 #endif
cdupaty 0:d974bcee4f69 1776 }
cdupaty 0:d974bcee4f69 1777 }
cdupaty 0:d974bcee4f69 1778 return state;
cdupaty 0:d974bcee4f69 1779 }
cdupaty 0:d974bcee4f69 1780
cdupaty 0:d974bcee4f69 1781 /*
cdupaty 0:d974bcee4f69 1782 Function: Sets the indicated SF in the module.
cdupaty 0:d974bcee4f69 1783 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 1784 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 1785 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 1786 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 1787 Parameters:
cdupaty 0:d974bcee4f69 1788 spr: spreading factor value to set in LoRa modem configuration.
cdupaty 0:d974bcee4f69 1789 */
cdupaty 0:d974bcee4f69 1790 uint8_t SX1272::setSF(uint8_t spr)
cdupaty 0:d974bcee4f69 1791 {
cdupaty 0:d974bcee4f69 1792 byte st0;
cdupaty 0:d974bcee4f69 1793 int8_t state = 2;
cdupaty 0:d974bcee4f69 1794 byte config1;
cdupaty 0:d974bcee4f69 1795 byte config2;
cdupaty 0:d974bcee4f69 1796
cdupaty 0:d974bcee4f69 1797 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1798 printf("\n");
cdupaty 0:d974bcee4f69 1799 printf("Starting 'setSF'\n");
cdupaty 0:d974bcee4f69 1800 #endif
cdupaty 0:d974bcee4f69 1801
cdupaty 0:d974bcee4f69 1802 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 1803
cdupaty 0:d974bcee4f69 1804 if( _modem == FSK )
cdupaty 0:d974bcee4f69 1805 {
cdupaty 0:d974bcee4f69 1806 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 1807 printf("## Notice that FSK hasn't Spreading Factor parameter, ");
cdupaty 0:d974bcee4f69 1808 printf("so you are configuring it in LoRa mode ##");
cdupaty 0:d974bcee4f69 1809 #endif
cdupaty 0:d974bcee4f69 1810 state = setLORA(); // Setting LoRa mode
cdupaty 0:d974bcee4f69 1811 }
cdupaty 0:d974bcee4f69 1812 else
cdupaty 0:d974bcee4f69 1813 { // LoRa mode
cdupaty 0:d974bcee4f69 1814 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // LoRa standby mode
cdupaty 0:d974bcee4f69 1815 config2 = (readRegister(REG_MODEM_CONFIG2)); // Save config2 to modify SF value (bits 7-4)
cdupaty 0:d974bcee4f69 1816 switch(spr)
cdupaty 0:d974bcee4f69 1817 {
cdupaty 0:d974bcee4f69 1818 case SF_6: config2 = config2 & 0B01101111; // clears bits 7 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1819 config2 = config2 | 0B01100000; // sets bits 6 & 5 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1820 setHeaderOFF(); // Mandatory headerOFF with SF = 6
cdupaty 0:d974bcee4f69 1821 break;
cdupaty 0:d974bcee4f69 1822 case SF_7: config2 = config2 & 0B01111111; // clears bits 7 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1823 config2 = config2 | 0B01110000; // sets bits 6, 5 & 4
cdupaty 0:d974bcee4f69 1824 break;
cdupaty 0:d974bcee4f69 1825 case SF_8: config2 = config2 & 0B10001111; // clears bits 6, 5 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1826 config2 = config2 | 0B10000000; // sets bit 7 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1827 break;
cdupaty 0:d974bcee4f69 1828 case SF_9: config2 = config2 & 0B10011111; // clears bits 6, 5 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1829 config2 = config2 | 0B10010000; // sets bits 7 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1830 break;
cdupaty 0:d974bcee4f69 1831 case SF_10: config2 = config2 & 0B10101111; // clears bits 6 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1832 config2 = config2 | 0B10100000; // sets bits 7 & 5 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1833 break;
cdupaty 0:d974bcee4f69 1834 case SF_11: config2 = config2 & 0B10111111; // clears bit 6 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1835 config2 = config2 | 0B10110000; // sets bits 7, 5 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1836 getBW();
cdupaty 0:d974bcee4f69 1837
cdupaty 0:d974bcee4f69 1838 // modified by C. Pham
cdupaty 0:d974bcee4f69 1839 if( _bandwidth == BW_125)
cdupaty 0:d974bcee4f69 1840 { // LowDataRateOptimize (Mandatory with SF_11 if BW_125)
cdupaty 0:d974bcee4f69 1841 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1842 config1 = (readRegister(REG_MODEM_CONFIG1)); // Save config1 to modify only the LowDataRateOptimize
cdupaty 0:d974bcee4f69 1843 config1 = config1 | 0B00000001;
cdupaty 0:d974bcee4f69 1844 writeRegister(REG_MODEM_CONFIG1,config1);
cdupaty 0:d974bcee4f69 1845 }
cdupaty 0:d974bcee4f69 1846 else {
cdupaty 0:d974bcee4f69 1847 byte config3=readRegister(REG_MODEM_CONFIG3);
cdupaty 0:d974bcee4f69 1848 config3 = config3 | 0B00001000;
cdupaty 0:d974bcee4f69 1849 writeRegister(REG_MODEM_CONFIG3,config3);
cdupaty 0:d974bcee4f69 1850 }
cdupaty 0:d974bcee4f69 1851 }
cdupaty 0:d974bcee4f69 1852 break;
cdupaty 0:d974bcee4f69 1853 case SF_12: config2 = config2 & 0B11001111; // clears bits 5 & 4 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1854 config2 = config2 | 0B11000000; // sets bits 7 & 6 from REG_MODEM_CONFIG2
cdupaty 0:d974bcee4f69 1855 if( _bandwidth == BW_125)
cdupaty 0:d974bcee4f69 1856 { // LowDataRateOptimize (Mandatory with SF_12 if BW_125)
cdupaty 0:d974bcee4f69 1857 // modified by C. Pham
cdupaty 0:d974bcee4f69 1858 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1859 config1 = (readRegister(REG_MODEM_CONFIG1)); // Save config1 to modify only the LowDataRateOptimize
cdupaty 0:d974bcee4f69 1860 config1 = config1 | 0B00000001;
cdupaty 0:d974bcee4f69 1861 writeRegister(REG_MODEM_CONFIG1,config1);
cdupaty 0:d974bcee4f69 1862 }
cdupaty 0:d974bcee4f69 1863 else {
cdupaty 0:d974bcee4f69 1864 byte config3=readRegister(REG_MODEM_CONFIG3);
cdupaty 0:d974bcee4f69 1865 config3 = config3 | 0B00001000;
cdupaty 0:d974bcee4f69 1866 writeRegister(REG_MODEM_CONFIG3,config3);
cdupaty 0:d974bcee4f69 1867 }
cdupaty 0:d974bcee4f69 1868 }
cdupaty 0:d974bcee4f69 1869 break;
cdupaty 0:d974bcee4f69 1870 }
cdupaty 0:d974bcee4f69 1871
cdupaty 0:d974bcee4f69 1872 // Check if it is neccesary to set special settings for SF=6
cdupaty 0:d974bcee4f69 1873 if( spr == SF_6 )
cdupaty 0:d974bcee4f69 1874 {
cdupaty 0:d974bcee4f69 1875 // Mandatory headerOFF with SF = 6 (Implicit mode)
cdupaty 0:d974bcee4f69 1876 setHeaderOFF();
cdupaty 0:d974bcee4f69 1877
cdupaty 0:d974bcee4f69 1878 // Set the bit field DetectionOptimize of
cdupaty 0:d974bcee4f69 1879 // register RegLoRaDetectOptimize to value "0b101".
cdupaty 0:d974bcee4f69 1880 writeRegister(REG_DETECT_OPTIMIZE, 0x05);
cdupaty 0:d974bcee4f69 1881
cdupaty 0:d974bcee4f69 1882 // Write 0x0C in the register RegDetectionThreshold.
cdupaty 0:d974bcee4f69 1883 writeRegister(REG_DETECTION_THRESHOLD, 0x0C);
cdupaty 0:d974bcee4f69 1884 }
cdupaty 0:d974bcee4f69 1885 else
cdupaty 0:d974bcee4f69 1886 {
cdupaty 0:d974bcee4f69 1887 // added by C. Pham
cdupaty 0:d974bcee4f69 1888 setHeaderON();
cdupaty 0:d974bcee4f69 1889
cdupaty 0:d974bcee4f69 1890 // LoRa detection Optimize: 0x03 --> SF7 to SF12
cdupaty 0:d974bcee4f69 1891 writeRegister(REG_DETECT_OPTIMIZE, 0x03);
cdupaty 0:d974bcee4f69 1892
cdupaty 0:d974bcee4f69 1893 // LoRa detection threshold: 0x0A --> SF7 to SF12
cdupaty 0:d974bcee4f69 1894 writeRegister(REG_DETECTION_THRESHOLD, 0x0A);
cdupaty 0:d974bcee4f69 1895 }
cdupaty 0:d974bcee4f69 1896
cdupaty 0:d974bcee4f69 1897 // added by C. Pham
cdupaty 0:d974bcee4f69 1898 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1899 // comment by C. Pham
cdupaty 0:d974bcee4f69 1900 // bit 9:8 of SymbTimeout are then 11
cdupaty 0:d974bcee4f69 1901 // single_chan_pkt_fwd uses 00 and then 00001000
cdupaty 0:d974bcee4f69 1902 // why?
cdupaty 0:d974bcee4f69 1903 // sets bit 2-0 (AgcAutoOn and SymbTimout) for any SF value
cdupaty 0:d974bcee4f69 1904 //config2 = config2 | 0B00000111;
cdupaty 0:d974bcee4f69 1905 // modified by C. Pham
cdupaty 0:d974bcee4f69 1906 config2 = config2 | 0B00000100;
cdupaty 0:d974bcee4f69 1907 writeRegister(REG_MODEM_CONFIG1, config1); // Update config1
cdupaty 0:d974bcee4f69 1908 }
cdupaty 0:d974bcee4f69 1909 else {
cdupaty 0:d974bcee4f69 1910 // set the AgcAutoOn in bit 2 of REG_MODEM_CONFIG3
cdupaty 0:d974bcee4f69 1911 uint8_t config3 = (readRegister(REG_MODEM_CONFIG3));
cdupaty 0:d974bcee4f69 1912 config3=config3 | 0B00000100;
cdupaty 0:d974bcee4f69 1913 writeRegister(REG_MODEM_CONFIG3, config3);
cdupaty 0:d974bcee4f69 1914 }
cdupaty 0:d974bcee4f69 1915
cdupaty 0:d974bcee4f69 1916 // here we write the new SF
cdupaty 0:d974bcee4f69 1917 writeRegister(REG_MODEM_CONFIG2, config2); // Update config2
cdupaty 0:d974bcee4f69 1918
cdupaty 0:d974bcee4f69 1919 wait_ms(100);
cdupaty 0:d974bcee4f69 1920
cdupaty 0:d974bcee4f69 1921 // added by C. Pham
cdupaty 0:d974bcee4f69 1922 byte configAgc;
cdupaty 0:d974bcee4f69 1923 uint8_t theLDRBit;
cdupaty 0:d974bcee4f69 1924
cdupaty 0:d974bcee4f69 1925 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 1926 config1 = (readRegister(REG_MODEM_CONFIG1)); // Save config1 to check update
cdupaty 0:d974bcee4f69 1927 config2 = (readRegister(REG_MODEM_CONFIG2)); // Save config2 to check update
cdupaty 0:d974bcee4f69 1928 // comment by C. Pham
cdupaty 0:d974bcee4f69 1929 // (config2 >> 4) ---> take out bits 7-4 from REG_MODEM_CONFIG2 (=_spreadingFactor)
cdupaty 0:d974bcee4f69 1930 // bitRead(config1, 0) ---> take out bits 1 from config1 (=LowDataRateOptimize)
cdupaty 0:d974bcee4f69 1931 // config2 is only for the AgcAutoOn
cdupaty 0:d974bcee4f69 1932 configAgc=config2;
cdupaty 0:d974bcee4f69 1933 theLDRBit=0;
cdupaty 0:d974bcee4f69 1934 }
cdupaty 0:d974bcee4f69 1935 else {
cdupaty 0:d974bcee4f69 1936 config1 = (readRegister(REG_MODEM_CONFIG3)); // Save config1 to check update
cdupaty 0:d974bcee4f69 1937 config2 = (readRegister(REG_MODEM_CONFIG2));
cdupaty 0:d974bcee4f69 1938 // LowDataRateOptimize is in REG_MODEM_CONFIG3
cdupaty 0:d974bcee4f69 1939 // AgcAutoOn is in REG_MODEM_CONFIG3
cdupaty 0:d974bcee4f69 1940 configAgc=config1;
cdupaty 0:d974bcee4f69 1941 theLDRBit=3;
cdupaty 0:d974bcee4f69 1942 }
cdupaty 0:d974bcee4f69 1943
cdupaty 0:d974bcee4f69 1944
cdupaty 0:d974bcee4f69 1945 switch(spr)
cdupaty 0:d974bcee4f69 1946 {
cdupaty 0:d974bcee4f69 1947 case SF_6: if( ((config2 >> 4) == spr)
cdupaty 0:d974bcee4f69 1948 && (bitRead(configAgc, 2) == 1)
cdupaty 0:d974bcee4f69 1949 && (_header == HEADER_OFF))
cdupaty 0:d974bcee4f69 1950 {
cdupaty 0:d974bcee4f69 1951 state = 0;
cdupaty 0:d974bcee4f69 1952 }
cdupaty 0:d974bcee4f69 1953 break;
cdupaty 0:d974bcee4f69 1954 case SF_7: if( ((config2 >> 4) == 0x07)
cdupaty 0:d974bcee4f69 1955 && (bitRead(configAgc, 2) == 1))
cdupaty 0:d974bcee4f69 1956 {
cdupaty 0:d974bcee4f69 1957 state = 0;
cdupaty 0:d974bcee4f69 1958 }
cdupaty 0:d974bcee4f69 1959 break;
cdupaty 0:d974bcee4f69 1960 case SF_8: if( ((config2 >> 4) == 0x08)
cdupaty 0:d974bcee4f69 1961 && (bitRead(configAgc, 2) == 1))
cdupaty 0:d974bcee4f69 1962 {
cdupaty 0:d974bcee4f69 1963 state = 0;
cdupaty 0:d974bcee4f69 1964 }
cdupaty 0:d974bcee4f69 1965 break;
cdupaty 0:d974bcee4f69 1966 case SF_9: if( ((config2 >> 4) == 0x09)
cdupaty 0:d974bcee4f69 1967 && (bitRead(configAgc, 2) == 1))
cdupaty 0:d974bcee4f69 1968 {
cdupaty 0:d974bcee4f69 1969 state = 0;
cdupaty 0:d974bcee4f69 1970 }
cdupaty 0:d974bcee4f69 1971 break;
cdupaty 0:d974bcee4f69 1972 case SF_10: if( ((config2 >> 4) == 0x0A)
cdupaty 0:d974bcee4f69 1973 && (bitRead(configAgc, 2) == 1))
cdupaty 0:d974bcee4f69 1974 {
cdupaty 0:d974bcee4f69 1975 state = 0;
cdupaty 0:d974bcee4f69 1976 }
cdupaty 0:d974bcee4f69 1977 break;
cdupaty 0:d974bcee4f69 1978 case SF_11: if( ((config2 >> 4) == 0x0B)
cdupaty 0:d974bcee4f69 1979 && (bitRead(configAgc, 2) == 1)
cdupaty 0:d974bcee4f69 1980 && (bitRead(config1, theLDRBit) == 1))
cdupaty 0:d974bcee4f69 1981 {
cdupaty 0:d974bcee4f69 1982 state = 0;
cdupaty 0:d974bcee4f69 1983 }
cdupaty 0:d974bcee4f69 1984 break;
cdupaty 0:d974bcee4f69 1985 case SF_12: if( ((config2 >> 4) == 0x0C)
cdupaty 0:d974bcee4f69 1986 && (bitRead(configAgc, 2) == 1)
cdupaty 0:d974bcee4f69 1987 && (bitRead(config1, theLDRBit) == 1))
cdupaty 0:d974bcee4f69 1988 {
cdupaty 0:d974bcee4f69 1989 state = 0;
cdupaty 0:d974bcee4f69 1990 }
cdupaty 0:d974bcee4f69 1991 break;
cdupaty 0:d974bcee4f69 1992 default: state = 1;
cdupaty 0:d974bcee4f69 1993 }
cdupaty 0:d974bcee4f69 1994 }
cdupaty 0:d974bcee4f69 1995
cdupaty 0:d974bcee4f69 1996 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 1997 wait_ms(100);
cdupaty 0:d974bcee4f69 1998
cdupaty 0:d974bcee4f69 1999 if( isSF(spr) )
cdupaty 0:d974bcee4f69 2000 { // Checking available value for _spreadingFactor
cdupaty 0:d974bcee4f69 2001 state = 0;
cdupaty 0:d974bcee4f69 2002 _spreadingFactor = spr;
cdupaty 0:d974bcee4f69 2003 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2004 printf("## Spreading factor %d ",_spreadingFactor);
cdupaty 0:d974bcee4f69 2005 // Serial.print(_spreadingFactor, DEC);
cdupaty 0:d974bcee4f69 2006 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 2007 printf("\n");
cdupaty 0:d974bcee4f69 2008 #endif
cdupaty 0:d974bcee4f69 2009 }
cdupaty 0:d974bcee4f69 2010 else
cdupaty 0:d974bcee4f69 2011 {
cdupaty 0:d974bcee4f69 2012 if( state != 0 )
cdupaty 0:d974bcee4f69 2013 {
cdupaty 0:d974bcee4f69 2014 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2015 printf("** There has been an error while setting the spreading factor **");
cdupaty 0:d974bcee4f69 2016 printf("\n");
cdupaty 0:d974bcee4f69 2017 #endif
cdupaty 0:d974bcee4f69 2018 }
cdupaty 0:d974bcee4f69 2019 }
cdupaty 0:d974bcee4f69 2020 return state;
cdupaty 0:d974bcee4f69 2021 }
cdupaty 0:d974bcee4f69 2022
cdupaty 0:d974bcee4f69 2023 /*
cdupaty 0:d974bcee4f69 2024 Function: Checks if BW is a valid value.
cdupaty 0:d974bcee4f69 2025 Returns: Boolean that's 'true' if the BW value exists and
cdupaty 0:d974bcee4f69 2026 it's 'false' if the BW value does not exist.
cdupaty 0:d974bcee4f69 2027 Parameters:
cdupaty 0:d974bcee4f69 2028 band: bandwidth value to check.
cdupaty 0:d974bcee4f69 2029 */
cdupaty 0:d974bcee4f69 2030 boolean SX1272::isBW(uint16_t band)
cdupaty 0:d974bcee4f69 2031 {
cdupaty 0:d974bcee4f69 2032 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2033 printf("\n");
cdupaty 0:d974bcee4f69 2034 printf("Starting 'isBW'\n");
cdupaty 0:d974bcee4f69 2035 #endif
cdupaty 0:d974bcee4f69 2036
cdupaty 0:d974bcee4f69 2037 // Checking available values for _bandwidth
cdupaty 0:d974bcee4f69 2038 // added by C. Pham
cdupaty 0:d974bcee4f69 2039 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2040 switch(band)
cdupaty 0:d974bcee4f69 2041 {
cdupaty 0:d974bcee4f69 2042 case BW_125:
cdupaty 0:d974bcee4f69 2043 case BW_250:
cdupaty 0:d974bcee4f69 2044 case BW_500:
cdupaty 0:d974bcee4f69 2045 return true;
cdupaty 0:d974bcee4f69 2046 //break;
cdupaty 0:d974bcee4f69 2047
cdupaty 0:d974bcee4f69 2048 default:
cdupaty 0:d974bcee4f69 2049 return false;
cdupaty 0:d974bcee4f69 2050 }
cdupaty 0:d974bcee4f69 2051 }
cdupaty 0:d974bcee4f69 2052 else {
cdupaty 0:d974bcee4f69 2053 switch(band)
cdupaty 0:d974bcee4f69 2054 {
cdupaty 0:d974bcee4f69 2055 case BW_7_8:
cdupaty 0:d974bcee4f69 2056 case BW_10_4:
cdupaty 0:d974bcee4f69 2057 case BW_15_6:
cdupaty 0:d974bcee4f69 2058 case BW_20_8:
cdupaty 0:d974bcee4f69 2059 case BW_31_25:
cdupaty 0:d974bcee4f69 2060 case BW_41_7:
cdupaty 0:d974bcee4f69 2061 case BW_62_5:
cdupaty 0:d974bcee4f69 2062 case BW_125:
cdupaty 0:d974bcee4f69 2063 case BW_250:
cdupaty 0:d974bcee4f69 2064 case BW_500:
cdupaty 0:d974bcee4f69 2065 return true;
cdupaty 0:d974bcee4f69 2066 //break;
cdupaty 0:d974bcee4f69 2067
cdupaty 0:d974bcee4f69 2068 default:
cdupaty 0:d974bcee4f69 2069 return false;
cdupaty 0:d974bcee4f69 2070 }
cdupaty 0:d974bcee4f69 2071 }
cdupaty 0:d974bcee4f69 2072
cdupaty 0:d974bcee4f69 2073 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2074 printf("## Finished 'isBW' ##");
cdupaty 0:d974bcee4f69 2075 printf("\n");
cdupaty 0:d974bcee4f69 2076 #endif
cdupaty 0:d974bcee4f69 2077 }
cdupaty 0:d974bcee4f69 2078
cdupaty 0:d974bcee4f69 2079 /*
cdupaty 0:d974bcee4f69 2080 Function: Gets the BW within the module is configured.
cdupaty 0:d974bcee4f69 2081 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2082 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2083 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2084 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2085 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2086 */
cdupaty 0:d974bcee4f69 2087 int8_t SX1272::getBW()
cdupaty 0:d974bcee4f69 2088 {
cdupaty 0:d974bcee4f69 2089 int8_t state = 2;
cdupaty 0:d974bcee4f69 2090 byte config1;
cdupaty 0:d974bcee4f69 2091
cdupaty 0:d974bcee4f69 2092 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2093 printf("\n");
cdupaty 0:d974bcee4f69 2094 printf("Starting 'getBW'\n");
cdupaty 0:d974bcee4f69 2095 #endif
cdupaty 0:d974bcee4f69 2096
cdupaty 0:d974bcee4f69 2097 if( _modem == FSK )
cdupaty 0:d974bcee4f69 2098 {
cdupaty 0:d974bcee4f69 2099 state = -1; // BW is not available in FSK mode
cdupaty 0:d974bcee4f69 2100 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2101 printf("** FSK mode hasn't bandwidth **");
cdupaty 0:d974bcee4f69 2102 printf("\n");
cdupaty 0:d974bcee4f69 2103 #endif
cdupaty 0:d974bcee4f69 2104 }
cdupaty 0:d974bcee4f69 2105 else
cdupaty 0:d974bcee4f69 2106 {
cdupaty 0:d974bcee4f69 2107 // added by C. Pham
cdupaty 0:d974bcee4f69 2108 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2109 // take out bits 7-6 from REG_MODEM_CONFIG1 indicates _bandwidth
cdupaty 0:d974bcee4f69 2110 config1 = (readRegister(REG_MODEM_CONFIG1)) >> 6;
cdupaty 0:d974bcee4f69 2111 }
cdupaty 0:d974bcee4f69 2112 else {
cdupaty 0:d974bcee4f69 2113 // take out bits 7-4 from REG_MODEM_CONFIG1 indicates _bandwidth
cdupaty 0:d974bcee4f69 2114 config1 = (readRegister(REG_MODEM_CONFIG1)) >> 4;
cdupaty 0:d974bcee4f69 2115 }
cdupaty 0:d974bcee4f69 2116
cdupaty 0:d974bcee4f69 2117 _bandwidth = config1;
cdupaty 0:d974bcee4f69 2118
cdupaty 0:d974bcee4f69 2119 if( (config1 == _bandwidth) && isBW(_bandwidth) )
cdupaty 0:d974bcee4f69 2120 {
cdupaty 0:d974bcee4f69 2121 state = 0;
cdupaty 0:d974bcee4f69 2122 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2123 printf("## Bandwidth is %X ",_bandwidth);
cdupaty 0:d974bcee4f69 2124 // Serial.print(_bandwidth,HEX);
cdupaty 0:d974bcee4f69 2125 printf(" ##");
cdupaty 0:d974bcee4f69 2126 printf("\n");
cdupaty 0:d974bcee4f69 2127 #endif
cdupaty 0:d974bcee4f69 2128 }
cdupaty 0:d974bcee4f69 2129 else
cdupaty 0:d974bcee4f69 2130 {
cdupaty 0:d974bcee4f69 2131 state = 1;
cdupaty 0:d974bcee4f69 2132 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2133 printf("** There has been an error while getting bandwidth **");
cdupaty 0:d974bcee4f69 2134 printf("\n");
cdupaty 0:d974bcee4f69 2135 #endif
cdupaty 0:d974bcee4f69 2136 }
cdupaty 0:d974bcee4f69 2137 }
cdupaty 0:d974bcee4f69 2138 return state;
cdupaty 0:d974bcee4f69 2139 }
cdupaty 0:d974bcee4f69 2140
cdupaty 0:d974bcee4f69 2141 /*
cdupaty 0:d974bcee4f69 2142 Function: Sets the indicated BW in the module.
cdupaty 0:d974bcee4f69 2143 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2144 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2145 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2146 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2147 Parameters:
cdupaty 0:d974bcee4f69 2148 band: bandwith value to set in LoRa modem configuration.
cdupaty 0:d974bcee4f69 2149 */
cdupaty 0:d974bcee4f69 2150 int8_t SX1272::setBW(uint16_t band)
cdupaty 0:d974bcee4f69 2151 {
cdupaty 0:d974bcee4f69 2152 byte st0;
cdupaty 0:d974bcee4f69 2153 int8_t state = 2;
cdupaty 0:d974bcee4f69 2154 byte config1;
cdupaty 0:d974bcee4f69 2155
cdupaty 0:d974bcee4f69 2156 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2157 printf("\n");
cdupaty 0:d974bcee4f69 2158 printf("Starting 'setBW'\n");
cdupaty 0:d974bcee4f69 2159 #endif
cdupaty 0:d974bcee4f69 2160
cdupaty 0:d974bcee4f69 2161 if(!isBW(band) )
cdupaty 0:d974bcee4f69 2162 {
cdupaty 0:d974bcee4f69 2163 state = 1;
cdupaty 0:d974bcee4f69 2164 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2165 printf("** Bandwidth %X ",band);
cdupaty 0:d974bcee4f69 2166 // Serial.print(band, HEX);
cdupaty 0:d974bcee4f69 2167 printf(" is not a correct value **");
cdupaty 0:d974bcee4f69 2168 printf("\n");
cdupaty 0:d974bcee4f69 2169 #endif
cdupaty 0:d974bcee4f69 2170 return state;
cdupaty 0:d974bcee4f69 2171 }
cdupaty 0:d974bcee4f69 2172
cdupaty 0:d974bcee4f69 2173 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 2174
cdupaty 0:d974bcee4f69 2175 if( _modem == FSK )
cdupaty 0:d974bcee4f69 2176 {
cdupaty 0:d974bcee4f69 2177 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2178 printf("## Notice that FSK hasn't Bandwidth parameter, ");
cdupaty 0:d974bcee4f69 2179 printf("so you are configuring it in LoRa mode ##");
cdupaty 0:d974bcee4f69 2180 #endif
cdupaty 0:d974bcee4f69 2181 state = setLORA();
cdupaty 0:d974bcee4f69 2182 }
cdupaty 0:d974bcee4f69 2183 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // LoRa standby mode
cdupaty 0:d974bcee4f69 2184 config1 = (readRegister(REG_MODEM_CONFIG1)); // Save config1 to modify only the BW
cdupaty 0:d974bcee4f69 2185
cdupaty 0:d974bcee4f69 2186 // added by C. Pham for SX1276
cdupaty 0:d974bcee4f69 2187 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2188 switch(band)
cdupaty 0:d974bcee4f69 2189 {
cdupaty 0:d974bcee4f69 2190 case BW_125: config1 = config1 & 0B00111111; // clears bits 7 & 6 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2191 getSF();
cdupaty 0:d974bcee4f69 2192 if( _spreadingFactor == 11 )
cdupaty 0:d974bcee4f69 2193 { // LowDataRateOptimize (Mandatory with BW_125 if SF_11)
cdupaty 0:d974bcee4f69 2194 config1 = config1 | 0B00000001;
cdupaty 0:d974bcee4f69 2195 }
cdupaty 0:d974bcee4f69 2196 if( _spreadingFactor == 12 )
cdupaty 0:d974bcee4f69 2197 { // LowDataRateOptimize (Mandatory with BW_125 if SF_12)
cdupaty 0:d974bcee4f69 2198 config1 = config1 | 0B00000001;
cdupaty 0:d974bcee4f69 2199 }
cdupaty 0:d974bcee4f69 2200 break;
cdupaty 0:d974bcee4f69 2201 case BW_250: config1 = config1 & 0B01111111; // clears bit 7 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2202 config1 = config1 | 0B01000000; // sets bit 6 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2203 break;
cdupaty 0:d974bcee4f69 2204 case BW_500: config1 = config1 & 0B10111111; //clears bit 6 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2205 config1 = config1 | 0B10000000; //sets bit 7 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2206 break;
cdupaty 0:d974bcee4f69 2207 }
cdupaty 0:d974bcee4f69 2208 }
cdupaty 0:d974bcee4f69 2209 else {
cdupaty 0:d974bcee4f69 2210 // SX1276
cdupaty 0:d974bcee4f69 2211 config1 = config1 & 0B00001111; // clears bits 7 - 4 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2212 switch(band)
cdupaty 0:d974bcee4f69 2213 {
cdupaty 0:d974bcee4f69 2214 case BW_125:
cdupaty 0:d974bcee4f69 2215 // 0111
cdupaty 0:d974bcee4f69 2216 config1 = config1 | 0B01110000;
cdupaty 0:d974bcee4f69 2217 getSF();
cdupaty 0:d974bcee4f69 2218 if( _spreadingFactor == 11 || _spreadingFactor == 12)
cdupaty 0:d974bcee4f69 2219 { // LowDataRateOptimize (Mandatory with BW_125 if SF_11 or SF_12)
cdupaty 0:d974bcee4f69 2220 byte config3=readRegister(REG_MODEM_CONFIG3);
cdupaty 0:d974bcee4f69 2221 config3 = config3 | 0B00001000;
cdupaty 0:d974bcee4f69 2222 writeRegister(REG_MODEM_CONFIG3,config3);
cdupaty 0:d974bcee4f69 2223 }
cdupaty 0:d974bcee4f69 2224 break;
cdupaty 0:d974bcee4f69 2225 case BW_250:
cdupaty 0:d974bcee4f69 2226 // 1000
cdupaty 0:d974bcee4f69 2227 config1 = config1 | 0B10000000;
cdupaty 0:d974bcee4f69 2228 break;
cdupaty 0:d974bcee4f69 2229 case BW_500:
cdupaty 0:d974bcee4f69 2230 // 1001
cdupaty 0:d974bcee4f69 2231 config1 = config1 | 0B10010000;
cdupaty 0:d974bcee4f69 2232 break;
cdupaty 0:d974bcee4f69 2233 }
cdupaty 0:d974bcee4f69 2234 }
cdupaty 0:d974bcee4f69 2235 // end
cdupaty 0:d974bcee4f69 2236
cdupaty 0:d974bcee4f69 2237 writeRegister(REG_MODEM_CONFIG1,config1); // Update config1
cdupaty 0:d974bcee4f69 2238
cdupaty 0:d974bcee4f69 2239 wait_ms(100);
cdupaty 0:d974bcee4f69 2240
cdupaty 0:d974bcee4f69 2241 config1 = (readRegister(REG_MODEM_CONFIG1));
cdupaty 0:d974bcee4f69 2242
cdupaty 0:d974bcee4f69 2243 // added by C. Pham
cdupaty 0:d974bcee4f69 2244 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2245 // (config1 >> 6) ---> take out bits 7-6 from REG_MODEM_CONFIG1 (=_bandwidth)
cdupaty 0:d974bcee4f69 2246 switch(band)
cdupaty 0:d974bcee4f69 2247 {
cdupaty 0:d974bcee4f69 2248 case BW_125: if( (config1 >> 6) == SX1272_BW_125 )
cdupaty 0:d974bcee4f69 2249 {
cdupaty 0:d974bcee4f69 2250 state = 0;
cdupaty 0:d974bcee4f69 2251 if( _spreadingFactor == 11 )
cdupaty 0:d974bcee4f69 2252 {
cdupaty 0:d974bcee4f69 2253 if( bitRead(config1, 0) == 1 )
cdupaty 0:d974bcee4f69 2254 { // LowDataRateOptimize
cdupaty 0:d974bcee4f69 2255 state = 0;
cdupaty 0:d974bcee4f69 2256 }
cdupaty 0:d974bcee4f69 2257 else
cdupaty 0:d974bcee4f69 2258 {
cdupaty 0:d974bcee4f69 2259 state = 1;
cdupaty 0:d974bcee4f69 2260 }
cdupaty 0:d974bcee4f69 2261 }
cdupaty 0:d974bcee4f69 2262 if( _spreadingFactor == 12 )
cdupaty 0:d974bcee4f69 2263 {
cdupaty 0:d974bcee4f69 2264 if( bitRead(config1, 0) == 1 )
cdupaty 0:d974bcee4f69 2265 { // LowDataRateOptimize
cdupaty 0:d974bcee4f69 2266 state = 0;
cdupaty 0:d974bcee4f69 2267 }
cdupaty 0:d974bcee4f69 2268 else
cdupaty 0:d974bcee4f69 2269 {
cdupaty 0:d974bcee4f69 2270 state = 1;
cdupaty 0:d974bcee4f69 2271 }
cdupaty 0:d974bcee4f69 2272 }
cdupaty 0:d974bcee4f69 2273 }
cdupaty 0:d974bcee4f69 2274 break;
cdupaty 0:d974bcee4f69 2275 case BW_250: if( (config1 >> 6) == SX1272_BW_250 )
cdupaty 0:d974bcee4f69 2276 {
cdupaty 0:d974bcee4f69 2277 state = 0;
cdupaty 0:d974bcee4f69 2278 }
cdupaty 0:d974bcee4f69 2279 break;
cdupaty 0:d974bcee4f69 2280 case BW_500: if( (config1 >> 6) == SX1272_BW_500 )
cdupaty 0:d974bcee4f69 2281 {
cdupaty 0:d974bcee4f69 2282 state = 0;
cdupaty 0:d974bcee4f69 2283 }
cdupaty 0:d974bcee4f69 2284 break;
cdupaty 0:d974bcee4f69 2285 }
cdupaty 0:d974bcee4f69 2286 }
cdupaty 0:d974bcee4f69 2287 else {
cdupaty 0:d974bcee4f69 2288 // (config1 >> 4) ---> take out bits 7-4 from REG_MODEM_CONFIG1 (=_bandwidth)
cdupaty 0:d974bcee4f69 2289 switch(band)
cdupaty 0:d974bcee4f69 2290 {
cdupaty 0:d974bcee4f69 2291 case BW_125: if( (config1 >> 4) == BW_125 )
cdupaty 0:d974bcee4f69 2292 {
cdupaty 0:d974bcee4f69 2293 state = 0;
cdupaty 0:d974bcee4f69 2294
cdupaty 0:d974bcee4f69 2295 byte config3 = (readRegister(REG_MODEM_CONFIG3));
cdupaty 0:d974bcee4f69 2296
cdupaty 0:d974bcee4f69 2297 if( _spreadingFactor == 11 )
cdupaty 0:d974bcee4f69 2298 {
cdupaty 0:d974bcee4f69 2299 if( bitRead(config3, 3) == 1 )
cdupaty 0:d974bcee4f69 2300 { // LowDataRateOptimize
cdupaty 0:d974bcee4f69 2301 state = 0;
cdupaty 0:d974bcee4f69 2302 }
cdupaty 0:d974bcee4f69 2303 else
cdupaty 0:d974bcee4f69 2304 {
cdupaty 0:d974bcee4f69 2305 state = 1;
cdupaty 0:d974bcee4f69 2306 }
cdupaty 0:d974bcee4f69 2307 }
cdupaty 0:d974bcee4f69 2308 if( _spreadingFactor == 12 )
cdupaty 0:d974bcee4f69 2309 {
cdupaty 0:d974bcee4f69 2310 if( bitRead(config3, 3) == 1 )
cdupaty 0:d974bcee4f69 2311 { // LowDataRateOptimize
cdupaty 0:d974bcee4f69 2312 state = 0;
cdupaty 0:d974bcee4f69 2313 }
cdupaty 0:d974bcee4f69 2314 else
cdupaty 0:d974bcee4f69 2315 {
cdupaty 0:d974bcee4f69 2316 state = 1;
cdupaty 0:d974bcee4f69 2317 }
cdupaty 0:d974bcee4f69 2318 }
cdupaty 0:d974bcee4f69 2319 }
cdupaty 0:d974bcee4f69 2320 break;
cdupaty 0:d974bcee4f69 2321 case BW_250: if( (config1 >> 4) == BW_250 )
cdupaty 0:d974bcee4f69 2322 {
cdupaty 0:d974bcee4f69 2323 state = 0;
cdupaty 0:d974bcee4f69 2324 }
cdupaty 0:d974bcee4f69 2325 break;
cdupaty 0:d974bcee4f69 2326 case BW_500: if( (config1 >> 4) == BW_500 )
cdupaty 0:d974bcee4f69 2327 {
cdupaty 0:d974bcee4f69 2328 state = 0;
cdupaty 0:d974bcee4f69 2329 }
cdupaty 0:d974bcee4f69 2330 break;
cdupaty 0:d974bcee4f69 2331 }
cdupaty 0:d974bcee4f69 2332 }
cdupaty 0:d974bcee4f69 2333
cdupaty 0:d974bcee4f69 2334 if(state==0)
cdupaty 0:d974bcee4f69 2335 {
cdupaty 0:d974bcee4f69 2336 _bandwidth = band;
cdupaty 0:d974bcee4f69 2337 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2338 printf("## Bandwidth %X ",band);
cdupaty 0:d974bcee4f69 2339 // Serial.print(band, HEX);
cdupaty 0:d974bcee4f69 2340 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 2341 printf("\n");
cdupaty 0:d974bcee4f69 2342 #endif
cdupaty 0:d974bcee4f69 2343 }
cdupaty 0:d974bcee4f69 2344 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 2345 wait_ms(100);
cdupaty 0:d974bcee4f69 2346 return state;
cdupaty 0:d974bcee4f69 2347 }
cdupaty 0:d974bcee4f69 2348
cdupaty 0:d974bcee4f69 2349 /*
cdupaty 0:d974bcee4f69 2350 Function: Checks if CR is a valid value.
cdupaty 0:d974bcee4f69 2351 Returns: Boolean that's 'true' if the CR value exists and
cdupaty 0:d974bcee4f69 2352 it's 'false' if the CR value does not exist.
cdupaty 0:d974bcee4f69 2353 Parameters:
cdupaty 0:d974bcee4f69 2354 cod: coding rate value to check.
cdupaty 0:d974bcee4f69 2355 */
cdupaty 0:d974bcee4f69 2356 boolean SX1272::isCR(uint8_t cod)
cdupaty 0:d974bcee4f69 2357 {
cdupaty 0:d974bcee4f69 2358 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2359 printf("\n");
cdupaty 0:d974bcee4f69 2360 printf("Starting 'isCR'\n");
cdupaty 0:d974bcee4f69 2361 #endif
cdupaty 0:d974bcee4f69 2362
cdupaty 0:d974bcee4f69 2363 // Checking available values for _codingRate
cdupaty 0:d974bcee4f69 2364 switch(cod)
cdupaty 0:d974bcee4f69 2365 {
cdupaty 0:d974bcee4f69 2366 case CR_5:
cdupaty 0:d974bcee4f69 2367 case CR_6:
cdupaty 0:d974bcee4f69 2368 case CR_7:
cdupaty 0:d974bcee4f69 2369 case CR_8:
cdupaty 0:d974bcee4f69 2370 return true;
cdupaty 0:d974bcee4f69 2371 //break;
cdupaty 0:d974bcee4f69 2372
cdupaty 0:d974bcee4f69 2373 default:
cdupaty 0:d974bcee4f69 2374 return false;
cdupaty 0:d974bcee4f69 2375 }
cdupaty 0:d974bcee4f69 2376 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2377 printf("## Finished 'isCR' ##");
cdupaty 0:d974bcee4f69 2378 printf("\n");
cdupaty 0:d974bcee4f69 2379 #endif
cdupaty 0:d974bcee4f69 2380 }
cdupaty 0:d974bcee4f69 2381
cdupaty 0:d974bcee4f69 2382 /*
cdupaty 0:d974bcee4f69 2383 Function: Indicates the CR within the module is configured.
cdupaty 0:d974bcee4f69 2384 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2385 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2386 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2387 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2388 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2389 */
cdupaty 0:d974bcee4f69 2390 int8_t SX1272::getCR()
cdupaty 0:d974bcee4f69 2391 {
cdupaty 0:d974bcee4f69 2392 int8_t state = 2;
cdupaty 0:d974bcee4f69 2393 byte config1;
cdupaty 0:d974bcee4f69 2394
cdupaty 0:d974bcee4f69 2395 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2396 printf("\n");
cdupaty 0:d974bcee4f69 2397 printf("Starting 'getCR'\n");
cdupaty 0:d974bcee4f69 2398 #endif
cdupaty 0:d974bcee4f69 2399
cdupaty 0:d974bcee4f69 2400 if( _modem == FSK )
cdupaty 0:d974bcee4f69 2401 {
cdupaty 0:d974bcee4f69 2402 state = -1; // CR is not available in FSK mode
cdupaty 0:d974bcee4f69 2403 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2404 printf("** FSK mode hasn't coding rate **");
cdupaty 0:d974bcee4f69 2405 printf("\n");
cdupaty 0:d974bcee4f69 2406 #endif
cdupaty 0:d974bcee4f69 2407 }
cdupaty 0:d974bcee4f69 2408 else
cdupaty 0:d974bcee4f69 2409 {
cdupaty 0:d974bcee4f69 2410 // added by C. Pham
cdupaty 0:d974bcee4f69 2411 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2412 // take out bits 7-3 from REG_MODEM_CONFIG1 indicates _bandwidth & _codingRate
cdupaty 0:d974bcee4f69 2413 config1 = (readRegister(REG_MODEM_CONFIG1)) >> 3;
cdupaty 0:d974bcee4f69 2414 config1 = config1 & 0B00000111; // clears bits 7-3 ---> clears _bandwidth
cdupaty 0:d974bcee4f69 2415 }
cdupaty 0:d974bcee4f69 2416 else {
cdupaty 0:d974bcee4f69 2417 // take out bits 7-1 from REG_MODEM_CONFIG1 indicates _bandwidth & _codingRate
cdupaty 0:d974bcee4f69 2418 config1 = (readRegister(REG_MODEM_CONFIG1)) >> 1;
cdupaty 0:d974bcee4f69 2419 config1 = config1 & 0B00000111; // clears bits 7-3 ---> clears _bandwidth
cdupaty 0:d974bcee4f69 2420 }
cdupaty 0:d974bcee4f69 2421
cdupaty 0:d974bcee4f69 2422 _codingRate = config1;
cdupaty 0:d974bcee4f69 2423 state = 1;
cdupaty 0:d974bcee4f69 2424
cdupaty 0:d974bcee4f69 2425 if( (config1 == _codingRate) && isCR(_codingRate) )
cdupaty 0:d974bcee4f69 2426 {
cdupaty 0:d974bcee4f69 2427 state = 0;
cdupaty 0:d974bcee4f69 2428 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2429 printf("## Coding rate is %X ",_codingRate);
cdupaty 0:d974bcee4f69 2430 // Serial.print(_codingRate, HEX);
cdupaty 0:d974bcee4f69 2431 printf(" ##");
cdupaty 0:d974bcee4f69 2432 printf("\n");
cdupaty 0:d974bcee4f69 2433 #endif
cdupaty 0:d974bcee4f69 2434 }
cdupaty 0:d974bcee4f69 2435 }
cdupaty 0:d974bcee4f69 2436 return state;
cdupaty 0:d974bcee4f69 2437 }
cdupaty 0:d974bcee4f69 2438
cdupaty 0:d974bcee4f69 2439 /*
cdupaty 0:d974bcee4f69 2440 Function: Sets the indicated CR in the module.
cdupaty 0:d974bcee4f69 2441 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2442 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2443 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2444 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2445 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2446 Parameters:
cdupaty 0:d974bcee4f69 2447 cod: coding rate value to set in LoRa modem configuration.
cdupaty 0:d974bcee4f69 2448 */
cdupaty 0:d974bcee4f69 2449 int8_t SX1272::setCR(uint8_t cod)
cdupaty 0:d974bcee4f69 2450 {
cdupaty 0:d974bcee4f69 2451 byte st0;
cdupaty 0:d974bcee4f69 2452 int8_t state = 2;
cdupaty 0:d974bcee4f69 2453 byte config1;
cdupaty 0:d974bcee4f69 2454
cdupaty 0:d974bcee4f69 2455 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2456 printf("\n");
cdupaty 0:d974bcee4f69 2457 printf("Starting 'setCR'\n");
cdupaty 0:d974bcee4f69 2458 #endif
cdupaty 0:d974bcee4f69 2459
cdupaty 0:d974bcee4f69 2460 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 2461
cdupaty 0:d974bcee4f69 2462 if( _modem == FSK )
cdupaty 0:d974bcee4f69 2463 {
cdupaty 0:d974bcee4f69 2464 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2465 printf("## Notice that FSK hasn't Coding Rate parameter, ");
cdupaty 0:d974bcee4f69 2466 printf("so you are configuring it in LoRa mode ##");
cdupaty 0:d974bcee4f69 2467 #endif
cdupaty 0:d974bcee4f69 2468 state = setLORA();
cdupaty 0:d974bcee4f69 2469 }
cdupaty 0:d974bcee4f69 2470 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Set Standby mode to write in registers
cdupaty 0:d974bcee4f69 2471
cdupaty 0:d974bcee4f69 2472 config1 = readRegister(REG_MODEM_CONFIG1); // Save config1 to modify only the CR
cdupaty 0:d974bcee4f69 2473
cdupaty 0:d974bcee4f69 2474 // added by C. Pham
cdupaty 0:d974bcee4f69 2475 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2476 switch(cod)
cdupaty 0:d974bcee4f69 2477 {
cdupaty 0:d974bcee4f69 2478 case CR_5: config1 = config1 & 0B11001111; // clears bits 5 & 4 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2479 config1 = config1 | 0B00001000; // sets bit 3 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2480 break;
cdupaty 0:d974bcee4f69 2481 case CR_6: config1 = config1 & 0B11010111; // clears bits 5 & 3 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2482 config1 = config1 | 0B00010000; // sets bit 4 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2483 break;
cdupaty 0:d974bcee4f69 2484 case CR_7: config1 = config1 & 0B11011111; // clears bit 5 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2485 config1 = config1 | 0B00011000; // sets bits 4 & 3 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2486 break;
cdupaty 0:d974bcee4f69 2487 case CR_8: config1 = config1 & 0B11100111; // clears bits 4 & 3 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2488 config1 = config1 | 0B00100000; // sets bit 5 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2489 break;
cdupaty 0:d974bcee4f69 2490 }
cdupaty 0:d974bcee4f69 2491 }
cdupaty 0:d974bcee4f69 2492 else {
cdupaty 0:d974bcee4f69 2493 // SX1276
cdupaty 0:d974bcee4f69 2494 config1 = config1 & 0B11110001; // clears bits 3 - 1 from REG_MODEM_CONFIG1
cdupaty 0:d974bcee4f69 2495 switch(cod)
cdupaty 0:d974bcee4f69 2496 {
cdupaty 0:d974bcee4f69 2497 case CR_5:
cdupaty 0:d974bcee4f69 2498 config1 = config1 | 0B00000010;
cdupaty 0:d974bcee4f69 2499 break;
cdupaty 0:d974bcee4f69 2500 case CR_6:
cdupaty 0:d974bcee4f69 2501 config1 = config1 | 0B00000100;
cdupaty 0:d974bcee4f69 2502 break;
cdupaty 0:d974bcee4f69 2503 case CR_7:
cdupaty 0:d974bcee4f69 2504 config1 = config1 | 0B00000110;
cdupaty 0:d974bcee4f69 2505 break;
cdupaty 0:d974bcee4f69 2506 case CR_8:
cdupaty 0:d974bcee4f69 2507 config1 = config1 | 0B00001000;
cdupaty 0:d974bcee4f69 2508 break;
cdupaty 0:d974bcee4f69 2509 }
cdupaty 0:d974bcee4f69 2510 }
cdupaty 0:d974bcee4f69 2511 writeRegister(REG_MODEM_CONFIG1, config1); // Update config1
cdupaty 0:d974bcee4f69 2512
cdupaty 0:d974bcee4f69 2513 wait_ms(100);
cdupaty 0:d974bcee4f69 2514
cdupaty 0:d974bcee4f69 2515 config1 = readRegister(REG_MODEM_CONFIG1);
cdupaty 0:d974bcee4f69 2516
cdupaty 0:d974bcee4f69 2517 // added by C. Pham
cdupaty 0:d974bcee4f69 2518 uint8_t nshift=3;
cdupaty 0:d974bcee4f69 2519
cdupaty 0:d974bcee4f69 2520 // only 1 right shift for SX1276
cdupaty 0:d974bcee4f69 2521 if (_board==SX1276Chip)
cdupaty 0:d974bcee4f69 2522 nshift=1;
cdupaty 0:d974bcee4f69 2523
cdupaty 0:d974bcee4f69 2524 // ((config1 >> 3) & 0B0000111) ---> take out bits 5-3 from REG_MODEM_CONFIG1 (=_codingRate)
cdupaty 0:d974bcee4f69 2525 switch(cod)
cdupaty 0:d974bcee4f69 2526 {
cdupaty 0:d974bcee4f69 2527 case CR_5: if( ((config1 >> nshift) & 0B0000111) == 0x01 )
cdupaty 0:d974bcee4f69 2528 {
cdupaty 0:d974bcee4f69 2529 state = 0;
cdupaty 0:d974bcee4f69 2530 }
cdupaty 0:d974bcee4f69 2531 break;
cdupaty 0:d974bcee4f69 2532 case CR_6: if( ((config1 >> nshift) & 0B0000111) == 0x02 )
cdupaty 0:d974bcee4f69 2533 {
cdupaty 0:d974bcee4f69 2534 state = 0;
cdupaty 0:d974bcee4f69 2535 }
cdupaty 0:d974bcee4f69 2536 break;
cdupaty 0:d974bcee4f69 2537 case CR_7: if( ((config1 >> nshift) & 0B0000111) == 0x03 )
cdupaty 0:d974bcee4f69 2538 {
cdupaty 0:d974bcee4f69 2539 state = 0;
cdupaty 0:d974bcee4f69 2540 }
cdupaty 0:d974bcee4f69 2541 break;
cdupaty 0:d974bcee4f69 2542 case CR_8: if( ((config1 >> nshift) & 0B0000111) == 0x04 )
cdupaty 0:d974bcee4f69 2543 {
cdupaty 0:d974bcee4f69 2544 state = 0;
cdupaty 0:d974bcee4f69 2545 }
cdupaty 0:d974bcee4f69 2546 break;
cdupaty 0:d974bcee4f69 2547 }
cdupaty 0:d974bcee4f69 2548
cdupaty 0:d974bcee4f69 2549
cdupaty 0:d974bcee4f69 2550 if( isCR(cod) )
cdupaty 0:d974bcee4f69 2551 {
cdupaty 0:d974bcee4f69 2552 _codingRate = cod;
cdupaty 0:d974bcee4f69 2553 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2554 printf("## Coding Rate %X ",cod);
cdupaty 0:d974bcee4f69 2555 // Serial.print(cod, HEX);
cdupaty 0:d974bcee4f69 2556 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 2557 printf("\n");
cdupaty 0:d974bcee4f69 2558 #endif
cdupaty 0:d974bcee4f69 2559 }
cdupaty 0:d974bcee4f69 2560 else
cdupaty 0:d974bcee4f69 2561 {
cdupaty 0:d974bcee4f69 2562 state = 1;
cdupaty 0:d974bcee4f69 2563 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2564 printf("** There has been an error while configuring Coding Rate parameter **");
cdupaty 0:d974bcee4f69 2565 printf("\n");
cdupaty 0:d974bcee4f69 2566 #endif
cdupaty 0:d974bcee4f69 2567 }
cdupaty 0:d974bcee4f69 2568 writeRegister(REG_OP_MODE,st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 2569 wait_ms(100);
cdupaty 0:d974bcee4f69 2570 return state;
cdupaty 0:d974bcee4f69 2571 }
cdupaty 0:d974bcee4f69 2572
cdupaty 0:d974bcee4f69 2573 /*
cdupaty 0:d974bcee4f69 2574 Function: Checks if channel is a valid value.
cdupaty 0:d974bcee4f69 2575 Returns: Boolean that's 'true' if the CR value exists and
cdupaty 0:d974bcee4f69 2576 it's 'false' if the CR value does not exist.
cdupaty 0:d974bcee4f69 2577 Parameters:
cdupaty 0:d974bcee4f69 2578 ch: frequency channel value to check.
cdupaty 0:d974bcee4f69 2579 */
cdupaty 0:d974bcee4f69 2580 boolean SX1272::isChannel(uint32_t ch)
cdupaty 0:d974bcee4f69 2581 {
cdupaty 0:d974bcee4f69 2582 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2583 printf("\n");
cdupaty 0:d974bcee4f69 2584 printf("Starting 'isChannel'\n");
cdupaty 0:d974bcee4f69 2585 #endif
cdupaty 0:d974bcee4f69 2586
cdupaty 0:d974bcee4f69 2587 // Checking available values for _channel
cdupaty 0:d974bcee4f69 2588 switch(ch)
cdupaty 0:d974bcee4f69 2589 {
cdupaty 0:d974bcee4f69 2590 //added by C. Pham
cdupaty 0:d974bcee4f69 2591 case CH_04_868:
cdupaty 0:d974bcee4f69 2592 case CH_05_868:
cdupaty 0:d974bcee4f69 2593 case CH_06_868:
cdupaty 0:d974bcee4f69 2594 case CH_07_868:
cdupaty 0:d974bcee4f69 2595 case CH_08_868:
cdupaty 0:d974bcee4f69 2596 case CH_09_868:
cdupaty 0:d974bcee4f69 2597 //end
cdupaty 0:d974bcee4f69 2598 case CH_10_868:
cdupaty 0:d974bcee4f69 2599 case CH_11_868:
cdupaty 0:d974bcee4f69 2600 case CH_12_868:
cdupaty 0:d974bcee4f69 2601 case CH_13_868:
cdupaty 0:d974bcee4f69 2602 case CH_14_868:
cdupaty 0:d974bcee4f69 2603 case CH_15_868:
cdupaty 0:d974bcee4f69 2604 case CH_16_868:
cdupaty 0:d974bcee4f69 2605 case CH_17_868:
cdupaty 0:d974bcee4f69 2606 //added by C. Pham
cdupaty 0:d974bcee4f69 2607 case CH_18_868:
cdupaty 0:d974bcee4f69 2608 //end
cdupaty 0:d974bcee4f69 2609 case CH_00_900:
cdupaty 0:d974bcee4f69 2610 case CH_01_900:
cdupaty 0:d974bcee4f69 2611 case CH_02_900:
cdupaty 0:d974bcee4f69 2612 case CH_03_900:
cdupaty 0:d974bcee4f69 2613 case CH_04_900:
cdupaty 0:d974bcee4f69 2614 case CH_05_900:
cdupaty 0:d974bcee4f69 2615 case CH_06_900:
cdupaty 0:d974bcee4f69 2616 case CH_07_900:
cdupaty 0:d974bcee4f69 2617 case CH_08_900:
cdupaty 0:d974bcee4f69 2618 case CH_09_900:
cdupaty 0:d974bcee4f69 2619 case CH_10_900:
cdupaty 0:d974bcee4f69 2620 case CH_11_900:
cdupaty 0:d974bcee4f69 2621 //added by C. Pham
cdupaty 0:d974bcee4f69 2622 case CH_12_900:
cdupaty 0:d974bcee4f69 2623 case CH_00_433:
cdupaty 0:d974bcee4f69 2624 case CH_01_433:
cdupaty 0:d974bcee4f69 2625 case CH_02_433:
cdupaty 0:d974bcee4f69 2626 case CH_03_433:
cdupaty 0:d974bcee4f69 2627 //end
cdupaty 0:d974bcee4f69 2628 return true;
cdupaty 0:d974bcee4f69 2629 //break;
cdupaty 0:d974bcee4f69 2630
cdupaty 0:d974bcee4f69 2631 default:
cdupaty 0:d974bcee4f69 2632 return false;
cdupaty 0:d974bcee4f69 2633 }
cdupaty 0:d974bcee4f69 2634 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2635 printf("## Finished 'isChannel' ##");
cdupaty 0:d974bcee4f69 2636 printf("\n");
cdupaty 0:d974bcee4f69 2637 #endif
cdupaty 0:d974bcee4f69 2638 }
cdupaty 0:d974bcee4f69 2639
cdupaty 0:d974bcee4f69 2640 /*
cdupaty 0:d974bcee4f69 2641 Function: Indicates the frequency channel within the module is configured.
cdupaty 0:d974bcee4f69 2642 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2643 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2644 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2645 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2646 */
cdupaty 0:d974bcee4f69 2647 uint8_t SX1272::getChannel()
cdupaty 0:d974bcee4f69 2648 {
cdupaty 0:d974bcee4f69 2649 uint8_t state = 2;
cdupaty 0:d974bcee4f69 2650 uint32_t ch;
cdupaty 0:d974bcee4f69 2651 uint8_t freq3;
cdupaty 0:d974bcee4f69 2652 uint8_t freq2;
cdupaty 0:d974bcee4f69 2653 uint8_t freq1;
cdupaty 0:d974bcee4f69 2654
cdupaty 0:d974bcee4f69 2655 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2656 printf("\n");
cdupaty 0:d974bcee4f69 2657 printf("Starting 'getChannel'\n");
cdupaty 0:d974bcee4f69 2658 #endif
cdupaty 0:d974bcee4f69 2659
cdupaty 0:d974bcee4f69 2660 freq3 = readRegister(REG_FRF_MSB); // frequency channel MSB
cdupaty 0:d974bcee4f69 2661 freq2 = readRegister(REG_FRF_MID); // frequency channel MID
cdupaty 0:d974bcee4f69 2662 freq1 = readRegister(REG_FRF_LSB); // frequency channel LSB
cdupaty 0:d974bcee4f69 2663 ch = ((uint32_t)freq3 << 16) + ((uint32_t)freq2 << 8) + (uint32_t)freq1;
cdupaty 0:d974bcee4f69 2664 _channel = ch; // frequency channel
cdupaty 0:d974bcee4f69 2665
cdupaty 0:d974bcee4f69 2666 if( (_channel == ch) && isChannel(_channel) )
cdupaty 0:d974bcee4f69 2667 {
cdupaty 0:d974bcee4f69 2668 state = 0;
cdupaty 0:d974bcee4f69 2669 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2670 printf("## Frequency channel is %d ", _channel);
cdupaty 0:d974bcee4f69 2671 // Serial.print(_channel, HEX);
cdupaty 0:d974bcee4f69 2672 printf(" ##");
cdupaty 0:d974bcee4f69 2673 printf("\n");
cdupaty 0:d974bcee4f69 2674 #endif
cdupaty 0:d974bcee4f69 2675 }
cdupaty 0:d974bcee4f69 2676 else
cdupaty 0:d974bcee4f69 2677 {
cdupaty 0:d974bcee4f69 2678 state = 1;
cdupaty 0:d974bcee4f69 2679 }
cdupaty 0:d974bcee4f69 2680 return state;
cdupaty 0:d974bcee4f69 2681 }
cdupaty 0:d974bcee4f69 2682
cdupaty 0:d974bcee4f69 2683 /*
cdupaty 0:d974bcee4f69 2684 Function: Sets the indicated channel in the module.
cdupaty 0:d974bcee4f69 2685 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2686 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2687 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2688 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2689 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2690 Parameters:
cdupaty 0:d974bcee4f69 2691 ch: frequency channel value to set in configuration.
cdupaty 0:d974bcee4f69 2692 */
cdupaty 0:d974bcee4f69 2693 int8_t SX1272::setChannel(uint32_t ch)
cdupaty 0:d974bcee4f69 2694 {
cdupaty 0:d974bcee4f69 2695 byte st0;
cdupaty 0:d974bcee4f69 2696 int8_t state = 2;
cdupaty 0:d974bcee4f69 2697 unsigned int freq3;
cdupaty 0:d974bcee4f69 2698 unsigned int freq2;
cdupaty 0:d974bcee4f69 2699 uint8_t freq1;
cdupaty 0:d974bcee4f69 2700 uint32_t freq;
cdupaty 0:d974bcee4f69 2701
cdupaty 0:d974bcee4f69 2702 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2703 printf("\n");
cdupaty 0:d974bcee4f69 2704 printf("Starting 'setChannel'\n");
cdupaty 0:d974bcee4f69 2705 #endif
cdupaty 0:d974bcee4f69 2706
cdupaty 0:d974bcee4f69 2707 // added by C. Pham
cdupaty 0:d974bcee4f69 2708 _starttime=millis();
cdupaty 0:d974bcee4f69 2709
cdupaty 0:d974bcee4f69 2710 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 2711 if( _modem == LORA )
cdupaty 0:d974bcee4f69 2712 {
cdupaty 0:d974bcee4f69 2713 // LoRa Stdby mode in order to write in registers
cdupaty 0:d974bcee4f69 2714 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2715 }
cdupaty 0:d974bcee4f69 2716 else
cdupaty 0:d974bcee4f69 2717 {
cdupaty 0:d974bcee4f69 2718 // FSK Stdby mode in order to write in registers
cdupaty 0:d974bcee4f69 2719 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2720 }
cdupaty 0:d974bcee4f69 2721
cdupaty 0:d974bcee4f69 2722 freq3 = ((ch >> 16) & 0x0FF); // frequency channel MSB
cdupaty 0:d974bcee4f69 2723 freq2 = ((ch >> 8) & 0x0FF); // frequency channel MIB
cdupaty 0:d974bcee4f69 2724 freq1 = (ch & 0xFF); // frequency channel LSB
cdupaty 0:d974bcee4f69 2725
cdupaty 0:d974bcee4f69 2726 writeRegister(REG_FRF_MSB, freq3);
cdupaty 0:d974bcee4f69 2727 writeRegister(REG_FRF_MID, freq2);
cdupaty 0:d974bcee4f69 2728 writeRegister(REG_FRF_LSB, freq1);
cdupaty 0:d974bcee4f69 2729
cdupaty 0:d974bcee4f69 2730 // added by C. Pham
cdupaty 0:d974bcee4f69 2731 _stoptime=millis();
cdupaty 0:d974bcee4f69 2732
cdupaty 0:d974bcee4f69 2733 wait_ms(100);
cdupaty 0:d974bcee4f69 2734
cdupaty 0:d974bcee4f69 2735 // storing MSB in freq channel value
cdupaty 0:d974bcee4f69 2736 freq3 = (readRegister(REG_FRF_MSB));
cdupaty 0:d974bcee4f69 2737 freq = (freq3 << 8) & 0xFFFFFF;
cdupaty 0:d974bcee4f69 2738
cdupaty 0:d974bcee4f69 2739 // storing MID in freq channel value
cdupaty 0:d974bcee4f69 2740 freq2 = (readRegister(REG_FRF_MID));
cdupaty 0:d974bcee4f69 2741 freq = (freq << 8) + ((freq2 << 8) & 0xFFFFFF);
cdupaty 0:d974bcee4f69 2742
cdupaty 0:d974bcee4f69 2743 // storing LSB in freq channel value
cdupaty 0:d974bcee4f69 2744 freq = freq + ((readRegister(REG_FRF_LSB)) & 0xFFFFFF);
cdupaty 0:d974bcee4f69 2745
cdupaty 0:d974bcee4f69 2746 if( freq == ch )
cdupaty 0:d974bcee4f69 2747 {
cdupaty 0:d974bcee4f69 2748 state = 0;
cdupaty 0:d974bcee4f69 2749 _channel = ch;
cdupaty 0:d974bcee4f69 2750 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2751 printf("## Frequency channel %X ",ch);
cdupaty 0:d974bcee4f69 2752 // Serial.print(ch, HEX);
cdupaty 0:d974bcee4f69 2753 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 2754 printf("\n");
cdupaty 0:d974bcee4f69 2755 #endif
cdupaty 0:d974bcee4f69 2756 }
cdupaty 0:d974bcee4f69 2757 else
cdupaty 0:d974bcee4f69 2758 {
cdupaty 0:d974bcee4f69 2759 state = 1;
cdupaty 0:d974bcee4f69 2760 }
cdupaty 0:d974bcee4f69 2761
cdupaty 0:d974bcee4f69 2762 // commented by C. Pham to avoid adding new channel each time
cdupaty 0:d974bcee4f69 2763 // besides, the test above is sufficient
cdupaty 0:d974bcee4f69 2764 /*
cdupaty 0:d974bcee4f69 2765 if(!isChannel(ch) )
cdupaty 0:d974bcee4f69 2766 {
cdupaty 0:d974bcee4f69 2767 state = -1;
cdupaty 0:d974bcee4f69 2768 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2769 printf("** Frequency channel ");
cdupaty 0:d974bcee4f69 2770 Serial.print(ch, HEX);
cdupaty 0:d974bcee4f69 2771 printf("is not a correct value **");
cdupaty 0:d974bcee4f69 2772 printf("\n");
cdupaty 0:d974bcee4f69 2773 #endif
cdupaty 0:d974bcee4f69 2774 }
cdupaty 0:d974bcee4f69 2775 */
cdupaty 0:d974bcee4f69 2776
cdupaty 0:d974bcee4f69 2777 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 2778 wait_ms(100);
cdupaty 0:d974bcee4f69 2779 return state;
cdupaty 0:d974bcee4f69 2780 }
cdupaty 0:d974bcee4f69 2781
cdupaty 0:d974bcee4f69 2782 /*
cdupaty 0:d974bcee4f69 2783 Function: Gets the signal power within the module is configured.
cdupaty 0:d974bcee4f69 2784 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2785 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2786 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2787 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2788 */
cdupaty 0:d974bcee4f69 2789 uint8_t SX1272::getPower()
cdupaty 0:d974bcee4f69 2790 {
cdupaty 0:d974bcee4f69 2791 uint8_t state = 2;
cdupaty 0:d974bcee4f69 2792 int8_t value = 0x00; // modifie par C.Dupaty type byte a l'origine
cdupaty 0:d974bcee4f69 2793
cdupaty 0:d974bcee4f69 2794 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2795 printf("\n");
cdupaty 0:d974bcee4f69 2796 printf("Starting 'getPower'\n");
cdupaty 0:d974bcee4f69 2797 #endif
cdupaty 0:d974bcee4f69 2798
cdupaty 0:d974bcee4f69 2799 value = readRegister(REG_PA_CONFIG);
cdupaty 0:d974bcee4f69 2800 state = 1;
cdupaty 0:d974bcee4f69 2801
cdupaty 0:d974bcee4f69 2802 // modified by C. Pham
cdupaty 0:d974bcee4f69 2803 // get only the OutputPower
cdupaty 0:d974bcee4f69 2804 _power = value & 0B00001111;
cdupaty 0:d974bcee4f69 2805
cdupaty 0:d974bcee4f69 2806 if( (value > -1) & (value < 16) )
cdupaty 0:d974bcee4f69 2807 {
cdupaty 0:d974bcee4f69 2808 state = 0;
cdupaty 0:d974bcee4f69 2809 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2810 printf("## Output power is %X ",_power);
cdupaty 0:d974bcee4f69 2811 // Serial.print(_power, HEX);
cdupaty 0:d974bcee4f69 2812 printf(" ##");
cdupaty 0:d974bcee4f69 2813 printf("\n");
cdupaty 0:d974bcee4f69 2814 #endif
cdupaty 0:d974bcee4f69 2815 }
cdupaty 0:d974bcee4f69 2816
cdupaty 0:d974bcee4f69 2817 return state;
cdupaty 0:d974bcee4f69 2818 }
cdupaty 0:d974bcee4f69 2819
cdupaty 0:d974bcee4f69 2820 /*
cdupaty 0:d974bcee4f69 2821 Function: Sets the signal power indicated in the module.
cdupaty 0:d974bcee4f69 2822 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2823 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2824 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2825 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2826 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2827 Parameters:
cdupaty 0:d974bcee4f69 2828 p: power option to set in configuration.
cdupaty 0:d974bcee4f69 2829 */
cdupaty 0:d974bcee4f69 2830 int8_t SX1272::setPower(char p)
cdupaty 0:d974bcee4f69 2831 {
cdupaty 0:d974bcee4f69 2832 byte st0;
cdupaty 0:d974bcee4f69 2833 int8_t state = 2;
cdupaty 0:d974bcee4f69 2834 byte value = 0x00;
cdupaty 0:d974bcee4f69 2835
cdupaty 0:d974bcee4f69 2836 byte RegPaDacReg=(_board==SX1272Chip)?0x5A:0x4D;
cdupaty 0:d974bcee4f69 2837
cdupaty 0:d974bcee4f69 2838 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2839 printf("\n");
cdupaty 0:d974bcee4f69 2840 printf("Starting 'setPower'\n");
cdupaty 0:d974bcee4f69 2841 #endif
cdupaty 0:d974bcee4f69 2842
cdupaty 0:d974bcee4f69 2843 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 2844 if( _modem == LORA )
cdupaty 0:d974bcee4f69 2845 { // LoRa Stdby mode to write in registers
cdupaty 0:d974bcee4f69 2846 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2847 }
cdupaty 0:d974bcee4f69 2848 else
cdupaty 0:d974bcee4f69 2849 { // FSK Stdby mode to write in registers
cdupaty 0:d974bcee4f69 2850 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2851 }
cdupaty 0:d974bcee4f69 2852
cdupaty 0:d974bcee4f69 2853 switch (p)
cdupaty 0:d974bcee4f69 2854 {
cdupaty 0:d974bcee4f69 2855 // L = Low. On SX1272/76: PA0 on RFO setting
cdupaty 0:d974bcee4f69 2856 // H = High. On SX1272/76: PA0 on RFO setting
cdupaty 0:d974bcee4f69 2857 // M = MAX. On SX1272/76: PA0 on RFO setting
cdupaty 0:d974bcee4f69 2858
cdupaty 0:d974bcee4f69 2859 // x = extreme; added by C. Pham. On SX1272/76: PA1&PA2 PA_BOOST setting
cdupaty 0:d974bcee4f69 2860 // X = eXtreme; added by C. Pham. On SX1272/76: PA1&PA2 PA_BOOST setting + 20dBm settings
cdupaty 0:d974bcee4f69 2861
cdupaty 0:d974bcee4f69 2862 // added by C. Pham
cdupaty 0:d974bcee4f69 2863 //
cdupaty 0:d974bcee4f69 2864 case 'x':
cdupaty 0:d974bcee4f69 2865 case 'X':
cdupaty 0:d974bcee4f69 2866 case 'M': value = 0x0F;
cdupaty 0:d974bcee4f69 2867 // SX1272/76: 14dBm
cdupaty 0:d974bcee4f69 2868 break;
cdupaty 0:d974bcee4f69 2869
cdupaty 0:d974bcee4f69 2870 // modified by C. Pham, set to 0x03 instead of 0x00
cdupaty 0:d974bcee4f69 2871 case 'L': value = 0x03;
cdupaty 0:d974bcee4f69 2872 // SX1272/76: 2dBm
cdupaty 0:d974bcee4f69 2873 break;
cdupaty 0:d974bcee4f69 2874
cdupaty 0:d974bcee4f69 2875 case 'H': value = 0x07;
cdupaty 0:d974bcee4f69 2876 // SX1272/76: 6dBm
cdupaty 0:d974bcee4f69 2877 break;
cdupaty 0:d974bcee4f69 2878
cdupaty 0:d974bcee4f69 2879 default: state = -1;
cdupaty 0:d974bcee4f69 2880 break;
cdupaty 0:d974bcee4f69 2881 }
cdupaty 0:d974bcee4f69 2882
cdupaty 0:d974bcee4f69 2883 // 100mA
cdupaty 0:d974bcee4f69 2884 setMaxCurrent(0x0B);
cdupaty 0:d974bcee4f69 2885
cdupaty 0:d974bcee4f69 2886 if (p=='x') {
cdupaty 0:d974bcee4f69 2887 // we set only the PA_BOOST pin
cdupaty 0:d974bcee4f69 2888 // limit to 14dBm
cdupaty 0:d974bcee4f69 2889 value = 0x0C;
cdupaty 0:d974bcee4f69 2890 value = value & 0B10000000;
cdupaty 0:d974bcee4f69 2891 // set RegOcp for OcpOn and OcpTrim
cdupaty 0:d974bcee4f69 2892 // 130mA
cdupaty 0:d974bcee4f69 2893 setMaxCurrent(0x10);
cdupaty 0:d974bcee4f69 2894 }
cdupaty 0:d974bcee4f69 2895
cdupaty 0:d974bcee4f69 2896 if (p=='X') {
cdupaty 0:d974bcee4f69 2897 // normally value = 0x0F;
cdupaty 0:d974bcee4f69 2898 // we set the PA_BOOST pin
cdupaty 0:d974bcee4f69 2899 value = value & 0B10000000;
cdupaty 0:d974bcee4f69 2900 // and then set the high output power config with register REG_PA_DAC
cdupaty 0:d974bcee4f69 2901 writeRegister(RegPaDacReg, 0x87);
cdupaty 0:d974bcee4f69 2902 // set RegOcp for OcpOn and OcpTrim
cdupaty 0:d974bcee4f69 2903 // 150mA
cdupaty 0:d974bcee4f69 2904 setMaxCurrent(0x12);
cdupaty 0:d974bcee4f69 2905 }
cdupaty 0:d974bcee4f69 2906 else {
cdupaty 0:d974bcee4f69 2907 // disable high power output in all other cases
cdupaty 0:d974bcee4f69 2908 writeRegister(RegPaDacReg, 0x84);
cdupaty 0:d974bcee4f69 2909 }
cdupaty 0:d974bcee4f69 2910
cdupaty 0:d974bcee4f69 2911 // added by C. Pham
cdupaty 0:d974bcee4f69 2912 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 2913 // Pout = -1 + _power[3:0] on RFO
cdupaty 0:d974bcee4f69 2914 // Pout = 2 + _power[3:0] on PA_BOOST
cdupaty 0:d974bcee4f69 2915 // so: L=2dBm; H=6dBm, M=14dBm, x=14dBm (PA), X=20dBm(PA+PADAC)
cdupaty 0:d974bcee4f69 2916 writeRegister(REG_PA_CONFIG, value); // Setting output power value
cdupaty 0:d974bcee4f69 2917 }
cdupaty 0:d974bcee4f69 2918 else {
cdupaty 0:d974bcee4f69 2919 // for the SX1276
cdupaty 0:d974bcee4f69 2920
cdupaty 0:d974bcee4f69 2921 // set MaxPower to 7 -> Pmax=10.8+0.6*MaxPower [dBm] = 15
cdupaty 0:d974bcee4f69 2922 value = value & 0B01110000;
cdupaty 0:d974bcee4f69 2923
cdupaty 0:d974bcee4f69 2924 // then Pout = Pmax-(15-_power[3:0]) if PaSelect=0 (RFO pin for +14dBm)
cdupaty 0:d974bcee4f69 2925 // so L=3dBm; H=7dBm; M=15dBm (but should be limited to 14dBm by RFO pin)
cdupaty 0:d974bcee4f69 2926
cdupaty 0:d974bcee4f69 2927 // and Pout = 17-(15-_power[3:0]) if PaSelect=1 (PA_BOOST pin for +14dBm)
cdupaty 0:d974bcee4f69 2928 // so x= 14dBm (PA);
cdupaty 0:d974bcee4f69 2929 // when p=='X' for 20dBm, value is 0x0F and RegPaDacReg=0x87 so 20dBm is enabled
cdupaty 0:d974bcee4f69 2930
cdupaty 0:d974bcee4f69 2931 writeRegister(REG_PA_CONFIG, value);
cdupaty 0:d974bcee4f69 2932 }
cdupaty 0:d974bcee4f69 2933
cdupaty 0:d974bcee4f69 2934 _power=value;
cdupaty 0:d974bcee4f69 2935
cdupaty 0:d974bcee4f69 2936 value = readRegister(REG_PA_CONFIG);
cdupaty 0:d974bcee4f69 2937
cdupaty 0:d974bcee4f69 2938 if( value == _power )
cdupaty 0:d974bcee4f69 2939 {
cdupaty 0:d974bcee4f69 2940 state = 0;
cdupaty 0:d974bcee4f69 2941 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2942 printf("## Output power has been successfully set ##");
cdupaty 0:d974bcee4f69 2943 printf("\n");
cdupaty 0:d974bcee4f69 2944 #endif
cdupaty 0:d974bcee4f69 2945 }
cdupaty 0:d974bcee4f69 2946 else
cdupaty 0:d974bcee4f69 2947 {
cdupaty 0:d974bcee4f69 2948 state = 1;
cdupaty 0:d974bcee4f69 2949 }
cdupaty 0:d974bcee4f69 2950
cdupaty 0:d974bcee4f69 2951 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 2952 wait_ms(100);
cdupaty 0:d974bcee4f69 2953 return state;
cdupaty 0:d974bcee4f69 2954 }
cdupaty 0:d974bcee4f69 2955
cdupaty 0:d974bcee4f69 2956 /*
cdupaty 0:d974bcee4f69 2957 Function: Sets the signal power indicated in the module.
cdupaty 0:d974bcee4f69 2958 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 2959 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 2960 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 2961 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 2962 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 2963 Parameters:
cdupaty 0:d974bcee4f69 2964 p: power option to set in configuration.
cdupaty 0:d974bcee4f69 2965 */
cdupaty 0:d974bcee4f69 2966 int8_t SX1272::setPowerNum(uint8_t pow)
cdupaty 0:d974bcee4f69 2967 {
cdupaty 0:d974bcee4f69 2968 byte st0;
cdupaty 0:d974bcee4f69 2969 int8_t state = 2;
cdupaty 0:d974bcee4f69 2970 byte value = 0x00;
cdupaty 0:d974bcee4f69 2971
cdupaty 0:d974bcee4f69 2972 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2973 printf("\n");
cdupaty 0:d974bcee4f69 2974 printf("Starting 'setPower'\n");
cdupaty 0:d974bcee4f69 2975 #endif
cdupaty 0:d974bcee4f69 2976
cdupaty 0:d974bcee4f69 2977 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 2978 if( _modem == LORA )
cdupaty 0:d974bcee4f69 2979 { // LoRa Stdby mode to write in registers
cdupaty 0:d974bcee4f69 2980 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2981 }
cdupaty 0:d974bcee4f69 2982 else
cdupaty 0:d974bcee4f69 2983 { // FSK Stdby mode to write in registers
cdupaty 0:d974bcee4f69 2984 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);
cdupaty 0:d974bcee4f69 2985 }
cdupaty 0:d974bcee4f69 2986
cdupaty 0:d974bcee4f69 2987 // Modifie par C.Dupaty
cdupaty 0:d974bcee4f69 2988 // if ( (pow >= 0) & (pow < 15) )
cdupaty 0:d974bcee4f69 2989 if (pow < 15)
cdupaty 0:d974bcee4f69 2990 {
cdupaty 0:d974bcee4f69 2991 _power = pow;
cdupaty 0:d974bcee4f69 2992 }
cdupaty 0:d974bcee4f69 2993 else
cdupaty 0:d974bcee4f69 2994 {
cdupaty 0:d974bcee4f69 2995 state = -1;
cdupaty 0:d974bcee4f69 2996 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 2997 printf("## Power value is not valid ##");
cdupaty 0:d974bcee4f69 2998 printf("\n");
cdupaty 0:d974bcee4f69 2999 #endif
cdupaty 0:d974bcee4f69 3000 }
cdupaty 0:d974bcee4f69 3001
cdupaty 0:d974bcee4f69 3002 // added by C. Pham
cdupaty 0:d974bcee4f69 3003 if (_board==SX1276Chip) {
cdupaty 0:d974bcee4f69 3004 value=readRegister(REG_PA_CONFIG);
cdupaty 0:d974bcee4f69 3005 // clear OutputPower, but keep current value of PaSelect and MaxPower
cdupaty 0:d974bcee4f69 3006 value=value & 0B11110000;
cdupaty 0:d974bcee4f69 3007 value=value + _power;
cdupaty 0:d974bcee4f69 3008 _power=value;
cdupaty 0:d974bcee4f69 3009 }
cdupaty 0:d974bcee4f69 3010 writeRegister(REG_PA_CONFIG, _power); // Setting output power value
cdupaty 0:d974bcee4f69 3011 value = readRegister(REG_PA_CONFIG);
cdupaty 0:d974bcee4f69 3012
cdupaty 0:d974bcee4f69 3013 if( value == _power )
cdupaty 0:d974bcee4f69 3014 {
cdupaty 0:d974bcee4f69 3015 state = 0;
cdupaty 0:d974bcee4f69 3016 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3017 printf("## Output power has been successfully set ##");
cdupaty 0:d974bcee4f69 3018 printf("\n");
cdupaty 0:d974bcee4f69 3019 #endif
cdupaty 0:d974bcee4f69 3020 }
cdupaty 0:d974bcee4f69 3021 else
cdupaty 0:d974bcee4f69 3022 {
cdupaty 0:d974bcee4f69 3023 state = 1;
cdupaty 0:d974bcee4f69 3024 }
cdupaty 0:d974bcee4f69 3025
cdupaty 0:d974bcee4f69 3026 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3027 wait_ms(100);
cdupaty 0:d974bcee4f69 3028 return state;
cdupaty 0:d974bcee4f69 3029 }
cdupaty 0:d974bcee4f69 3030
cdupaty 0:d974bcee4f69 3031
cdupaty 0:d974bcee4f69 3032 /*
cdupaty 0:d974bcee4f69 3033 Function: Gets the preamble length from the module.
cdupaty 0:d974bcee4f69 3034 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3035 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3036 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3037 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3038 */
cdupaty 0:d974bcee4f69 3039 uint8_t SX1272::getPreambleLength()
cdupaty 0:d974bcee4f69 3040 {
cdupaty 0:d974bcee4f69 3041 int8_t state = 2;
cdupaty 0:d974bcee4f69 3042 uint8_t p_length;
cdupaty 0:d974bcee4f69 3043
cdupaty 0:d974bcee4f69 3044 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3045 printf("\n");
cdupaty 0:d974bcee4f69 3046 printf("Starting 'getPreambleLength'\n");
cdupaty 0:d974bcee4f69 3047 #endif
cdupaty 0:d974bcee4f69 3048
cdupaty 0:d974bcee4f69 3049 state = 1;
cdupaty 0:d974bcee4f69 3050 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3051 { // LORA mode
cdupaty 0:d974bcee4f69 3052 p_length = readRegister(REG_PREAMBLE_MSB_LORA);
cdupaty 0:d974bcee4f69 3053 // Saving MSB preamble length in LoRa mode
cdupaty 0:d974bcee4f69 3054 _preamblelength = (p_length << 8) & 0xFFFF;
cdupaty 0:d974bcee4f69 3055 p_length = readRegister(REG_PREAMBLE_LSB_LORA);
cdupaty 0:d974bcee4f69 3056 // Saving LSB preamble length in LoRa mode
cdupaty 0:d974bcee4f69 3057 _preamblelength = _preamblelength + (p_length & 0xFFFF);
cdupaty 0:d974bcee4f69 3058 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3059 printf("## Preamble length configured is %X ",_preamblelength);
cdupaty 0:d974bcee4f69 3060 // Serial.print(_preamblelength, HEX);
cdupaty 0:d974bcee4f69 3061 printf(" ##");
cdupaty 0:d974bcee4f69 3062 printf("\n");
cdupaty 0:d974bcee4f69 3063 #endif
cdupaty 0:d974bcee4f69 3064 }
cdupaty 0:d974bcee4f69 3065 else
cdupaty 0:d974bcee4f69 3066 { // FSK mode
cdupaty 0:d974bcee4f69 3067 p_length = readRegister(REG_PREAMBLE_MSB_FSK);
cdupaty 0:d974bcee4f69 3068 // Saving MSB preamble length in FSK mode
cdupaty 0:d974bcee4f69 3069 _preamblelength = (p_length << 8) & 0xFFFF;
cdupaty 0:d974bcee4f69 3070 p_length = readRegister(REG_PREAMBLE_LSB_FSK);
cdupaty 0:d974bcee4f69 3071 // Saving LSB preamble length in FSK mode
cdupaty 0:d974bcee4f69 3072 _preamblelength = _preamblelength + (p_length & 0xFFFF);
cdupaty 0:d974bcee4f69 3073 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3074 printf("## Preamble length configured is %X ",_preamblelength);
cdupaty 0:d974bcee4f69 3075 // Serial.print(_preamblelength, HEX);
cdupaty 0:d974bcee4f69 3076 printf(" ##");
cdupaty 0:d974bcee4f69 3077 printf("\n");
cdupaty 0:d974bcee4f69 3078 #endif
cdupaty 0:d974bcee4f69 3079 }
cdupaty 0:d974bcee4f69 3080 state = 0;
cdupaty 0:d974bcee4f69 3081 return state;
cdupaty 0:d974bcee4f69 3082 }
cdupaty 0:d974bcee4f69 3083
cdupaty 0:d974bcee4f69 3084 /*
cdupaty 0:d974bcee4f69 3085 Function: Sets the preamble length in the module
cdupaty 0:d974bcee4f69 3086 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3087 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3088 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3089 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3090 Parameters:
cdupaty 0:d974bcee4f69 3091 l: length value to set as preamble length.
cdupaty 0:d974bcee4f69 3092 */
cdupaty 0:d974bcee4f69 3093 uint8_t SX1272::setPreambleLength(uint16_t l)
cdupaty 0:d974bcee4f69 3094 {
cdupaty 0:d974bcee4f69 3095 byte st0;
cdupaty 0:d974bcee4f69 3096 uint8_t p_length;
cdupaty 0:d974bcee4f69 3097 int8_t state = 2;
cdupaty 0:d974bcee4f69 3098
cdupaty 0:d974bcee4f69 3099 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3100 printf("\n");
cdupaty 0:d974bcee4f69 3101 printf("Starting 'setPreambleLength'\n");
cdupaty 0:d974bcee4f69 3102 #endif
cdupaty 0:d974bcee4f69 3103
cdupaty 0:d974bcee4f69 3104 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 3105 state = 1;
cdupaty 0:d974bcee4f69 3106 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3107 { // LoRa mode
cdupaty 0:d974bcee4f69 3108 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Set Standby mode to write in registers
cdupaty 0:d974bcee4f69 3109 p_length = ((l >> 8) & 0x0FF);
cdupaty 0:d974bcee4f69 3110 // Storing MSB preamble length in LoRa mode
cdupaty 0:d974bcee4f69 3111 writeRegister(REG_PREAMBLE_MSB_LORA, p_length);
cdupaty 0:d974bcee4f69 3112 p_length = (l & 0x0FF);
cdupaty 0:d974bcee4f69 3113 // Storing LSB preamble length in LoRa mode
cdupaty 0:d974bcee4f69 3114 writeRegister(REG_PREAMBLE_LSB_LORA, p_length);
cdupaty 0:d974bcee4f69 3115 }
cdupaty 0:d974bcee4f69 3116 else
cdupaty 0:d974bcee4f69 3117 { // FSK mode
cdupaty 0:d974bcee4f69 3118 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Set Standby mode to write in registers
cdupaty 0:d974bcee4f69 3119 p_length = ((l >> 8) & 0x0FF);
cdupaty 0:d974bcee4f69 3120 // Storing MSB preamble length in FSK mode
cdupaty 0:d974bcee4f69 3121 writeRegister(REG_PREAMBLE_MSB_FSK, p_length);
cdupaty 0:d974bcee4f69 3122 p_length = (l & 0x0FF);
cdupaty 0:d974bcee4f69 3123 // Storing LSB preamble length in FSK mode
cdupaty 0:d974bcee4f69 3124 writeRegister(REG_PREAMBLE_LSB_FSK, p_length);
cdupaty 0:d974bcee4f69 3125 }
cdupaty 0:d974bcee4f69 3126
cdupaty 0:d974bcee4f69 3127 state = 0;
cdupaty 0:d974bcee4f69 3128 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3129 printf("## Preamble length %X ",l);
cdupaty 0:d974bcee4f69 3130 // Serial.print(l, HEX);
cdupaty 0:d974bcee4f69 3131 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 3132 printf("\n");
cdupaty 0:d974bcee4f69 3133 #endif
cdupaty 0:d974bcee4f69 3134
cdupaty 0:d974bcee4f69 3135 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3136 wait_ms(100);
cdupaty 0:d974bcee4f69 3137 return state;
cdupaty 0:d974bcee4f69 3138 }
cdupaty 0:d974bcee4f69 3139
cdupaty 0:d974bcee4f69 3140 /*
cdupaty 0:d974bcee4f69 3141 Function: Gets the payload length from the module.
cdupaty 0:d974bcee4f69 3142 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3143 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3144 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3145 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3146 */
cdupaty 0:d974bcee4f69 3147 uint8_t SX1272::getPayloadLength()
cdupaty 0:d974bcee4f69 3148 {
cdupaty 0:d974bcee4f69 3149 uint8_t state = 2;
cdupaty 0:d974bcee4f69 3150
cdupaty 0:d974bcee4f69 3151 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3152 printf("\n");
cdupaty 0:d974bcee4f69 3153 printf("Starting 'getPayloadLength'\n");
cdupaty 0:d974bcee4f69 3154 #endif
cdupaty 0:d974bcee4f69 3155
cdupaty 0:d974bcee4f69 3156 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3157 { // LORA mode
cdupaty 0:d974bcee4f69 3158 // Saving payload length in LoRa mode
cdupaty 0:d974bcee4f69 3159 _payloadlength = readRegister(REG_PAYLOAD_LENGTH_LORA);
cdupaty 0:d974bcee4f69 3160 state = 1;
cdupaty 0:d974bcee4f69 3161 }
cdupaty 0:d974bcee4f69 3162 else
cdupaty 0:d974bcee4f69 3163 { // FSK mode
cdupaty 0:d974bcee4f69 3164 // Saving payload length in FSK mode
cdupaty 0:d974bcee4f69 3165 _payloadlength = readRegister(REG_PAYLOAD_LENGTH_FSK);
cdupaty 0:d974bcee4f69 3166 state = 1;
cdupaty 0:d974bcee4f69 3167 }
cdupaty 0:d974bcee4f69 3168
cdupaty 0:d974bcee4f69 3169 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3170 printf("## Payload length configured is %X ",_payloadlength);
cdupaty 0:d974bcee4f69 3171 // Serial.print(_payloadlength, HEX);
cdupaty 0:d974bcee4f69 3172 printf(" ##");
cdupaty 0:d974bcee4f69 3173 printf("\n");
cdupaty 0:d974bcee4f69 3174 #endif
cdupaty 0:d974bcee4f69 3175
cdupaty 0:d974bcee4f69 3176 state = 0;
cdupaty 0:d974bcee4f69 3177 return state;
cdupaty 0:d974bcee4f69 3178 }
cdupaty 0:d974bcee4f69 3179
cdupaty 0:d974bcee4f69 3180 /*
cdupaty 0:d974bcee4f69 3181 Function: Sets the packet length in the module.
cdupaty 0:d974bcee4f69 3182 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3183 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3184 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3185 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3186 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 3187 */
cdupaty 0:d974bcee4f69 3188 int8_t SX1272::setPacketLength()
cdupaty 0:d974bcee4f69 3189 {
cdupaty 0:d974bcee4f69 3190 uint16_t length;
cdupaty 0:d974bcee4f69 3191
cdupaty 0:d974bcee4f69 3192 // added by C. Pham
cdupaty 0:d974bcee4f69 3193 // if gateway is in rawFormat mode for packet reception, it will also send in rawFormat
cdupaty 0:d974bcee4f69 3194 // unless we switch it back to normal format just for transmission, e.g. for downlink transmission
cdupaty 0:d974bcee4f69 3195 if (_rawFormat)
cdupaty 0:d974bcee4f69 3196 length = _payloadlength;
cdupaty 0:d974bcee4f69 3197 else
cdupaty 0:d974bcee4f69 3198 length = _payloadlength + OFFSET_PAYLOADLENGTH;
cdupaty 0:d974bcee4f69 3199
cdupaty 0:d974bcee4f69 3200 return setPacketLength(length);
cdupaty 0:d974bcee4f69 3201 }
cdupaty 0:d974bcee4f69 3202
cdupaty 0:d974bcee4f69 3203 /*
cdupaty 0:d974bcee4f69 3204 Function: Sets the packet length in the module.
cdupaty 0:d974bcee4f69 3205 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3206 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3207 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3208 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3209 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 3210 Parameters:
cdupaty 0:d974bcee4f69 3211 l: length value to set as payload length.
cdupaty 0:d974bcee4f69 3212 */
cdupaty 0:d974bcee4f69 3213 int8_t SX1272::setPacketLength(uint8_t l)
cdupaty 0:d974bcee4f69 3214 {
cdupaty 0:d974bcee4f69 3215 byte st0;
cdupaty 0:d974bcee4f69 3216 byte value = 0x00;
cdupaty 0:d974bcee4f69 3217 int8_t state = 2;
cdupaty 0:d974bcee4f69 3218
cdupaty 0:d974bcee4f69 3219 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3220 printf("\n");
cdupaty 0:d974bcee4f69 3221 printf("Starting 'setPacketLength'\n");
cdupaty 0:d974bcee4f69 3222 #endif
cdupaty 0:d974bcee4f69 3223
cdupaty 0:d974bcee4f69 3224 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 3225 packet_sent.length = l;
cdupaty 0:d974bcee4f69 3226
cdupaty 0:d974bcee4f69 3227 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3228 { // LORA mode
cdupaty 0:d974bcee4f69 3229 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Set LoRa Standby mode to write in registers
cdupaty 0:d974bcee4f69 3230 writeRegister(REG_PAYLOAD_LENGTH_LORA, packet_sent.length);
cdupaty 0:d974bcee4f69 3231 // Storing payload length in LoRa mode
cdupaty 0:d974bcee4f69 3232 value = readRegister(REG_PAYLOAD_LENGTH_LORA);
cdupaty 0:d974bcee4f69 3233 }
cdupaty 0:d974bcee4f69 3234 else
cdupaty 0:d974bcee4f69 3235 { // FSK mode
cdupaty 0:d974bcee4f69 3236 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Set FSK Standby mode to write in registers
cdupaty 0:d974bcee4f69 3237 writeRegister(REG_PAYLOAD_LENGTH_FSK, packet_sent.length);
cdupaty 0:d974bcee4f69 3238 // Storing payload length in FSK mode
cdupaty 0:d974bcee4f69 3239 value = readRegister(REG_PAYLOAD_LENGTH_FSK);
cdupaty 0:d974bcee4f69 3240 }
cdupaty 0:d974bcee4f69 3241
cdupaty 0:d974bcee4f69 3242 if( packet_sent.length == value )
cdupaty 0:d974bcee4f69 3243 {
cdupaty 0:d974bcee4f69 3244 state = 0;
cdupaty 0:d974bcee4f69 3245 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3246 printf("## Packet length %d ",packet_sent.length);
cdupaty 0:d974bcee4f69 3247 // Serial.print(packet_sent.length, DEC);
cdupaty 0:d974bcee4f69 3248 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 3249 printf("\n");
cdupaty 0:d974bcee4f69 3250 #endif
cdupaty 0:d974bcee4f69 3251 }
cdupaty 0:d974bcee4f69 3252 else
cdupaty 0:d974bcee4f69 3253 {
cdupaty 0:d974bcee4f69 3254 state = 1;
cdupaty 0:d974bcee4f69 3255 }
cdupaty 0:d974bcee4f69 3256
cdupaty 0:d974bcee4f69 3257 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3258 // comment by C. Pham
cdupaty 0:d974bcee4f69 3259 // this delay is included in the send delay overhead
cdupaty 0:d974bcee4f69 3260 // TODO: do we really need this delay?
cdupaty 0:d974bcee4f69 3261 wait_ms(250);
cdupaty 0:d974bcee4f69 3262 return state;
cdupaty 0:d974bcee4f69 3263 }
cdupaty 0:d974bcee4f69 3264
cdupaty 0:d974bcee4f69 3265 /*
cdupaty 0:d974bcee4f69 3266 Function: Gets the node address in the module.
cdupaty 0:d974bcee4f69 3267 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3268 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3269 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3270 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3271 */
cdupaty 0:d974bcee4f69 3272 uint8_t SX1272::getNodeAddress()
cdupaty 0:d974bcee4f69 3273 {
cdupaty 0:d974bcee4f69 3274 byte st0 = 0;
cdupaty 0:d974bcee4f69 3275 uint8_t state = 2;
cdupaty 0:d974bcee4f69 3276
cdupaty 0:d974bcee4f69 3277 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3278 printf("\n");
cdupaty 0:d974bcee4f69 3279 printf("Starting 'getNodeAddress'\n");
cdupaty 0:d974bcee4f69 3280 #endif
cdupaty 0:d974bcee4f69 3281
cdupaty 0:d974bcee4f69 3282 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3283 { // LoRa mode
cdupaty 0:d974bcee4f69 3284 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 3285 // Allowing access to FSK registers while in LoRa standby mode
cdupaty 0:d974bcee4f69 3286 writeRegister(REG_OP_MODE, LORA_STANDBY_FSK_REGS_MODE);
cdupaty 0:d974bcee4f69 3287 }
cdupaty 0:d974bcee4f69 3288
cdupaty 0:d974bcee4f69 3289 // Saving node address
cdupaty 0:d974bcee4f69 3290 _nodeAddress = readRegister(REG_NODE_ADRS);
cdupaty 0:d974bcee4f69 3291 state = 1;
cdupaty 0:d974bcee4f69 3292
cdupaty 0:d974bcee4f69 3293 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3294 {
cdupaty 0:d974bcee4f69 3295 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3296 }
cdupaty 0:d974bcee4f69 3297
cdupaty 0:d974bcee4f69 3298 state = 0;
cdupaty 0:d974bcee4f69 3299 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3300 printf("## Node address configured is %d ", _nodeAddress);
cdupaty 0:d974bcee4f69 3301 // Serial.print(_nodeAddress);
cdupaty 0:d974bcee4f69 3302 printf(" ##");
cdupaty 0:d974bcee4f69 3303 printf("\n");
cdupaty 0:d974bcee4f69 3304 #endif
cdupaty 0:d974bcee4f69 3305 return state;
cdupaty 0:d974bcee4f69 3306 }
cdupaty 0:d974bcee4f69 3307
cdupaty 0:d974bcee4f69 3308 /*
cdupaty 0:d974bcee4f69 3309 Function: Sets the node address in the module.
cdupaty 0:d974bcee4f69 3310 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3311 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3312 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3313 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3314 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 3315 Parameters:
cdupaty 0:d974bcee4f69 3316 addr: address value to set as node address.
cdupaty 0:d974bcee4f69 3317 */
cdupaty 0:d974bcee4f69 3318 int8_t SX1272::setNodeAddress(uint8_t addr)
cdupaty 0:d974bcee4f69 3319 {
cdupaty 0:d974bcee4f69 3320 byte st0;
cdupaty 0:d974bcee4f69 3321 uint8_t value; // type byte a l origine. Modifie par C.Dupaty
cdupaty 0:d974bcee4f69 3322 int8_t state = 2; // type uint_8 a l origine
cdupaty 0:d974bcee4f69 3323
cdupaty 0:d974bcee4f69 3324 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3325 printf("\n");
cdupaty 0:d974bcee4f69 3326 printf("Starting 'setNodeAddress'\n");
cdupaty 0:d974bcee4f69 3327 #endif
cdupaty 0:d974bcee4f69 3328
cdupaty 0:d974bcee4f69 3329 if( addr > 255 )
cdupaty 0:d974bcee4f69 3330 {
cdupaty 0:d974bcee4f69 3331 state = -1;
cdupaty 0:d974bcee4f69 3332 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3333 printf("** Node address must be less than 255 **");
cdupaty 0:d974bcee4f69 3334 printf("\n");
cdupaty 0:d974bcee4f69 3335 #endif
cdupaty 0:d974bcee4f69 3336 }
cdupaty 0:d974bcee4f69 3337 else
cdupaty 0:d974bcee4f69 3338 {
cdupaty 0:d974bcee4f69 3339 // Saving node address
cdupaty 0:d974bcee4f69 3340 _nodeAddress = addr;
cdupaty 0:d974bcee4f69 3341 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 3342
cdupaty 0:d974bcee4f69 3343 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3344 { // Allowing access to FSK registers while in LoRa standby mode
cdupaty 0:d974bcee4f69 3345 writeRegister(REG_OP_MODE, LORA_STANDBY_FSK_REGS_MODE);
cdupaty 0:d974bcee4f69 3346 }
cdupaty 0:d974bcee4f69 3347 else
cdupaty 0:d974bcee4f69 3348 { //Set FSK Standby mode to write in registers
cdupaty 0:d974bcee4f69 3349 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);
cdupaty 0:d974bcee4f69 3350 }
cdupaty 0:d974bcee4f69 3351
cdupaty 0:d974bcee4f69 3352 // Storing node and broadcast address
cdupaty 0:d974bcee4f69 3353 writeRegister(REG_NODE_ADRS, addr);
cdupaty 0:d974bcee4f69 3354 writeRegister(REG_BROADCAST_ADRS, BROADCAST_0);
cdupaty 0:d974bcee4f69 3355
cdupaty 0:d974bcee4f69 3356 value = readRegister(REG_NODE_ADRS);
cdupaty 0:d974bcee4f69 3357 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3358
cdupaty 0:d974bcee4f69 3359 if( value == _nodeAddress )
cdupaty 0:d974bcee4f69 3360 {
cdupaty 0:d974bcee4f69 3361 state = 0;
cdupaty 0:d974bcee4f69 3362 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3363 printf("## Node address %d ",addr);
cdupaty 0:d974bcee4f69 3364 // Serial.print(addr);
cdupaty 0:d974bcee4f69 3365 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 3366 printf("\n");
cdupaty 0:d974bcee4f69 3367 #endif
cdupaty 0:d974bcee4f69 3368 }
cdupaty 0:d974bcee4f69 3369 else
cdupaty 0:d974bcee4f69 3370 {
cdupaty 0:d974bcee4f69 3371 state = 1;
cdupaty 0:d974bcee4f69 3372 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3373 printf("** There has been an error while setting address ##");
cdupaty 0:d974bcee4f69 3374 printf("\n");
cdupaty 0:d974bcee4f69 3375 #endif
cdupaty 0:d974bcee4f69 3376 }
cdupaty 0:d974bcee4f69 3377 }
cdupaty 0:d974bcee4f69 3378 return state;
cdupaty 0:d974bcee4f69 3379 }
cdupaty 0:d974bcee4f69 3380
cdupaty 0:d974bcee4f69 3381 /*
cdupaty 0:d974bcee4f69 3382 Function: Gets the SNR value in LoRa mode.
cdupaty 0:d974bcee4f69 3383 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3384 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3385 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3386 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3387 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 3388 */
cdupaty 0:d974bcee4f69 3389 int8_t SX1272::getSNR()
cdupaty 0:d974bcee4f69 3390 { // getSNR exists only in LoRa mode
cdupaty 0:d974bcee4f69 3391 int8_t state = 2;
cdupaty 0:d974bcee4f69 3392 byte value;
cdupaty 0:d974bcee4f69 3393
cdupaty 0:d974bcee4f69 3394 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3395 printf("\n");
cdupaty 0:d974bcee4f69 3396 printf("Starting 'getSNR'\n");
cdupaty 0:d974bcee4f69 3397 #endif
cdupaty 0:d974bcee4f69 3398
cdupaty 0:d974bcee4f69 3399 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3400 { // LoRa mode
cdupaty 0:d974bcee4f69 3401 state = 1;
cdupaty 0:d974bcee4f69 3402 value = readRegister(REG_PKT_SNR_VALUE);
cdupaty 0:d974bcee4f69 3403 _rawSNR = value;
cdupaty 0:d974bcee4f69 3404
cdupaty 0:d974bcee4f69 3405 if( value & 0x80 ) // The SNR sign bit is 1
cdupaty 0:d974bcee4f69 3406 {
cdupaty 0:d974bcee4f69 3407 // Invert and divide by 4
cdupaty 0:d974bcee4f69 3408 value = ( ( ~value + 1 ) & 0xFF ) >> 2;
cdupaty 0:d974bcee4f69 3409 _SNR = -value;
cdupaty 0:d974bcee4f69 3410 }
cdupaty 0:d974bcee4f69 3411 else
cdupaty 0:d974bcee4f69 3412 {
cdupaty 0:d974bcee4f69 3413 // Divide by 4
cdupaty 0:d974bcee4f69 3414 _SNR = ( value & 0xFF ) >> 2;
cdupaty 0:d974bcee4f69 3415 }
cdupaty 0:d974bcee4f69 3416 state = 0;
cdupaty 0:d974bcee4f69 3417 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 3418 printf("## SNR value is %d\n",_SNR);
cdupaty 0:d974bcee4f69 3419 // Serial.print(_SNR, DEC);
cdupaty 0:d974bcee4f69 3420 printf(" ##");
cdupaty 0:d974bcee4f69 3421 printf("\n");
cdupaty 0:d974bcee4f69 3422 #endif
cdupaty 0:d974bcee4f69 3423 }
cdupaty 0:d974bcee4f69 3424 else
cdupaty 0:d974bcee4f69 3425 { // forbidden command if FSK mode
cdupaty 0:d974bcee4f69 3426 state = -1;
cdupaty 0:d974bcee4f69 3427 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 3428 printf("** SNR does not exist in FSK mode **");
cdupaty 0:d974bcee4f69 3429 printf("\n");
cdupaty 0:d974bcee4f69 3430 #endif
cdupaty 0:d974bcee4f69 3431 }
cdupaty 0:d974bcee4f69 3432 return state;
cdupaty 0:d974bcee4f69 3433 }
cdupaty 0:d974bcee4f69 3434
cdupaty 0:d974bcee4f69 3435 /*
cdupaty 0:d974bcee4f69 3436 Function: Gets the current value of RSSI.
cdupaty 0:d974bcee4f69 3437 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3438 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3439 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3440 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3441 */
cdupaty 0:d974bcee4f69 3442 uint8_t SX1272::getRSSI()
cdupaty 0:d974bcee4f69 3443 {
cdupaty 0:d974bcee4f69 3444 uint8_t state = 2;
cdupaty 0:d974bcee4f69 3445 int rssi_mean = 0;
cdupaty 0:d974bcee4f69 3446 int total = 5;
cdupaty 0:d974bcee4f69 3447
afzalsamira 7:e99757b7c421 3448 //#if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3449 printf("\n");
afzalsamira 7:e99757b7c421 3450 pc.printf("Starting 'getRSSI'\n");
afzalsamira 7:e99757b7c421 3451 //#endif
cdupaty 0:d974bcee4f69 3452
cdupaty 0:d974bcee4f69 3453 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3454 {
cdupaty 0:d974bcee4f69 3455 /// LoRa mode
cdupaty 0:d974bcee4f69 3456 // get mean value of RSSI
cdupaty 0:d974bcee4f69 3457 for(int i = 0; i < total; i++)
cdupaty 0:d974bcee4f69 3458 {
cdupaty 0:d974bcee4f69 3459 // modified by C. Pham
cdupaty 0:d974bcee4f69 3460 // with SX1276 we have to add 18 to OFFSET_RSSI to obtain -157
cdupaty 0:d974bcee4f69 3461 _RSSI = -(OFFSET_RSSI+(_board==SX1276Chip?18:0)) + readRegister(REG_RSSI_VALUE_LORA);
cdupaty 0:d974bcee4f69 3462 rssi_mean += _RSSI;
cdupaty 0:d974bcee4f69 3463 }
cdupaty 0:d974bcee4f69 3464
cdupaty 0:d974bcee4f69 3465 rssi_mean = rssi_mean / total;
cdupaty 0:d974bcee4f69 3466 _RSSI = rssi_mean;
cdupaty 0:d974bcee4f69 3467
cdupaty 0:d974bcee4f69 3468 state = 0;
afzalsamira 7:e99757b7c421 3469 //#if (SX1272_debug_mode > 0)
afzalsamira 7:e99757b7c421 3470 pc.printf("## RSSI value is %d",_RSSI);
cdupaty 0:d974bcee4f69 3471 // Serial.print(_RSSI, DEC);
cdupaty 0:d974bcee4f69 3472 printf(" ##");
cdupaty 0:d974bcee4f69 3473 printf("\n");
afzalsamira 7:e99757b7c421 3474 //#endif
cdupaty 0:d974bcee4f69 3475 }
cdupaty 0:d974bcee4f69 3476 else
cdupaty 0:d974bcee4f69 3477 {
cdupaty 0:d974bcee4f69 3478 /// FSK mode
cdupaty 0:d974bcee4f69 3479 // get mean value of RSSI
cdupaty 0:d974bcee4f69 3480 for(int i = 0; i < total; i++)
cdupaty 0:d974bcee4f69 3481 {
cdupaty 0:d974bcee4f69 3482 _RSSI = (readRegister(REG_RSSI_VALUE_FSK) >> 1);
cdupaty 0:d974bcee4f69 3483 rssi_mean += _RSSI;
cdupaty 0:d974bcee4f69 3484 }
cdupaty 0:d974bcee4f69 3485 rssi_mean = rssi_mean / total;
cdupaty 0:d974bcee4f69 3486 _RSSI = rssi_mean;
cdupaty 0:d974bcee4f69 3487
cdupaty 0:d974bcee4f69 3488 state = 0;
cdupaty 0:d974bcee4f69 3489
afzalsamira 7:e99757b7c421 3490 //#if (SX1272_debug_mode > 0)
afzalsamira 7:e99757b7c421 3491 pc.printf("## RSSI value is %d ",_RSSI);
cdupaty 0:d974bcee4f69 3492 //Serial.print(_RSSI);
cdupaty 0:d974bcee4f69 3493 printf(" ##");
cdupaty 0:d974bcee4f69 3494 printf("\n");
afzalsamira 7:e99757b7c421 3495 //#endif
cdupaty 0:d974bcee4f69 3496 }
cdupaty 0:d974bcee4f69 3497 return state;
cdupaty 0:d974bcee4f69 3498 }
cdupaty 0:d974bcee4f69 3499
cdupaty 0:d974bcee4f69 3500 /*
cdupaty 0:d974bcee4f69 3501 Function: Gets the RSSI of the last packet received in LoRa mode.
cdupaty 0:d974bcee4f69 3502 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3503 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3504 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3505 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3506 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 3507 */
cdupaty 0:d974bcee4f69 3508 int16_t SX1272::getRSSIpacket()
cdupaty 0:d974bcee4f69 3509 { // RSSIpacket only exists in LoRa
cdupaty 0:d974bcee4f69 3510 int8_t state = 2;
cdupaty 0:d974bcee4f69 3511
cdupaty 0:d974bcee4f69 3512 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3513 printf("\n");
cdupaty 0:d974bcee4f69 3514 printf("Starting 'getRSSIpacket'\n");
cdupaty 0:d974bcee4f69 3515 #endif
cdupaty 0:d974bcee4f69 3516
cdupaty 0:d974bcee4f69 3517 state = 1;
cdupaty 0:d974bcee4f69 3518 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3519 { // LoRa mode
cdupaty 0:d974bcee4f69 3520 state = getSNR();
cdupaty 0:d974bcee4f69 3521 if( state == 0 )
cdupaty 0:d974bcee4f69 3522 {
cdupaty 0:d974bcee4f69 3523 // added by C. Pham
cdupaty 0:d974bcee4f69 3524 _RSSIpacket = readRegister(REG_PKT_RSSI_VALUE);
cdupaty 0:d974bcee4f69 3525
cdupaty 0:d974bcee4f69 3526 if( _SNR < 0 )
cdupaty 0:d974bcee4f69 3527 {
cdupaty 0:d974bcee4f69 3528 // commented by C. Pham
cdupaty 0:d974bcee4f69 3529 //_RSSIpacket = -NOISE_ABSOLUTE_ZERO + 10.0 * SignalBwLog[_bandwidth] + NOISE_FIGURE + ( double )_SNR;
cdupaty 0:d974bcee4f69 3530
cdupaty 0:d974bcee4f69 3531 // added by C. Pham, using Semtech SX1272 rev3 March 2015
cdupaty 0:d974bcee4f69 3532 // for SX1272 we use -139, for SX1276, we use -157
cdupaty 0:d974bcee4f69 3533 // then for SX1276 when using low-frequency (i.e. 433MHz) then we use -164
cdupaty 0:d974bcee4f69 3534 _RSSIpacket = -(OFFSET_RSSI+(_board==SX1276Chip?18:0)+(_channel<CH_04_868?7:0)) + (double)_RSSIpacket + (double)_rawSNR*0.25;
cdupaty 0:d974bcee4f69 3535 state = 0;
cdupaty 0:d974bcee4f69 3536 }
cdupaty 0:d974bcee4f69 3537 else
cdupaty 0:d974bcee4f69 3538 {
cdupaty 0:d974bcee4f69 3539 // commented by C. Pham
cdupaty 0:d974bcee4f69 3540 //_RSSIpacket = readRegister(REG_PKT_RSSI_VALUE);
cdupaty 0:d974bcee4f69 3541 _RSSIpacket = -(OFFSET_RSSI+(_board==SX1276Chip?18:0)+(_channel<CH_04_868?7:0)) + (double)_RSSIpacket;
cdupaty 0:d974bcee4f69 3542 //end
cdupaty 0:d974bcee4f69 3543 state = 0;
cdupaty 0:d974bcee4f69 3544 }
cdupaty 0:d974bcee4f69 3545 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 3546 printf("## RSSI packet value is %d",_RSSIpacket);
cdupaty 0:d974bcee4f69 3547 // Serial.print(_RSSIpacket, DEC);
cdupaty 0:d974bcee4f69 3548 printf(" ##");
cdupaty 0:d974bcee4f69 3549 printf("\n");
cdupaty 0:d974bcee4f69 3550 #endif
cdupaty 0:d974bcee4f69 3551 }
cdupaty 0:d974bcee4f69 3552 }
cdupaty 0:d974bcee4f69 3553 else
cdupaty 0:d974bcee4f69 3554 { // RSSI packet doesn't exist in FSK mode
cdupaty 0:d974bcee4f69 3555 state = -1;
cdupaty 0:d974bcee4f69 3556 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 3557 printf("** RSSI packet does not exist in FSK mode **");
cdupaty 0:d974bcee4f69 3558 printf("\n");
cdupaty 0:d974bcee4f69 3559 #endif
cdupaty 0:d974bcee4f69 3560 }
cdupaty 0:d974bcee4f69 3561 return state;
cdupaty 0:d974bcee4f69 3562 }
cdupaty 0:d974bcee4f69 3563
cdupaty 0:d974bcee4f69 3564 /*
cdupaty 0:d974bcee4f69 3565 Function: It sets the maximum number of retries.
cdupaty 0:d974bcee4f69 3566 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3567 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3568 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3569 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3570 state = -1 -->
cdupaty 0:d974bcee4f69 3571 */
cdupaty 0:d974bcee4f69 3572 uint8_t SX1272::setRetries(uint8_t ret)
cdupaty 0:d974bcee4f69 3573 {
cdupaty 0:d974bcee4f69 3574 int8_t state = 2; // uint8_t a l origine
cdupaty 0:d974bcee4f69 3575
cdupaty 0:d974bcee4f69 3576 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3577 printf("\n");
cdupaty 0:d974bcee4f69 3578 printf("Starting 'setRetries'\n");
cdupaty 0:d974bcee4f69 3579 #endif
cdupaty 0:d974bcee4f69 3580
cdupaty 0:d974bcee4f69 3581 state = 1;
cdupaty 0:d974bcee4f69 3582 if( ret > MAX_RETRIES )
cdupaty 0:d974bcee4f69 3583 {
cdupaty 0:d974bcee4f69 3584 state = -1;
cdupaty 0:d974bcee4f69 3585 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3586 printf("** Retries value can't be greater than %d ",MAX_RETRIES );
cdupaty 0:d974bcee4f69 3587 // Serial.print(MAX_RETRIES, DEC);
cdupaty 0:d974bcee4f69 3588 printf(" **");
cdupaty 0:d974bcee4f69 3589 printf("\n");
cdupaty 0:d974bcee4f69 3590 #endif
cdupaty 0:d974bcee4f69 3591 }
cdupaty 0:d974bcee4f69 3592 else
cdupaty 0:d974bcee4f69 3593 {
cdupaty 0:d974bcee4f69 3594 _maxRetries = ret;
cdupaty 0:d974bcee4f69 3595 state = 0;
cdupaty 0:d974bcee4f69 3596 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3597 printf("## Maximum retries value = %d ",_maxRetries);
cdupaty 0:d974bcee4f69 3598 // Serial.print(_maxRetries, DEC);
cdupaty 0:d974bcee4f69 3599 printf(" ##");
cdupaty 0:d974bcee4f69 3600 printf("\n");
cdupaty 0:d974bcee4f69 3601 #endif
cdupaty 0:d974bcee4f69 3602 }
cdupaty 0:d974bcee4f69 3603 return state;
cdupaty 0:d974bcee4f69 3604 }
cdupaty 0:d974bcee4f69 3605
cdupaty 0:d974bcee4f69 3606 /*
cdupaty 0:d974bcee4f69 3607 Function: Gets the current supply limit of the power amplifier, protecting battery chemistries.
cdupaty 0:d974bcee4f69 3608 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3609 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3610 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3611 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3612 Parameters:
cdupaty 0:d974bcee4f69 3613 rate: value to compute the maximum current supply. Maximum current is 45+5*'rate' [mA]
cdupaty 0:d974bcee4f69 3614 */
cdupaty 0:d974bcee4f69 3615 uint8_t SX1272::getMaxCurrent()
cdupaty 0:d974bcee4f69 3616 {
cdupaty 0:d974bcee4f69 3617 int8_t state = 2;
cdupaty 0:d974bcee4f69 3618 byte value;
cdupaty 0:d974bcee4f69 3619
cdupaty 0:d974bcee4f69 3620 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3621 printf("\n");
cdupaty 0:d974bcee4f69 3622 printf("Starting 'getMaxCurrent'\n");
cdupaty 0:d974bcee4f69 3623 #endif
cdupaty 0:d974bcee4f69 3624
cdupaty 0:d974bcee4f69 3625 state = 1;
cdupaty 0:d974bcee4f69 3626 _maxCurrent = readRegister(REG_OCP);
cdupaty 0:d974bcee4f69 3627
cdupaty 0:d974bcee4f69 3628 // extract only the OcpTrim value from the OCP register
cdupaty 0:d974bcee4f69 3629 _maxCurrent &= 0B00011111;
cdupaty 0:d974bcee4f69 3630
cdupaty 0:d974bcee4f69 3631 if( _maxCurrent <= 15 )
cdupaty 0:d974bcee4f69 3632 {
cdupaty 0:d974bcee4f69 3633 value = (45 + (5 * _maxCurrent));
cdupaty 0:d974bcee4f69 3634 }
cdupaty 0:d974bcee4f69 3635 else if( _maxCurrent <= 27 )
cdupaty 0:d974bcee4f69 3636 {
cdupaty 0:d974bcee4f69 3637 value = (-30 + (10 * _maxCurrent));
cdupaty 0:d974bcee4f69 3638 }
cdupaty 0:d974bcee4f69 3639 else
cdupaty 0:d974bcee4f69 3640 {
cdupaty 0:d974bcee4f69 3641 value = 240;
cdupaty 0:d974bcee4f69 3642 }
cdupaty 0:d974bcee4f69 3643
cdupaty 0:d974bcee4f69 3644 _maxCurrent = value;
cdupaty 0:d974bcee4f69 3645 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3646 printf("## Maximum current supply configured is %d ",value);
cdupaty 0:d974bcee4f69 3647 // Serial.print(value, DEC);
cdupaty 0:d974bcee4f69 3648 printf(" mA ##");
cdupaty 0:d974bcee4f69 3649 printf("\n");
cdupaty 0:d974bcee4f69 3650 #endif
cdupaty 0:d974bcee4f69 3651 state = 0;
cdupaty 0:d974bcee4f69 3652 return state;
cdupaty 0:d974bcee4f69 3653 }
cdupaty 0:d974bcee4f69 3654
cdupaty 0:d974bcee4f69 3655 /*
cdupaty 0:d974bcee4f69 3656 Function: Limits the current supply of the power amplifier, protecting battery chemistries.
cdupaty 0:d974bcee4f69 3657 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3658 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3659 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3660 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3661 state = -1 --> Forbidden parameter value for this function
cdupaty 0:d974bcee4f69 3662 Parameters:
cdupaty 0:d974bcee4f69 3663 rate: value to compute the maximum current supply. Maximum current is 45+5*'rate' [mA]
cdupaty 0:d974bcee4f69 3664 */
cdupaty 0:d974bcee4f69 3665 int8_t SX1272::setMaxCurrent(uint8_t rate)
cdupaty 0:d974bcee4f69 3666 {
cdupaty 0:d974bcee4f69 3667 int8_t state = 2;
cdupaty 0:d974bcee4f69 3668 byte st0;
cdupaty 0:d974bcee4f69 3669
cdupaty 0:d974bcee4f69 3670 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3671 printf("\n");
cdupaty 0:d974bcee4f69 3672 printf("Starting 'setMaxCurrent'\n");
cdupaty 0:d974bcee4f69 3673 #endif
cdupaty 0:d974bcee4f69 3674
cdupaty 0:d974bcee4f69 3675 // Maximum rate value = 0x1B, because maximum current supply = 240 mA
cdupaty 0:d974bcee4f69 3676 if (rate > 0x1B)
cdupaty 0:d974bcee4f69 3677 {
cdupaty 0:d974bcee4f69 3678 state = -1;
cdupaty 0:d974bcee4f69 3679 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3680 printf("** Maximum current supply is 240 mA, ");
cdupaty 0:d974bcee4f69 3681 printf("so maximum parameter value must be 27 (DEC) or 0x1B (HEX) **");
cdupaty 0:d974bcee4f69 3682 printf("\n");
cdupaty 0:d974bcee4f69 3683 #endif
cdupaty 0:d974bcee4f69 3684 }
cdupaty 0:d974bcee4f69 3685 else
cdupaty 0:d974bcee4f69 3686 {
cdupaty 0:d974bcee4f69 3687 // Enable Over Current Protection
cdupaty 0:d974bcee4f69 3688 rate |= 0B00100000;
cdupaty 0:d974bcee4f69 3689
cdupaty 0:d974bcee4f69 3690 state = 1;
cdupaty 0:d974bcee4f69 3691 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 3692 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3693 { // LoRa mode
cdupaty 0:d974bcee4f69 3694 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Set LoRa Standby mode to write in registers
cdupaty 0:d974bcee4f69 3695 }
cdupaty 0:d974bcee4f69 3696 else
cdupaty 0:d974bcee4f69 3697 { // FSK mode
cdupaty 0:d974bcee4f69 3698 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Set FSK Standby mode to write in registers
cdupaty 0:d974bcee4f69 3699 }
cdupaty 0:d974bcee4f69 3700 writeRegister(REG_OCP, rate); // Modifying maximum current supply
cdupaty 0:d974bcee4f69 3701 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 3702 state = 0;
cdupaty 0:d974bcee4f69 3703 }
cdupaty 0:d974bcee4f69 3704 return state;
cdupaty 0:d974bcee4f69 3705 }
cdupaty 0:d974bcee4f69 3706
cdupaty 0:d974bcee4f69 3707 /*
cdupaty 0:d974bcee4f69 3708 Function: Gets the content of different registers.
cdupaty 0:d974bcee4f69 3709 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3710 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3711 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3712 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3713 */
cdupaty 0:d974bcee4f69 3714 uint8_t SX1272::getRegs()
cdupaty 0:d974bcee4f69 3715 {
cdupaty 0:d974bcee4f69 3716 int8_t state = 2;
cdupaty 0:d974bcee4f69 3717 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 3718
cdupaty 0:d974bcee4f69 3719 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3720 printf("\n");
cdupaty 0:d974bcee4f69 3721 printf("Starting 'getRegs'\n");
cdupaty 0:d974bcee4f69 3722 #endif
cdupaty 0:d974bcee4f69 3723
cdupaty 0:d974bcee4f69 3724 state_f = 1;
cdupaty 0:d974bcee4f69 3725 state = getMode(); // Stores the BW, CR and SF.
cdupaty 0:d974bcee4f69 3726 if( state == 0 )
cdupaty 0:d974bcee4f69 3727 {
cdupaty 0:d974bcee4f69 3728 state = getPower(); // Stores the power.
cdupaty 0:d974bcee4f69 3729 }
cdupaty 0:d974bcee4f69 3730 else
cdupaty 0:d974bcee4f69 3731 {
cdupaty 0:d974bcee4f69 3732 state_f = 1;
cdupaty 0:d974bcee4f69 3733 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3734 printf("** Error getting mode **");
cdupaty 0:d974bcee4f69 3735 #endif
cdupaty 0:d974bcee4f69 3736 }
cdupaty 0:d974bcee4f69 3737 if( state == 0 )
cdupaty 0:d974bcee4f69 3738 {
cdupaty 0:d974bcee4f69 3739 state = getChannel(); // Stores the channel.
cdupaty 0:d974bcee4f69 3740 }
cdupaty 0:d974bcee4f69 3741 else
cdupaty 0:d974bcee4f69 3742 {
cdupaty 0:d974bcee4f69 3743 state_f = 1;
cdupaty 0:d974bcee4f69 3744 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3745 printf("** Error getting power **");
cdupaty 0:d974bcee4f69 3746 #endif
cdupaty 0:d974bcee4f69 3747 }
cdupaty 0:d974bcee4f69 3748 if( state == 0 )
cdupaty 0:d974bcee4f69 3749 {
cdupaty 0:d974bcee4f69 3750 state = getCRC(); // Stores the CRC configuration.
cdupaty 0:d974bcee4f69 3751 }
cdupaty 0:d974bcee4f69 3752 else
cdupaty 0:d974bcee4f69 3753 {
cdupaty 0:d974bcee4f69 3754 state_f = 1;
cdupaty 0:d974bcee4f69 3755 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3756 printf("** Error getting channel **");
cdupaty 0:d974bcee4f69 3757 #endif
cdupaty 0:d974bcee4f69 3758 }
cdupaty 0:d974bcee4f69 3759 if( state == 0 )
cdupaty 0:d974bcee4f69 3760 {
cdupaty 0:d974bcee4f69 3761 state = getHeader(); // Stores the header configuration.
cdupaty 0:d974bcee4f69 3762 }
cdupaty 0:d974bcee4f69 3763 else
cdupaty 0:d974bcee4f69 3764 {
cdupaty 0:d974bcee4f69 3765 state_f = 1;
cdupaty 0:d974bcee4f69 3766 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3767 printf("** Error getting CRC **");
cdupaty 0:d974bcee4f69 3768 #endif
cdupaty 0:d974bcee4f69 3769 }
cdupaty 0:d974bcee4f69 3770 if( state == 0 )
cdupaty 0:d974bcee4f69 3771 {
cdupaty 0:d974bcee4f69 3772 state = getPreambleLength(); // Stores the preamble length.
cdupaty 0:d974bcee4f69 3773 }
cdupaty 0:d974bcee4f69 3774 else
cdupaty 0:d974bcee4f69 3775 {
cdupaty 0:d974bcee4f69 3776 state_f = 1;
cdupaty 0:d974bcee4f69 3777 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3778 printf("** Error getting header **");
cdupaty 0:d974bcee4f69 3779 #endif
cdupaty 0:d974bcee4f69 3780 }
cdupaty 0:d974bcee4f69 3781 if( state == 0 )
cdupaty 0:d974bcee4f69 3782 {
cdupaty 0:d974bcee4f69 3783 state = getPayloadLength(); // Stores the payload length.
cdupaty 0:d974bcee4f69 3784 }
cdupaty 0:d974bcee4f69 3785 else
cdupaty 0:d974bcee4f69 3786 {
cdupaty 0:d974bcee4f69 3787 state_f = 1;
cdupaty 0:d974bcee4f69 3788 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3789 printf("** Error getting preamble length **");
cdupaty 0:d974bcee4f69 3790 #endif
cdupaty 0:d974bcee4f69 3791 }
cdupaty 0:d974bcee4f69 3792 if( state == 0 )
cdupaty 0:d974bcee4f69 3793 {
cdupaty 0:d974bcee4f69 3794 state = getNodeAddress(); // Stores the node address.
cdupaty 0:d974bcee4f69 3795 }
cdupaty 0:d974bcee4f69 3796 else
cdupaty 0:d974bcee4f69 3797 {
cdupaty 0:d974bcee4f69 3798 state_f = 1;
cdupaty 0:d974bcee4f69 3799 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3800 printf("** Error getting payload length **");
cdupaty 0:d974bcee4f69 3801 #endif
cdupaty 0:d974bcee4f69 3802 }
cdupaty 0:d974bcee4f69 3803 if( state == 0 )
cdupaty 0:d974bcee4f69 3804 {
cdupaty 0:d974bcee4f69 3805 state = getMaxCurrent(); // Stores the maximum current supply.
cdupaty 0:d974bcee4f69 3806 }
cdupaty 0:d974bcee4f69 3807 else
cdupaty 0:d974bcee4f69 3808 {
cdupaty 0:d974bcee4f69 3809 state_f = 1;
cdupaty 0:d974bcee4f69 3810 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3811 printf("** Error getting node address **");
cdupaty 0:d974bcee4f69 3812 #endif
cdupaty 0:d974bcee4f69 3813 }
cdupaty 0:d974bcee4f69 3814 if( state == 0 )
cdupaty 0:d974bcee4f69 3815 {
cdupaty 0:d974bcee4f69 3816 state_f = getTemp(); // Stores the module temperature.
cdupaty 0:d974bcee4f69 3817 }
cdupaty 0:d974bcee4f69 3818 else
cdupaty 0:d974bcee4f69 3819 {
cdupaty 0:d974bcee4f69 3820 state_f = 1;
cdupaty 0:d974bcee4f69 3821 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3822 printf("** Error getting maximum current supply **");
cdupaty 0:d974bcee4f69 3823 #endif
cdupaty 0:d974bcee4f69 3824 }
cdupaty 0:d974bcee4f69 3825 if( state_f != 0 )
cdupaty 0:d974bcee4f69 3826 {
cdupaty 0:d974bcee4f69 3827 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3828 printf("** Error getting temperature **");
cdupaty 0:d974bcee4f69 3829 printf("\n");
cdupaty 0:d974bcee4f69 3830 #endif
cdupaty 0:d974bcee4f69 3831 }
cdupaty 0:d974bcee4f69 3832 return state_f;
cdupaty 0:d974bcee4f69 3833 }
cdupaty 0:d974bcee4f69 3834
cdupaty 0:d974bcee4f69 3835 /*
cdupaty 0:d974bcee4f69 3836 Function: It truncs the payload length if it is greater than 0xFF.
cdupaty 0:d974bcee4f69 3837 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3838 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3839 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3840 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3841 */
cdupaty 0:d974bcee4f69 3842 uint8_t SX1272::truncPayload(uint16_t length16)
cdupaty 0:d974bcee4f69 3843 {
cdupaty 0:d974bcee4f69 3844 uint8_t state = 2;
cdupaty 0:d974bcee4f69 3845
cdupaty 0:d974bcee4f69 3846 state = 1;
cdupaty 0:d974bcee4f69 3847
cdupaty 0:d974bcee4f69 3848 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3849 printf("\n");
cdupaty 0:d974bcee4f69 3850 printf("Starting 'truncPayload'\n");
cdupaty 0:d974bcee4f69 3851 #endif
cdupaty 0:d974bcee4f69 3852
cdupaty 0:d974bcee4f69 3853 if( length16 > MAX_PAYLOAD )
cdupaty 0:d974bcee4f69 3854 {
afzalsamira 6:d7c7e731da3a 3855 printf("XXXXXXXXXXXXXXXXXXXXX MAX_PAYLOAD XXXXXXXXXXXXXXXX\n");
cdupaty 0:d974bcee4f69 3856 _payloadlength = MAX_PAYLOAD;
cdupaty 0:d974bcee4f69 3857 }
cdupaty 0:d974bcee4f69 3858 else
cdupaty 0:d974bcee4f69 3859 {
cdupaty 0:d974bcee4f69 3860 _payloadlength = (length16 & 0xFF);
cdupaty 0:d974bcee4f69 3861 }
cdupaty 0:d974bcee4f69 3862 state = 0;
cdupaty 0:d974bcee4f69 3863
cdupaty 0:d974bcee4f69 3864 return state;
cdupaty 0:d974bcee4f69 3865 }
cdupaty 0:d974bcee4f69 3866
cdupaty 0:d974bcee4f69 3867 /*
cdupaty 0:d974bcee4f69 3868 Function: It sets an ACK in FIFO in order to send it.
cdupaty 0:d974bcee4f69 3869 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3870 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3871 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3872 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3873 */
cdupaty 0:d974bcee4f69 3874 uint8_t SX1272::setACK()
cdupaty 0:d974bcee4f69 3875 {
cdupaty 0:d974bcee4f69 3876 uint8_t state = 2;
cdupaty 0:d974bcee4f69 3877
cdupaty 0:d974bcee4f69 3878 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3879 printf("\n");
cdupaty 0:d974bcee4f69 3880 printf("Starting 'setACK'\n");
cdupaty 0:d974bcee4f69 3881 #endif
cdupaty 0:d974bcee4f69 3882
cdupaty 0:d974bcee4f69 3883 // added by C. Pham
cdupaty 0:d974bcee4f69 3884 // check for enough remaining ToA
cdupaty 0:d974bcee4f69 3885 // when operating under duty-cycle mode
cdupaty 0:d974bcee4f69 3886 if (_limitToA) {
cdupaty 0:d974bcee4f69 3887 if (getRemainingToA() - getToA(ACK_LENGTH) < 0) {
cdupaty 0:d974bcee4f69 3888 printf("## not enough ToA for ACK at %d",millis());
cdupaty 0:d974bcee4f69 3889 // Serial.println(millis());
cdupaty 0:d974bcee4f69 3890 return SX1272_ERROR_TOA;
cdupaty 0:d974bcee4f69 3891 }
cdupaty 0:d974bcee4f69 3892 }
cdupaty 0:d974bcee4f69 3893
cdupaty 0:d974bcee4f69 3894 // delay(1000);
cdupaty 0:d974bcee4f69 3895
cdupaty 0:d974bcee4f69 3896 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 3897
cdupaty 0:d974bcee4f69 3898 if( _modem == LORA )
cdupaty 0:d974bcee4f69 3899 { // LoRa mode
cdupaty 0:d974bcee4f69 3900 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO
cdupaty 0:d974bcee4f69 3901 }
cdupaty 0:d974bcee4f69 3902 else
cdupaty 0:d974bcee4f69 3903 { // FSK mode
cdupaty 0:d974bcee4f69 3904 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Stdby FSK mode to write in FIFO
cdupaty 0:d974bcee4f69 3905 }
cdupaty 0:d974bcee4f69 3906
cdupaty 0:d974bcee4f69 3907 // Setting ACK length in order to send it
cdupaty 0:d974bcee4f69 3908 state = setPacketLength(ACK_LENGTH);
cdupaty 0:d974bcee4f69 3909 if( state == 0 )
cdupaty 0:d974bcee4f69 3910 {
cdupaty 0:d974bcee4f69 3911 // Setting ACK
cdupaty 0:d974bcee4f69 3912 ACK.dst = packet_received.src; // ACK destination is packet source
cdupaty 0:d974bcee4f69 3913 ACK.type = PKT_TYPE_ACK;
cdupaty 0:d974bcee4f69 3914 ACK.src = packet_received.dst; // ACK source is packet destination
cdupaty 0:d974bcee4f69 3915 ACK.packnum = packet_received.packnum; // packet number that has been correctly received
cdupaty 0:d974bcee4f69 3916 ACK.length = 2;
cdupaty 0:d974bcee4f69 3917 ACK.data[0] = _reception; // CRC of the received packet
cdupaty 0:d974bcee4f69 3918 // added by C. Pham
cdupaty 0:d974bcee4f69 3919 // store the SNR
cdupaty 0:d974bcee4f69 3920 ACK.data[1]= readRegister(REG_PKT_SNR_VALUE);
cdupaty 0:d974bcee4f69 3921
cdupaty 0:d974bcee4f69 3922 // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 3923 writeRegister(REG_FIFO_ADDR_PTR, 0x80);
cdupaty 0:d974bcee4f69 3924
cdupaty 0:d974bcee4f69 3925 state = 1;
cdupaty 0:d974bcee4f69 3926
cdupaty 0:d974bcee4f69 3927 // Writing ACK to send in FIFO
cdupaty 0:d974bcee4f69 3928 writeRegister(REG_FIFO, ACK.dst); // Writing the destination in FIFO
cdupaty 0:d974bcee4f69 3929 writeRegister(REG_FIFO, ACK.type);
cdupaty 0:d974bcee4f69 3930 writeRegister(REG_FIFO, ACK.src); // Writing the source in FIFO
cdupaty 0:d974bcee4f69 3931 writeRegister(REG_FIFO, ACK.packnum); // Writing the packet number in FIFO
cdupaty 0:d974bcee4f69 3932 writeRegister(REG_FIFO, ACK.length); // Writing the packet length in FIFO
cdupaty 0:d974bcee4f69 3933 writeRegister(REG_FIFO, ACK.data[0]); // Writing the ACK in FIFO
cdupaty 0:d974bcee4f69 3934 writeRegister(REG_FIFO, ACK.data[1]); // Writing the ACK in FIFO
cdupaty 0:d974bcee4f69 3935
cdupaty 0:d974bcee4f69 3936 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 3937 printf("## ACK set and written in FIFO ##\n");
cdupaty 0:d974bcee4f69 3938 // Print the complete ACK if debug_mode
cdupaty 0:d974bcee4f69 3939 printf("## ACK to send:\n");
cdupaty 0:d974bcee4f69 3940 printf("Destination: %d\n",ACK.dst);
cdupaty 0:d974bcee4f69 3941 // Serial.println(ACK.dst); // Printing destination
cdupaty 0:d974bcee4f69 3942 printf("Source: %d\n",ACK.src);
cdupaty 0:d974bcee4f69 3943 // Serial.println(ACK.src); // Printing source
cdupaty 0:d974bcee4f69 3944 printf("ACK number: %d\n",ACK.packnum);
cdupaty 0:d974bcee4f69 3945 // Serial.println(ACK.packnum); // Printing ACK number
cdupaty 0:d974bcee4f69 3946 printf("ACK length: %d\n",ACK.length);
cdupaty 0:d974bcee4f69 3947 // Serial.println(ACK.length); // Printing ACK length
cdupaty 0:d974bcee4f69 3948 printf("ACK payload: %d\n",ACK.data[0]);
cdupaty 0:d974bcee4f69 3949 // Serial.println(ACK.data[0]); // Printing ACK payload
cdupaty 0:d974bcee4f69 3950 printf("ACK SNR last rcv pkt: %d\n",_SNR);
cdupaty 0:d974bcee4f69 3951 // Serial.println(_SNR);
cdupaty 0:d974bcee4f69 3952 printf("##\n");
cdupaty 0:d974bcee4f69 3953 printf("\n");
cdupaty 0:d974bcee4f69 3954 //#endif
cdupaty 0:d974bcee4f69 3955
cdupaty 0:d974bcee4f69 3956 state = 0;
cdupaty 0:d974bcee4f69 3957 _reception = CORRECT_PACKET; // Updating value to next packet
cdupaty 0:d974bcee4f69 3958
cdupaty 0:d974bcee4f69 3959 // comment by C. Pham
cdupaty 0:d974bcee4f69 3960 // TODO: do we really need this delay?
cdupaty 0:d974bcee4f69 3961 wait_ms(500);
cdupaty 0:d974bcee4f69 3962 }
cdupaty 0:d974bcee4f69 3963 return state;
cdupaty 0:d974bcee4f69 3964 }
cdupaty 0:d974bcee4f69 3965
cdupaty 0:d974bcee4f69 3966 /*
cdupaty 0:d974bcee4f69 3967 Function: Configures the module to receive information.
cdupaty 0:d974bcee4f69 3968 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 3969 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 3970 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 3971 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 3972 */
cdupaty 0:d974bcee4f69 3973 uint8_t SX1272::receive()
cdupaty 0:d974bcee4f69 3974 {
cdupaty 0:d974bcee4f69 3975 uint8_t state = 1;
cdupaty 0:d974bcee4f69 3976
cdupaty 0:d974bcee4f69 3977 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 3978 printf("\n");
cdupaty 0:d974bcee4f69 3979 printf("Starting 'receive'\n");
cdupaty 0:d974bcee4f69 3980 #endif
cdupaty 0:d974bcee4f69 3981
cdupaty 0:d974bcee4f69 3982 // Initializing packet_received struct
cdupaty 0:d974bcee4f69 3983 memset( &packet_received, 0x00, sizeof(packet_received) );
cdupaty 0:d974bcee4f69 3984
cdupaty 0:d974bcee4f69 3985 // Setting Testmode
cdupaty 0:d974bcee4f69 3986 // commented by C. Pham
cdupaty 0:d974bcee4f69 3987 //writeRegister(0x31,0x43);
cdupaty 0:d974bcee4f69 3988
cdupaty 0:d974bcee4f69 3989 // Set LowPnTxPllOff
cdupaty 0:d974bcee4f69 3990 // modified by C. Pham from 0x09 to 0x08
cdupaty 0:d974bcee4f69 3991 writeRegister(REG_PA_RAMP, 0x08);
cdupaty 0:d974bcee4f69 3992
cdupaty 0:d974bcee4f69 3993 //writeRegister(REG_LNA, 0x23); // Important in reception
cdupaty 0:d974bcee4f69 3994 // modified by C. Pham
cdupaty 0:d974bcee4f69 3995 writeRegister(REG_LNA, LNA_MAX_GAIN);
cdupaty 0:d974bcee4f69 3996 writeRegister(REG_FIFO_ADDR_PTR, 0x00); // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 3997 // change RegSymbTimeoutLsb
cdupaty 0:d974bcee4f69 3998 // comment by C. Pham
cdupaty 0:d974bcee4f69 3999 // single_chan_pkt_fwd uses 00 00001000
cdupaty 0:d974bcee4f69 4000 // why here we have 11 11111111
cdupaty 0:d974bcee4f69 4001 // change RegSymbTimeoutLsb
cdupaty 0:d974bcee4f69 4002 //writeRegister(REG_SYMB_TIMEOUT_LSB, 0xFF);
cdupaty 0:d974bcee4f69 4003
cdupaty 0:d974bcee4f69 4004 // modified by C. Pham
cdupaty 0:d974bcee4f69 4005 if (_spreadingFactor == SF_10 || _spreadingFactor == SF_11 || _spreadingFactor == SF_12) {
cdupaty 0:d974bcee4f69 4006 writeRegister(REG_SYMB_TIMEOUT_LSB,0x05);
cdupaty 0:d974bcee4f69 4007 } else {
cdupaty 0:d974bcee4f69 4008 writeRegister(REG_SYMB_TIMEOUT_LSB,0x08);
cdupaty 0:d974bcee4f69 4009 }
cdupaty 0:d974bcee4f69 4010 //end
cdupaty 0:d974bcee4f69 4011
cdupaty 0:d974bcee4f69 4012 writeRegister(REG_FIFO_RX_BYTE_ADDR, 0x00); // Setting current value of reception buffer pointer
cdupaty 0:d974bcee4f69 4013 //clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 4014 //state = 1;
cdupaty 0:d974bcee4f69 4015 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4016 { // LoRa mode
cdupaty 0:d974bcee4f69 4017 state = setPacketLength(MAX_LENGTH); // With MAX_LENGTH gets all packets with length < MAX_LENGTH
cdupaty 0:d974bcee4f69 4018 writeRegister(REG_OP_MODE, LORA_RX_MODE); // LORA mode - Rx
cdupaty 0:d974bcee4f69 4019 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4020 printf("## Receiving LoRa mode activated with success ##");
cdupaty 0:d974bcee4f69 4021 printf("\n");
cdupaty 0:d974bcee4f69 4022 #endif
cdupaty 0:d974bcee4f69 4023 }
cdupaty 0:d974bcee4f69 4024 else
cdupaty 0:d974bcee4f69 4025 { // FSK mode
cdupaty 0:d974bcee4f69 4026 state = setPacketLength();
cdupaty 0:d974bcee4f69 4027 writeRegister(REG_OP_MODE, FSK_RX_MODE); // FSK mode - Rx
cdupaty 0:d974bcee4f69 4028 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4029 printf("## Receiving FSK mode activated with success ##");
cdupaty 0:d974bcee4f69 4030 printf("\n");
cdupaty 0:d974bcee4f69 4031 #endif
cdupaty 0:d974bcee4f69 4032 }
cdupaty 0:d974bcee4f69 4033 return state;
cdupaty 0:d974bcee4f69 4034 }
cdupaty 0:d974bcee4f69 4035
cdupaty 0:d974bcee4f69 4036 /*
cdupaty 0:d974bcee4f69 4037 Function: Configures the module to receive information.
cdupaty 0:d974bcee4f69 4038 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4039 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4040 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4041 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4042 */
cdupaty 0:d974bcee4f69 4043 uint8_t SX1272::receivePacketMAXTimeout()
cdupaty 0:d974bcee4f69 4044 {
cdupaty 0:d974bcee4f69 4045 return receivePacketTimeout(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4046 }
cdupaty 0:d974bcee4f69 4047
cdupaty 0:d974bcee4f69 4048 /*
cdupaty 0:d974bcee4f69 4049 Function: Configures the module to receive information.
cdupaty 0:d974bcee4f69 4050 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4051 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4052 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4053 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4054 */
cdupaty 0:d974bcee4f69 4055 uint8_t SX1272::receivePacketTimeout()
cdupaty 0:d974bcee4f69 4056 {
cdupaty 0:d974bcee4f69 4057 setTimeout();
cdupaty 0:d974bcee4f69 4058 return receivePacketTimeout(_sendTime);
cdupaty 0:d974bcee4f69 4059 }
cdupaty 0:d974bcee4f69 4060
cdupaty 0:d974bcee4f69 4061 /*
cdupaty 0:d974bcee4f69 4062 Function: Configures the module to receive information.
cdupaty 0:d974bcee4f69 4063 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4064 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4065 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4066 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4067 */
cdupaty 0:d974bcee4f69 4068 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 4069
cdupaty 0:d974bcee4f69 4070 // added by C. Pham
cdupaty 0:d974bcee4f69 4071 // receiver always use receivePacketTimeout()
cdupaty 0:d974bcee4f69 4072 // sender should either use sendPacketTimeout() or sendPacketTimeoutACK()
cdupaty 0:d974bcee4f69 4073
cdupaty 0:d974bcee4f69 4074 uint8_t SX1272::receivePacketTimeout(uint16_t wait)
cdupaty 0:d974bcee4f69 4075 {
cdupaty 0:d974bcee4f69 4076 uint8_t state = 2;
cdupaty 0:d974bcee4f69 4077 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 4078
cdupaty 0:d974bcee4f69 4079
cdupaty 0:d974bcee4f69 4080 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4081 printf("\n");
cdupaty 0:d974bcee4f69 4082 printf("Starting 'receivePacketTimeout'\n");
cdupaty 0:d974bcee4f69 4083 #endif
cdupaty 0:d974bcee4f69 4084
cdupaty 0:d974bcee4f69 4085 state = receive();
cdupaty 0:d974bcee4f69 4086 if( state == 0 )
cdupaty 0:d974bcee4f69 4087 {
cdupaty 0:d974bcee4f69 4088 if( availableData(wait) )
cdupaty 0:d974bcee4f69 4089 {
cdupaty 0:d974bcee4f69 4090 state = getPacket();
cdupaty 0:d974bcee4f69 4091 }
cdupaty 0:d974bcee4f69 4092 else
cdupaty 0:d974bcee4f69 4093 {
cdupaty 0:d974bcee4f69 4094 state = 1;
cdupaty 0:d974bcee4f69 4095 state_f = 3; // There is no packet received
cdupaty 0:d974bcee4f69 4096 }
cdupaty 0:d974bcee4f69 4097 }
cdupaty 0:d974bcee4f69 4098 else
cdupaty 0:d974bcee4f69 4099 {
cdupaty 0:d974bcee4f69 4100 state = 1;
cdupaty 0:d974bcee4f69 4101 state_f = 1; // There has been an error with the 'receive' function
cdupaty 0:d974bcee4f69 4102 }
cdupaty 0:d974bcee4f69 4103
cdupaty 0:d974bcee4f69 4104 if( (state == 0) || (state == 3) || (state == 5) )
cdupaty 0:d974bcee4f69 4105 {
cdupaty 0:d974bcee4f69 4106 if( _reception == INCORRECT_PACKET )
cdupaty 0:d974bcee4f69 4107 {
cdupaty 0:d974bcee4f69 4108 state_f = 4; // The packet has been incorrectly received
cdupaty 0:d974bcee4f69 4109 }
cdupaty 0:d974bcee4f69 4110 else
cdupaty 0:d974bcee4f69 4111 {
cdupaty 0:d974bcee4f69 4112 state_f = 0; // The packet has been correctly received
cdupaty 0:d974bcee4f69 4113 // added by C. Pham
cdupaty 0:d974bcee4f69 4114 // we get the SNR and RSSI of the received packet for future usage
cdupaty 0:d974bcee4f69 4115 getSNR();
cdupaty 0:d974bcee4f69 4116 getRSSIpacket();
cdupaty 0:d974bcee4f69 4117 }
cdupaty 0:d974bcee4f69 4118
cdupaty 0:d974bcee4f69 4119 // need to send an ACK
cdupaty 0:d974bcee4f69 4120 if ( state == 5 && state_f == 0) {
cdupaty 0:d974bcee4f69 4121
cdupaty 0:d974bcee4f69 4122 state = setACK();
cdupaty 0:d974bcee4f69 4123
cdupaty 0:d974bcee4f69 4124 if( state == 0 )
cdupaty 0:d974bcee4f69 4125 {
cdupaty 0:d974bcee4f69 4126 state = sendWithTimeout();
cdupaty 0:d974bcee4f69 4127 if( state == 0 )
cdupaty 0:d974bcee4f69 4128 {
cdupaty 0:d974bcee4f69 4129 state_f = 0;
cdupaty 0:d974bcee4f69 4130 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4131 printf("This last packet was an ACK, so ...");
cdupaty 0:d974bcee4f69 4132 printf("ACK successfully sent");
cdupaty 0:d974bcee4f69 4133 printf("\n");
cdupaty 0:d974bcee4f69 4134 #endif
cdupaty 0:d974bcee4f69 4135 }
cdupaty 0:d974bcee4f69 4136 else
cdupaty 0:d974bcee4f69 4137 {
cdupaty 0:d974bcee4f69 4138 state_f = 1; // There has been an error with the 'sendWithTimeout' function
cdupaty 0:d974bcee4f69 4139 }
cdupaty 0:d974bcee4f69 4140 }
cdupaty 0:d974bcee4f69 4141 else
cdupaty 0:d974bcee4f69 4142 {
cdupaty 0:d974bcee4f69 4143 state_f = 1; // There has been an error with the 'setACK' function
cdupaty 0:d974bcee4f69 4144 }
cdupaty 0:d974bcee4f69 4145 }
cdupaty 0:d974bcee4f69 4146 }
cdupaty 0:d974bcee4f69 4147 else
cdupaty 0:d974bcee4f69 4148 {
cdupaty 0:d974bcee4f69 4149 // we need to conserve state_f=3 to indicate that no packet has been received after timeout
cdupaty 0:d974bcee4f69 4150 //state_f = 1;
cdupaty 0:d974bcee4f69 4151 }
cdupaty 0:d974bcee4f69 4152 return state_f;
cdupaty 0:d974bcee4f69 4153 }
cdupaty 0:d974bcee4f69 4154 #else
cdupaty 0:d974bcee4f69 4155
cdupaty 0:d974bcee4f69 4156 uint8_t SX1272::receivePacketTimeout(uint16_t wait)
cdupaty 0:d974bcee4f69 4157 {
cdupaty 0:d974bcee4f69 4158 uint8_t state = 2;
cdupaty 0:d974bcee4f69 4159 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 4160
cdupaty 0:d974bcee4f69 4161 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4162 printf("\n");
cdupaty 0:d974bcee4f69 4163 printf("Starting 'receivePacketTimeout'\n");
cdupaty 0:d974bcee4f69 4164 #endif
cdupaty 0:d974bcee4f69 4165
cdupaty 0:d974bcee4f69 4166 state = receive();
cdupaty 0:d974bcee4f69 4167 if( state == 0 )
cdupaty 0:d974bcee4f69 4168 {
cdupaty 0:d974bcee4f69 4169 if( availableData(wait) )
cdupaty 0:d974bcee4f69 4170 {
cdupaty 0:d974bcee4f69 4171 // If packet received, getPacket
cdupaty 0:d974bcee4f69 4172 state_f = getPacket();
cdupaty 0:d974bcee4f69 4173 }
cdupaty 0:d974bcee4f69 4174 else
cdupaty 0:d974bcee4f69 4175 {
cdupaty 0:d974bcee4f69 4176 state_f = 1;
cdupaty 0:d974bcee4f69 4177 }
cdupaty 0:d974bcee4f69 4178 }
cdupaty 0:d974bcee4f69 4179 else
cdupaty 0:d974bcee4f69 4180 {
cdupaty 0:d974bcee4f69 4181 state_f = state;
cdupaty 0:d974bcee4f69 4182 }
cdupaty 0:d974bcee4f69 4183 return state_f;
cdupaty 0:d974bcee4f69 4184 }
cdupaty 0:d974bcee4f69 4185 #endif
cdupaty 0:d974bcee4f69 4186
cdupaty 0:d974bcee4f69 4187 /*
cdupaty 0:d974bcee4f69 4188 Function: Configures the module to receive information and send an ACK.
cdupaty 0:d974bcee4f69 4189 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4190 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4191 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4192 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4193 */
cdupaty 0:d974bcee4f69 4194 uint8_t SX1272::receivePacketMAXTimeoutACK()
cdupaty 0:d974bcee4f69 4195 {
cdupaty 0:d974bcee4f69 4196 return receivePacketTimeoutACK(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4197 }
cdupaty 0:d974bcee4f69 4198
cdupaty 0:d974bcee4f69 4199 /*
cdupaty 0:d974bcee4f69 4200 Function: Configures the module to receive information and send an ACK.
cdupaty 0:d974bcee4f69 4201 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4202 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4203 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4204 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4205 */
cdupaty 0:d974bcee4f69 4206 uint8_t SX1272::receivePacketTimeoutACK()
cdupaty 0:d974bcee4f69 4207 {
cdupaty 0:d974bcee4f69 4208 setTimeout();
cdupaty 0:d974bcee4f69 4209 return receivePacketTimeoutACK(_sendTime);
cdupaty 0:d974bcee4f69 4210 }
cdupaty 0:d974bcee4f69 4211
cdupaty 0:d974bcee4f69 4212 /*
cdupaty 0:d974bcee4f69 4213 Function: Configures the module to receive information and send an ACK.
cdupaty 0:d974bcee4f69 4214 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4215 state = 4 --> The command has been executed but the packet received is incorrect
cdupaty 0:d974bcee4f69 4216 state = 3 --> The command has been executed but there is no packet received
cdupaty 0:d974bcee4f69 4217 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4218 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4219 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4220 */
cdupaty 0:d974bcee4f69 4221 uint8_t SX1272::receivePacketTimeoutACK(uint16_t wait)
cdupaty 0:d974bcee4f69 4222 {
cdupaty 0:d974bcee4f69 4223 // commented by C. Pham because not used
cdupaty 0:d974bcee4f69 4224 /*
cdupaty 0:d974bcee4f69 4225 uint8_t state = 2;
cdupaty 0:d974bcee4f69 4226 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 4227
cdupaty 0:d974bcee4f69 4228
cdupaty 0:d974bcee4f69 4229 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4230 printf("\n");
cdupaty 0:d974bcee4f69 4231 printf("Starting 'receivePacketTimeoutACK'");
cdupaty 0:d974bcee4f69 4232 #endif
cdupaty 0:d974bcee4f69 4233
cdupaty 0:d974bcee4f69 4234 state = receive();
cdupaty 0:d974bcee4f69 4235 if( state == 0 )
cdupaty 0:d974bcee4f69 4236 {
cdupaty 0:d974bcee4f69 4237 if( availableData(wait) )
cdupaty 0:d974bcee4f69 4238 {
cdupaty 0:d974bcee4f69 4239 state = getPacket();
cdupaty 0:d974bcee4f69 4240 }
cdupaty 0:d974bcee4f69 4241 else
cdupaty 0:d974bcee4f69 4242 {
cdupaty 0:d974bcee4f69 4243 state = 1;
cdupaty 0:d974bcee4f69 4244 state_f = 3; // There is no packet received
cdupaty 0:d974bcee4f69 4245 }
cdupaty 0:d974bcee4f69 4246 }
cdupaty 0:d974bcee4f69 4247 else
cdupaty 0:d974bcee4f69 4248 {
cdupaty 0:d974bcee4f69 4249 state = 1;
cdupaty 0:d974bcee4f69 4250 state_f = 1; // There has been an error with the 'receive' function
cdupaty 0:d974bcee4f69 4251 }
cdupaty 0:d974bcee4f69 4252 if( (state == 0) || (state == 3) )
cdupaty 0:d974bcee4f69 4253 {
cdupaty 0:d974bcee4f69 4254 if( _reception == INCORRECT_PACKET )
cdupaty 0:d974bcee4f69 4255 {
cdupaty 0:d974bcee4f69 4256 state_f = 4; // The packet has been incorrectly received
cdupaty 0:d974bcee4f69 4257 }
cdupaty 0:d974bcee4f69 4258 else
cdupaty 0:d974bcee4f69 4259 {
cdupaty 0:d974bcee4f69 4260 state_f = 1; // The packet has been correctly received
cdupaty 0:d974bcee4f69 4261 }
cdupaty 0:d974bcee4f69 4262 state = setACK();
cdupaty 0:d974bcee4f69 4263 if( state == 0 )
cdupaty 0:d974bcee4f69 4264 {
cdupaty 0:d974bcee4f69 4265 state = sendWithTimeout();
cdupaty 0:d974bcee4f69 4266 if( state == 0 )
cdupaty 0:d974bcee4f69 4267 {
cdupaty 0:d974bcee4f69 4268 state_f = 0;
cdupaty 0:d974bcee4f69 4269 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4270 printf("This last packet was an ACK, so ...");
cdupaty 0:d974bcee4f69 4271 printf("ACK successfully sent");
cdupaty 0:d974bcee4f69 4272 printf("\n");
cdupaty 0:d974bcee4f69 4273 #endif
cdupaty 0:d974bcee4f69 4274 }
cdupaty 0:d974bcee4f69 4275 else
cdupaty 0:d974bcee4f69 4276 {
cdupaty 0:d974bcee4f69 4277 state_f = 1; // There has been an error with the 'sendWithTimeout' function
cdupaty 0:d974bcee4f69 4278 }
cdupaty 0:d974bcee4f69 4279 }
cdupaty 0:d974bcee4f69 4280 else
cdupaty 0:d974bcee4f69 4281 {
cdupaty 0:d974bcee4f69 4282 state_f = 1; // There has been an error with the 'setACK' function
cdupaty 0:d974bcee4f69 4283 }
cdupaty 0:d974bcee4f69 4284 }
cdupaty 0:d974bcee4f69 4285 else
cdupaty 0:d974bcee4f69 4286 {
cdupaty 0:d974bcee4f69 4287 state_f = 1;
cdupaty 0:d974bcee4f69 4288 }
cdupaty 0:d974bcee4f69 4289 return state_f;
cdupaty 0:d974bcee4f69 4290 */
cdupaty 0:d974bcee4f69 4291 // ajoute par C.Dupaty
cdupaty 0:d974bcee4f69 4292 return 0;
cdupaty 0:d974bcee4f69 4293 }
cdupaty 0:d974bcee4f69 4294
cdupaty 0:d974bcee4f69 4295 /*
cdupaty 0:d974bcee4f69 4296 Function: Configures the module to receive all the information on air, before MAX_TIMEOUT expires.
cdupaty 0:d974bcee4f69 4297 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4298 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4299 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4300 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4301 */
cdupaty 0:d974bcee4f69 4302 uint8_t SX1272::receiveAll()
cdupaty 0:d974bcee4f69 4303 {
cdupaty 0:d974bcee4f69 4304 return receiveAll(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4305 }
cdupaty 0:d974bcee4f69 4306
cdupaty 0:d974bcee4f69 4307 /*
cdupaty 0:d974bcee4f69 4308 Function: Configures the module to receive all the information on air.
cdupaty 0:d974bcee4f69 4309 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4310 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4311 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4312 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4313 */
cdupaty 0:d974bcee4f69 4314 uint8_t SX1272::receiveAll(uint16_t wait)
cdupaty 0:d974bcee4f69 4315 {
cdupaty 0:d974bcee4f69 4316 uint8_t state = 2;
cdupaty 0:d974bcee4f69 4317 byte config1;
cdupaty 0:d974bcee4f69 4318
cdupaty 0:d974bcee4f69 4319 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4320 printf("\n");
cdupaty 0:d974bcee4f69 4321 printf("Starting 'receiveAll'\n");
cdupaty 0:d974bcee4f69 4322 #endif
cdupaty 0:d974bcee4f69 4323
cdupaty 0:d974bcee4f69 4324 if( _modem == FSK )
cdupaty 0:d974bcee4f69 4325 { // FSK mode
cdupaty 0:d974bcee4f69 4326 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Setting standby FSK mode
cdupaty 0:d974bcee4f69 4327 config1 = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 4328 config1 = config1 & 0B11111001; // clears bits 2-1 from REG_PACKET_CONFIG1
cdupaty 0:d974bcee4f69 4329 writeRegister(REG_PACKET_CONFIG1, config1); // AddressFiltering = None
cdupaty 0:d974bcee4f69 4330 }
cdupaty 0:d974bcee4f69 4331 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4332 printf("## Address filtering desactivated ##");
cdupaty 0:d974bcee4f69 4333 printf("\n");
cdupaty 0:d974bcee4f69 4334 #endif
cdupaty 0:d974bcee4f69 4335 state = receive(); // Setting Rx mode
cdupaty 0:d974bcee4f69 4336 if( state == 0 )
cdupaty 0:d974bcee4f69 4337 {
cdupaty 0:d974bcee4f69 4338 state = getPacket(wait); // Getting all packets received in wait
cdupaty 0:d974bcee4f69 4339 }
cdupaty 0:d974bcee4f69 4340 return state;
cdupaty 0:d974bcee4f69 4341 }
cdupaty 0:d974bcee4f69 4342
cdupaty 0:d974bcee4f69 4343 /*
cdupaty 0:d974bcee4f69 4344 Function: If a packet is received, checks its destination.
cdupaty 0:d974bcee4f69 4345 Returns: Boolean that's 'true' if the packet is for the module and
cdupaty 0:d974bcee4f69 4346 it's 'false' if the packet is not for the module.
cdupaty 0:d974bcee4f69 4347 */
cdupaty 0:d974bcee4f69 4348 boolean SX1272::availableData()
cdupaty 0:d974bcee4f69 4349 {
cdupaty 0:d974bcee4f69 4350 return availableData(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4351 }
cdupaty 0:d974bcee4f69 4352
cdupaty 0:d974bcee4f69 4353 /*
cdupaty 0:d974bcee4f69 4354 Function: If a packet is received, checks its destination.
cdupaty 0:d974bcee4f69 4355 Returns: Boolean that's 'true' if the packet is for the module and
cdupaty 0:d974bcee4f69 4356 it's 'false' if the packet is not for the module.
cdupaty 0:d974bcee4f69 4357 Parameters:
cdupaty 0:d974bcee4f69 4358 wait: time to wait while there is no a valid header received.
cdupaty 0:d974bcee4f69 4359 */
cdupaty 0:d974bcee4f69 4360 boolean SX1272::availableData(uint16_t wait)
cdupaty 0:d974bcee4f69 4361 {
cdupaty 0:d974bcee4f69 4362 byte value;
cdupaty 0:d974bcee4f69 4363 byte header = 0;
cdupaty 0:d974bcee4f69 4364 boolean forme = false;
cdupaty 0:d974bcee4f69 4365 boolean _hreceived = false;
cdupaty 0:d974bcee4f69 4366 //unsigned long previous;
cdupaty 0:d974bcee4f69 4367 unsigned long exitTime;
cdupaty 0:d974bcee4f69 4368
cdupaty 0:d974bcee4f69 4369
cdupaty 0:d974bcee4f69 4370 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4371 printf("\n");
cdupaty 0:d974bcee4f69 4372 printf("Starting 'availableData'\n");
cdupaty 0:d974bcee4f69 4373 #endif
cdupaty 0:d974bcee4f69 4374
cdupaty 0:d974bcee4f69 4375 exitTime=millis()+(unsigned long)wait;
cdupaty 0:d974bcee4f69 4376
cdupaty 0:d974bcee4f69 4377 //previous = millis();
cdupaty 0:d974bcee4f69 4378 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4379 { // LoRa mode
cdupaty 0:d974bcee4f69 4380 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 4381 // Wait to ValidHeader interrupt
cdupaty 0:d974bcee4f69 4382 //while( (bitRead(value, 4) == 0) && (millis() - previous < (unsigned long)wait) )
cdupaty 0:d974bcee4f69 4383 while( (bitRead(value, 4) == 0) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4384 {
cdupaty 0:d974bcee4f69 4385 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 4386 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 4387 //if( millis() < previous )
cdupaty 0:d974bcee4f69 4388 //{
cdupaty 0:d974bcee4f69 4389 // previous = millis();
cdupaty 0:d974bcee4f69 4390 //}
cdupaty 0:d974bcee4f69 4391 } // end while (millis)
cdupaty 0:d974bcee4f69 4392
cdupaty 0:d974bcee4f69 4393 if( bitRead(value, 4) == 1 )
cdupaty 0:d974bcee4f69 4394 { // header received
cdupaty 0:d974bcee4f69 4395 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4396 printf("## Valid Header received in LoRa mode ##");
cdupaty 0:d974bcee4f69 4397 #endif
cdupaty 0:d974bcee4f69 4398 _hreceived = true;
cdupaty 0:d974bcee4f69 4399
cdupaty 0:d974bcee4f69 4400 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 4401 // actually, need to wait until 3 bytes have been received
cdupaty 0:d974bcee4f69 4402 //while( (header < 3) && (millis() - previous < (unsigned long)wait) )
cdupaty 0:d974bcee4f69 4403 while( (header < 3) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4404 #else
cdupaty 0:d974bcee4f69 4405 //while( (header == 0) && (millis() - previous < (unsigned long)wait) )
cdupaty 0:d974bcee4f69 4406 while( (header == 0) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4407 #endif
cdupaty 0:d974bcee4f69 4408 { // Waiting to read first payload bytes from packet
cdupaty 0:d974bcee4f69 4409 header = readRegister(REG_FIFO_RX_BYTE_ADDR);
cdupaty 0:d974bcee4f69 4410 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 4411 //if( millis() < previous )
cdupaty 0:d974bcee4f69 4412 //{
cdupaty 0:d974bcee4f69 4413 // previous = millis();
cdupaty 0:d974bcee4f69 4414 //}
cdupaty 0:d974bcee4f69 4415 }
cdupaty 0:d974bcee4f69 4416
cdupaty 0:d974bcee4f69 4417 if( header != 0 )
cdupaty 0:d974bcee4f69 4418 { // Reading first byte of the received packet
cdupaty 0:d974bcee4f69 4419 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 4420 // added by C. Pham
cdupaty 0:d974bcee4f69 4421 // if we actually wait for an ACK, there is no net key before ACK data
cdupaty 0:d974bcee4f69 4422 if (_requestACK==0) {
cdupaty 0:d974bcee4f69 4423 _the_net_key_0 = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4424 _the_net_key_1 = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4425 }
cdupaty 0:d974bcee4f69 4426 #endif
cdupaty 0:d974bcee4f69 4427 _destination = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4428 }
cdupaty 0:d974bcee4f69 4429 }
cdupaty 0:d974bcee4f69 4430 else
cdupaty 0:d974bcee4f69 4431 {
cdupaty 0:d974bcee4f69 4432 forme = false;
cdupaty 0:d974bcee4f69 4433 _hreceived = false;
cdupaty 0:d974bcee4f69 4434 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4435 printf("** The timeout has expired **");
cdupaty 0:d974bcee4f69 4436 printf("\n");
cdupaty 0:d974bcee4f69 4437 #endif
cdupaty 0:d974bcee4f69 4438 }
cdupaty 0:d974bcee4f69 4439 }
cdupaty 0:d974bcee4f69 4440 else
cdupaty 0:d974bcee4f69 4441 { // FSK mode
cdupaty 0:d974bcee4f69 4442 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 4443 // Wait to Payload Ready interrupt
cdupaty 0:d974bcee4f69 4444 //while( (bitRead(value, 2) == 0) && (millis() - previous < wait) )
cdupaty 0:d974bcee4f69 4445 while( (bitRead(value, 2) == 0) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4446 {
cdupaty 0:d974bcee4f69 4447 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 4448 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 4449 //if( millis() < previous )
cdupaty 0:d974bcee4f69 4450 //{
cdupaty 0:d974bcee4f69 4451 // previous = millis();
cdupaty 0:d974bcee4f69 4452 //}
cdupaty 0:d974bcee4f69 4453 }// end while (millis)
cdupaty 0:d974bcee4f69 4454
cdupaty 0:d974bcee4f69 4455 if( bitRead(value, 2) == 1 ) // something received
cdupaty 0:d974bcee4f69 4456 {
cdupaty 0:d974bcee4f69 4457 _hreceived = true;
cdupaty 0:d974bcee4f69 4458 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4459 printf("## Valid Preamble detected in FSK mode ##");
cdupaty 0:d974bcee4f69 4460 #endif
cdupaty 0:d974bcee4f69 4461 // Reading first byte of the received packet
cdupaty 0:d974bcee4f69 4462 _destination = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4463 }
cdupaty 0:d974bcee4f69 4464 else
cdupaty 0:d974bcee4f69 4465 {
cdupaty 0:d974bcee4f69 4466 forme = false;
cdupaty 0:d974bcee4f69 4467 _hreceived = false;
cdupaty 0:d974bcee4f69 4468 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4469 printf("** The timeout has expired **");
cdupaty 0:d974bcee4f69 4470 printf("\n");
cdupaty 0:d974bcee4f69 4471 #endif
cdupaty 0:d974bcee4f69 4472 }
cdupaty 0:d974bcee4f69 4473 }
cdupaty 0:d974bcee4f69 4474 // We use _hreceived because we need to ensure that _destination value is correctly
cdupaty 0:d974bcee4f69 4475 // updated and is not the _destination value from the previously packet
cdupaty 0:d974bcee4f69 4476 if( _hreceived == true )
cdupaty 0:d974bcee4f69 4477 { // Checking destination
cdupaty 0:d974bcee4f69 4478 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4479 printf("## Checking destination ##");
cdupaty 0:d974bcee4f69 4480 #endif
cdupaty 0:d974bcee4f69 4481
cdupaty 0:d974bcee4f69 4482 // added by C. Pham
cdupaty 0:d974bcee4f69 4483 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 4484 forme=true;
cdupaty 0:d974bcee4f69 4485
cdupaty 0:d974bcee4f69 4486 // if we wait for an ACK, then we do not check for net key
cdupaty 0:d974bcee4f69 4487 if (_requestACK==0)
cdupaty 0:d974bcee4f69 4488 if (_the_net_key_0!=_my_netkey[0] || _the_net_key_1!=_my_netkey[1]) {
cdupaty 0:d974bcee4f69 4489 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4490 printf("## Wrong net key ##");
cdupaty 0:d974bcee4f69 4491 //#endif
cdupaty 0:d974bcee4f69 4492 forme=false;
cdupaty 0:d974bcee4f69 4493 }
cdupaty 0:d974bcee4f69 4494 else
cdupaty 0:d974bcee4f69 4495 {
cdupaty 0:d974bcee4f69 4496 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4497 printf("## Good net key ##");
cdupaty 0:d974bcee4f69 4498 //#endif
cdupaty 0:d974bcee4f69 4499 }
cdupaty 0:d974bcee4f69 4500
cdupaty 0:d974bcee4f69 4501
cdupaty 0:d974bcee4f69 4502 if( forme && ((_destination == _nodeAddress) || (_destination == BROADCAST_0)) )
cdupaty 0:d974bcee4f69 4503 #else
cdupaty 0:d974bcee4f69 4504 // modified by C. Pham
cdupaty 0:d974bcee4f69 4505 // if _rawFormat, accept all
cdupaty 0:d974bcee4f69 4506 if( (_destination == _nodeAddress) || (_destination == BROADCAST_0) || _rawFormat)
cdupaty 0:d974bcee4f69 4507 #endif
cdupaty 0:d974bcee4f69 4508 { // LoRa or FSK mode
cdupaty 0:d974bcee4f69 4509 forme = true;
cdupaty 0:d974bcee4f69 4510 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4511 printf("## Packet received is for me ##");
cdupaty 0:d974bcee4f69 4512 #endif
cdupaty 0:d974bcee4f69 4513 }
cdupaty 0:d974bcee4f69 4514 else
cdupaty 0:d974bcee4f69 4515 {
cdupaty 0:d974bcee4f69 4516 forme = false;
cdupaty 0:d974bcee4f69 4517 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4518 printf("## Packet received is not for me ##");
cdupaty 0:d974bcee4f69 4519 printf("\n");
cdupaty 0:d974bcee4f69 4520 #endif
cdupaty 0:d974bcee4f69 4521 if( _modem == LORA ) // STANDBY PARA MINIMIZAR EL CONSUMO
cdupaty 0:d974bcee4f69 4522 { // LoRa mode
cdupaty 0:d974bcee4f69 4523 //writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Setting standby LoRa mode
cdupaty 0:d974bcee4f69 4524 }
cdupaty 0:d974bcee4f69 4525 else
cdupaty 0:d974bcee4f69 4526 { // FSK mode
cdupaty 0:d974bcee4f69 4527 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Setting standby FSK mode
cdupaty 0:d974bcee4f69 4528 }
cdupaty 0:d974bcee4f69 4529 }
cdupaty 0:d974bcee4f69 4530 }
cdupaty 0:d974bcee4f69 4531 //----else
cdupaty 0:d974bcee4f69 4532 // {
cdupaty 0:d974bcee4f69 4533 // }
cdupaty 0:d974bcee4f69 4534 return forme;
cdupaty 0:d974bcee4f69 4535 }
cdupaty 0:d974bcee4f69 4536
cdupaty 0:d974bcee4f69 4537 /*
cdupaty 0:d974bcee4f69 4538 Function: It gets and stores a packet if it is received before MAX_TIMEOUT expires.
cdupaty 0:d974bcee4f69 4539 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4540 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4541 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4542 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4543 */
cdupaty 0:d974bcee4f69 4544 uint8_t SX1272::getPacketMAXTimeout()
cdupaty 0:d974bcee4f69 4545 {
cdupaty 0:d974bcee4f69 4546 return getPacket(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4547 }
cdupaty 0:d974bcee4f69 4548
cdupaty 0:d974bcee4f69 4549 /*
cdupaty 0:d974bcee4f69 4550 Function: It gets and stores a packet if it is received.
cdupaty 0:d974bcee4f69 4551 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4552 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4553 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4554 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4555 */
cdupaty 0:d974bcee4f69 4556 int8_t SX1272::getPacket()
cdupaty 0:d974bcee4f69 4557 {
cdupaty 0:d974bcee4f69 4558 return getPacket(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 4559 }
cdupaty 0:d974bcee4f69 4560
cdupaty 0:d974bcee4f69 4561 /*
cdupaty 0:d974bcee4f69 4562 Function: It gets and stores a packet if it is received before ending 'wait' time.
cdupaty 0:d974bcee4f69 4563 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4564 // added by C. Pham
cdupaty 0:d974bcee4f69 4565 state = 5 --> The command has been executed with no errors and an ACK is requested
cdupaty 0:d974bcee4f69 4566 state = 3 --> The command has been executed but packet has been incorrectly received
cdupaty 0:d974bcee4f69 4567 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4568 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4569 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4570 state = -1 --> Forbidden parameter value for this function
cdupaty 0:d974bcee4f69 4571 Parameters:
cdupaty 0:d974bcee4f69 4572 wait: time to wait while there is no a valid header received.
cdupaty 0:d974bcee4f69 4573 */
cdupaty 0:d974bcee4f69 4574 int8_t SX1272::getPacket(uint16_t wait)
cdupaty 0:d974bcee4f69 4575 {
cdupaty 0:d974bcee4f69 4576 int8_t state = 2; // uint8_t a l origine
cdupaty 0:d974bcee4f69 4577 byte value = 0x00;
cdupaty 0:d974bcee4f69 4578 //unsigned long previous;
cdupaty 0:d974bcee4f69 4579 unsigned long exitTime;
cdupaty 0:d974bcee4f69 4580 boolean p_received = false;
cdupaty 0:d974bcee4f69 4581
cdupaty 0:d974bcee4f69 4582 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4583 printf("\n");
cdupaty 0:d974bcee4f69 4584 printf("Starting 'getPacket'\n");
cdupaty 0:d974bcee4f69 4585 #endif
cdupaty 0:d974bcee4f69 4586
cdupaty 0:d974bcee4f69 4587 //previous = millis();
cdupaty 0:d974bcee4f69 4588 exitTime = millis() + (unsigned long)wait;
cdupaty 0:d974bcee4f69 4589 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4590 { // LoRa mode
cdupaty 0:d974bcee4f69 4591 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 4592 // Wait until the packet is received (RxDone flag) or the timeout expires
cdupaty 0:d974bcee4f69 4593 //while( (bitRead(value, 6) == 0) && (millis() - previous < (unsigned long)wait) )
cdupaty 0:d974bcee4f69 4594 while( (bitRead(value, 6) == 0) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4595 {
cdupaty 0:d974bcee4f69 4596 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 4597 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 4598 //if( millis() < previous )
cdupaty 0:d974bcee4f69 4599 //{
cdupaty 0:d974bcee4f69 4600 // previous = millis();
cdupaty 0:d974bcee4f69 4601 //}
cdupaty 0:d974bcee4f69 4602 } // end while (millis)
cdupaty 0:d974bcee4f69 4603
cdupaty 0:d974bcee4f69 4604 if( (bitRead(value, 6) == 1) && (bitRead(value, 5) == 0) )
cdupaty 0:d974bcee4f69 4605 { // packet received & CRC correct
cdupaty 0:d974bcee4f69 4606 p_received = true; // packet correctly received
cdupaty 0:d974bcee4f69 4607 _reception = CORRECT_PACKET;
cdupaty 0:d974bcee4f69 4608 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4609 printf("## Packet correctly received in LoRa mode ##");
cdupaty 0:d974bcee4f69 4610 #endif
cdupaty 0:d974bcee4f69 4611 }
cdupaty 0:d974bcee4f69 4612 else
cdupaty 0:d974bcee4f69 4613 {
cdupaty 0:d974bcee4f69 4614 if( bitRead(value, 5) != 0 )
cdupaty 0:d974bcee4f69 4615 { // CRC incorrect
cdupaty 0:d974bcee4f69 4616 _reception = INCORRECT_PACKET;
cdupaty 0:d974bcee4f69 4617 state = 3;
cdupaty 0:d974bcee4f69 4618 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4619 printf("** The CRC is incorrect **");
cdupaty 0:d974bcee4f69 4620 printf("\n");
cdupaty 0:d974bcee4f69 4621 #endif
cdupaty 0:d974bcee4f69 4622 }
cdupaty 0:d974bcee4f69 4623 }
cdupaty 0:d974bcee4f69 4624 //writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Setting standby LoRa mode
cdupaty 0:d974bcee4f69 4625 }
cdupaty 0:d974bcee4f69 4626 else
cdupaty 0:d974bcee4f69 4627 { // FSK mode
cdupaty 0:d974bcee4f69 4628 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 4629 //while( (bitRead(value, 2) == 0) && (millis() - previous < wait) )
cdupaty 0:d974bcee4f69 4630 while( (bitRead(value, 2) == 0) && (millis() < exitTime) )
cdupaty 0:d974bcee4f69 4631 {
cdupaty 0:d974bcee4f69 4632 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 4633 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 4634 //if( millis() < previous )
cdupaty 0:d974bcee4f69 4635 //{
cdupaty 0:d974bcee4f69 4636 // previous = millis();
cdupaty 0:d974bcee4f69 4637 //}
cdupaty 0:d974bcee4f69 4638 } // end while (millis)
cdupaty 0:d974bcee4f69 4639
cdupaty 0:d974bcee4f69 4640 if( bitRead(value, 2) == 1 )
cdupaty 0:d974bcee4f69 4641 { // packet received
cdupaty 0:d974bcee4f69 4642 if( bitRead(value, 1) == 1 )
cdupaty 0:d974bcee4f69 4643 { // CRC correct
cdupaty 0:d974bcee4f69 4644 _reception = CORRECT_PACKET;
cdupaty 0:d974bcee4f69 4645 p_received = true;
cdupaty 0:d974bcee4f69 4646 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4647 printf("## Packet correctly received in FSK mode ##");
cdupaty 0:d974bcee4f69 4648 #endif
cdupaty 0:d974bcee4f69 4649 }
cdupaty 0:d974bcee4f69 4650 else
cdupaty 0:d974bcee4f69 4651 { // CRC incorrect
cdupaty 0:d974bcee4f69 4652 _reception = INCORRECT_PACKET;
cdupaty 0:d974bcee4f69 4653 state = 3;
cdupaty 0:d974bcee4f69 4654 p_received = false;
cdupaty 0:d974bcee4f69 4655 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4656 printf("## Packet incorrectly received in FSK mode ##");
cdupaty 0:d974bcee4f69 4657 #endif
cdupaty 0:d974bcee4f69 4658 }
cdupaty 0:d974bcee4f69 4659 }
cdupaty 0:d974bcee4f69 4660 else
cdupaty 0:d974bcee4f69 4661 {
cdupaty 0:d974bcee4f69 4662 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4663 printf("** The timeout has expired **");
cdupaty 0:d974bcee4f69 4664 printf("\n");
cdupaty 0:d974bcee4f69 4665 #endif
cdupaty 0:d974bcee4f69 4666 }
cdupaty 0:d974bcee4f69 4667 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Setting standby FSK mode
cdupaty 0:d974bcee4f69 4668 }
cdupaty 0:d974bcee4f69 4669 if( p_received == true )
cdupaty 0:d974bcee4f69 4670 {
cdupaty 0:d974bcee4f69 4671 // Store the packet
cdupaty 0:d974bcee4f69 4672 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4673 {
cdupaty 0:d974bcee4f69 4674 // comment by C. Pham
cdupaty 0:d974bcee4f69 4675 // set the FIFO addr to 0 to read again all the bytes
cdupaty 0:d974bcee4f69 4676 writeRegister(REG_FIFO_ADDR_PTR, 0x00); // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 4677
cdupaty 0:d974bcee4f69 4678 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 4679 // added by C. Pham
cdupaty 0:d974bcee4f69 4680 packet_received.netkey[0]=readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4681 packet_received.netkey[1]=readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4682 #endif
cdupaty 0:d974bcee4f69 4683 //modified by C. Pham
cdupaty 0:d974bcee4f69 4684 if (!_rawFormat)
cdupaty 0:d974bcee4f69 4685 packet_received.dst = readRegister(REG_FIFO); // Storing first byte of the received packet
cdupaty 0:d974bcee4f69 4686 else
cdupaty 0:d974bcee4f69 4687 packet_received.dst = 0;
cdupaty 0:d974bcee4f69 4688 }
cdupaty 0:d974bcee4f69 4689 else
cdupaty 0:d974bcee4f69 4690 {
cdupaty 0:d974bcee4f69 4691 value = readRegister(REG_PACKET_CONFIG1);
cdupaty 0:d974bcee4f69 4692 if( (bitRead(value, 2) == 0) && (bitRead(value, 1) == 0) )
cdupaty 0:d974bcee4f69 4693 {
cdupaty 0:d974bcee4f69 4694 packet_received.dst = readRegister(REG_FIFO); // Storing first byte of the received packet
cdupaty 0:d974bcee4f69 4695 }
cdupaty 0:d974bcee4f69 4696 else
cdupaty 0:d974bcee4f69 4697 {
cdupaty 0:d974bcee4f69 4698 packet_received.dst = _destination; // Storing first byte of the received packet
cdupaty 0:d974bcee4f69 4699 }
cdupaty 0:d974bcee4f69 4700 }
cdupaty 0:d974bcee4f69 4701
cdupaty 0:d974bcee4f69 4702 // modified by C. Pham
cdupaty 0:d974bcee4f69 4703 if (!_rawFormat) {
cdupaty 0:d974bcee4f69 4704 packet_received.type = readRegister(REG_FIFO); // Reading second byte of the received packet
cdupaty 0:d974bcee4f69 4705 packet_received.src = readRegister(REG_FIFO); // Reading second byte of the received packet
cdupaty 0:d974bcee4f69 4706 packet_received.packnum = readRegister(REG_FIFO); // Reading third byte of the received packet
cdupaty 0:d974bcee4f69 4707 //packet_received.length = readRegister(REG_FIFO); // Reading fourth byte of the received packet
cdupaty 0:d974bcee4f69 4708 }
cdupaty 0:d974bcee4f69 4709 else {
cdupaty 0:d974bcee4f69 4710 packet_received.type = 0;
cdupaty 0:d974bcee4f69 4711 packet_received.src = 0;
cdupaty 0:d974bcee4f69 4712 packet_received.packnum = 0;
cdupaty 0:d974bcee4f69 4713 }
cdupaty 0:d974bcee4f69 4714
cdupaty 0:d974bcee4f69 4715 packet_received.length = readRegister(REG_RX_NB_BYTES);
cdupaty 0:d974bcee4f69 4716
cdupaty 0:d974bcee4f69 4717 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4718 {
cdupaty 0:d974bcee4f69 4719 if (_rawFormat) {
cdupaty 0:d974bcee4f69 4720 _payloadlength=packet_received.length;
cdupaty 0:d974bcee4f69 4721 }
cdupaty 0:d974bcee4f69 4722 else
cdupaty 0:d974bcee4f69 4723 _payloadlength = packet_received.length - OFFSET_PAYLOADLENGTH;
cdupaty 0:d974bcee4f69 4724 }
cdupaty 0:d974bcee4f69 4725 if( packet_received.length > (MAX_LENGTH + 1) )
cdupaty 0:d974bcee4f69 4726 {
cdupaty 0:d974bcee4f69 4727 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4728 printf("Corrupted packet, length must be less than 256");
cdupaty 0:d974bcee4f69 4729 #endif
cdupaty 0:d974bcee4f69 4730 }
cdupaty 0:d974bcee4f69 4731 else
cdupaty 0:d974bcee4f69 4732 {
cdupaty 0:d974bcee4f69 4733 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 4734 {
cdupaty 0:d974bcee4f69 4735 packet_received.data[i] = readRegister(REG_FIFO); // Storing payload
cdupaty 0:d974bcee4f69 4736 }
cdupaty 0:d974bcee4f69 4737
cdupaty 0:d974bcee4f69 4738 // commented by C. Pham
cdupaty 0:d974bcee4f69 4739 //packet_received.retry = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 4740
cdupaty 0:d974bcee4f69 4741 // Print the packet if debug_mode
cdupaty 0:d974bcee4f69 4742 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4743 printf("## Packet received:\n");
cdupaty 0:d974bcee4f69 4744 printf("Destination: %d\n",packet_received.dst);
cdupaty 0:d974bcee4f69 4745 // Serial.println(packet_received.dst); // Printing destination
cdupaty 0:d974bcee4f69 4746 printf("Type: %d\n",packet_received.type);
cdupaty 0:d974bcee4f69 4747 // Serial.println(packet_received.type); // Printing source
cdupaty 0:d974bcee4f69 4748 printf("Source: %d\n",packet_received.src);
cdupaty 0:d974bcee4f69 4749 // Serial.println(packet_received.src); // Printing source
cdupaty 0:d974bcee4f69 4750 printf("Packet number: %d\n",packet_received.packnum);
cdupaty 0:d974bcee4f69 4751 // Serial.println(packet_received.packnum); // Printing packet number
cdupaty 0:d974bcee4f69 4752 //printf("Packet length: ");
cdupaty 0:d974bcee4f69 4753 //Serial.println(packet_received.length); // Printing packet length
cdupaty 0:d974bcee4f69 4754 printf("Data: ");
cdupaty 0:d974bcee4f69 4755 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 4756 {
cdupaty 0:d974bcee4f69 4757 //Serial.print((char)packet_received.data[i]); // Printing payload
cdupaty 0:d974bcee4f69 4758 printf("%c",packet_received.data[i]);
cdupaty 0:d974bcee4f69 4759 }
cdupaty 0:d974bcee4f69 4760 printf("\n");
cdupaty 0:d974bcee4f69 4761 //printf("Retry number: ");
cdupaty 0:d974bcee4f69 4762 //Serial.println(packet_received.retry); // Printing number retry
cdupaty 0:d974bcee4f69 4763 printf("##");
cdupaty 0:d974bcee4f69 4764 printf("\n");
cdupaty 0:d974bcee4f69 4765 #endif
cdupaty 0:d974bcee4f69 4766 state = 0;
cdupaty 0:d974bcee4f69 4767
cdupaty 0:d974bcee4f69 4768 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 4769 // added by C. Pham
cdupaty 0:d974bcee4f69 4770 // need to send an ACK
cdupaty 0:d974bcee4f69 4771 if (packet_received.type & PKT_FLAG_ACK_REQ) {
cdupaty 0:d974bcee4f69 4772 state = 5;
cdupaty 0:d974bcee4f69 4773 _requestACK_indicator=1;
cdupaty 0:d974bcee4f69 4774 }
cdupaty 0:d974bcee4f69 4775 else
cdupaty 0:d974bcee4f69 4776 _requestACK_indicator=0;
cdupaty 0:d974bcee4f69 4777 #endif
cdupaty 0:d974bcee4f69 4778 }
cdupaty 0:d974bcee4f69 4779 }
cdupaty 0:d974bcee4f69 4780 else
cdupaty 0:d974bcee4f69 4781 {
cdupaty 0:d974bcee4f69 4782 state = 1;
cdupaty 0:d974bcee4f69 4783 if( (_reception == INCORRECT_PACKET) && (_retries < _maxRetries) )
cdupaty 0:d974bcee4f69 4784 {
cdupaty 0:d974bcee4f69 4785 // comment by C. Pham
cdupaty 0:d974bcee4f69 4786 // what is the purpose of incrementing retries here?
cdupaty 0:d974bcee4f69 4787 // bug? not needed?
cdupaty 0:d974bcee4f69 4788 _retries++;
cdupaty 0:d974bcee4f69 4789 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4790 printf("## Retrying to send the last packet ##");
cdupaty 0:d974bcee4f69 4791 printf("\n");
cdupaty 0:d974bcee4f69 4792 #endif
cdupaty 0:d974bcee4f69 4793 }
cdupaty 0:d974bcee4f69 4794 }
cdupaty 0:d974bcee4f69 4795 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4796 {
cdupaty 0:d974bcee4f69 4797 writeRegister(REG_FIFO_ADDR_PTR, 0x00); // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 4798 }
cdupaty 0:d974bcee4f69 4799 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 4800 if( wait > MAX_WAIT )
cdupaty 0:d974bcee4f69 4801 {
cdupaty 0:d974bcee4f69 4802 state = -1;
cdupaty 0:d974bcee4f69 4803 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 4804 printf("** The timeout must be smaller than 12.5 seconds **");
cdupaty 0:d974bcee4f69 4805 printf("\n");
cdupaty 0:d974bcee4f69 4806 #endif
cdupaty 0:d974bcee4f69 4807 }
cdupaty 0:d974bcee4f69 4808
cdupaty 0:d974bcee4f69 4809 return state;
cdupaty 0:d974bcee4f69 4810 }
cdupaty 0:d974bcee4f69 4811
cdupaty 0:d974bcee4f69 4812 /*
cdupaty 0:d974bcee4f69 4813 Function: It sets the packet destination.
cdupaty 0:d974bcee4f69 4814 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4815 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4816 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4817 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4818 Parameters:
cdupaty 0:d974bcee4f69 4819 dest: destination value of the packet sent.
cdupaty 0:d974bcee4f69 4820 */
cdupaty 0:d974bcee4f69 4821 int8_t SX1272::setDestination(uint8_t dest)
cdupaty 0:d974bcee4f69 4822 {
cdupaty 0:d974bcee4f69 4823 int8_t state = 2;
cdupaty 0:d974bcee4f69 4824
cdupaty 0:d974bcee4f69 4825 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4826 printf("\n");
cdupaty 0:d974bcee4f69 4827 printf("Starting 'setDestination'\n");
cdupaty 0:d974bcee4f69 4828 #endif
cdupaty 0:d974bcee4f69 4829
cdupaty 0:d974bcee4f69 4830 state = 1;
cdupaty 0:d974bcee4f69 4831 _destination = dest; // Storing destination in a global variable
cdupaty 0:d974bcee4f69 4832 packet_sent.dst = dest; // Setting destination in packet structure
cdupaty 0:d974bcee4f69 4833 packet_sent.src = _nodeAddress; // Setting source in packet structure
cdupaty 0:d974bcee4f69 4834 packet_sent.packnum = _packetNumber; // Setting packet number in packet structure
cdupaty 0:d974bcee4f69 4835 _packetNumber++;
cdupaty 0:d974bcee4f69 4836 state = 0;
cdupaty 0:d974bcee4f69 4837
cdupaty 0:d974bcee4f69 4838 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4839 printf("## Destination %X\n",_destination);
cdupaty 0:d974bcee4f69 4840 // Serial.print(_destination, HEX);
cdupaty 0:d974bcee4f69 4841 printf(" successfully set ##");
cdupaty 0:d974bcee4f69 4842 printf("## Source %d\n",packet_sent.src);
cdupaty 0:d974bcee4f69 4843 // Serial.print(packet_sent.src, DEC);
cdupaty 0:d974bcee4f69 4844 printf(" successfully set ##");
cdupaty 0:d974bcee4f69 4845 printf("## Packet number %d\n",packet_sent.packnum);
cdupaty 0:d974bcee4f69 4846 // Serial.print(packet_sent.packnum, DEC);
cdupaty 0:d974bcee4f69 4847 printf(" successfully set ##");
cdupaty 0:d974bcee4f69 4848 printf("\n");
cdupaty 0:d974bcee4f69 4849 #endif
cdupaty 0:d974bcee4f69 4850 return state;
cdupaty 0:d974bcee4f69 4851 }
cdupaty 0:d974bcee4f69 4852
cdupaty 0:d974bcee4f69 4853 /*
cdupaty 0:d974bcee4f69 4854 Function: It sets the timeout according to the configured mode.
cdupaty 0:d974bcee4f69 4855 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 4856 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 4857 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 4858 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 4859 */
cdupaty 0:d974bcee4f69 4860 uint8_t SX1272::setTimeout()
cdupaty 0:d974bcee4f69 4861 {
cdupaty 0:d974bcee4f69 4862 uint8_t state = 2;
cdupaty 0:d974bcee4f69 4863 // retire par C.Dupaty
cdupaty 0:d974bcee4f69 4864 // uint16_t delay;
cdupaty 0:d974bcee4f69 4865
cdupaty 0:d974bcee4f69 4866 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 4867 printf("\n");
cdupaty 0:d974bcee4f69 4868 printf("Starting 'setTimeout'\n");
cdupaty 0:d974bcee4f69 4869 #endif
cdupaty 0:d974bcee4f69 4870
cdupaty 0:d974bcee4f69 4871 state = 1;
cdupaty 0:d974bcee4f69 4872
cdupaty 0:d974bcee4f69 4873 // changed by C. Pham
cdupaty 0:d974bcee4f69 4874 // we always use MAX_TIMEOUT
cdupaty 0:d974bcee4f69 4875 _sendTime = MAX_TIMEOUT;
cdupaty 0:d974bcee4f69 4876
cdupaty 0:d974bcee4f69 4877 /*
cdupaty 0:d974bcee4f69 4878 if( _modem == LORA )
cdupaty 0:d974bcee4f69 4879 {
cdupaty 0:d974bcee4f69 4880 switch(_spreadingFactor)
cdupaty 0:d974bcee4f69 4881 { // Choosing Spreading Factor
cdupaty 0:d974bcee4f69 4882 case SF_6: switch(_bandwidth)
cdupaty 0:d974bcee4f69 4883 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 4884 case BW_125:
cdupaty 0:d974bcee4f69 4885 switch(_codingRate)
cdupaty 0:d974bcee4f69 4886 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4887 case CR_5: _sendTime = 335;
cdupaty 0:d974bcee4f69 4888 break;
cdupaty 0:d974bcee4f69 4889 case CR_6: _sendTime = 352;
cdupaty 0:d974bcee4f69 4890 break;
cdupaty 0:d974bcee4f69 4891 case CR_7: _sendTime = 368;
cdupaty 0:d974bcee4f69 4892 break;
cdupaty 0:d974bcee4f69 4893 case CR_8: _sendTime = 386;
cdupaty 0:d974bcee4f69 4894 break;
cdupaty 0:d974bcee4f69 4895 }
cdupaty 0:d974bcee4f69 4896 break;
cdupaty 0:d974bcee4f69 4897 case BW_250:
cdupaty 0:d974bcee4f69 4898 switch(_codingRate)
cdupaty 0:d974bcee4f69 4899 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4900 case CR_5: _sendTime = 287;
cdupaty 0:d974bcee4f69 4901 break;
cdupaty 0:d974bcee4f69 4902 case CR_6: _sendTime = 296;
cdupaty 0:d974bcee4f69 4903 break;
cdupaty 0:d974bcee4f69 4904 case CR_7: _sendTime = 305;
cdupaty 0:d974bcee4f69 4905 break;
cdupaty 0:d974bcee4f69 4906 case CR_8: _sendTime = 312;
cdupaty 0:d974bcee4f69 4907 break;
cdupaty 0:d974bcee4f69 4908 }
cdupaty 0:d974bcee4f69 4909 break;
cdupaty 0:d974bcee4f69 4910 case BW_500:
cdupaty 0:d974bcee4f69 4911 switch(_codingRate)
cdupaty 0:d974bcee4f69 4912 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4913 case CR_5: _sendTime = 242;
cdupaty 0:d974bcee4f69 4914 break;
cdupaty 0:d974bcee4f69 4915 case CR_6: _sendTime = 267;
cdupaty 0:d974bcee4f69 4916 break;
cdupaty 0:d974bcee4f69 4917 case CR_7: _sendTime = 272;
cdupaty 0:d974bcee4f69 4918 break;
cdupaty 0:d974bcee4f69 4919 case CR_8: _sendTime = 276;
cdupaty 0:d974bcee4f69 4920 break;
cdupaty 0:d974bcee4f69 4921 }
cdupaty 0:d974bcee4f69 4922 break;
cdupaty 0:d974bcee4f69 4923 }
cdupaty 0:d974bcee4f69 4924 break;
cdupaty 0:d974bcee4f69 4925
cdupaty 0:d974bcee4f69 4926 case SF_7: switch(_bandwidth)
cdupaty 0:d974bcee4f69 4927 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 4928 case BW_125:
cdupaty 0:d974bcee4f69 4929 switch(_codingRate)
cdupaty 0:d974bcee4f69 4930 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4931 case CR_5: _sendTime = 408;
cdupaty 0:d974bcee4f69 4932 break;
cdupaty 0:d974bcee4f69 4933 case CR_6: _sendTime = 438;
cdupaty 0:d974bcee4f69 4934 break;
cdupaty 0:d974bcee4f69 4935 case CR_7: _sendTime = 468;
cdupaty 0:d974bcee4f69 4936 break;
cdupaty 0:d974bcee4f69 4937 case CR_8: _sendTime = 497;
cdupaty 0:d974bcee4f69 4938 break;
cdupaty 0:d974bcee4f69 4939 }
cdupaty 0:d974bcee4f69 4940 break;
cdupaty 0:d974bcee4f69 4941 case BW_250:
cdupaty 0:d974bcee4f69 4942 switch(_codingRate)
cdupaty 0:d974bcee4f69 4943 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4944 case CR_5: _sendTime = 325;
cdupaty 0:d974bcee4f69 4945 break;
cdupaty 0:d974bcee4f69 4946 case CR_6: _sendTime = 339;
cdupaty 0:d974bcee4f69 4947 break;
cdupaty 0:d974bcee4f69 4948 case CR_7: _sendTime = 355;
cdupaty 0:d974bcee4f69 4949 break;
cdupaty 0:d974bcee4f69 4950 case CR_8: _sendTime = 368;
cdupaty 0:d974bcee4f69 4951 break;
cdupaty 0:d974bcee4f69 4952 }
cdupaty 0:d974bcee4f69 4953 break;
cdupaty 0:d974bcee4f69 4954 case BW_500:
cdupaty 0:d974bcee4f69 4955 switch(_codingRate)
cdupaty 0:d974bcee4f69 4956 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4957 case CR_5: _sendTime = 282;
cdupaty 0:d974bcee4f69 4958 break;
cdupaty 0:d974bcee4f69 4959 case CR_6: _sendTime = 290;
cdupaty 0:d974bcee4f69 4960 break;
cdupaty 0:d974bcee4f69 4961 case CR_7: _sendTime = 296;
cdupaty 0:d974bcee4f69 4962 break;
cdupaty 0:d974bcee4f69 4963 case CR_8: _sendTime = 305;
cdupaty 0:d974bcee4f69 4964 break;
cdupaty 0:d974bcee4f69 4965 }
cdupaty 0:d974bcee4f69 4966 break;
cdupaty 0:d974bcee4f69 4967 }
cdupaty 0:d974bcee4f69 4968 break;
cdupaty 0:d974bcee4f69 4969
cdupaty 0:d974bcee4f69 4970 case SF_8: switch(_bandwidth)
cdupaty 0:d974bcee4f69 4971 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 4972 case BW_125:
cdupaty 0:d974bcee4f69 4973 switch(_codingRate)
cdupaty 0:d974bcee4f69 4974 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4975 case CR_5: _sendTime = 537;
cdupaty 0:d974bcee4f69 4976 break;
cdupaty 0:d974bcee4f69 4977 case CR_6: _sendTime = 588;
cdupaty 0:d974bcee4f69 4978 break;
cdupaty 0:d974bcee4f69 4979 case CR_7: _sendTime = 640;
cdupaty 0:d974bcee4f69 4980 break;
cdupaty 0:d974bcee4f69 4981 case CR_8: _sendTime = 691;
cdupaty 0:d974bcee4f69 4982 break;
cdupaty 0:d974bcee4f69 4983 }
cdupaty 0:d974bcee4f69 4984 break;
cdupaty 0:d974bcee4f69 4985 case BW_250:
cdupaty 0:d974bcee4f69 4986 switch(_codingRate)
cdupaty 0:d974bcee4f69 4987 { // Choosing coding rate
cdupaty 0:d974bcee4f69 4988 case CR_5: _sendTime = 388;
cdupaty 0:d974bcee4f69 4989 break;
cdupaty 0:d974bcee4f69 4990 case CR_6: _sendTime = 415;
cdupaty 0:d974bcee4f69 4991 break;
cdupaty 0:d974bcee4f69 4992 case CR_7: _sendTime = 440;
cdupaty 0:d974bcee4f69 4993 break;
cdupaty 0:d974bcee4f69 4994 case CR_8: _sendTime = 466;
cdupaty 0:d974bcee4f69 4995 break;
cdupaty 0:d974bcee4f69 4996 }
cdupaty 0:d974bcee4f69 4997 break;
cdupaty 0:d974bcee4f69 4998 case BW_500:
cdupaty 0:d974bcee4f69 4999 switch(_codingRate)
cdupaty 0:d974bcee4f69 5000 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5001 case CR_5: _sendTime = 315;
cdupaty 0:d974bcee4f69 5002 break;
cdupaty 0:d974bcee4f69 5003 case CR_6: _sendTime = 326;
cdupaty 0:d974bcee4f69 5004 break;
cdupaty 0:d974bcee4f69 5005 case CR_7: _sendTime = 340;
cdupaty 0:d974bcee4f69 5006 break;
cdupaty 0:d974bcee4f69 5007 case CR_8: _sendTime = 352;
cdupaty 0:d974bcee4f69 5008 break;
cdupaty 0:d974bcee4f69 5009 }
cdupaty 0:d974bcee4f69 5010 break;
cdupaty 0:d974bcee4f69 5011 }
cdupaty 0:d974bcee4f69 5012 break;
cdupaty 0:d974bcee4f69 5013
cdupaty 0:d974bcee4f69 5014 case SF_9: switch(_bandwidth)
cdupaty 0:d974bcee4f69 5015 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 5016 case BW_125:
cdupaty 0:d974bcee4f69 5017 switch(_codingRate)
cdupaty 0:d974bcee4f69 5018 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5019 case CR_5: _sendTime = 774;
cdupaty 0:d974bcee4f69 5020 break;
cdupaty 0:d974bcee4f69 5021 case CR_6: _sendTime = 864;
cdupaty 0:d974bcee4f69 5022 break;
cdupaty 0:d974bcee4f69 5023 case CR_7: _sendTime = 954;
cdupaty 0:d974bcee4f69 5024 break;
cdupaty 0:d974bcee4f69 5025 case CR_8: _sendTime = 1044;
cdupaty 0:d974bcee4f69 5026 break;
cdupaty 0:d974bcee4f69 5027 }
cdupaty 0:d974bcee4f69 5028 break;
cdupaty 0:d974bcee4f69 5029 case BW_250:
cdupaty 0:d974bcee4f69 5030 switch(_codingRate)
cdupaty 0:d974bcee4f69 5031 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5032 case CR_5: _sendTime = 506;
cdupaty 0:d974bcee4f69 5033 break;
cdupaty 0:d974bcee4f69 5034 case CR_6: _sendTime = 552;
cdupaty 0:d974bcee4f69 5035 break;
cdupaty 0:d974bcee4f69 5036 case CR_7: _sendTime = 596;
cdupaty 0:d974bcee4f69 5037 break;
cdupaty 0:d974bcee4f69 5038 case CR_8: _sendTime = 642;
cdupaty 0:d974bcee4f69 5039 break;
cdupaty 0:d974bcee4f69 5040 }
cdupaty 0:d974bcee4f69 5041 break;
cdupaty 0:d974bcee4f69 5042 case BW_500:
cdupaty 0:d974bcee4f69 5043 switch(_codingRate)
cdupaty 0:d974bcee4f69 5044 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5045 case CR_5: _sendTime = 374;
cdupaty 0:d974bcee4f69 5046 break;
cdupaty 0:d974bcee4f69 5047 case CR_6: _sendTime = 396;
cdupaty 0:d974bcee4f69 5048 break;
cdupaty 0:d974bcee4f69 5049 case CR_7: _sendTime = 418;
cdupaty 0:d974bcee4f69 5050 break;
cdupaty 0:d974bcee4f69 5051 case CR_8: _sendTime = 441;
cdupaty 0:d974bcee4f69 5052 break;
cdupaty 0:d974bcee4f69 5053 }
cdupaty 0:d974bcee4f69 5054 break;
cdupaty 0:d974bcee4f69 5055 }
cdupaty 0:d974bcee4f69 5056 break;
cdupaty 0:d974bcee4f69 5057
cdupaty 0:d974bcee4f69 5058 case SF_10: switch(_bandwidth)
cdupaty 0:d974bcee4f69 5059 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 5060 case BW_125:
cdupaty 0:d974bcee4f69 5061 switch(_codingRate)
cdupaty 0:d974bcee4f69 5062 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5063 case CR_5: _sendTime = 1226;
cdupaty 0:d974bcee4f69 5064 break;
cdupaty 0:d974bcee4f69 5065 case CR_6: _sendTime = 1388;
cdupaty 0:d974bcee4f69 5066 break;
cdupaty 0:d974bcee4f69 5067 case CR_7: _sendTime = 1552;
cdupaty 0:d974bcee4f69 5068 break;
cdupaty 0:d974bcee4f69 5069 case CR_8: _sendTime = 1716;
cdupaty 0:d974bcee4f69 5070 break;
cdupaty 0:d974bcee4f69 5071 }
cdupaty 0:d974bcee4f69 5072 break;
cdupaty 0:d974bcee4f69 5073 case BW_250:
cdupaty 0:d974bcee4f69 5074 switch(_codingRate)
cdupaty 0:d974bcee4f69 5075 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5076 case CR_5: _sendTime = 732;
cdupaty 0:d974bcee4f69 5077 break;
cdupaty 0:d974bcee4f69 5078 case CR_6: _sendTime = 815;
cdupaty 0:d974bcee4f69 5079 break;
cdupaty 0:d974bcee4f69 5080 case CR_7: _sendTime = 896;
cdupaty 0:d974bcee4f69 5081 break;
cdupaty 0:d974bcee4f69 5082 case CR_8: _sendTime = 977;
cdupaty 0:d974bcee4f69 5083 break;
cdupaty 0:d974bcee4f69 5084 }
cdupaty 0:d974bcee4f69 5085 break;
cdupaty 0:d974bcee4f69 5086 case BW_500:
cdupaty 0:d974bcee4f69 5087 switch(_codingRate)
cdupaty 0:d974bcee4f69 5088 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5089 case CR_5: _sendTime = 486;
cdupaty 0:d974bcee4f69 5090 break;
cdupaty 0:d974bcee4f69 5091 case CR_6: _sendTime = 527;
cdupaty 0:d974bcee4f69 5092 break;
cdupaty 0:d974bcee4f69 5093 case CR_7: _sendTime = 567;
cdupaty 0:d974bcee4f69 5094 break;
cdupaty 0:d974bcee4f69 5095 case CR_8: _sendTime = 608;
cdupaty 0:d974bcee4f69 5096 break;
cdupaty 0:d974bcee4f69 5097 }
cdupaty 0:d974bcee4f69 5098 break;
cdupaty 0:d974bcee4f69 5099 }
cdupaty 0:d974bcee4f69 5100 break;
cdupaty 0:d974bcee4f69 5101
cdupaty 0:d974bcee4f69 5102 case SF_11: switch(_bandwidth)
cdupaty 0:d974bcee4f69 5103 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 5104 case BW_125:
cdupaty 0:d974bcee4f69 5105 switch(_codingRate)
cdupaty 0:d974bcee4f69 5106 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5107 case CR_5: _sendTime = 2375;
cdupaty 0:d974bcee4f69 5108 break;
cdupaty 0:d974bcee4f69 5109 case CR_6: _sendTime = 2735;
cdupaty 0:d974bcee4f69 5110 break;
cdupaty 0:d974bcee4f69 5111 case CR_7: _sendTime = 3095;
cdupaty 0:d974bcee4f69 5112 break;
cdupaty 0:d974bcee4f69 5113 case CR_8: _sendTime = 3456;
cdupaty 0:d974bcee4f69 5114 break;
cdupaty 0:d974bcee4f69 5115 }
cdupaty 0:d974bcee4f69 5116 break;
cdupaty 0:d974bcee4f69 5117 case BW_250:
cdupaty 0:d974bcee4f69 5118 switch(_codingRate)
cdupaty 0:d974bcee4f69 5119 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5120 case CR_5: _sendTime = 1144;
cdupaty 0:d974bcee4f69 5121 break;
cdupaty 0:d974bcee4f69 5122 case CR_6: _sendTime = 1291;
cdupaty 0:d974bcee4f69 5123 break;
cdupaty 0:d974bcee4f69 5124 case CR_7: _sendTime = 1437;
cdupaty 0:d974bcee4f69 5125 break;
cdupaty 0:d974bcee4f69 5126 case CR_8: _sendTime = 1586;
cdupaty 0:d974bcee4f69 5127 break;
cdupaty 0:d974bcee4f69 5128 }
cdupaty 0:d974bcee4f69 5129 break;
cdupaty 0:d974bcee4f69 5130 case BW_500:
cdupaty 0:d974bcee4f69 5131 switch(_codingRate)
cdupaty 0:d974bcee4f69 5132 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5133 case CR_5: _sendTime = 691;
cdupaty 0:d974bcee4f69 5134 break;
cdupaty 0:d974bcee4f69 5135 case CR_6: _sendTime = 766;
cdupaty 0:d974bcee4f69 5136 break;
cdupaty 0:d974bcee4f69 5137 case CR_7: _sendTime = 838;
cdupaty 0:d974bcee4f69 5138 break;
cdupaty 0:d974bcee4f69 5139 case CR_8: _sendTime = 912;
cdupaty 0:d974bcee4f69 5140 break;
cdupaty 0:d974bcee4f69 5141 }
cdupaty 0:d974bcee4f69 5142 break;
cdupaty 0:d974bcee4f69 5143 }
cdupaty 0:d974bcee4f69 5144 break;
cdupaty 0:d974bcee4f69 5145
cdupaty 0:d974bcee4f69 5146 case SF_12: switch(_bandwidth)
cdupaty 0:d974bcee4f69 5147 { // Choosing bandwidth
cdupaty 0:d974bcee4f69 5148 case BW_125:
cdupaty 0:d974bcee4f69 5149 switch(_codingRate)
cdupaty 0:d974bcee4f69 5150 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5151 case CR_5: _sendTime = 4180;
cdupaty 0:d974bcee4f69 5152 break;
cdupaty 0:d974bcee4f69 5153 case CR_6: _sendTime = 4836;
cdupaty 0:d974bcee4f69 5154 break;
cdupaty 0:d974bcee4f69 5155 case CR_7: _sendTime = 5491;
cdupaty 0:d974bcee4f69 5156 break;
cdupaty 0:d974bcee4f69 5157 case CR_8: _sendTime = 6146;
cdupaty 0:d974bcee4f69 5158 break;
cdupaty 0:d974bcee4f69 5159 }
cdupaty 0:d974bcee4f69 5160 break;
cdupaty 0:d974bcee4f69 5161 case BW_250:
cdupaty 0:d974bcee4f69 5162 switch(_codingRate)
cdupaty 0:d974bcee4f69 5163 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5164 case CR_5: _sendTime = 1965;
cdupaty 0:d974bcee4f69 5165 break;
cdupaty 0:d974bcee4f69 5166 case CR_6: _sendTime = 2244;
cdupaty 0:d974bcee4f69 5167 break;
cdupaty 0:d974bcee4f69 5168 case CR_7: _sendTime = 2521;
cdupaty 0:d974bcee4f69 5169 break;
cdupaty 0:d974bcee4f69 5170 case CR_8: _sendTime = 2800;
cdupaty 0:d974bcee4f69 5171 break;
cdupaty 0:d974bcee4f69 5172 }
cdupaty 0:d974bcee4f69 5173 break;
cdupaty 0:d974bcee4f69 5174 case BW_500:
cdupaty 0:d974bcee4f69 5175 switch(_codingRate)
cdupaty 0:d974bcee4f69 5176 { // Choosing coding rate
cdupaty 0:d974bcee4f69 5177 case CR_5: _sendTime = 1102;
cdupaty 0:d974bcee4f69 5178 break;
cdupaty 0:d974bcee4f69 5179 case CR_6: _sendTime = 1241;
cdupaty 0:d974bcee4f69 5180 break;
cdupaty 0:d974bcee4f69 5181 case CR_7: _sendTime = 1381;
cdupaty 0:d974bcee4f69 5182 break;
cdupaty 0:d974bcee4f69 5183 case CR_8: _sendTime = 1520;
cdupaty 0:d974bcee4f69 5184 break;
cdupaty 0:d974bcee4f69 5185 }
cdupaty 0:d974bcee4f69 5186 break;
cdupaty 0:d974bcee4f69 5187 }
cdupaty 0:d974bcee4f69 5188 break;
cdupaty 0:d974bcee4f69 5189 default: _sendTime = MAX_TIMEOUT;
cdupaty 0:d974bcee4f69 5190 }
cdupaty 0:d974bcee4f69 5191 }
cdupaty 0:d974bcee4f69 5192 else
cdupaty 0:d974bcee4f69 5193 {
cdupaty 0:d974bcee4f69 5194 _sendTime = MAX_TIMEOUT;
cdupaty 0:d974bcee4f69 5195 }
cdupaty 0:d974bcee4f69 5196 delay = ((0.1*_sendTime) + 1);
cdupaty 0:d974bcee4f69 5197 _sendTime = (uint16_t) ((_sendTime * 1.2) + (rand()%delay));
cdupaty 0:d974bcee4f69 5198
cdupaty 0:d974bcee4f69 5199 */
cdupaty 0:d974bcee4f69 5200 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5201 printf("Timeout to send/receive is: %d\n",_sendTime);
cdupaty 0:d974bcee4f69 5202 // Serial.println(_sendTime, DEC);
cdupaty 0:d974bcee4f69 5203 #endif
cdupaty 0:d974bcee4f69 5204 state = 0;
cdupaty 0:d974bcee4f69 5205 return state;
cdupaty 0:d974bcee4f69 5206 }
cdupaty 0:d974bcee4f69 5207
cdupaty 0:d974bcee4f69 5208 /*
cdupaty 0:d974bcee4f69 5209 Function: It sets a char array payload packet in a packet struct.
cdupaty 0:d974bcee4f69 5210 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5211 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5212 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5213 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5214 */
cdupaty 0:d974bcee4f69 5215 uint8_t SX1272::setPayload(char *payload)
cdupaty 0:d974bcee4f69 5216 {
cdupaty 0:d974bcee4f69 5217 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5218 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 5219 uint16_t length16;
cdupaty 0:d974bcee4f69 5220
cdupaty 0:d974bcee4f69 5221 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5222 printf("\n");
cdupaty 0:d974bcee4f69 5223 printf("Starting 'setPayload'\n");
cdupaty 0:d974bcee4f69 5224 #endif
cdupaty 0:d974bcee4f69 5225
cdupaty 0:d974bcee4f69 5226 state = 1;
cdupaty 0:d974bcee4f69 5227 length16 = (uint16_t)strlen(payload);
cdupaty 0:d974bcee4f69 5228 state = truncPayload(length16);
cdupaty 0:d974bcee4f69 5229 if( state == 0 )
cdupaty 0:d974bcee4f69 5230 {
cdupaty 0:d974bcee4f69 5231 // fill data field until the end of the string
cdupaty 0:d974bcee4f69 5232 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5233 {
cdupaty 0:d974bcee4f69 5234 packet_sent.data[i] = payload[i];
cdupaty 0:d974bcee4f69 5235 }
cdupaty 0:d974bcee4f69 5236 }
cdupaty 0:d974bcee4f69 5237 else
cdupaty 0:d974bcee4f69 5238 {
cdupaty 0:d974bcee4f69 5239 state_f = state;
cdupaty 0:d974bcee4f69 5240 }
cdupaty 0:d974bcee4f69 5241 if( ( _modem == FSK ) && ( _payloadlength > MAX_PAYLOAD_FSK ) )
cdupaty 0:d974bcee4f69 5242 {
cdupaty 0:d974bcee4f69 5243 _payloadlength = MAX_PAYLOAD_FSK;
cdupaty 0:d974bcee4f69 5244 state = 1;
cdupaty 0:d974bcee4f69 5245 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5246 printf("In FSK, payload length must be less than 60 bytes.");
cdupaty 0:d974bcee4f69 5247 printf("\n");
cdupaty 0:d974bcee4f69 5248 #endif
cdupaty 0:d974bcee4f69 5249 }
cdupaty 0:d974bcee4f69 5250 // set length with the actual counter value
cdupaty 0:d974bcee4f69 5251 state_f = setPacketLength(); // Setting packet length in packet structure
cdupaty 0:d974bcee4f69 5252 return state_f;
cdupaty 0:d974bcee4f69 5253 }
cdupaty 0:d974bcee4f69 5254
cdupaty 0:d974bcee4f69 5255 /*
cdupaty 0:d974bcee4f69 5256 Function: It sets an uint8_t array payload packet in a packet struct.
cdupaty 0:d974bcee4f69 5257 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5258 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5259 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5260 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5261 */
cdupaty 0:d974bcee4f69 5262 uint8_t SX1272::setPayload(uint8_t *payload)
cdupaty 0:d974bcee4f69 5263 {
cdupaty 0:d974bcee4f69 5264 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5265
cdupaty 0:d974bcee4f69 5266 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5267 printf("\n");
cdupaty 0:d974bcee4f69 5268 printf("Starting 'setPayload'\n");
cdupaty 0:d974bcee4f69 5269 #endif
cdupaty 0:d974bcee4f69 5270
cdupaty 0:d974bcee4f69 5271 state = 1;
cdupaty 0:d974bcee4f69 5272 if( ( _modem == FSK ) && ( _payloadlength > MAX_PAYLOAD_FSK ) )
cdupaty 0:d974bcee4f69 5273 {
cdupaty 0:d974bcee4f69 5274 _payloadlength = MAX_PAYLOAD_FSK;
cdupaty 0:d974bcee4f69 5275 state = 1;
cdupaty 0:d974bcee4f69 5276 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5277 printf("In FSK, payload length must be less than 60 bytes.");
cdupaty 0:d974bcee4f69 5278 printf("\n");
cdupaty 0:d974bcee4f69 5279 #endif
cdupaty 0:d974bcee4f69 5280 }
cdupaty 0:d974bcee4f69 5281 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5282 {
cdupaty 0:d974bcee4f69 5283 packet_sent.data[i] = payload[i]; // Storing payload in packet structure
cdupaty 0:d974bcee4f69 5284 }
cdupaty 0:d974bcee4f69 5285 // set length with the actual counter value
cdupaty 0:d974bcee4f69 5286 state = setPacketLength(); // Setting packet length in packet structure
cdupaty 0:d974bcee4f69 5287 return state;
cdupaty 0:d974bcee4f69 5288 }
cdupaty 0:d974bcee4f69 5289
cdupaty 0:d974bcee4f69 5290 /*
cdupaty 0:d974bcee4f69 5291 Function: It sets a packet struct in FIFO in order to send it.
cdupaty 0:d974bcee4f69 5292 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5293 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5294 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5295 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5296 */
cdupaty 0:d974bcee4f69 5297 uint8_t SX1272::setPacket(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 5298 {
cdupaty 0:d974bcee4f69 5299 int8_t state = 2;
cdupaty 0:d974bcee4f69 5300
cdupaty 0:d974bcee4f69 5301 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5302 printf("\n");
cdupaty 0:d974bcee4f69 5303 printf("Starting 'setPacket'\n");
cdupaty 0:d974bcee4f69 5304 #endif
cdupaty 0:d974bcee4f69 5305
marcoantonioara 3:82630593359c 5306 // printf("dentro do setPacket payload = %s\n",payload);
cdupaty 0:d974bcee4f69 5307 // added by C. Pham
cdupaty 0:d974bcee4f69 5308 // check for enough remaining ToA
cdupaty 0:d974bcee4f69 5309 // when operating under duty-cycle mode
cdupaty 0:d974bcee4f69 5310 if (_limitToA) {
cdupaty 0:d974bcee4f69 5311 uint16_t length16 = (uint16_t)strlen(payload);
cdupaty 0:d974bcee4f69 5312
cdupaty 0:d974bcee4f69 5313 if (!_rawFormat)
cdupaty 0:d974bcee4f69 5314 length16 = length16 + OFFSET_PAYLOADLENGTH;
cdupaty 0:d974bcee4f69 5315
cdupaty 0:d974bcee4f69 5316 if (getRemainingToA() - getToA(length16) < 0) {
cdupaty 0:d974bcee4f69 5317 printf("## not enough ToA at %d\n",millis());
cdupaty 0:d974bcee4f69 5318 // Serial.println(millis());
cdupaty 0:d974bcee4f69 5319 return SX1272_ERROR_TOA;
cdupaty 0:d974bcee4f69 5320 }
cdupaty 0:d974bcee4f69 5321 }
cdupaty 0:d974bcee4f69 5322
cdupaty 0:d974bcee4f69 5323 clearFlags(); // Initializing flags
marcoantonioara 3:82630593359c 5324 printf("setPacket1____________________\n");
cdupaty 0:d974bcee4f69 5325 if( _modem == LORA )
cdupaty 0:d974bcee4f69 5326 { // LoRa mode
cdupaty 0:d974bcee4f69 5327 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO
marcoantonioara 3:82630593359c 5328 printf("_modem == Lora\n");
cdupaty 0:d974bcee4f69 5329 }
cdupaty 0:d974bcee4f69 5330 else
cdupaty 0:d974bcee4f69 5331 { // FSK mode
cdupaty 0:d974bcee4f69 5332 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Stdby FSK mode to write in FIFO
marcoantonioara 3:82630593359c 5333 printf("_modem == FSK\n");
cdupaty 0:d974bcee4f69 5334 }
cdupaty 0:d974bcee4f69 5335
cdupaty 0:d974bcee4f69 5336 _reception = CORRECT_PACKET; // Updating incorrect value
cdupaty 0:d974bcee4f69 5337 if( _retries == 0 )
cdupaty 0:d974bcee4f69 5338 { // Updating this values only if is not going to re-send the last packet
cdupaty 0:d974bcee4f69 5339 state = setDestination(dest); // Setting destination in packet structure
cdupaty 0:d974bcee4f69 5340 packet_sent.retry = _retries;
cdupaty 0:d974bcee4f69 5341 if( state == 0 )
cdupaty 0:d974bcee4f69 5342 {
cdupaty 0:d974bcee4f69 5343 state = setPayload(payload);
cdupaty 0:d974bcee4f69 5344 }
cdupaty 0:d974bcee4f69 5345 }
cdupaty 0:d974bcee4f69 5346 else
cdupaty 0:d974bcee4f69 5347 {
cdupaty 0:d974bcee4f69 5348 // comment by C. Pham
cdupaty 0:d974bcee4f69 5349 // why to increase the length here?
cdupaty 0:d974bcee4f69 5350 // bug?
cdupaty 0:d974bcee4f69 5351 if( _retries == 1 )
cdupaty 0:d974bcee4f69 5352 {
cdupaty 0:d974bcee4f69 5353 packet_sent.length++;
cdupaty 0:d974bcee4f69 5354 }
cdupaty 0:d974bcee4f69 5355 state = setPacketLength();
cdupaty 0:d974bcee4f69 5356 packet_sent.retry = _retries;
cdupaty 0:d974bcee4f69 5357 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 5358 printf("** Retrying to send last packet %d\n",_retries);
cdupaty 0:d974bcee4f69 5359 // Serial.print(_retries, DEC);
cdupaty 0:d974bcee4f69 5360 printf(" time **");
cdupaty 0:d974bcee4f69 5361 #endif
cdupaty 0:d974bcee4f69 5362 }
cdupaty 0:d974bcee4f69 5363
cdupaty 0:d974bcee4f69 5364 // added by C. Pham
cdupaty 0:d974bcee4f69 5365 // set the type to be a data packet
cdupaty 0:d974bcee4f69 5366 packet_sent.type |= PKT_TYPE_DATA;
cdupaty 0:d974bcee4f69 5367
cdupaty 0:d974bcee4f69 5368 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 5369 // added by C. Pham
cdupaty 0:d974bcee4f69 5370 // indicate that an ACK should be sent by the receiver
cdupaty 0:d974bcee4f69 5371 if (_requestACK)
cdupaty 0:d974bcee4f69 5372 packet_sent.type |= PKT_FLAG_ACK_REQ;
cdupaty 0:d974bcee4f69 5373 #endif
cdupaty 0:d974bcee4f69 5374
cdupaty 0:d974bcee4f69 5375 writeRegister(REG_FIFO_ADDR_PTR, 0x80); // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 5376 if( state == 0 )
cdupaty 0:d974bcee4f69 5377 {
cdupaty 0:d974bcee4f69 5378 state = 1;
cdupaty 0:d974bcee4f69 5379 // Writing packet to send in FIFO
cdupaty 0:d974bcee4f69 5380 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 5381 // added by C. Pham
cdupaty 0:d974bcee4f69 5382 packet_sent.netkey[0]=_my_netkey[0];
cdupaty 0:d974bcee4f69 5383 packet_sent.netkey[1]=_my_netkey[1];
cdupaty 0:d974bcee4f69 5384 //#if (SX1272_debug_mode > 0)
marcoantonioara 3:82630593359c 5385 // printf("## Setting net key ##");
cdupaty 0:d974bcee4f69 5386 //#endif
cdupaty 0:d974bcee4f69 5387 writeRegister(REG_FIFO, packet_sent.netkey[0]);
cdupaty 0:d974bcee4f69 5388 writeRegister(REG_FIFO, packet_sent.netkey[1]);
cdupaty 0:d974bcee4f69 5389 #endif
cdupaty 0:d974bcee4f69 5390 // added by C. Pham
cdupaty 0:d974bcee4f69 5391 // we can skip the header for instance when we want to generate
cdupaty 0:d974bcee4f69 5392 // at a higher layer a LoRaWAN packet
marcoantonioara 3:82630593359c 5393 writeRegister(0x01,129);//standby mode
cdupaty 0:d974bcee4f69 5394 if (!_rawFormat) {
cdupaty 0:d974bcee4f69 5395 writeRegister(REG_FIFO, packet_sent.dst); // Writing the destination in FIFO
marcoantonioara 3:82630593359c 5396 // // added by C. Pham
cdupaty 0:d974bcee4f69 5397 writeRegister(REG_FIFO, packet_sent.type); // Writing the packet type in FIFO
cdupaty 0:d974bcee4f69 5398 writeRegister(REG_FIFO, packet_sent.src); // Writing the source in FIFO
cdupaty 0:d974bcee4f69 5399 writeRegister(REG_FIFO, packet_sent.packnum); // Writing the packet number in FIFO
cdupaty 0:d974bcee4f69 5400 }
cdupaty 0:d974bcee4f69 5401 // commented by C. Pham
cdupaty 0:d974bcee4f69 5402 //writeRegister(REG_FIFO, packet_sent.length); // Writing the packet length in FIFO
cdupaty 0:d974bcee4f69 5403 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5404 {
marcoantonioara 3:82630593359c 5405 writeRegister(REG_FIFO, packet_sent.data[i]); // Writing the payload in FIFO
cdupaty 0:d974bcee4f69 5406 }
cdupaty 0:d974bcee4f69 5407 // commented by C. Pham
cdupaty 0:d974bcee4f69 5408 //writeRegister(REG_FIFO, packet_sent.retry); // Writing the number retry in FIFO
marcoantonioara 3:82630593359c 5409 // for (int i=0 ; i<255 ; i++)
marcoantonioara 3:82630593359c 5410 // printf("%d ",readRegister(REG_FIFO));
marcoantonioara 3:82630593359c 5411 // printf("/n");
cdupaty 0:d974bcee4f69 5412 state = 0;
cdupaty 0:d974bcee4f69 5413 #if (SX1272_debug_mode > 0)
marcoantonioara 3:82630593359c 5414 printf("in FIFO ##");
cdupaty 0:d974bcee4f69 5415 // Print the complete packet if debug_mode
cdupaty 0:d974bcee4f69 5416 printf("## Packet to send: \n");
cdupaty 0:d974bcee4f69 5417 printf("Destination: %d\n",packet_sent.dst);
cdupaty 0:d974bcee4f69 5418 // Serial.println(packet_sent.dst); // Printing destination
cdupaty 0:d974bcee4f69 5419 printf("Packet type: %d\n",packet_sent.type);
cdupaty 0:d974bcee4f69 5420 // Serial.println(packet_sent.type); // Printing packet type
cdupaty 0:d974bcee4f69 5421 printf("Source: %d\n",packet_sent.src);
cdupaty 0:d974bcee4f69 5422 // Serial.println(packet_sent.src); // Printing source
cdupaty 0:d974bcee4f69 5423 printf("Packet number: %d\n",packet_sent.packnum);
cdupaty 0:d974bcee4f69 5424 // Serial.println(packet_sent.packnum); // Printing packet number
cdupaty 0:d974bcee4f69 5425 printf("Packet length: %d\n",packet_sent.length);
cdupaty 0:d974bcee4f69 5426 // Serial.println(packet_sent.length); // Printing packet length
cdupaty 0:d974bcee4f69 5427 printf("Data: ");
cdupaty 0:d974bcee4f69 5428 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5429 {
cdupaty 0:d974bcee4f69 5430 // Serial.print((char)packet_sent.data[i]); // Printing payload
cdupaty 0:d974bcee4f69 5431 printf("%c",packet_sent.data[i]);
cdupaty 0:d974bcee4f69 5432 }
cdupaty 0:d974bcee4f69 5433 printf("\n");
cdupaty 0:d974bcee4f69 5434 //printf("Retry number: ");
cdupaty 0:d974bcee4f69 5435 //Serial.println(packet_sent.retry); // Printing retry number
cdupaty 0:d974bcee4f69 5436 printf("##");
cdupaty 0:d974bcee4f69 5437 #endif
cdupaty 0:d974bcee4f69 5438 }
cdupaty 0:d974bcee4f69 5439
cdupaty 0:d974bcee4f69 5440 return state;
cdupaty 0:d974bcee4f69 5441 }
cdupaty 0:d974bcee4f69 5442
cdupaty 0:d974bcee4f69 5443 /*
cdupaty 0:d974bcee4f69 5444 Function: It sets a packet struct in FIFO in order to sent it.
cdupaty 0:d974bcee4f69 5445 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5446 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5447 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5448 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5449 */
cdupaty 0:d974bcee4f69 5450 uint8_t SX1272::setPacket(uint8_t dest, uint8_t *payload)
cdupaty 0:d974bcee4f69 5451 {
cdupaty 0:d974bcee4f69 5452 int8_t state = 2;
cdupaty 0:d974bcee4f69 5453 byte st0;
marcoantonioara 3:82630593359c 5454
marcoantonioara 3:82630593359c 5455 // sx1272.writeRegister(0x01,129); //standby mode
cdupaty 0:d974bcee4f69 5456 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5457 printf("\n");
cdupaty 0:d974bcee4f69 5458 printf("Starting 'setPacket'\n");
cdupaty 0:d974bcee4f69 5459 #endif
cdupaty 0:d974bcee4f69 5460
cdupaty 0:d974bcee4f69 5461 // added by C. Pham
cdupaty 0:d974bcee4f69 5462 // check for enough remaining ToA
cdupaty 0:d974bcee4f69 5463 // when operating under duty-cycle mode
cdupaty 0:d974bcee4f69 5464 if (_limitToA) {
cdupaty 0:d974bcee4f69 5465 // here truncPayload() should have been called before in
cdupaty 0:d974bcee4f69 5466 // sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 5467 uint16_t length16 = _payloadlength;
cdupaty 0:d974bcee4f69 5468
cdupaty 0:d974bcee4f69 5469 if (!_rawFormat)
cdupaty 0:d974bcee4f69 5470 length16 = length16 + OFFSET_PAYLOADLENGTH;
cdupaty 0:d974bcee4f69 5471
cdupaty 0:d974bcee4f69 5472 if (getRemainingToA() - getToA(length16) < 0) {
cdupaty 0:d974bcee4f69 5473 printf("## not enough ToA at %d\n",millis());
cdupaty 0:d974bcee4f69 5474 // Serial.println(millis());
cdupaty 0:d974bcee4f69 5475 return SX1272_ERROR_TOA;
cdupaty 0:d974bcee4f69 5476 }
cdupaty 0:d974bcee4f69 5477 }
cdupaty 0:d974bcee4f69 5478
cdupaty 0:d974bcee4f69 5479 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 5480 clearFlags(); // Initializing flags
marcoantonioara 3:82630593359c 5481 // printf("setPacket_2_____________________\n");
marcoantonioara 3:82630593359c 5482 // printf("mode =%d\n",readRegister(REG_OP_MODE)&3);
cdupaty 0:d974bcee4f69 5483 if( _modem == LORA )
cdupaty 0:d974bcee4f69 5484 { // LoRa mode
cdupaty 0:d974bcee4f69 5485 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO
marcoantonioara 3:82630593359c 5486 // printf("setpacket_2 LORA\n");
cdupaty 0:d974bcee4f69 5487 }
cdupaty 0:d974bcee4f69 5488 else
cdupaty 0:d974bcee4f69 5489 { // FSK mode
cdupaty 0:d974bcee4f69 5490 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Stdby FSK mode to write in FIFO
marcoantonioara 3:82630593359c 5491 printf("setpacket_2 FSK\n");
marcoantonioara 3:82630593359c 5492 }
marcoantonioara 3:82630593359c 5493 // printf("mode =%d\n",readRegister(REG_OP_MODE)&3);
cdupaty 0:d974bcee4f69 5494 _reception = CORRECT_PACKET; // Updating incorrect value to send a packet (old or new)
cdupaty 0:d974bcee4f69 5495 if( _retries == 0 )
cdupaty 0:d974bcee4f69 5496 { // Sending new packet
cdupaty 0:d974bcee4f69 5497 state = setDestination(dest); // Setting destination in packet structure
cdupaty 0:d974bcee4f69 5498 packet_sent.retry = _retries;
cdupaty 0:d974bcee4f69 5499 if( state == 0 )
cdupaty 0:d974bcee4f69 5500 {
cdupaty 0:d974bcee4f69 5501 state = setPayload(payload);
cdupaty 0:d974bcee4f69 5502 }
cdupaty 0:d974bcee4f69 5503 }
cdupaty 0:d974bcee4f69 5504 else
cdupaty 0:d974bcee4f69 5505 {
cdupaty 0:d974bcee4f69 5506 // comment by C. Pham
cdupaty 0:d974bcee4f69 5507 // why to increase the length here?
cdupaty 0:d974bcee4f69 5508 // bug?
cdupaty 0:d974bcee4f69 5509 if( _retries == 1 )
cdupaty 0:d974bcee4f69 5510 {
cdupaty 0:d974bcee4f69 5511 packet_sent.length++;
cdupaty 0:d974bcee4f69 5512 }
cdupaty 0:d974bcee4f69 5513 state = setPacketLength();
cdupaty 0:d974bcee4f69 5514 packet_sent.retry = _retries;
cdupaty 0:d974bcee4f69 5515 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 5516 printf("** Retrying to send last packet %d\n",_retries);
cdupaty 0:d974bcee4f69 5517 // Serial.print(_retries, DEC);
cdupaty 0:d974bcee4f69 5518 printf(" time **");
cdupaty 0:d974bcee4f69 5519 #endif
cdupaty 0:d974bcee4f69 5520 }
cdupaty 0:d974bcee4f69 5521
cdupaty 0:d974bcee4f69 5522 // added by C. Pham
cdupaty 0:d974bcee4f69 5523 // set the type to be a data packet
cdupaty 0:d974bcee4f69 5524 packet_sent.type |= PKT_TYPE_DATA;
cdupaty 0:d974bcee4f69 5525
cdupaty 0:d974bcee4f69 5526 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 5527 // added by C. Pham
cdupaty 0:d974bcee4f69 5528 // indicate that an ACK should be sent by the receiver
cdupaty 0:d974bcee4f69 5529 if (_requestACK)
cdupaty 0:d974bcee4f69 5530 packet_sent.type |= PKT_FLAG_ACK_REQ;
cdupaty 0:d974bcee4f69 5531 #endif
cdupaty 0:d974bcee4f69 5532 writeRegister(REG_FIFO_ADDR_PTR, 0x80); // Setting address pointer in FIFO data buffer
cdupaty 0:d974bcee4f69 5533 if( state == 0 )
cdupaty 0:d974bcee4f69 5534 {
cdupaty 0:d974bcee4f69 5535 state = 1;
cdupaty 0:d974bcee4f69 5536 // Writing packet to send in FIFO
cdupaty 0:d974bcee4f69 5537 #ifdef W_NET_KEY
cdupaty 0:d974bcee4f69 5538 // added by C. Pham
marcoantonioara 3:82630593359c 5539
cdupaty 0:d974bcee4f69 5540 packet_sent.netkey[0]=_my_netkey[0];
cdupaty 0:d974bcee4f69 5541 packet_sent.netkey[1]=_my_netkey[1];
cdupaty 0:d974bcee4f69 5542 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 5543 printf("## Setting net key ##");
cdupaty 0:d974bcee4f69 5544 //#endif
cdupaty 0:d974bcee4f69 5545 writeRegister(REG_FIFO, packet_sent.netkey[0]);
cdupaty 0:d974bcee4f69 5546 writeRegister(REG_FIFO, packet_sent.netkey[1]);
cdupaty 0:d974bcee4f69 5547 #endif
cdupaty 0:d974bcee4f69 5548 // added by C. Pham
cdupaty 0:d974bcee4f69 5549 // we can skip the header for instance when we want to generate
cdupaty 0:d974bcee4f69 5550 // at a higher layer a LoRaWAN packet
marcoantonioara 3:82630593359c 5551
cdupaty 0:d974bcee4f69 5552 if (!_rawFormat) {
cdupaty 0:d974bcee4f69 5553 writeRegister(REG_FIFO, packet_sent.dst); // Writing the destination in FIFO
cdupaty 0:d974bcee4f69 5554 // added by C. Pham
cdupaty 0:d974bcee4f69 5555 writeRegister(REG_FIFO, packet_sent.type); // Writing the packet type in FIFO
cdupaty 0:d974bcee4f69 5556 writeRegister(REG_FIFO, packet_sent.src); // Writing the source in FIFO
cdupaty 0:d974bcee4f69 5557 writeRegister(REG_FIFO, packet_sent.packnum); // Writing the packet number in FIFO
cdupaty 0:d974bcee4f69 5558 }
marcoantonioara 3:82630593359c 5559 this->packet_sent.packnum++;
cdupaty 0:d974bcee4f69 5560 // commented by C. Pham
cdupaty 0:d974bcee4f69 5561 //writeRegister(REG_FIFO, packet_sent.length); // Writing the packet length in FIFO
cdupaty 0:d974bcee4f69 5562 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5563 {
cdupaty 0:d974bcee4f69 5564 writeRegister(REG_FIFO, packet_sent.data[i]); // Writing the payload in FIFO
cdupaty 0:d974bcee4f69 5565 }
marcoantonioara 3:82630593359c 5566 // printf("FIFO depois da escrita\n");
marcoantonioara 3:82630593359c 5567 // for(int i=0; i!= 255 ;i++)
marcoantonioara 3:82630593359c 5568 // printf("%c",readRegister(REG_FIFO));
marcoantonioara 3:82630593359c 5569 // printf("\n");
cdupaty 0:d974bcee4f69 5570 // commented by C. Pham
cdupaty 0:d974bcee4f69 5571 //writeRegister(REG_FIFO, packet_sent.retry); // Writing the number retry in FIFO
cdupaty 0:d974bcee4f69 5572 state = 0;
cdupaty 0:d974bcee4f69 5573 #if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 5574 printf("## Packet set and written in FIFO ##");
cdupaty 0:d974bcee4f69 5575 // Print the complete packet if debug_mode
cdupaty 0:d974bcee4f69 5576 printf("## Packet to send: ");
cdupaty 0:d974bcee4f69 5577 printf("Destination: %d\n",packet_sent.dst);
cdupaty 0:d974bcee4f69 5578 // Serial.println(packet_sent.dst); // Printing destination
cdupaty 0:d974bcee4f69 5579 printf("Packet type: %d\n",packet_sent.type);
cdupaty 0:d974bcee4f69 5580 // Serial.println(packet_sent.type); // Printing packet type
cdupaty 0:d974bcee4f69 5581 printf("Source: %d\n",packet_sent.src);
cdupaty 0:d974bcee4f69 5582 // Serial.println(packet_sent.src); // Printing source
cdupaty 0:d974bcee4f69 5583 printf("Packet number: %d\n",packet_sent.packnum);
cdupaty 0:d974bcee4f69 5584 // Serial.println(packet_sent.packnum); // Printing packet number
cdupaty 0:d974bcee4f69 5585 printf("Packet length: %d\n",packet_sent.length);
cdupaty 0:d974bcee4f69 5586 // Serial.println(packet_sent.length); // Printing packet length
cdupaty 0:d974bcee4f69 5587 printf("Data: ");
cdupaty 0:d974bcee4f69 5588 for(unsigned int i = 0; i < _payloadlength; i++)
cdupaty 0:d974bcee4f69 5589 {
cdupaty 0:d974bcee4f69 5590 //Serial.print((char)packet_sent.data[i]); // Printing payload
cdupaty 0:d974bcee4f69 5591 printf("%c",packet_sent.data[i]);
cdupaty 0:d974bcee4f69 5592 }
cdupaty 0:d974bcee4f69 5593 printf("\n");
cdupaty 0:d974bcee4f69 5594 //printf("Retry number: ");
cdupaty 0:d974bcee4f69 5595 //Serial.println(packet_sent.retry); // Printing retry number
cdupaty 0:d974bcee4f69 5596 printf("##");
cdupaty 0:d974bcee4f69 5597 #endif
cdupaty 0:d974bcee4f69 5598 }
marcoantonioara 3:82630593359c 5599 //writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 5600 return state;
cdupaty 0:d974bcee4f69 5601 }
cdupaty 0:d974bcee4f69 5602
cdupaty 0:d974bcee4f69 5603 /*
cdupaty 0:d974bcee4f69 5604 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5605 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5606 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5607 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5608 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5609 */
cdupaty 0:d974bcee4f69 5610 uint8_t SX1272::sendWithMAXTimeout()
cdupaty 0:d974bcee4f69 5611 {
cdupaty 0:d974bcee4f69 5612 return sendWithTimeout(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 5613 }
cdupaty 0:d974bcee4f69 5614
cdupaty 0:d974bcee4f69 5615 /*
cdupaty 0:d974bcee4f69 5616 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5617 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5618 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5619 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5620 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5621 */
cdupaty 0:d974bcee4f69 5622 uint8_t SX1272::sendWithTimeout()
cdupaty 0:d974bcee4f69 5623 {
cdupaty 0:d974bcee4f69 5624 setTimeout();
cdupaty 0:d974bcee4f69 5625 return sendWithTimeout(_sendTime);
cdupaty 0:d974bcee4f69 5626 }
cdupaty 0:d974bcee4f69 5627
cdupaty 0:d974bcee4f69 5628 /*
cdupaty 0:d974bcee4f69 5629 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5630 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5631 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5632 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5633 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5634 */
cdupaty 0:d974bcee4f69 5635 uint8_t SX1272::sendWithTimeout(uint16_t wait)
cdupaty 0:d974bcee4f69 5636 {
cdupaty 0:d974bcee4f69 5637 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5638 byte value = 0x00;
cdupaty 0:d974bcee4f69 5639 //unsigned long previous;
cdupaty 0:d974bcee4f69 5640 unsigned long exitTime;
cdupaty 0:d974bcee4f69 5641
cdupaty 0:d974bcee4f69 5642 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5643 printf("\n");
cdupaty 0:d974bcee4f69 5644 printf("Starting 'sendWithTimeout'\n");
cdupaty 0:d974bcee4f69 5645 #endif
cdupaty 0:d974bcee4f69 5646
cdupaty 0:d974bcee4f69 5647 // clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 5648
cdupaty 0:d974bcee4f69 5649 // wait to TxDone flag
cdupaty 0:d974bcee4f69 5650 //previous = millis();
cdupaty 0:d974bcee4f69 5651 exitTime = millis() + (unsigned long)wait;
cdupaty 0:d974bcee4f69 5652 if( _modem == LORA )
cdupaty 0:d974bcee4f69 5653 { // LoRa mode
cdupaty 0:d974bcee4f69 5654 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 5655
cdupaty 0:d974bcee4f69 5656 writeRegister(REG_OP_MODE, LORA_TX_MODE); // LORA mode - Tx
cdupaty 0:d974bcee4f69 5657
cdupaty 0:d974bcee4f69 5658 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5659 value = readRegister(REG_OP_MODE);
cdupaty 0:d974bcee4f69 5660
cdupaty 0:d974bcee4f69 5661 if (value & LORA_TX_MODE == LORA_TX_MODE)
cdupaty 0:d974bcee4f69 5662 printf("OK");
cdupaty 0:d974bcee4f69 5663 else
cdupaty 0:d974bcee4f69 5664 printf("ERROR");
cdupaty 0:d974bcee4f69 5665 #endif
cdupaty 0:d974bcee4f69 5666 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 5667 // Wait until the packet is sent (TX Done flag) or the timeout expires
cdupaty 0:d974bcee4f69 5668 //while ((bitRead(value, 3) == 0) && (millis() - previous < wait))
cdupaty 0:d974bcee4f69 5669 while ((bitRead(value, 3) == 0) && (millis() < exitTime))
cdupaty 0:d974bcee4f69 5670 {
cdupaty 0:d974bcee4f69 5671 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 5672 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 5673 //if( millis() < previous )
cdupaty 0:d974bcee4f69 5674 //{
cdupaty 0:d974bcee4f69 5675 // previous = millis();
cdupaty 0:d974bcee4f69 5676 //}
cdupaty 0:d974bcee4f69 5677 }
cdupaty 0:d974bcee4f69 5678 state = 1;
cdupaty 0:d974bcee4f69 5679 }
cdupaty 0:d974bcee4f69 5680 else
cdupaty 0:d974bcee4f69 5681 { // FSK mode
cdupaty 0:d974bcee4f69 5682 writeRegister(REG_OP_MODE, FSK_TX_MODE); // FSK mode - Tx
cdupaty 0:d974bcee4f69 5683
cdupaty 0:d974bcee4f69 5684 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 5685 // Wait until the packet is sent (Packet Sent flag) or the timeout expires
cdupaty 0:d974bcee4f69 5686 //while ((bitRead(value, 3) == 0) && (millis() - previous < wait))
cdupaty 0:d974bcee4f69 5687 while ((bitRead(value, 3) == 0) && (millis() < exitTime))
cdupaty 0:d974bcee4f69 5688 {
cdupaty 0:d974bcee4f69 5689 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 5690 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 5691 //if( millis() < previous )
cdupaty 0:d974bcee4f69 5692 //{
cdupaty 0:d974bcee4f69 5693 // previous = millis();
cdupaty 0:d974bcee4f69 5694 //}
cdupaty 0:d974bcee4f69 5695 }
cdupaty 0:d974bcee4f69 5696 state = 1;
cdupaty 0:d974bcee4f69 5697 }
cdupaty 0:d974bcee4f69 5698 if( bitRead(value, 3) == 1 )
cdupaty 0:d974bcee4f69 5699 {
cdupaty 0:d974bcee4f69 5700 state = 0; // Packet successfully sent
cdupaty 0:d974bcee4f69 5701 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5702 printf("## Packet successfully sent ##");
cdupaty 0:d974bcee4f69 5703 printf("\n");
cdupaty 0:d974bcee4f69 5704 #endif
cdupaty 0:d974bcee4f69 5705 // added by C. Pham
cdupaty 0:d974bcee4f69 5706 // normally there should be enough remaing ToA as the test has been done earlier
cdupaty 0:d974bcee4f69 5707 if (_limitToA)
cdupaty 0:d974bcee4f69 5708 removeToA(_currentToA);
cdupaty 0:d974bcee4f69 5709 }
cdupaty 0:d974bcee4f69 5710 else
cdupaty 0:d974bcee4f69 5711 {
cdupaty 0:d974bcee4f69 5712 if( state == 1 )
cdupaty 0:d974bcee4f69 5713 {
cdupaty 0:d974bcee4f69 5714 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5715 printf("** Timeout has expired **");
cdupaty 0:d974bcee4f69 5716 printf("\n");
cdupaty 0:d974bcee4f69 5717 #endif
cdupaty 0:d974bcee4f69 5718 }
cdupaty 0:d974bcee4f69 5719 else
cdupaty 0:d974bcee4f69 5720 {
cdupaty 0:d974bcee4f69 5721 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5722 printf("** There has been an error and packet has not been sent **");
cdupaty 0:d974bcee4f69 5723 printf("\n");
cdupaty 0:d974bcee4f69 5724 #endif
cdupaty 0:d974bcee4f69 5725 }
cdupaty 0:d974bcee4f69 5726 }
cdupaty 0:d974bcee4f69 5727
cdupaty 0:d974bcee4f69 5728 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 5729 return state;
cdupaty 0:d974bcee4f69 5730 }
cdupaty 0:d974bcee4f69 5731
cdupaty 0:d974bcee4f69 5732 /*
cdupaty 0:d974bcee4f69 5733 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5734 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5735 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5736 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5737 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5738 */
cdupaty 0:d974bcee4f69 5739 uint8_t SX1272::sendPacketMAXTimeout(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 5740 {
cdupaty 0:d974bcee4f69 5741 return sendPacketTimeout(dest, payload, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 5742 }
cdupaty 0:d974bcee4f69 5743
cdupaty 0:d974bcee4f69 5744 /*
cdupaty 0:d974bcee4f69 5745 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5746 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5747 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5748 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5749 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5750 */
cdupaty 0:d974bcee4f69 5751 uint8_t SX1272::sendPacketMAXTimeout(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 5752 {
cdupaty 0:d974bcee4f69 5753 return sendPacketTimeout(dest, payload, length16, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 5754 }
cdupaty 0:d974bcee4f69 5755
cdupaty 0:d974bcee4f69 5756 /*
cdupaty 0:d974bcee4f69 5757 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5758 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5759 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5760 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5761 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5762 */
cdupaty 0:d974bcee4f69 5763 uint8_t SX1272::sendPacketTimeout(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 5764 {
cdupaty 0:d974bcee4f69 5765 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5766
cdupaty 0:d974bcee4f69 5767 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5768 printf("\n");
marcoantonioara 4:f77a79f4239a 5769 printf("Starting sendPacketTimeout\n");
cdupaty 0:d974bcee4f69 5770 #endif
cdupaty 0:d974bcee4f69 5771
cdupaty 0:d974bcee4f69 5772 state = setPacket(dest, payload); // Setting a packet with 'dest' destination
marcoantonioara 4:f77a79f4239a 5773 printf("sendPacketTimeout saio da fifo\n");
cdupaty 0:d974bcee4f69 5774 if (state == 0) // and writing it in FIFO.
cdupaty 0:d974bcee4f69 5775 {
marcoantonioara 4:f77a79f4239a 5776 printf("sendPacketTimeout vai enviar\n");
cdupaty 0:d974bcee4f69 5777 state = sendWithTimeout(); // Sending the packet
marcoantonioara 4:f77a79f4239a 5778 printf("sendPacketTimeout enviou\n");
cdupaty 0:d974bcee4f69 5779 }
cdupaty 0:d974bcee4f69 5780 return state;
cdupaty 0:d974bcee4f69 5781 }
cdupaty 0:d974bcee4f69 5782
cdupaty 0:d974bcee4f69 5783 /*
cdupaty 0:d974bcee4f69 5784 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5785 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5786 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5787 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5788 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5789 */
cdupaty 0:d974bcee4f69 5790 uint8_t SX1272::sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 5791 {
cdupaty 0:d974bcee4f69 5792 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5793 uint8_t state_f = 2;
marcoantonioara 3:82630593359c 5794 // printf("chegou no sendPacketTimeout\n");
cdupaty 0:d974bcee4f69 5795 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5796 printf("\n");
cdupaty 0:d974bcee4f69 5797 printf("Starting 'sendPacketTimeout'\n");
cdupaty 0:d974bcee4f69 5798 #endif
cdupaty 0:d974bcee4f69 5799 state = truncPayload(length16);
cdupaty 0:d974bcee4f69 5800 if( state == 0 )
cdupaty 0:d974bcee4f69 5801 {
marcoantonioara 3:82630593359c 5802 // printf("vai pro setpacket\n");
cdupaty 0:d974bcee4f69 5803 state_f = setPacket(dest, payload); // Setting a packet with 'dest' destination
cdupaty 0:d974bcee4f69 5804 } // and writing it in FIFO.
cdupaty 0:d974bcee4f69 5805 else
cdupaty 0:d974bcee4f69 5806 {
cdupaty 0:d974bcee4f69 5807 state_f = state;
cdupaty 0:d974bcee4f69 5808 }
cdupaty 0:d974bcee4f69 5809 if( state_f == 0 )
cdupaty 0:d974bcee4f69 5810 {
cdupaty 0:d974bcee4f69 5811 state_f = sendWithTimeout(); // Sending the packet
cdupaty 0:d974bcee4f69 5812 }
cdupaty 0:d974bcee4f69 5813 return state_f;
cdupaty 0:d974bcee4f69 5814 }
cdupaty 0:d974bcee4f69 5815
cdupaty 0:d974bcee4f69 5816 /*
cdupaty 0:d974bcee4f69 5817 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5818 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5819 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5820 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5821 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5822 */
cdupaty 0:d974bcee4f69 5823 uint8_t SX1272::sendPacketTimeout(uint8_t dest, char *payload, uint16_t wait)
cdupaty 0:d974bcee4f69 5824 {
cdupaty 0:d974bcee4f69 5825 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5826
cdupaty 0:d974bcee4f69 5827 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5828 printf("\n");
cdupaty 0:d974bcee4f69 5829 printf("Starting 'sendPacketTimeout'\n");
cdupaty 0:d974bcee4f69 5830 #endif
cdupaty 0:d974bcee4f69 5831
cdupaty 0:d974bcee4f69 5832 state = setPacket(dest, payload); // Setting a packet with 'dest' destination
cdupaty 0:d974bcee4f69 5833 if (state == 0) // and writing it in FIFO.
cdupaty 0:d974bcee4f69 5834 {
cdupaty 0:d974bcee4f69 5835 state = sendWithTimeout(wait); // Sending the packet
cdupaty 0:d974bcee4f69 5836 }
cdupaty 0:d974bcee4f69 5837 return state;
cdupaty 0:d974bcee4f69 5838 }
cdupaty 0:d974bcee4f69 5839
cdupaty 0:d974bcee4f69 5840 /*
cdupaty 0:d974bcee4f69 5841 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5842 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5843 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5844 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5845 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5846 */
cdupaty 0:d974bcee4f69 5847 uint8_t SX1272::sendPacketTimeout(uint8_t dest, uint8_t *payload, uint16_t length16, uint16_t wait)
cdupaty 0:d974bcee4f69 5848 {
cdupaty 0:d974bcee4f69 5849 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5850 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 5851
cdupaty 0:d974bcee4f69 5852 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5853 printf("\n");
cdupaty 0:d974bcee4f69 5854 printf("Starting 'sendPacketTimeout'\n");
cdupaty 0:d974bcee4f69 5855 #endif
cdupaty 0:d974bcee4f69 5856
cdupaty 0:d974bcee4f69 5857 state = truncPayload(length16);
cdupaty 0:d974bcee4f69 5858 if( state == 0 )
cdupaty 0:d974bcee4f69 5859 {
cdupaty 0:d974bcee4f69 5860 state_f = setPacket(dest, payload); // Setting a packet with 'dest' destination
cdupaty 0:d974bcee4f69 5861 }
cdupaty 0:d974bcee4f69 5862 else
cdupaty 0:d974bcee4f69 5863 {
cdupaty 0:d974bcee4f69 5864 state_f = state;
cdupaty 0:d974bcee4f69 5865 }
cdupaty 0:d974bcee4f69 5866 if( state_f == 0 ) // and writing it in FIFO.
cdupaty 0:d974bcee4f69 5867 {
cdupaty 0:d974bcee4f69 5868 state_f = sendWithTimeout(wait); // Sending the packet
cdupaty 0:d974bcee4f69 5869 }
cdupaty 0:d974bcee4f69 5870 return state_f;
cdupaty 0:d974bcee4f69 5871 }
cdupaty 0:d974bcee4f69 5872
cdupaty 0:d974bcee4f69 5873 /*
cdupaty 0:d974bcee4f69 5874 Function: Configures the module to transmit information.
cdupaty 0:d974bcee4f69 5875 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5876 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5877 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5878 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5879 */
cdupaty 0:d974bcee4f69 5880 uint8_t SX1272::sendPacketMAXTimeoutACK(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 5881 {
cdupaty 0:d974bcee4f69 5882 return sendPacketTimeoutACK(dest, payload, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 5883 }
cdupaty 0:d974bcee4f69 5884
cdupaty 0:d974bcee4f69 5885 /*
cdupaty 0:d974bcee4f69 5886 Function: Configures the module to transmit information and receive an ACK.
cdupaty 0:d974bcee4f69 5887 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5888 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5889 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5890 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5891 */
cdupaty 0:d974bcee4f69 5892 uint8_t SX1272::sendPacketMAXTimeoutACK(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 5893 {
cdupaty 0:d974bcee4f69 5894 return sendPacketTimeoutACK(dest, payload, length16, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 5895 }
cdupaty 0:d974bcee4f69 5896
cdupaty 0:d974bcee4f69 5897 /*
cdupaty 0:d974bcee4f69 5898 Function: Configures the module to transmit information and receive an ACK.
cdupaty 0:d974bcee4f69 5899 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5900 state = 3 --> Packet has been sent but ACK has not been received
cdupaty 0:d974bcee4f69 5901 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5902 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5903 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5904 */
cdupaty 0:d974bcee4f69 5905 uint8_t SX1272::sendPacketTimeoutACK(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 5906 {
cdupaty 0:d974bcee4f69 5907 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5908 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 5909
cdupaty 0:d974bcee4f69 5910 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5911 printf("\n");
cdupaty 0:d974bcee4f69 5912 printf("Starting 'sendPacketTimeoutACK'\n");
cdupaty 0:d974bcee4f69 5913 #endif
cdupaty 0:d974bcee4f69 5914
cdupaty 0:d974bcee4f69 5915 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 5916 _requestACK = 1;
cdupaty 0:d974bcee4f69 5917 #endif
cdupaty 0:d974bcee4f69 5918 state = sendPacketTimeout(dest, payload); // Sending packet to 'dest' destination
cdupaty 0:d974bcee4f69 5919
cdupaty 0:d974bcee4f69 5920 if( state == 0 )
cdupaty 0:d974bcee4f69 5921 {
cdupaty 0:d974bcee4f69 5922 state = receive(); // Setting Rx mode to wait an ACK
cdupaty 0:d974bcee4f69 5923 }
cdupaty 0:d974bcee4f69 5924 else
cdupaty 0:d974bcee4f69 5925 {
cdupaty 0:d974bcee4f69 5926 state_f = state;
cdupaty 0:d974bcee4f69 5927 }
cdupaty 0:d974bcee4f69 5928 if( state == 0 )
cdupaty 0:d974bcee4f69 5929 {
cdupaty 0:d974bcee4f69 5930 // added by C. Pham
cdupaty 0:d974bcee4f69 5931 printf("wait for ACK");
cdupaty 0:d974bcee4f69 5932
cdupaty 0:d974bcee4f69 5933 if( availableData() )
cdupaty 0:d974bcee4f69 5934 {
cdupaty 0:d974bcee4f69 5935 state_f = getACK(); // Getting ACK
cdupaty 0:d974bcee4f69 5936 }
cdupaty 0:d974bcee4f69 5937 else
cdupaty 0:d974bcee4f69 5938 {
cdupaty 0:d974bcee4f69 5939 state_f = SX1272_ERROR_ACK;
cdupaty 0:d974bcee4f69 5940 // added by C. Pham
cdupaty 0:d974bcee4f69 5941 printf("no ACK");
cdupaty 0:d974bcee4f69 5942 }
cdupaty 0:d974bcee4f69 5943 }
cdupaty 0:d974bcee4f69 5944 else
cdupaty 0:d974bcee4f69 5945 {
cdupaty 0:d974bcee4f69 5946 state_f = state;
cdupaty 0:d974bcee4f69 5947 }
cdupaty 0:d974bcee4f69 5948
cdupaty 0:d974bcee4f69 5949 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 5950 _requestACK = 0;
cdupaty 0:d974bcee4f69 5951 #endif
cdupaty 0:d974bcee4f69 5952 return state_f;
cdupaty 0:d974bcee4f69 5953 }
cdupaty 0:d974bcee4f69 5954
cdupaty 0:d974bcee4f69 5955 /*
cdupaty 0:d974bcee4f69 5956 Function: Configures the module to transmit information and receive an ACK.
cdupaty 0:d974bcee4f69 5957 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 5958 state = 3 --> Packet has been sent but ACK has not been received
cdupaty 0:d974bcee4f69 5959 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 5960 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 5961 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 5962 */
cdupaty 0:d974bcee4f69 5963 uint8_t SX1272::sendPacketTimeoutACK(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 5964 {
cdupaty 0:d974bcee4f69 5965 uint8_t state = 2;
cdupaty 0:d974bcee4f69 5966 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 5967
cdupaty 0:d974bcee4f69 5968 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 5969 printf("\n");
cdupaty 0:d974bcee4f69 5970 printf("Starting 'sendPacketTimeoutACK'\n");
cdupaty 0:d974bcee4f69 5971 #endif
cdupaty 0:d974bcee4f69 5972
cdupaty 0:d974bcee4f69 5973 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 5974 _requestACK = 1;
cdupaty 0:d974bcee4f69 5975 #endif
cdupaty 0:d974bcee4f69 5976 // Sending packet to 'dest' destination
cdupaty 0:d974bcee4f69 5977 state = sendPacketTimeout(dest, payload, length16);
cdupaty 0:d974bcee4f69 5978
cdupaty 0:d974bcee4f69 5979 // Trying to receive the ACK
cdupaty 0:d974bcee4f69 5980 if( state == 0 )
cdupaty 0:d974bcee4f69 5981 {
cdupaty 0:d974bcee4f69 5982 state = receive(); // Setting Rx mode to wait an ACK
cdupaty 0:d974bcee4f69 5983 }
cdupaty 0:d974bcee4f69 5984 else
cdupaty 0:d974bcee4f69 5985 {
cdupaty 0:d974bcee4f69 5986 state_f = state;
cdupaty 0:d974bcee4f69 5987 }
cdupaty 0:d974bcee4f69 5988 if( state == 0 )
cdupaty 0:d974bcee4f69 5989 {
cdupaty 0:d974bcee4f69 5990 // added by C. Pham
cdupaty 0:d974bcee4f69 5991 printf("wait for ACK");
cdupaty 0:d974bcee4f69 5992
cdupaty 0:d974bcee4f69 5993 if( availableData() )
cdupaty 0:d974bcee4f69 5994 {
cdupaty 0:d974bcee4f69 5995 state_f = getACK(); // Getting ACK
cdupaty 0:d974bcee4f69 5996 }
cdupaty 0:d974bcee4f69 5997 else
cdupaty 0:d974bcee4f69 5998 {
cdupaty 0:d974bcee4f69 5999 state_f = SX1272_ERROR_ACK;
cdupaty 0:d974bcee4f69 6000 // added by C. Pham
cdupaty 0:d974bcee4f69 6001 printf("no ACK");
cdupaty 0:d974bcee4f69 6002 }
cdupaty 0:d974bcee4f69 6003 }
cdupaty 0:d974bcee4f69 6004 else
cdupaty 0:d974bcee4f69 6005 {
cdupaty 0:d974bcee4f69 6006 state_f = state;
cdupaty 0:d974bcee4f69 6007 }
cdupaty 0:d974bcee4f69 6008
cdupaty 0:d974bcee4f69 6009 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 6010 _requestACK = 0;
cdupaty 0:d974bcee4f69 6011 #endif
cdupaty 0:d974bcee4f69 6012 return state_f;
cdupaty 0:d974bcee4f69 6013 }
cdupaty 0:d974bcee4f69 6014
cdupaty 0:d974bcee4f69 6015 /*
cdupaty 0:d974bcee4f69 6016 Function: Configures the module to transmit information and receive an ACK.
cdupaty 0:d974bcee4f69 6017 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6018 state = 3 --> Packet has been sent but ACK has not been received
cdupaty 0:d974bcee4f69 6019 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6020 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6021 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6022 */
cdupaty 0:d974bcee4f69 6023 uint8_t SX1272::sendPacketTimeoutACK(uint8_t dest, char *payload, uint16_t wait)
cdupaty 0:d974bcee4f69 6024 {
cdupaty 0:d974bcee4f69 6025 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6026 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 6027
cdupaty 0:d974bcee4f69 6028 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6029 printf("\n");
cdupaty 0:d974bcee4f69 6030 printf("Starting 'sendPacketTimeoutACK'\n");
cdupaty 0:d974bcee4f69 6031 #endif
cdupaty 0:d974bcee4f69 6032
cdupaty 0:d974bcee4f69 6033 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 6034 _requestACK = 1;
cdupaty 0:d974bcee4f69 6035 #endif
cdupaty 0:d974bcee4f69 6036 state = sendPacketTimeout(dest, payload, wait); // Sending packet to 'dest' destination
cdupaty 0:d974bcee4f69 6037
cdupaty 0:d974bcee4f69 6038 if( state == 0 )
cdupaty 0:d974bcee4f69 6039 {
cdupaty 0:d974bcee4f69 6040 state = receive(); // Setting Rx mode to wait an ACK
cdupaty 0:d974bcee4f69 6041 }
cdupaty 0:d974bcee4f69 6042 else
cdupaty 0:d974bcee4f69 6043 {
cdupaty 0:d974bcee4f69 6044 state_f = 1;
cdupaty 0:d974bcee4f69 6045 }
cdupaty 0:d974bcee4f69 6046 if( state == 0 )
cdupaty 0:d974bcee4f69 6047 {
cdupaty 0:d974bcee4f69 6048 // added by C. Pham
cdupaty 0:d974bcee4f69 6049 printf("wait for ACK");
cdupaty 0:d974bcee4f69 6050
cdupaty 0:d974bcee4f69 6051 if( availableData() )
cdupaty 0:d974bcee4f69 6052 {
cdupaty 0:d974bcee4f69 6053 state_f = getACK(); // Getting ACK
cdupaty 0:d974bcee4f69 6054 }
cdupaty 0:d974bcee4f69 6055 else
cdupaty 0:d974bcee4f69 6056 {
cdupaty 0:d974bcee4f69 6057 state_f = SX1272_ERROR_ACK;
cdupaty 0:d974bcee4f69 6058 // added by C. Pham
cdupaty 0:d974bcee4f69 6059 printf("no ACK");
cdupaty 0:d974bcee4f69 6060 }
cdupaty 0:d974bcee4f69 6061 }
cdupaty 0:d974bcee4f69 6062 else
cdupaty 0:d974bcee4f69 6063 {
cdupaty 0:d974bcee4f69 6064 state_f = 1;
cdupaty 0:d974bcee4f69 6065 }
cdupaty 0:d974bcee4f69 6066
cdupaty 0:d974bcee4f69 6067 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 6068 _requestACK = 0;
cdupaty 0:d974bcee4f69 6069 #endif
cdupaty 0:d974bcee4f69 6070 return state_f;
cdupaty 0:d974bcee4f69 6071 }
cdupaty 0:d974bcee4f69 6072
cdupaty 0:d974bcee4f69 6073 /*
cdupaty 0:d974bcee4f69 6074 Function: Configures the module to transmit information and receive an ACK.
cdupaty 0:d974bcee4f69 6075 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6076 state = 3 --> Packet has been sent but ACK has not been received
cdupaty 0:d974bcee4f69 6077 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6078 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6079 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6080 */
cdupaty 0:d974bcee4f69 6081 uint8_t SX1272::sendPacketTimeoutACK(uint8_t dest, uint8_t *payload, uint16_t length16, uint16_t wait)
cdupaty 0:d974bcee4f69 6082 {
cdupaty 0:d974bcee4f69 6083 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6084 uint8_t state_f = 2;
cdupaty 0:d974bcee4f69 6085
cdupaty 0:d974bcee4f69 6086 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6087 printf("\n");
cdupaty 0:d974bcee4f69 6088 printf("Starting 'sendPacketTimeoutACK'\n");
cdupaty 0:d974bcee4f69 6089 #endif
cdupaty 0:d974bcee4f69 6090
cdupaty 0:d974bcee4f69 6091 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 6092 _requestACK = 1;
cdupaty 0:d974bcee4f69 6093 #endif
cdupaty 0:d974bcee4f69 6094 state = sendPacketTimeout(dest, payload, length16, wait); // Sending packet to 'dest' destination
cdupaty 0:d974bcee4f69 6095
cdupaty 0:d974bcee4f69 6096 if( state == 0 )
cdupaty 0:d974bcee4f69 6097 {
cdupaty 0:d974bcee4f69 6098 state = receive(); // Setting Rx mode to wait an ACK
cdupaty 0:d974bcee4f69 6099 }
cdupaty 0:d974bcee4f69 6100 else
cdupaty 0:d974bcee4f69 6101 {
cdupaty 0:d974bcee4f69 6102 state_f = 1;
cdupaty 0:d974bcee4f69 6103 }
cdupaty 0:d974bcee4f69 6104 if( state == 0 )
cdupaty 0:d974bcee4f69 6105 {
cdupaty 0:d974bcee4f69 6106 // added by C. Pham
cdupaty 0:d974bcee4f69 6107 printf("wait for ACK");
cdupaty 0:d974bcee4f69 6108
cdupaty 0:d974bcee4f69 6109 if( availableData() )
cdupaty 0:d974bcee4f69 6110 {
cdupaty 0:d974bcee4f69 6111 state_f = getACK(); // Getting ACK
cdupaty 0:d974bcee4f69 6112 }
cdupaty 0:d974bcee4f69 6113 else
cdupaty 0:d974bcee4f69 6114 {
cdupaty 0:d974bcee4f69 6115 state_f = SX1272_ERROR_ACK;
cdupaty 0:d974bcee4f69 6116 // added by C. Pham
cdupaty 0:d974bcee4f69 6117 printf("no ACK");
cdupaty 0:d974bcee4f69 6118 }
cdupaty 0:d974bcee4f69 6119 }
cdupaty 0:d974bcee4f69 6120 else
cdupaty 0:d974bcee4f69 6121 {
cdupaty 0:d974bcee4f69 6122 state_f = 1;
cdupaty 0:d974bcee4f69 6123 }
cdupaty 0:d974bcee4f69 6124
cdupaty 0:d974bcee4f69 6125 #ifdef W_REQUESTED_ACK
cdupaty 0:d974bcee4f69 6126 _requestACK = 0;
cdupaty 0:d974bcee4f69 6127 #endif
cdupaty 0:d974bcee4f69 6128 return state_f;
cdupaty 0:d974bcee4f69 6129 }
cdupaty 0:d974bcee4f69 6130
cdupaty 0:d974bcee4f69 6131 /*
cdupaty 0:d974bcee4f69 6132 Function: It gets and stores an ACK if it is received.
cdupaty 0:d974bcee4f69 6133 Returns:
cdupaty 0:d974bcee4f69 6134 */
cdupaty 0:d974bcee4f69 6135 uint8_t SX1272::getACK()
cdupaty 0:d974bcee4f69 6136 {
cdupaty 0:d974bcee4f69 6137 return getACK(MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 6138 }
cdupaty 0:d974bcee4f69 6139
cdupaty 0:d974bcee4f69 6140 /*
cdupaty 0:d974bcee4f69 6141 Function: It gets and stores an ACK if it is received, before ending 'wait' time.
cdupaty 0:d974bcee4f69 6142 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6143 state = 2 --> The ACK has not been received
cdupaty 0:d974bcee4f69 6144 state = 1 --> The N-ACK has been received with no errors
cdupaty 0:d974bcee4f69 6145 state = 0 --> The ACK has been received with no errors
cdupaty 0:d974bcee4f69 6146 Parameters:
cdupaty 0:d974bcee4f69 6147 wait: time to wait while there is no a valid header received.
cdupaty 0:d974bcee4f69 6148 */
cdupaty 0:d974bcee4f69 6149 uint8_t SX1272::getACK(uint16_t wait)
cdupaty 0:d974bcee4f69 6150 {
cdupaty 0:d974bcee4f69 6151 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6152 byte value = 0x00;
cdupaty 0:d974bcee4f69 6153 //unsigned long previous;
cdupaty 0:d974bcee4f69 6154 unsigned long exitTime;
cdupaty 0:d974bcee4f69 6155 boolean a_received = false;
cdupaty 0:d974bcee4f69 6156
cdupaty 0:d974bcee4f69 6157 //#if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6158 printf("\n");
cdupaty 0:d974bcee4f69 6159 printf("Starting 'getACK'\n");
cdupaty 0:d974bcee4f69 6160 //#endif
cdupaty 0:d974bcee4f69 6161
cdupaty 0:d974bcee4f69 6162 //previous = millis();
cdupaty 0:d974bcee4f69 6163 exitTime = millis()+(unsigned long)wait;
cdupaty 0:d974bcee4f69 6164 if( _modem == LORA )
cdupaty 0:d974bcee4f69 6165 { // LoRa mode
cdupaty 0:d974bcee4f69 6166 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 6167 // Wait until the ACK is received (RxDone flag) or the timeout expires
cdupaty 0:d974bcee4f69 6168 //while ((bitRead(value, 6) == 0) && (millis() - previous < wait))
cdupaty 0:d974bcee4f69 6169 while ((bitRead(value, 6) == 0) && (millis() < exitTime))
cdupaty 0:d974bcee4f69 6170 {
cdupaty 0:d974bcee4f69 6171 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 6172 //if( millis() < previous )
cdupaty 0:d974bcee4f69 6173 //{
cdupaty 0:d974bcee4f69 6174 // previous = millis();
cdupaty 0:d974bcee4f69 6175 //}
cdupaty 0:d974bcee4f69 6176 }
cdupaty 0:d974bcee4f69 6177 if( bitRead(value, 6) == 1 )
cdupaty 0:d974bcee4f69 6178 { // ACK received
cdupaty 0:d974bcee4f69 6179 // comment by C. Pham
cdupaty 0:d974bcee4f69 6180 // not really safe because the received packet may not be an ACK
cdupaty 0:d974bcee4f69 6181 // probability is low if using unicast to gateway, but if broadcast
cdupaty 0:d974bcee4f69 6182 // can get a packet from another node!!
cdupaty 0:d974bcee4f69 6183 a_received = true;
cdupaty 0:d974bcee4f69 6184 }
cdupaty 0:d974bcee4f69 6185 // Standby para minimizar el consumo
cdupaty 0:d974bcee4f69 6186 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Setting standby LoRa mode
cdupaty 0:d974bcee4f69 6187 }
cdupaty 0:d974bcee4f69 6188 else
cdupaty 0:d974bcee4f69 6189 { // FSK mode
cdupaty 0:d974bcee4f69 6190 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 6191 // Wait until the packet is received (RxDone flag) or the timeout expires
cdupaty 0:d974bcee4f69 6192 //while ((bitRead(value, 2) == 0) && (millis() - previous < wait))
cdupaty 0:d974bcee4f69 6193 while ((bitRead(value, 2) == 0) && (millis() < exitTime))
cdupaty 0:d974bcee4f69 6194 {
cdupaty 0:d974bcee4f69 6195 value = readRegister(REG_IRQ_FLAGS2);
cdupaty 0:d974bcee4f69 6196 //if( millis() < previous )
cdupaty 0:d974bcee4f69 6197 //{
cdupaty 0:d974bcee4f69 6198 // previous = millis();
cdupaty 0:d974bcee4f69 6199 //}
cdupaty 0:d974bcee4f69 6200 }
cdupaty 0:d974bcee4f69 6201 if( bitRead(value, 2) == 1 )
cdupaty 0:d974bcee4f69 6202 { // ACK received
cdupaty 0:d974bcee4f69 6203 a_received = true;
cdupaty 0:d974bcee4f69 6204 }
cdupaty 0:d974bcee4f69 6205 // Standby para minimizar el consumo
cdupaty 0:d974bcee4f69 6206 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Setting standby FSK mode
cdupaty 0:d974bcee4f69 6207 }
cdupaty 0:d974bcee4f69 6208
cdupaty 0:d974bcee4f69 6209 // comment by C. Pham
cdupaty 0:d974bcee4f69 6210 // not safe because the received packet may not be an ACK!
cdupaty 0:d974bcee4f69 6211 if( a_received )
cdupaty 0:d974bcee4f69 6212 {
cdupaty 0:d974bcee4f69 6213 // Storing the received ACK
cdupaty 0:d974bcee4f69 6214 ACK.dst = _destination;
cdupaty 0:d974bcee4f69 6215 ACK.type = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6216 ACK.src = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6217 ACK.packnum = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6218 ACK.length = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6219 ACK.data[0] = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6220 ACK.data[1] = readRegister(REG_FIFO);
cdupaty 0:d974bcee4f69 6221
cdupaty 0:d974bcee4f69 6222 if (ACK.type == PKT_TYPE_ACK) {
cdupaty 0:d974bcee4f69 6223
cdupaty 0:d974bcee4f69 6224 // Checking the received ACK
cdupaty 0:d974bcee4f69 6225 if( ACK.dst == packet_sent.src )
cdupaty 0:d974bcee4f69 6226 {
cdupaty 0:d974bcee4f69 6227 if( ACK.src == packet_sent.dst )
cdupaty 0:d974bcee4f69 6228 {
cdupaty 0:d974bcee4f69 6229 if( ACK.packnum == packet_sent.packnum )
cdupaty 0:d974bcee4f69 6230 {
cdupaty 0:d974bcee4f69 6231 if( ACK.length == 2 )
cdupaty 0:d974bcee4f69 6232 {
cdupaty 0:d974bcee4f69 6233 if( ACK.data[0] == CORRECT_PACKET )
cdupaty 0:d974bcee4f69 6234 {
cdupaty 0:d974bcee4f69 6235 state = 0;
cdupaty 0:d974bcee4f69 6236 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6237 // Printing the received ACK
cdupaty 0:d974bcee4f69 6238 printf("## ACK received:");
cdupaty 0:d974bcee4f69 6239 printf("Destination: %d\n",ACK.dst);
cdupaty 0:d974bcee4f69 6240 // Serial.println(ACK.dst); // Printing destination
cdupaty 0:d974bcee4f69 6241 printf("Source: %d\n",ACK.src);
cdupaty 0:d974bcee4f69 6242 // Serial.println(ACK.src); // Printing source
cdupaty 0:d974bcee4f69 6243 printf("ACK number: %d\n",ACK.packnum);
cdupaty 0:d974bcee4f69 6244 // Serial.println(ACK.packnum); // Printing ACK number
cdupaty 0:d974bcee4f69 6245 printf("ACK length: %d\n",ACK.length);
cdupaty 0:d974bcee4f69 6246 // Serial.println(ACK.length); // Printing ACK length
cdupaty 0:d974bcee4f69 6247 printf("ACK payload: %d\n",ACK.data[0]);
cdupaty 0:d974bcee4f69 6248 // Serial.println(ACK.data[0]); // Printing ACK payload
cdupaty 0:d974bcee4f69 6249 printf("ACK SNR of rcv pkt at gw: ");
cdupaty 0:d974bcee4f69 6250
cdupaty 0:d974bcee4f69 6251 value = ACK.data[1];
cdupaty 0:d974bcee4f69 6252
cdupaty 0:d974bcee4f69 6253 if( value & 0x80 ) // The SNR sign bit is 1
cdupaty 0:d974bcee4f69 6254 {
cdupaty 0:d974bcee4f69 6255 // Invert and divide by 4
cdupaty 0:d974bcee4f69 6256 value = ( ( ~value + 1 ) & 0xFF ) >> 2;
cdupaty 0:d974bcee4f69 6257 _rcv_snr_in_ack = -value;
cdupaty 0:d974bcee4f69 6258 }
cdupaty 0:d974bcee4f69 6259 else
cdupaty 0:d974bcee4f69 6260 {
cdupaty 0:d974bcee4f69 6261 // Divide by 4
cdupaty 0:d974bcee4f69 6262 _rcv_snr_in_ack = ( value & 0xFF ) >> 2;
cdupaty 0:d974bcee4f69 6263 }
cdupaty 0:d974bcee4f69 6264
cdupaty 0:d974bcee4f69 6265 //Serial.println(_rcv_snr_in_ack);
cdupaty 0:d974bcee4f69 6266 printf("%d\n",_rcv_snr_in_ack);
cdupaty 0:d974bcee4f69 6267 printf("##");
cdupaty 0:d974bcee4f69 6268 printf("\n");
cdupaty 0:d974bcee4f69 6269 //#endif
cdupaty 0:d974bcee4f69 6270 }
cdupaty 0:d974bcee4f69 6271 else
cdupaty 0:d974bcee4f69 6272 {
cdupaty 0:d974bcee4f69 6273 state = 1;
cdupaty 0:d974bcee4f69 6274 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6275 printf("** N-ACK received **");
cdupaty 0:d974bcee4f69 6276 printf("\n");
cdupaty 0:d974bcee4f69 6277 //#endif
cdupaty 0:d974bcee4f69 6278 }
cdupaty 0:d974bcee4f69 6279 }
cdupaty 0:d974bcee4f69 6280 else
cdupaty 0:d974bcee4f69 6281 {
cdupaty 0:d974bcee4f69 6282 state = 1;
cdupaty 0:d974bcee4f69 6283 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6284 printf("** ACK length incorrectly received **");
cdupaty 0:d974bcee4f69 6285 printf("\n");
cdupaty 0:d974bcee4f69 6286 //#endif
cdupaty 0:d974bcee4f69 6287 }
cdupaty 0:d974bcee4f69 6288 }
cdupaty 0:d974bcee4f69 6289 else
cdupaty 0:d974bcee4f69 6290 {
cdupaty 0:d974bcee4f69 6291 state = 1;
cdupaty 0:d974bcee4f69 6292 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6293 printf("** ACK number incorrectly received **");
cdupaty 0:d974bcee4f69 6294 printf("\n");
cdupaty 0:d974bcee4f69 6295 //#endif
cdupaty 0:d974bcee4f69 6296 }
cdupaty 0:d974bcee4f69 6297 }
cdupaty 0:d974bcee4f69 6298 else
cdupaty 0:d974bcee4f69 6299 {
cdupaty 0:d974bcee4f69 6300 state = 1;
cdupaty 0:d974bcee4f69 6301 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6302 printf("** ACK source incorrectly received **");
cdupaty 0:d974bcee4f69 6303 printf("\n");
cdupaty 0:d974bcee4f69 6304 //#endif
cdupaty 0:d974bcee4f69 6305 }
cdupaty 0:d974bcee4f69 6306 }
cdupaty 0:d974bcee4f69 6307 }
cdupaty 0:d974bcee4f69 6308 else
cdupaty 0:d974bcee4f69 6309 {
cdupaty 0:d974bcee4f69 6310 state = 1;
cdupaty 0:d974bcee4f69 6311 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6312 printf("** ACK destination incorrectly received **");
cdupaty 0:d974bcee4f69 6313 printf("\n");
cdupaty 0:d974bcee4f69 6314 //#endif
cdupaty 0:d974bcee4f69 6315 }
cdupaty 0:d974bcee4f69 6316 }
cdupaty 0:d974bcee4f69 6317 else
cdupaty 0:d974bcee4f69 6318 {
cdupaty 0:d974bcee4f69 6319 state = 1;
cdupaty 0:d974bcee4f69 6320 //#if (SX1272_debug_mode > 0)
cdupaty 0:d974bcee4f69 6321 printf("** ACK lost **");
cdupaty 0:d974bcee4f69 6322 printf("\n");
cdupaty 0:d974bcee4f69 6323 //#endif
cdupaty 0:d974bcee4f69 6324 }
cdupaty 0:d974bcee4f69 6325 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 6326 return state;
cdupaty 0:d974bcee4f69 6327 }
cdupaty 0:d974bcee4f69 6328
cdupaty 0:d974bcee4f69 6329 /*
cdupaty 0:d974bcee4f69 6330 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6331 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6332 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6333 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6334 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6335 */
cdupaty 0:d974bcee4f69 6336 uint8_t SX1272::sendPacketMAXTimeoutACKRetries(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 6337 {
cdupaty 0:d974bcee4f69 6338 return sendPacketTimeoutACKRetries(dest, payload, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 6339 }
cdupaty 0:d974bcee4f69 6340
cdupaty 0:d974bcee4f69 6341 /*
cdupaty 0:d974bcee4f69 6342 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6343 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6344 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6345 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6346 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6347 */
cdupaty 0:d974bcee4f69 6348 uint8_t SX1272::sendPacketMAXTimeoutACKRetries(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 6349 {
cdupaty 0:d974bcee4f69 6350 return sendPacketTimeoutACKRetries(dest, payload, length16, MAX_TIMEOUT);
cdupaty 0:d974bcee4f69 6351 }
cdupaty 0:d974bcee4f69 6352
cdupaty 0:d974bcee4f69 6353 /*
cdupaty 0:d974bcee4f69 6354 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6355 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6356 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6357 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6358 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6359 */
cdupaty 0:d974bcee4f69 6360 uint8_t SX1272::sendPacketTimeoutACKRetries(uint8_t dest, char *payload)
cdupaty 0:d974bcee4f69 6361 {
cdupaty 0:d974bcee4f69 6362 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6363
cdupaty 0:d974bcee4f69 6364 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6365 printf("\n");
cdupaty 0:d974bcee4f69 6366 printf("Starting 'sendPacketTimeoutACKRetries'\n");
cdupaty 0:d974bcee4f69 6367 #endif
cdupaty 0:d974bcee4f69 6368
cdupaty 0:d974bcee4f69 6369 // Sending packet to 'dest' destination and waiting an ACK response.
cdupaty 0:d974bcee4f69 6370 state = 1;
cdupaty 0:d974bcee4f69 6371 while( (state != 0) && (_retries <= _maxRetries) )
cdupaty 0:d974bcee4f69 6372 {
cdupaty 0:d974bcee4f69 6373 state = sendPacketTimeoutACK(dest, payload);
cdupaty 0:d974bcee4f69 6374 _retries++;
cdupaty 0:d974bcee4f69 6375 }
cdupaty 0:d974bcee4f69 6376 _retries = 0;
cdupaty 0:d974bcee4f69 6377
cdupaty 0:d974bcee4f69 6378 return state;
cdupaty 0:d974bcee4f69 6379 }
cdupaty 0:d974bcee4f69 6380
cdupaty 0:d974bcee4f69 6381 /*
cdupaty 0:d974bcee4f69 6382 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6383 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6384 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6385 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6386 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6387 */
cdupaty 0:d974bcee4f69 6388 uint8_t SX1272::sendPacketTimeoutACKRetries(uint8_t dest, uint8_t *payload, uint16_t length16)
cdupaty 0:d974bcee4f69 6389 {
cdupaty 0:d974bcee4f69 6390 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6391
cdupaty 0:d974bcee4f69 6392 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6393 printf("\n");
cdupaty 0:d974bcee4f69 6394 printf("Starting 'sendPacketTimeoutACKRetries'\n");
cdupaty 0:d974bcee4f69 6395 #endif
cdupaty 0:d974bcee4f69 6396
cdupaty 0:d974bcee4f69 6397 // Sending packet to 'dest' destination and waiting an ACK response.
cdupaty 0:d974bcee4f69 6398 state = 1;
cdupaty 0:d974bcee4f69 6399 while((state != 0) && (_retries <= _maxRetries))
cdupaty 0:d974bcee4f69 6400 {
cdupaty 0:d974bcee4f69 6401 state = sendPacketTimeoutACK(dest, payload, length16);
cdupaty 0:d974bcee4f69 6402 _retries++;
cdupaty 0:d974bcee4f69 6403
cdupaty 0:d974bcee4f69 6404 }
cdupaty 0:d974bcee4f69 6405 _retries = 0;
cdupaty 0:d974bcee4f69 6406
cdupaty 0:d974bcee4f69 6407 return state;
cdupaty 0:d974bcee4f69 6408 }
cdupaty 0:d974bcee4f69 6409
cdupaty 0:d974bcee4f69 6410 /*
cdupaty 0:d974bcee4f69 6411 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6412 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6413 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6414 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6415 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6416 */
cdupaty 0:d974bcee4f69 6417 uint8_t SX1272::sendPacketTimeoutACKRetries(uint8_t dest, char *payload, uint16_t wait)
cdupaty 0:d974bcee4f69 6418 {
cdupaty 0:d974bcee4f69 6419 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6420
cdupaty 0:d974bcee4f69 6421 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6422 printf("\n");
cdupaty 0:d974bcee4f69 6423 printf("Starting 'sendPacketTimeoutACKRetries'\n");
cdupaty 0:d974bcee4f69 6424 #endif
cdupaty 0:d974bcee4f69 6425
cdupaty 0:d974bcee4f69 6426 // Sending packet to 'dest' destination and waiting an ACK response.
cdupaty 0:d974bcee4f69 6427 state = 1;
cdupaty 0:d974bcee4f69 6428 while((state != 0) && (_retries <= _maxRetries))
cdupaty 0:d974bcee4f69 6429 {
cdupaty 0:d974bcee4f69 6430 state = sendPacketTimeoutACK(dest, payload, wait);
cdupaty 0:d974bcee4f69 6431 _retries++;
cdupaty 0:d974bcee4f69 6432 }
cdupaty 0:d974bcee4f69 6433 _retries = 0;
cdupaty 0:d974bcee4f69 6434
cdupaty 0:d974bcee4f69 6435 return state;
cdupaty 0:d974bcee4f69 6436 }
cdupaty 0:d974bcee4f69 6437
cdupaty 0:d974bcee4f69 6438 /*
cdupaty 0:d974bcee4f69 6439 Function: Configures the module to transmit information with retries in case of error.
cdupaty 0:d974bcee4f69 6440 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6441 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6442 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6443 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6444 */
cdupaty 0:d974bcee4f69 6445 uint8_t SX1272::sendPacketTimeoutACKRetries(uint8_t dest, uint8_t *payload, uint16_t length16, uint16_t wait)
cdupaty 0:d974bcee4f69 6446 {
cdupaty 0:d974bcee4f69 6447 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6448
cdupaty 0:d974bcee4f69 6449 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6450 printf("\n");
cdupaty 0:d974bcee4f69 6451 printf("Starting 'sendPacketTimeoutACKRetries'\n");
cdupaty 0:d974bcee4f69 6452 #endif
cdupaty 0:d974bcee4f69 6453
cdupaty 0:d974bcee4f69 6454 // Sending packet to 'dest' destination and waiting an ACK response.
cdupaty 0:d974bcee4f69 6455 state = 1;
cdupaty 0:d974bcee4f69 6456 while((state != 0) && (_retries <= _maxRetries))
cdupaty 0:d974bcee4f69 6457 {
cdupaty 0:d974bcee4f69 6458 state = sendPacketTimeoutACK(dest, payload, length16, wait);
cdupaty 0:d974bcee4f69 6459 _retries++;
cdupaty 0:d974bcee4f69 6460 }
cdupaty 0:d974bcee4f69 6461 _retries = 0;
cdupaty 0:d974bcee4f69 6462
cdupaty 0:d974bcee4f69 6463 return state;
cdupaty 0:d974bcee4f69 6464 }
cdupaty 0:d974bcee4f69 6465
cdupaty 0:d974bcee4f69 6466 /*
cdupaty 0:d974bcee4f69 6467 Function: It gets the temperature from the measurement block module.
cdupaty 0:d974bcee4f69 6468 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6469 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6470 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6471 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6472 */
cdupaty 0:d974bcee4f69 6473 uint8_t SX1272::getTemp()
cdupaty 0:d974bcee4f69 6474 {
cdupaty 0:d974bcee4f69 6475 byte st0;
cdupaty 0:d974bcee4f69 6476 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6477
cdupaty 0:d974bcee4f69 6478 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6479 printf("\n");
cdupaty 0:d974bcee4f69 6480 printf("Starting 'getTemp'\n");
cdupaty 0:d974bcee4f69 6481 #endif
cdupaty 0:d974bcee4f69 6482
cdupaty 0:d974bcee4f69 6483 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 6484
cdupaty 0:d974bcee4f69 6485 if( _modem == LORA )
cdupaty 0:d974bcee4f69 6486 { // Allowing access to FSK registers while in LoRa standby mode
cdupaty 0:d974bcee4f69 6487 writeRegister(REG_OP_MODE, LORA_STANDBY_FSK_REGS_MODE);
cdupaty 0:d974bcee4f69 6488 }
cdupaty 0:d974bcee4f69 6489
cdupaty 0:d974bcee4f69 6490 state = 1;
cdupaty 0:d974bcee4f69 6491 // Saving temperature value
cdupaty 0:d974bcee4f69 6492 _temp = readRegister(REG_TEMP);
cdupaty 0:d974bcee4f69 6493 if( _temp & 0x80 ) // The SNR sign bit is 1
cdupaty 0:d974bcee4f69 6494 {
cdupaty 0:d974bcee4f69 6495 // Invert and divide by 4
cdupaty 0:d974bcee4f69 6496 _temp = ( ( ~_temp + 1 ) & 0xFF );
cdupaty 0:d974bcee4f69 6497 }
cdupaty 0:d974bcee4f69 6498 else
cdupaty 0:d974bcee4f69 6499 {
cdupaty 0:d974bcee4f69 6500 // Divide by 4
cdupaty 0:d974bcee4f69 6501 _temp = ( _temp & 0xFF );
cdupaty 0:d974bcee4f69 6502 }
cdupaty 0:d974bcee4f69 6503
cdupaty 0:d974bcee4f69 6504
cdupaty 0:d974bcee4f69 6505 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6506 printf("## Temperature is: %d ",_temp);
marcoantonioara 3:82630593359c 6507 Serial.print(_temp);
cdupaty 0:d974bcee4f69 6508 printf(" ##");
cdupaty 0:d974bcee4f69 6509 printf("\n");
cdupaty 0:d974bcee4f69 6510 #endif
cdupaty 0:d974bcee4f69 6511
cdupaty 0:d974bcee4f69 6512 if( _modem == LORA )
cdupaty 0:d974bcee4f69 6513 {
cdupaty 0:d974bcee4f69 6514 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 6515 }
cdupaty 0:d974bcee4f69 6516
cdupaty 0:d974bcee4f69 6517 state = 0;
cdupaty 0:d974bcee4f69 6518 return state;
cdupaty 0:d974bcee4f69 6519 }
cdupaty 0:d974bcee4f69 6520
cdupaty 0:d974bcee4f69 6521 //**********************************************************************/
cdupaty 0:d974bcee4f69 6522 // Added by C. Pham
cdupaty 0:d974bcee4f69 6523 //**********************************************************************/
cdupaty 0:d974bcee4f69 6524
cdupaty 0:d974bcee4f69 6525 void SX1272::setPacketType(uint8_t type)
cdupaty 0:d974bcee4f69 6526 {
cdupaty 0:d974bcee4f69 6527 packet_sent.type=type;
cdupaty 0:d974bcee4f69 6528
cdupaty 0:d974bcee4f69 6529 if (type & PKT_FLAG_ACK_REQ)
cdupaty 0:d974bcee4f69 6530 _requestACK=1;
cdupaty 0:d974bcee4f69 6531 }
cdupaty 0:d974bcee4f69 6532
cdupaty 0:d974bcee4f69 6533 /*
cdupaty 0:d974bcee4f69 6534 Function: Configures the module to perform CAD.
cdupaty 0:d974bcee4f69 6535 Returns: Integer that determines if the number of requested CAD have been successfull
cdupaty 0:d974bcee4f69 6536 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6537 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6538 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6539 */
cdupaty 0:d974bcee4f69 6540 uint8_t SX1272::doCAD(uint8_t counter)
cdupaty 0:d974bcee4f69 6541 {
cdupaty 0:d974bcee4f69 6542 uint8_t state = 2;
cdupaty 0:d974bcee4f69 6543 byte value = 0x00;
cdupaty 0:d974bcee4f69 6544 unsigned long startCAD, endCAD, startDoCad, endDoCad;
cdupaty 0:d974bcee4f69 6545 //unsigned long previous;
cdupaty 0:d974bcee4f69 6546 unsigned long exitTime;
cdupaty 0:d974bcee4f69 6547 uint16_t wait = 100;
cdupaty 0:d974bcee4f69 6548 bool failedCAD=false;
cdupaty 0:d974bcee4f69 6549 uint8_t retryCAD = 3;
cdupaty 0:d974bcee4f69 6550 uint8_t save_counter;
cdupaty 0:d974bcee4f69 6551 byte st0;
cdupaty 0:d974bcee4f69 6552 int rssi_count=0;
cdupaty 0:d974bcee4f69 6553 int rssi_mean=0;
cdupaty 0:d974bcee4f69 6554 double bw=0.0;
cdupaty 0:d974bcee4f69 6555 bool hasRSSI=false;
cdupaty 0:d974bcee4f69 6556 unsigned long startRSSI=0;
cdupaty 0:d974bcee4f69 6557
cdupaty 0:d974bcee4f69 6558 bw=(_bandwidth==BW_125)?125e3:((_bandwidth==BW_250)?250e3:500e3);
cdupaty 0:d974bcee4f69 6559 // Symbol rate : time for one symbol (usecs)
cdupaty 0:d974bcee4f69 6560 double rs = bw / ( 1 << _spreadingFactor);
cdupaty 0:d974bcee4f69 6561 double ts = 1 / rs;
cdupaty 0:d974bcee4f69 6562 ts = ts * 1000000.0;
cdupaty 0:d974bcee4f69 6563
cdupaty 0:d974bcee4f69 6564 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 6565
cdupaty 0:d974bcee4f69 6566 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6567 printf("SX1272::Starting 'doCAD'\n");
cdupaty 0:d974bcee4f69 6568 #endif
cdupaty 0:d974bcee4f69 6569
cdupaty 0:d974bcee4f69 6570 save_counter = counter;
cdupaty 0:d974bcee4f69 6571
cdupaty 0:d974bcee4f69 6572 startDoCad=millis();
cdupaty 0:d974bcee4f69 6573
cdupaty 0:d974bcee4f69 6574 if( _modem == LORA ) { // LoRa mode
cdupaty 0:d974bcee4f69 6575
cdupaty 0:d974bcee4f69 6576 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 6577
cdupaty 0:d974bcee4f69 6578 do {
cdupaty 0:d974bcee4f69 6579
cdupaty 0:d974bcee4f69 6580 hasRSSI=false;
cdupaty 0:d974bcee4f69 6581
cdupaty 0:d974bcee4f69 6582 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 6583
cdupaty 0:d974bcee4f69 6584 // wait to CadDone flag
cdupaty 0:d974bcee4f69 6585 // previous = millis();
cdupaty 0:d974bcee4f69 6586 startCAD = millis();
cdupaty 0:d974bcee4f69 6587 exitTime = millis()+(unsigned long)wait;
cdupaty 0:d974bcee4f69 6588
cdupaty 0:d974bcee4f69 6589 writeRegister(REG_OP_MODE, LORA_CAD_MODE); // LORA mode - Cad
cdupaty 0:d974bcee4f69 6590
cdupaty 0:d974bcee4f69 6591 startRSSI=micros();
cdupaty 0:d974bcee4f69 6592
cdupaty 0:d974bcee4f69 6593 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 6594 // Wait until CAD ends (CAD Done flag) or the timeout expires
cdupaty 0:d974bcee4f69 6595 //while ((bitRead(value, 2) == 0) && (millis() - previous < wait))
cdupaty 0:d974bcee4f69 6596 while ((bitRead(value, 2) == 0) && (millis() < exitTime))
cdupaty 0:d974bcee4f69 6597 {
cdupaty 0:d974bcee4f69 6598 // only one reading per CAD
cdupaty 0:d974bcee4f69 6599 if (micros()-startRSSI > ts+240 && !hasRSSI) {
cdupaty 0:d974bcee4f69 6600 _RSSI = -(OFFSET_RSSI+(_board==SX1276Chip?18:0)) + readRegister(REG_RSSI_VALUE_LORA);
cdupaty 0:d974bcee4f69 6601 rssi_mean += _RSSI;
cdupaty 0:d974bcee4f69 6602 rssi_count++;
cdupaty 0:d974bcee4f69 6603 hasRSSI=true;
cdupaty 0:d974bcee4f69 6604 }
cdupaty 0:d974bcee4f69 6605
cdupaty 0:d974bcee4f69 6606 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 6607 // Condition to avoid an overflow (DO NOT REMOVE)
cdupaty 0:d974bcee4f69 6608 //if( millis() < previous )
cdupaty 0:d974bcee4f69 6609 //{
cdupaty 0:d974bcee4f69 6610 // previous = millis();
cdupaty 0:d974bcee4f69 6611 //}
cdupaty 0:d974bcee4f69 6612 }
cdupaty 0:d974bcee4f69 6613 state = 1;
cdupaty 0:d974bcee4f69 6614
cdupaty 0:d974bcee4f69 6615 endCAD = millis();
cdupaty 0:d974bcee4f69 6616
cdupaty 0:d974bcee4f69 6617 if( bitRead(value, 2) == 1 )
cdupaty 0:d974bcee4f69 6618 {
cdupaty 0:d974bcee4f69 6619 state = 0; // CAD successfully performed
cdupaty 0:d974bcee4f69 6620 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6621 printf("SX1272::CAD duration %X\n",endCAD-startCAD);
cdupaty 0:d974bcee4f69 6622
cdupaty 0:d974bcee4f69 6623 // Serial.println(endCAD-startCAD);
cdupaty 0:d974bcee4f69 6624 printf("SX1272::CAD successfully performed");
cdupaty 0:d974bcee4f69 6625 #endif
cdupaty 0:d974bcee4f69 6626
cdupaty 0:d974bcee4f69 6627 value = readRegister(REG_IRQ_FLAGS);
cdupaty 0:d974bcee4f69 6628
cdupaty 0:d974bcee4f69 6629 // look for the CAD detected bit
cdupaty 0:d974bcee4f69 6630 if( bitRead(value, 0) == 1 )
cdupaty 0:d974bcee4f69 6631 {
cdupaty 0:d974bcee4f69 6632 // we detected activity
cdupaty 0:d974bcee4f69 6633 failedCAD=true;
cdupaty 0:d974bcee4f69 6634 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6635 printf("SX1272::CAD exits after %d\n",save_counter-counter);
cdupaty 0:d974bcee4f69 6636 // Serial.println(save_counter-counter);
cdupaty 0:d974bcee4f69 6637 #endif
cdupaty 0:d974bcee4f69 6638 }
cdupaty 0:d974bcee4f69 6639
cdupaty 0:d974bcee4f69 6640 counter--;
cdupaty 0:d974bcee4f69 6641 }
cdupaty 0:d974bcee4f69 6642 else
cdupaty 0:d974bcee4f69 6643 {
cdupaty 0:d974bcee4f69 6644 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6645 printf("SX1272::CAD duration %d\n",endCAD-startCAD);
cdupaty 0:d974bcee4f69 6646 // Serial.println(endCAD-startCAD);
cdupaty 0:d974bcee4f69 6647 #endif
cdupaty 0:d974bcee4f69 6648 if( state == 1 )
cdupaty 0:d974bcee4f69 6649 {
cdupaty 0:d974bcee4f69 6650 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6651 printf("SX1272::Timeout has expired");
cdupaty 0:d974bcee4f69 6652 #endif
cdupaty 0:d974bcee4f69 6653 }
cdupaty 0:d974bcee4f69 6654 else
cdupaty 0:d974bcee4f69 6655 {
cdupaty 0:d974bcee4f69 6656 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6657 printf("SX1272::Error and CAD has not been performed");
cdupaty 0:d974bcee4f69 6658 #endif
cdupaty 0:d974bcee4f69 6659 }
cdupaty 0:d974bcee4f69 6660
cdupaty 0:d974bcee4f69 6661 retryCAD--;
cdupaty 0:d974bcee4f69 6662
cdupaty 0:d974bcee4f69 6663 // to many errors, so exit by indicating that channel is not free
cdupaty 0:d974bcee4f69 6664 if (!retryCAD)
cdupaty 0:d974bcee4f69 6665 failedCAD=true;
cdupaty 0:d974bcee4f69 6666 }
cdupaty 0:d974bcee4f69 6667
cdupaty 0:d974bcee4f69 6668 } while (counter && !failedCAD);
cdupaty 0:d974bcee4f69 6669
cdupaty 0:d974bcee4f69 6670 rssi_mean = rssi_mean / rssi_count;
cdupaty 0:d974bcee4f69 6671 _RSSI = rssi_mean;
cdupaty 0:d974bcee4f69 6672 }
cdupaty 0:d974bcee4f69 6673
cdupaty 0:d974bcee4f69 6674 writeRegister(REG_OP_MODE, st0);
cdupaty 0:d974bcee4f69 6675
cdupaty 0:d974bcee4f69 6676 endDoCad=millis();
cdupaty 0:d974bcee4f69 6677
cdupaty 0:d974bcee4f69 6678 clearFlags(); // Initializing flags
cdupaty 0:d974bcee4f69 6679
cdupaty 0:d974bcee4f69 6680 #ifdef DEBUG_CAD
cdupaty 0:d974bcee4f69 6681 printf("SX1272::doCAD duration %d\n",endDoCad-startDoCad);
cdupaty 0:d974bcee4f69 6682 // Serial.println(endDoCad-startDoCad);
cdupaty 0:d974bcee4f69 6683 #endif
cdupaty 0:d974bcee4f69 6684
cdupaty 0:d974bcee4f69 6685 if (failedCAD)
cdupaty 0:d974bcee4f69 6686 return 2;
cdupaty 0:d974bcee4f69 6687
cdupaty 0:d974bcee4f69 6688 return state;
cdupaty 0:d974bcee4f69 6689 }
cdupaty 0:d974bcee4f69 6690
cdupaty 0:d974bcee4f69 6691 //#define DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6692
cdupaty 0:d974bcee4f69 6693 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6694
cdupaty 0:d974bcee4f69 6695 void printDouble( double val, byte precision){
cdupaty 0:d974bcee4f69 6696 // prints val with number of decimal places determine by precision
cdupaty 0:d974bcee4f69 6697 // precision is a number from 0 to 6 indicating the desired decimial places
cdupaty 0:d974bcee4f69 6698 // example: lcdPrintDouble( 3.1415, 2); // prints 3.14 (two decimal places)
cdupaty 0:d974bcee4f69 6699
cdupaty 0:d974bcee4f69 6700 if(val < 0.0){
cdupaty 0:d974bcee4f69 6701 Serial.print('-');
cdupaty 0:d974bcee4f69 6702 val = -val;
cdupaty 0:d974bcee4f69 6703 }
cdupaty 0:d974bcee4f69 6704
cdupaty 0:d974bcee4f69 6705 Serial.print (int(val)); //prints the int part
cdupaty 0:d974bcee4f69 6706 if( precision > 0) {
cdupaty 0:d974bcee4f69 6707 Serial.print("."); // print the decimal point
cdupaty 0:d974bcee4f69 6708 unsigned long frac;
cdupaty 0:d974bcee4f69 6709 unsigned long mult = 1;
cdupaty 0:d974bcee4f69 6710 byte padding = precision -1;
cdupaty 0:d974bcee4f69 6711 while(precision--)
cdupaty 0:d974bcee4f69 6712 mult *=10;
cdupaty 0:d974bcee4f69 6713
cdupaty 0:d974bcee4f69 6714 if(val >= 0)
cdupaty 0:d974bcee4f69 6715 frac = (val - int(val)) * mult;
cdupaty 0:d974bcee4f69 6716 else
cdupaty 0:d974bcee4f69 6717 frac = (int(val)- val ) * mult;
cdupaty 0:d974bcee4f69 6718 unsigned long frac1 = frac;
cdupaty 0:d974bcee4f69 6719 while( frac1 /= 10 )
cdupaty 0:d974bcee4f69 6720 padding--;
cdupaty 0:d974bcee4f69 6721 while( padding--)
cdupaty 0:d974bcee4f69 6722 Serial.print("0");
cdupaty 0:d974bcee4f69 6723 printfrac,DEC) ;
cdupaty 0:d974bcee4f69 6724 }
cdupaty 0:d974bcee4f69 6725 }
cdupaty 0:d974bcee4f69 6726
cdupaty 0:d974bcee4f69 6727 #endif
cdupaty 0:d974bcee4f69 6728
cdupaty 0:d974bcee4f69 6729 uint16_t SX1272::getToA(uint8_t pl) {
cdupaty 0:d974bcee4f69 6730
cdupaty 0:d974bcee4f69 6731 uint8_t DE = 0;
cdupaty 0:d974bcee4f69 6732 uint32_t airTime = 0;
cdupaty 0:d974bcee4f69 6733
cdupaty 0:d974bcee4f69 6734 double bw=0.0;
cdupaty 0:d974bcee4f69 6735
cdupaty 0:d974bcee4f69 6736 bw=(_bandwidth==BW_125)?125e3:((_bandwidth==BW_250)?250e3:500e3);
cdupaty 0:d974bcee4f69 6737
cdupaty 0:d974bcee4f69 6738 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6739 printf("SX1272::bw is ");
cdupaty 0:d974bcee4f69 6740 Serial.println(bw);
cdupaty 0:d974bcee4f69 6741
cdupaty 0:d974bcee4f69 6742 printf("SX1272::SF is ");
cdupaty 0:d974bcee4f69 6743 Serial.println(_spreadingFactor);
cdupaty 0:d974bcee4f69 6744 #endif
cdupaty 0:d974bcee4f69 6745
cdupaty 0:d974bcee4f69 6746 //double ts=pow(2,_spreadingFactor)/bw;
cdupaty 0:d974bcee4f69 6747
cdupaty 0:d974bcee4f69 6748 ////// from LoRaMAC SX1272GetTimeOnAir()
cdupaty 0:d974bcee4f69 6749
cdupaty 0:d974bcee4f69 6750 // Symbol rate : time for one symbol (secs)
cdupaty 0:d974bcee4f69 6751 double rs = bw / ( 1 << _spreadingFactor);
cdupaty 0:d974bcee4f69 6752 double ts = 1 / rs;
cdupaty 0:d974bcee4f69 6753
cdupaty 0:d974bcee4f69 6754 // must add 4 to the programmed preamble length to get the effective preamble length
cdupaty 0:d974bcee4f69 6755 double tPreamble=((_preamblelength+4)+4.25)*ts;
cdupaty 0:d974bcee4f69 6756
cdupaty 0:d974bcee4f69 6757 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6758 printf("SX1272::ts is ");
cdupaty 0:d974bcee4f69 6759 printDouble(ts,6);
cdupaty 0:d974bcee4f69 6760 printf("\n");
cdupaty 0:d974bcee4f69 6761 printf("SX1272::tPreamble is ");
cdupaty 0:d974bcee4f69 6762 printDouble(tPreamble,6);
cdupaty 0:d974bcee4f69 6763 printf("\n");
cdupaty 0:d974bcee4f69 6764 #endif
cdupaty 0:d974bcee4f69 6765
cdupaty 0:d974bcee4f69 6766 // for low data rate optimization
cdupaty 0:d974bcee4f69 6767 if ((_bandwidth == BW_125) && _spreadingFactor == 12)
cdupaty 0:d974bcee4f69 6768 DE = 1;
cdupaty 0:d974bcee4f69 6769
cdupaty 0:d974bcee4f69 6770 // Symbol length of payload and time
cdupaty 0:d974bcee4f69 6771 double tmp = (8*pl - 4*_spreadingFactor + 28 + 16 - 20*_header) /
cdupaty 0:d974bcee4f69 6772 (double)(4*(_spreadingFactor-2*DE) );
cdupaty 0:d974bcee4f69 6773
cdupaty 0:d974bcee4f69 6774 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6775 printf("SX1272::tmp is ");
cdupaty 0:d974bcee4f69 6776 printDouble(tmp,6);
cdupaty 0:d974bcee4f69 6777 printf("\n");
cdupaty 0:d974bcee4f69 6778 #endif
cdupaty 0:d974bcee4f69 6779
cdupaty 0:d974bcee4f69 6780 tmp = ceil(tmp)*(_codingRate + 4);
cdupaty 0:d974bcee4f69 6781
cdupaty 0:d974bcee4f69 6782 double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 );
cdupaty 0:d974bcee4f69 6783
cdupaty 0:d974bcee4f69 6784 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6785 printf("SX1272::nPayload is ");
cdupaty 0:d974bcee4f69 6786 Serial.println(nPayload);
cdupaty 0:d974bcee4f69 6787 #endif
cdupaty 0:d974bcee4f69 6788
cdupaty 0:d974bcee4f69 6789 double tPayload = nPayload * ts;
cdupaty 0:d974bcee4f69 6790 // Time on air
cdupaty 0:d974bcee4f69 6791 double tOnAir = tPreamble + tPayload;
cdupaty 0:d974bcee4f69 6792 // in us secs
cdupaty 0:d974bcee4f69 6793 airTime = floor( tOnAir * 1e6 + 0.999 );
cdupaty 0:d974bcee4f69 6794
cdupaty 0:d974bcee4f69 6795 //////
cdupaty 0:d974bcee4f69 6796
cdupaty 0:d974bcee4f69 6797 #ifdef DEBUG_GETTOA
cdupaty 0:d974bcee4f69 6798 printf("SX1272::airTime is ");
cdupaty 0:d974bcee4f69 6799 Serial.println(airTime);
cdupaty 0:d974bcee4f69 6800 #endif
cdupaty 0:d974bcee4f69 6801 // return in ms
cdupaty 0:d974bcee4f69 6802 // Modifie par C.Dupaty A VERIFIER !!!!!!!!!!!!!!!!!!!!!!!!!
cdupaty 0:d974bcee4f69 6803 // _currentToA=ceil(airTime/1000)+1;
cdupaty 0:d974bcee4f69 6804 _currentToA=(airTime/1000)+1;
cdupaty 0:d974bcee4f69 6805 return _currentToA;
cdupaty 0:d974bcee4f69 6806 }
cdupaty 0:d974bcee4f69 6807
cdupaty 0:d974bcee4f69 6808 // need to set _send_cad_number to a value > 0
cdupaty 0:d974bcee4f69 6809 // we advise using _send_cad_number=3 for a SIFS and _send_cad_number=9 for a DIFS
cdupaty 0:d974bcee4f69 6810 // prior to send any data
cdupaty 0:d974bcee4f69 6811 // TODO: have a maximum number of trials
cdupaty 0:d974bcee4f69 6812 void SX1272::CarrierSense() {
cdupaty 0:d974bcee4f69 6813
cdupaty 0:d974bcee4f69 6814 int e;
cdupaty 0:d974bcee4f69 6815 bool carrierSenseRetry=false;
cdupaty 0:d974bcee4f69 6816
cdupaty 0:d974bcee4f69 6817 if (_send_cad_number && _enableCarrierSense) {
cdupaty 0:d974bcee4f69 6818 do {
cdupaty 0:d974bcee4f69 6819 do {
cdupaty 0:d974bcee4f69 6820
cdupaty 0:d974bcee4f69 6821 // check for free channel (SIFS/DIFS)
cdupaty 0:d974bcee4f69 6822 _startDoCad=millis();
cdupaty 0:d974bcee4f69 6823 e = doCAD(_send_cad_number);
cdupaty 0:d974bcee4f69 6824 _endDoCad=millis();
cdupaty 0:d974bcee4f69 6825
marcoantonioara 3:82630593359c 6826 // printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
cdupaty 0:d974bcee4f69 6827 // Serial.print(_endDoCad-_startDoCad);
marcoantonioara 3:82630593359c 6828 // printf("\n");
cdupaty 0:d974bcee4f69 6829
cdupaty 0:d974bcee4f69 6830 if (!e) {
marcoantonioara 3:82630593359c 6831 // printf("OK1\n");
cdupaty 0:d974bcee4f69 6832
cdupaty 0:d974bcee4f69 6833 if (_extendedIFS) {
cdupaty 0:d974bcee4f69 6834 // wait for random number of CAD
cdupaty 0:d974bcee4f69 6835 // uint8_t w = random(1,8);
cdupaty 0:d974bcee4f69 6836 // ajoute par C.Dupaty
cdupaty 0:d974bcee4f69 6837 uint8_t w = rand()%8+1;
marcoantonioara 3:82630593359c 6838 // printf("--> waiting for %d\n",w);
cdupaty 0:d974bcee4f69 6839 // Serial.print(w);
marcoantonioara 3:82630593359c 6840 // printf(" CAD = %d\n",sx1272_CAD_value[_loraMode]*w);
cdupaty 0:d974bcee4f69 6841 // Serial.print(sx1272_CAD_value[_loraMode]*w);
cdupaty 0:d974bcee4f69 6842 printf("\n");
cdupaty 0:d974bcee4f69 6843
cdupaty 0:d974bcee4f69 6844 wait_ms(sx1272_CAD_value[_loraMode]*w);
cdupaty 0:d974bcee4f69 6845
cdupaty 0:d974bcee4f69 6846 // check for free channel (SIFS/DIFS) once again
cdupaty 0:d974bcee4f69 6847 _startDoCad=millis();
cdupaty 0:d974bcee4f69 6848 e = doCAD(_send_cad_number);
cdupaty 0:d974bcee4f69 6849 _endDoCad=millis();
cdupaty 0:d974bcee4f69 6850
marcoantonioara 3:82630593359c 6851 // printf("--> CAD duration %d\n",_endDoCad-_startDoCad);
cdupaty 0:d974bcee4f69 6852 // Serial.print(_endDoCad-_startDoCad);
marcoantonioara 3:82630593359c 6853 // printf("\n");
marcoantonioara 3:82630593359c 6854
marcoantonioara 3:82630593359c 6855 // if (!e)
marcoantonioara 3:82630593359c 6856 //// printf("OK2");
marcoantonioara 3:82630593359c 6857 // else
marcoantonioara 3:82630593359c 6858 // printf("###2");
marcoantonioara 3:82630593359c 6859 //
marcoantonioara 3:82630593359c 6860 // printf("\n");
cdupaty 0:d974bcee4f69 6861 }
cdupaty 0:d974bcee4f69 6862 }
cdupaty 0:d974bcee4f69 6863 else {
cdupaty 0:d974bcee4f69 6864 printf("###1\n");
cdupaty 0:d974bcee4f69 6865
cdupaty 0:d974bcee4f69 6866 // wait for random number of DIFS
cdupaty 0:d974bcee4f69 6867 uint8_t w = rand()%8+1;
cdupaty 0:d974bcee4f69 6868
marcoantonioara 3:82630593359c 6869 // printf("--> waiting for %d\n",w);
cdupaty 0:d974bcee4f69 6870 // Serial.print(w);
cdupaty 0:d974bcee4f69 6871 printf(" DIFS (DIFS=3SIFS) = %d\n",sx1272_SIFS_value[_loraMode]*3*w);
cdupaty 0:d974bcee4f69 6872 // Serial.print(sx1272_SIFS_value[_loraMode]*3*w);
cdupaty 0:d974bcee4f69 6873 printf("\n");
cdupaty 0:d974bcee4f69 6874
cdupaty 0:d974bcee4f69 6875 wait_ms(sx1272_SIFS_value[_loraMode]*3*w);
cdupaty 0:d974bcee4f69 6876
cdupaty 0:d974bcee4f69 6877 printf("--> retry\n");
cdupaty 0:d974bcee4f69 6878 }
cdupaty 0:d974bcee4f69 6879
cdupaty 0:d974bcee4f69 6880 } while (e);
cdupaty 0:d974bcee4f69 6881
cdupaty 0:d974bcee4f69 6882 // CAD is OK, but need to check RSSI
cdupaty 0:d974bcee4f69 6883 if (_RSSIonSend) {
cdupaty 0:d974bcee4f69 6884
cdupaty 0:d974bcee4f69 6885 e=getRSSI();
cdupaty 0:d974bcee4f69 6886
cdupaty 0:d974bcee4f69 6887 uint8_t rssi_retry_count=10;
cdupaty 0:d974bcee4f69 6888
cdupaty 0:d974bcee4f69 6889 if (!e) {
cdupaty 0:d974bcee4f69 6890
cdupaty 0:d974bcee4f69 6891 printf("--> RSSI %d\n",_RSSI);
cdupaty 0:d974bcee4f69 6892 //Serial.print(_RSSI);
cdupaty 0:d974bcee4f69 6893 printf("\n");
cdupaty 0:d974bcee4f69 6894
cdupaty 0:d974bcee4f69 6895 while (_RSSI > -90 && rssi_retry_count) {
cdupaty 0:d974bcee4f69 6896
cdupaty 0:d974bcee4f69 6897 wait_ms(1);
cdupaty 0:d974bcee4f69 6898 getRSSI();
cdupaty 0:d974bcee4f69 6899 printf("--> RSSI %d\n",_RSSI);
cdupaty 0:d974bcee4f69 6900 //Serial.print(_RSSI);
cdupaty 0:d974bcee4f69 6901 printf("\n");
cdupaty 0:d974bcee4f69 6902 rssi_retry_count--;
cdupaty 0:d974bcee4f69 6903 }
cdupaty 0:d974bcee4f69 6904 }
cdupaty 0:d974bcee4f69 6905 else
cdupaty 0:d974bcee4f69 6906 printf("--> RSSI error\n");
cdupaty 0:d974bcee4f69 6907
cdupaty 0:d974bcee4f69 6908 if (!rssi_retry_count)
cdupaty 0:d974bcee4f69 6909 carrierSenseRetry=true;
cdupaty 0:d974bcee4f69 6910 else
cdupaty 0:d974bcee4f69 6911 carrierSenseRetry=false;
cdupaty 0:d974bcee4f69 6912 }
cdupaty 0:d974bcee4f69 6913
cdupaty 0:d974bcee4f69 6914 } while (carrierSenseRetry);
cdupaty 0:d974bcee4f69 6915 }
cdupaty 0:d974bcee4f69 6916 }
cdupaty 0:d974bcee4f69 6917
cdupaty 0:d974bcee4f69 6918 /*
cdupaty 0:d974bcee4f69 6919 Function: Indicates the CR within the module is configured.
cdupaty 0:d974bcee4f69 6920 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6921 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6922 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6923 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6924 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 6925 */
cdupaty 0:d974bcee4f69 6926 int8_t SX1272::getSyncWord()
cdupaty 0:d974bcee4f69 6927 {
cdupaty 0:d974bcee4f69 6928 int8_t state = 2;
cdupaty 0:d974bcee4f69 6929
cdupaty 0:d974bcee4f69 6930 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6931 printf("\n");
cdupaty 0:d974bcee4f69 6932 printf("Starting 'getSyncWord'\n");
cdupaty 0:d974bcee4f69 6933 #endif
cdupaty 0:d974bcee4f69 6934
cdupaty 0:d974bcee4f69 6935 if( _modem == FSK )
cdupaty 0:d974bcee4f69 6936 {
cdupaty 0:d974bcee4f69 6937 state = -1; // sync word is not available in FSK mode
cdupaty 0:d974bcee4f69 6938 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6939 printf("** FSK mode hasn't sync word **");
cdupaty 0:d974bcee4f69 6940 printf("\n");
cdupaty 0:d974bcee4f69 6941 #endif
cdupaty 0:d974bcee4f69 6942 }
cdupaty 0:d974bcee4f69 6943 else
cdupaty 0:d974bcee4f69 6944 {
cdupaty 0:d974bcee4f69 6945 _syncWord = readRegister(REG_SYNC_WORD);
cdupaty 0:d974bcee4f69 6946
cdupaty 0:d974bcee4f69 6947 state = 0;
cdupaty 0:d974bcee4f69 6948
cdupaty 0:d974bcee4f69 6949 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6950 printf("## Sync word is %X ",_syncWord);
cdupaty 0:d974bcee4f69 6951 // Serial.print(_syncWord, HEX);
cdupaty 0:d974bcee4f69 6952 printf(" ##");
cdupaty 0:d974bcee4f69 6953 printf("\n");
cdupaty 0:d974bcee4f69 6954 #endif
cdupaty 0:d974bcee4f69 6955 }
cdupaty 0:d974bcee4f69 6956 return state;
cdupaty 0:d974bcee4f69 6957 }
cdupaty 0:d974bcee4f69 6958
cdupaty 0:d974bcee4f69 6959 /*
cdupaty 0:d974bcee4f69 6960 Function: Sets the sync word in the module.
cdupaty 0:d974bcee4f69 6961 Returns: Integer that determines if there has been any error
cdupaty 0:d974bcee4f69 6962 state = 2 --> The command has not been executed
cdupaty 0:d974bcee4f69 6963 state = 1 --> There has been an error while executing the command
cdupaty 0:d974bcee4f69 6964 state = 0 --> The command has been executed with no errors
cdupaty 0:d974bcee4f69 6965 state = -1 --> Forbidden command for this protocol
cdupaty 0:d974bcee4f69 6966 Parameters:
cdupaty 0:d974bcee4f69 6967 cod: sw is sync word value to set in LoRa modem configuration.
cdupaty 0:d974bcee4f69 6968 */
cdupaty 0:d974bcee4f69 6969 int8_t SX1272::setSyncWord(uint8_t sw)
cdupaty 0:d974bcee4f69 6970 {
cdupaty 0:d974bcee4f69 6971 byte st0;
cdupaty 0:d974bcee4f69 6972 int8_t state = 2;
cdupaty 0:d974bcee4f69 6973 byte config1;
cdupaty 0:d974bcee4f69 6974
cdupaty 0:d974bcee4f69 6975 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6976 printf("\n");
cdupaty 0:d974bcee4f69 6977 printf("Starting 'setSyncWord'\n");
cdupaty 0:d974bcee4f69 6978 #endif
cdupaty 0:d974bcee4f69 6979
cdupaty 0:d974bcee4f69 6980 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 6981
cdupaty 0:d974bcee4f69 6982 if( _modem == FSK )
cdupaty 0:d974bcee4f69 6983 {
cdupaty 0:d974bcee4f69 6984 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 6985 printf("## Notice that FSK hasn't sync word parameter, ");
cdupaty 0:d974bcee4f69 6986 printf("so you are configuring it in LoRa mode ##");
cdupaty 0:d974bcee4f69 6987 #endif
cdupaty 0:d974bcee4f69 6988 state = setLORA();
cdupaty 0:d974bcee4f69 6989 }
cdupaty 0:d974bcee4f69 6990 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Set Standby mode to write in registers
cdupaty 0:d974bcee4f69 6991
cdupaty 0:d974bcee4f69 6992 writeRegister(REG_SYNC_WORD, sw);
cdupaty 0:d974bcee4f69 6993
cdupaty 0:d974bcee4f69 6994 wait_ms(100);
cdupaty 0:d974bcee4f69 6995
cdupaty 0:d974bcee4f69 6996 config1 = readRegister(REG_SYNC_WORD);
cdupaty 0:d974bcee4f69 6997
cdupaty 0:d974bcee4f69 6998 if (config1==sw) {
cdupaty 0:d974bcee4f69 6999 state=0;
cdupaty 0:d974bcee4f69 7000 _syncWord = sw;
cdupaty 0:d974bcee4f69 7001 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 7002 printf("## Sync Word %X ",sw);
cdupaty 0:d974bcee4f69 7003 // Serial.print(sw, HEX);
cdupaty 0:d974bcee4f69 7004 printf(" has been successfully set ##");
cdupaty 0:d974bcee4f69 7005 printf("\n");
cdupaty 0:d974bcee4f69 7006 #endif
cdupaty 0:d974bcee4f69 7007 }
cdupaty 0:d974bcee4f69 7008 else {
cdupaty 0:d974bcee4f69 7009 state=1;
cdupaty 0:d974bcee4f69 7010 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 7011 printf("** There has been an error while configuring Sync Word parameter **");
cdupaty 0:d974bcee4f69 7012 printf("\n");
cdupaty 0:d974bcee4f69 7013 #endif
cdupaty 0:d974bcee4f69 7014 }
cdupaty 0:d974bcee4f69 7015
cdupaty 0:d974bcee4f69 7016 writeRegister(REG_OP_MODE,st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 7017 wait_ms(100);
cdupaty 0:d974bcee4f69 7018 return state;
cdupaty 0:d974bcee4f69 7019 }
cdupaty 0:d974bcee4f69 7020
cdupaty 0:d974bcee4f69 7021
cdupaty 0:d974bcee4f69 7022 int8_t SX1272::setSleepMode() {
cdupaty 0:d974bcee4f69 7023
cdupaty 0:d974bcee4f69 7024 int8_t state = 2;
cdupaty 0:d974bcee4f69 7025 byte value;
cdupaty 0:d974bcee4f69 7026
cdupaty 0:d974bcee4f69 7027 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 7028 // proposed by escyes
cdupaty 0:d974bcee4f69 7029 // https://github.com/CongducPham/LowCostLoRaGw/issues/53#issuecomment-289237532
cdupaty 0:d974bcee4f69 7030 //
cdupaty 0:d974bcee4f69 7031 // inserted to avoid REG_OP_MODE stay = 0x40 (no sleep mode)
cdupaty 0:d974bcee4f69 7032 wait_ms(100);
cdupaty 0:d974bcee4f69 7033 writeRegister(REG_OP_MODE, LORA_SLEEP_MODE); // LoRa sleep mode
cdupaty 0:d974bcee4f69 7034
cdupaty 0:d974bcee4f69 7035 //delay(50);
cdupaty 0:d974bcee4f69 7036
cdupaty 0:d974bcee4f69 7037 value = readRegister(REG_OP_MODE);
cdupaty 0:d974bcee4f69 7038
cdupaty 0:d974bcee4f69 7039 //printf("## REG_OP_MODE 0x");
cdupaty 0:d974bcee4f69 7040 //Serial.println(value, HEX);
cdupaty 0:d974bcee4f69 7041
cdupaty 0:d974bcee4f69 7042 if (value == LORA_SLEEP_MODE)
cdupaty 0:d974bcee4f69 7043 state=0;
cdupaty 0:d974bcee4f69 7044 else
cdupaty 0:d974bcee4f69 7045 state=1;
cdupaty 0:d974bcee4f69 7046
cdupaty 0:d974bcee4f69 7047 return state;
cdupaty 0:d974bcee4f69 7048 }
cdupaty 0:d974bcee4f69 7049
cdupaty 0:d974bcee4f69 7050 int8_t SX1272::setPowerDBM(uint8_t dbm) {
cdupaty 0:d974bcee4f69 7051 byte st0;
cdupaty 0:d974bcee4f69 7052 int8_t state = 2;
cdupaty 0:d974bcee4f69 7053 byte value = 0x00;
cdupaty 0:d974bcee4f69 7054
cdupaty 0:d974bcee4f69 7055 byte RegPaDacReg=(_board==SX1272Chip)?0x5A:0x4D;
cdupaty 0:d974bcee4f69 7056
cdupaty 0:d974bcee4f69 7057 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 7058 printf("\n");
cdupaty 0:d974bcee4f69 7059 printf("Starting 'setPowerDBM'\n");
cdupaty 0:d974bcee4f69 7060 #endif
cdupaty 0:d974bcee4f69 7061
cdupaty 0:d974bcee4f69 7062 st0 = readRegister(REG_OP_MODE); // Save the previous status
cdupaty 0:d974bcee4f69 7063 if( _modem == LORA )
cdupaty 0:d974bcee4f69 7064 { // LoRa Stdby mode to write in registers
cdupaty 0:d974bcee4f69 7065 writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
cdupaty 0:d974bcee4f69 7066 }
cdupaty 0:d974bcee4f69 7067 else
cdupaty 0:d974bcee4f69 7068 { // FSK Stdby mode to write in registers
cdupaty 0:d974bcee4f69 7069 writeRegister(REG_OP_MODE, FSK_STANDBY_MODE);
cdupaty 0:d974bcee4f69 7070 }
cdupaty 0:d974bcee4f69 7071
cdupaty 0:d974bcee4f69 7072 if (dbm == 20) {
cdupaty 0:d974bcee4f69 7073 return setPower('X');
cdupaty 0:d974bcee4f69 7074 }
cdupaty 0:d974bcee4f69 7075
cdupaty 0:d974bcee4f69 7076 if (dbm > 14)
cdupaty 0:d974bcee4f69 7077 return state;
cdupaty 0:d974bcee4f69 7078
cdupaty 0:d974bcee4f69 7079 // disable high power output in all other cases
cdupaty 0:d974bcee4f69 7080 writeRegister(RegPaDacReg, 0x84);
cdupaty 0:d974bcee4f69 7081
cdupaty 0:d974bcee4f69 7082 if (dbm > 10)
cdupaty 0:d974bcee4f69 7083 // set RegOcp for OcpOn and OcpTrim
cdupaty 0:d974bcee4f69 7084 // 130mA
cdupaty 0:d974bcee4f69 7085 setMaxCurrent(0x10);
cdupaty 0:d974bcee4f69 7086 else
cdupaty 0:d974bcee4f69 7087 // 100mA
cdupaty 0:d974bcee4f69 7088 setMaxCurrent(0x0B);
cdupaty 0:d974bcee4f69 7089
cdupaty 0:d974bcee4f69 7090 if (_board==SX1272Chip) {
cdupaty 0:d974bcee4f69 7091 // Pout = -1 + _power[3:0] on RFO
cdupaty 0:d974bcee4f69 7092 // Pout = 2 + _power[3:0] on PA_BOOST
cdupaty 0:d974bcee4f69 7093 if (_needPABOOST) {
cdupaty 0:d974bcee4f69 7094 value = dbm - 2;
cdupaty 0:d974bcee4f69 7095 // we set the PA_BOOST pin
cdupaty 0:d974bcee4f69 7096 value = value & 0B10000000;
cdupaty 0:d974bcee4f69 7097 }
cdupaty 0:d974bcee4f69 7098 else
cdupaty 0:d974bcee4f69 7099 value = dbm + 1;
cdupaty 0:d974bcee4f69 7100
cdupaty 0:d974bcee4f69 7101 writeRegister(REG_PA_CONFIG, value); // Setting output power value
cdupaty 0:d974bcee4f69 7102 }
cdupaty 0:d974bcee4f69 7103 else {
cdupaty 0:d974bcee4f69 7104 // for the SX1276
cdupaty 0:d974bcee4f69 7105 uint8_t pmax=15;
cdupaty 0:d974bcee4f69 7106
cdupaty 0:d974bcee4f69 7107 // then Pout = Pmax-(15-_power[3:0]) if PaSelect=0 (RFO pin for +14dBm)
cdupaty 0:d974bcee4f69 7108 // so L=3dBm; H=7dBm; M=15dBm (but should be limited to 14dBm by RFO pin)
cdupaty 0:d974bcee4f69 7109
cdupaty 0:d974bcee4f69 7110 // and Pout = 17-(15-_power[3:0]) if PaSelect=1 (PA_BOOST pin for +14dBm)
cdupaty 0:d974bcee4f69 7111 // so x= 14dBm (PA);
cdupaty 0:d974bcee4f69 7112 // when p=='X' for 20dBm, value is 0x0F and RegPaDacReg=0x87 so 20dBm is enabled
cdupaty 0:d974bcee4f69 7113
cdupaty 0:d974bcee4f69 7114 if (_needPABOOST) {
cdupaty 0:d974bcee4f69 7115 value = dbm - 17 + 15;
cdupaty 0:d974bcee4f69 7116 // we set the PA_BOOST pin
cdupaty 0:d974bcee4f69 7117 value = value & 0B10000000;
cdupaty 0:d974bcee4f69 7118 }
cdupaty 0:d974bcee4f69 7119 else
cdupaty 0:d974bcee4f69 7120 value = dbm - pmax + 15;
cdupaty 0:d974bcee4f69 7121
cdupaty 0:d974bcee4f69 7122 // set MaxPower to 7 -> Pmax=10.8+0.6*MaxPower [dBm] = 15
cdupaty 0:d974bcee4f69 7123 value = value & 0B01110000;
cdupaty 0:d974bcee4f69 7124
cdupaty 0:d974bcee4f69 7125 writeRegister(REG_PA_CONFIG, value);
cdupaty 0:d974bcee4f69 7126 }
cdupaty 0:d974bcee4f69 7127
cdupaty 0:d974bcee4f69 7128 _power=value;
cdupaty 0:d974bcee4f69 7129
cdupaty 0:d974bcee4f69 7130 value = readRegister(REG_PA_CONFIG);
cdupaty 0:d974bcee4f69 7131
cdupaty 0:d974bcee4f69 7132 if( value == _power )
cdupaty 0:d974bcee4f69 7133 {
cdupaty 0:d974bcee4f69 7134 state = 0;
cdupaty 0:d974bcee4f69 7135 #if (SX1272_debug_mode > 1)
cdupaty 0:d974bcee4f69 7136 printf("## Output power has been successfully set ##");
cdupaty 0:d974bcee4f69 7137 printf("\n");
cdupaty 0:d974bcee4f69 7138 #endif
cdupaty 0:d974bcee4f69 7139 }
cdupaty 0:d974bcee4f69 7140 else
cdupaty 0:d974bcee4f69 7141 {
cdupaty 0:d974bcee4f69 7142 state = 1;
cdupaty 0:d974bcee4f69 7143 }
cdupaty 0:d974bcee4f69 7144
cdupaty 0:d974bcee4f69 7145 writeRegister(REG_OP_MODE, st0); // Getting back to previous status
cdupaty 0:d974bcee4f69 7146 wait_ms(100);
cdupaty 0:d974bcee4f69 7147 return state;
cdupaty 0:d974bcee4f69 7148 }
cdupaty 0:d974bcee4f69 7149
cdupaty 0:d974bcee4f69 7150 long SX1272::limitToA() {
cdupaty 0:d974bcee4f69 7151
cdupaty 0:d974bcee4f69 7152 // first time we set limitToA?
cdupaty 0:d974bcee4f69 7153 // in this design, once you set _limitToA to true
cdupaty 0:d974bcee4f69 7154 // it is not possible to set it back to false
cdupaty 0:d974bcee4f69 7155 if (_limitToA==false) {
cdupaty 0:d974bcee4f69 7156 _startToAcycle=millis();
cdupaty 0:d974bcee4f69 7157 _remainingToA=MAX_DUTY_CYCLE_PER_HOUR;
cdupaty 0:d974bcee4f69 7158 // we are handling millis() rollover by calculating the end of cycle time
cdupaty 0:d974bcee4f69 7159 _endToAcycle=_startToAcycle+DUTYCYCLE_DURATION;
cdupaty 0:d974bcee4f69 7160 }
cdupaty 0:d974bcee4f69 7161
cdupaty 0:d974bcee4f69 7162 _limitToA=true;
cdupaty 0:d974bcee4f69 7163 return getRemainingToA();
cdupaty 0:d974bcee4f69 7164 }
cdupaty 0:d974bcee4f69 7165
cdupaty 0:d974bcee4f69 7166 long SX1272::getRemainingToA() {
cdupaty 0:d974bcee4f69 7167
cdupaty 0:d974bcee4f69 7168 if (_limitToA==false)
cdupaty 0:d974bcee4f69 7169 return MAX_DUTY_CYCLE_PER_HOUR;
cdupaty 0:d974bcee4f69 7170
cdupaty 0:d974bcee4f69 7171 // we compare to the end of cycle so that millis() rollover is taken into account
cdupaty 0:d974bcee4f69 7172 // using unsigned long modulo operation
cdupaty 0:d974bcee4f69 7173 if ( (millis() > _endToAcycle ) ) {
cdupaty 0:d974bcee4f69 7174 _startToAcycle=_endToAcycle;
cdupaty 0:d974bcee4f69 7175 _remainingToA=MAX_DUTY_CYCLE_PER_HOUR;
cdupaty 0:d974bcee4f69 7176 _endToAcycle=_startToAcycle+DUTYCYCLE_DURATION;
cdupaty 0:d974bcee4f69 7177
cdupaty 0:d974bcee4f69 7178 printf("## new cycle for ToA ##");
cdupaty 0:d974bcee4f69 7179 printf("cycle begins at %d\n",_startToAcycle);
cdupaty 0:d974bcee4f69 7180 // Serial.print(_startToAcycle);
cdupaty 0:d974bcee4f69 7181 printf(" cycle ends at %d\n",_endToAcycle);
cdupaty 0:d974bcee4f69 7182 // Serial.print(_endToAcycle);
cdupaty 0:d974bcee4f69 7183 printf(" remaining ToA is %d\n",_remainingToA);
cdupaty 0:d974bcee4f69 7184 // Serial.print(_remainingToA);
cdupaty 0:d974bcee4f69 7185 printf("\n");
cdupaty 0:d974bcee4f69 7186 }
cdupaty 0:d974bcee4f69 7187
cdupaty 0:d974bcee4f69 7188 return _remainingToA;
cdupaty 0:d974bcee4f69 7189 }
cdupaty 0:d974bcee4f69 7190
cdupaty 0:d974bcee4f69 7191 long SX1272::removeToA(uint16_t toa) {
cdupaty 0:d974bcee4f69 7192
cdupaty 0:d974bcee4f69 7193 // first, update _remainingToA
cdupaty 0:d974bcee4f69 7194 getRemainingToA();
cdupaty 0:d974bcee4f69 7195
cdupaty 0:d974bcee4f69 7196 if (_limitToA) {
cdupaty 0:d974bcee4f69 7197 _remainingToA-=toa;
cdupaty 0:d974bcee4f69 7198 }
cdupaty 0:d974bcee4f69 7199
cdupaty 0:d974bcee4f69 7200 return _remainingToA;
cdupaty 0:d974bcee4f69 7201 }
cdupaty 0:d974bcee4f69 7202
cdupaty 0:d974bcee4f69 7203 SX1272 sx1272 = SX1272();