p2p with rssi value

Dependents:   Lora_SX1272_serial_apr29-rssi

Committer:
marcoantonioara
Date:
Wed Nov 13 16:37:41 2019 +0000
Revision:
5:d5e647e6def9
Parent:
4:f77a79f4239a
Coragem Adaptation

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