p2p with rssi value

Dependents:   Lora_SX1272_serial_apr29-rssi

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