p2p with rssi value

Dependents:   Lora_SX1272_serial_apr29-rssi

Committer:
cdupaty
Date:
Sun Nov 05 17:02:01 2017 +0000
Revision:
1:5d57c6a92509
Parent:
0:d974bcee4f69
Child:
2:a5a72d30cb18
version OK

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