2018-04-05 12:10:42 -04:00
|
|
|
// Copyright (c) Sandeep Mistry. All rights reserved.
|
2020-05-21 06:41:39 -04:00
|
|
|
// Licensed under the MIT license.
|
|
|
|
|
2024-10-11 10:39:38 -04:00
|
|
|
// Modifications and additions copyright 2024 by Mark Qvist
|
2020-05-21 06:41:39 -04:00
|
|
|
// Obviously still under the MIT license.
|
2018-04-05 12:10:42 -04:00
|
|
|
|
2024-02-09 15:46:39 -05:00
|
|
|
#ifndef SX1276_H
|
|
|
|
#define SX1276_H
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <SPI.h>
|
2024-01-19 05:08:55 -05:00
|
|
|
#include "Modem.h"
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
#define LORA_DEFAULT_SS_PIN 10
|
|
|
|
#define LORA_DEFAULT_RESET_PIN 9
|
|
|
|
#define LORA_DEFAULT_DIO0_PIN 2
|
2024-01-19 05:08:55 -05:00
|
|
|
#define LORA_DEFAULT_BUSY_PIN -1
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
#define PA_OUTPUT_RFO_PIN 0
|
|
|
|
#define PA_OUTPUT_PA_BOOST_PIN 1
|
|
|
|
|
2019-11-07 09:26:21 -05:00
|
|
|
#define RSSI_OFFSET 157
|
|
|
|
|
2024-02-09 15:46:39 -05:00
|
|
|
class sx127x : public Stream {
|
2018-04-05 12:10:42 -04:00
|
|
|
public:
|
2024-02-09 15:46:39 -05:00
|
|
|
sx127x();
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
int begin(long frequency);
|
|
|
|
void end();
|
|
|
|
|
|
|
|
int beginPacket(int implicitHeader = false);
|
|
|
|
int endPacket();
|
|
|
|
|
|
|
|
int parsePacket(int size = 0);
|
|
|
|
int packetRssi();
|
2024-10-11 10:29:17 -04:00
|
|
|
int packetRssi(uint8_t pkt_snr_raw);
|
2022-10-28 18:52:51 -04:00
|
|
|
int currentRssi();
|
2018-06-27 08:08:16 -04:00
|
|
|
uint8_t packetRssiRaw();
|
2022-10-28 18:52:51 -04:00
|
|
|
uint8_t currentRssiRaw();
|
2020-05-21 06:41:39 -04:00
|
|
|
uint8_t packetSnrRaw();
|
2018-04-05 12:10:42 -04:00
|
|
|
float packetSnr();
|
|
|
|
long packetFrequencyError();
|
|
|
|
|
|
|
|
// from Print
|
|
|
|
virtual size_t write(uint8_t byte);
|
|
|
|
virtual size_t write(const uint8_t *buffer, size_t size);
|
|
|
|
|
|
|
|
// from Stream
|
|
|
|
virtual int available();
|
|
|
|
virtual int read();
|
|
|
|
virtual int peek();
|
|
|
|
virtual void flush();
|
|
|
|
|
|
|
|
void onReceive(void(*callback)(int));
|
|
|
|
|
|
|
|
void receive(int size = 0);
|
2024-02-13 08:19:14 -05:00
|
|
|
void standby();
|
2018-04-05 12:10:42 -04:00
|
|
|
void sleep();
|
|
|
|
|
2023-01-07 10:35:07 -05:00
|
|
|
bool preInit();
|
|
|
|
uint8_t getTxPower();
|
2018-04-05 12:10:42 -04:00
|
|
|
void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN);
|
2018-06-20 10:32:30 -04:00
|
|
|
uint32_t getFrequency();
|
2024-02-09 15:46:39 -05:00
|
|
|
void setFrequency(unsigned long frequency);
|
2018-04-05 12:10:42 -04:00
|
|
|
void setSpreadingFactor(int sf);
|
|
|
|
long getSignalBandwidth();
|
|
|
|
void setSignalBandwidth(long sbw);
|
|
|
|
void setCodingRate4(int denominator);
|
|
|
|
void setPreambleLength(long length);
|
2024-02-11 12:27:47 -05:00
|
|
|
void setSyncWord(uint8_t sw);
|
2018-04-26 09:52:43 -04:00
|
|
|
uint8_t modemStatus();
|
2018-04-05 12:10:42 -04:00
|
|
|
void enableCrc();
|
|
|
|
void disableCrc();
|
2023-06-07 14:49:26 -04:00
|
|
|
void enableTCXO();
|
|
|
|
void disableTCXO();
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
byte random();
|
|
|
|
|
2024-02-09 15:46:39 -05:00
|
|
|
void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN, int busy = LORA_DEFAULT_BUSY_PIN);
|
2018-04-05 12:10:42 -04:00
|
|
|
void setSPIFrequency(uint32_t frequency);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void explicitHeaderMode();
|
|
|
|
void implicitHeaderMode();
|
|
|
|
|
|
|
|
void handleDio0Rise();
|
|
|
|
|
2024-02-09 15:46:39 -05:00
|
|
|
uint8_t readRegister(uint8_t address);
|
|
|
|
void writeRegister(uint8_t address, uint8_t value);
|
|
|
|
uint8_t singleTransfer(uint8_t address, uint8_t value);
|
2018-04-05 12:10:42 -04:00
|
|
|
|
|
|
|
static void onDio0Rise();
|
|
|
|
|
2019-10-14 17:16:30 -04:00
|
|
|
void handleLowDataRate();
|
2023-04-22 01:56:46 -04:00
|
|
|
void optimizeModemSensitivity();
|
2019-10-14 17:16:30 -04:00
|
|
|
|
2018-04-05 12:10:42 -04:00
|
|
|
private:
|
|
|
|
SPISettings _spiSettings;
|
|
|
|
int _ss;
|
|
|
|
int _reset;
|
|
|
|
int _dio0;
|
2024-01-19 05:08:55 -05:00
|
|
|
int _busy;
|
2018-04-05 12:10:42 -04:00
|
|
|
long _frequency;
|
|
|
|
int _packetIndex;
|
|
|
|
int _implicitHeaderMode;
|
2024-02-09 15:46:39 -05:00
|
|
|
bool _preinit_done;
|
2018-04-05 12:10:42 -04:00
|
|
|
void (*_onReceive)(int);
|
|
|
|
};
|
|
|
|
|
2024-02-09 15:46:39 -05:00
|
|
|
extern sx127x sx127x_modem;
|
2018-04-05 12:10:42 -04:00
|
|
|
|
2024-01-19 05:08:55 -05:00
|
|
|
#endif
|