RNode_Firmware/LoRa.h

149 lines
3.4 KiB
C
Raw Permalink Normal View History

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.
// Modifications and additions copyright 2018 by Mark Qvist
// Obviously still under the MIT license.
2018-04-05 12:10:42 -04:00
#ifndef LORA_H
#define LORA_H
2022-02-13 16:58:04 -05:00
#include "Platform.h"
#if LIBRARY_TYPE == LIBRARY_ARDUINO
#include <Arduino.h>
#include <SPI.h>
#elif LIBRARY_TYPE == LIBRARY_C
#include <cstdlib>
#include <cstdint>
2022-02-13 18:19:33 -05:00
#include <iostream>
// Arduino Stream is not available, but not actually needed.
class Stream {};
typedef unsigned char byte;
2022-02-13 18:40:30 -05:00
// Arduino SPI is not available, so make a Linux-ish version of SPISettings
2022-02-13 18:19:33 -05:00
#define MSBFIRST 0
2022-02-13 18:40:30 -05:00
#define LSBFIRST 1
#define SPI_MODE0 SPI_MODE_0
#define SPI_MODE1 SPI_MODE_1
#define SPI_MODE2 SPI_MODE_2
#define SPI_MODE3 SPI_MODE_3
2022-02-13 18:19:33 -05:00
class SPISettings {
public:
2022-02-13 18:40:30 -05:00
inline SPISettings(uint32_t frequency, byte bitness, byte mode) : frequency(frequency), bitness(bitness), mode(mode) {};
2022-02-13 18:19:33 -05:00
SPISettings& operator=(const SPISettings& other) = default;
2022-02-13 18:40:30 -05:00
uint32_t frequency;
byte bitness;
byte mode;
2022-02-13 18:19:33 -05:00
};
2022-02-13 16:58:04 -05:00
#endif
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
#define PA_OUTPUT_RFO_PIN 0
#define PA_OUTPUT_PA_BOOST_PIN 1
#define RSSI_OFFSET 157
2018-04-05 12:10:42 -04:00
class LoRaClass : public Stream {
public:
LoRaClass();
int begin(long frequency);
void end();
int beginPacket(int implicitHeader = false);
int endPacket();
int parsePacket(int size = 0);
int packetRssi();
2018-06-27 08:08:16 -04:00
uint8_t packetRssiRaw();
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();
2022-02-16 21:02:49 -05:00
void pollReceive();
2018-04-05 12:10:42 -04:00
void onReceive(void(*callback)(int));
void receive(int size = 0);
void idle();
void sleep();
void setTxPower(int level, int outputPin = PA_OUTPUT_PA_BOOST_PIN);
2018-06-20 10:32:30 -04:00
uint32_t getFrequency();
2018-04-05 12:10:42 -04:00
void setFrequency(long frequency);
void setSpreadingFactor(int sf);
long getSignalBandwidth();
void setSignalBandwidth(long sbw);
void setCodingRate4(int denominator);
void setPreambleLength(long length);
void setSyncWord(int sw);
2018-04-26 09:52:43 -04:00
uint8_t modemStatus();
2018-04-05 12:10:42 -04:00
void enableCrc();
void disableCrc();
// deprecated
void crc() { enableCrc(); }
void noCrc() { disableCrc(); }
byte random();
void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
void setSPIFrequency(uint32_t frequency);
2022-02-13 18:19:33 -05:00
#if LIBRARY_TYPE == LIBRARY_ARDUINO
2018-04-05 12:10:42 -04:00
void dumpRegisters(Stream& out);
2022-02-13 18:19:33 -05:00
#elif LIBRARY_TYPE == LIBRARY_C
void dumpRegisters(std::ostream& out);
#endif
2018-04-05 12:10:42 -04:00
private:
2022-02-21 13:48:53 -05:00
bool resetModem();
2018-04-05 12:10:42 -04:00
void explicitHeaderMode();
void implicitHeaderMode();
void handleDio0Rise();
2022-02-16 21:02:49 -05:00
void handleRx();
2018-04-05 12:10:42 -04:00
uint8_t readRegister(uint8_t address);
void writeRegister(uint8_t address, uint8_t value);
uint8_t singleTransfer(uint8_t address, uint8_t value);
static void onDio0Rise();
void handleLowDataRate();
2018-04-05 12:10:42 -04:00
private:
SPISettings _spiSettings;
int _ss;
int _reset;
int _dio0;
long _frequency;
int _packetIndex;
int _implicitHeaderMode;
void (*_onReceive)(int);
bool _spiBegun;
2022-02-13 18:19:33 -05:00
#if LIBRARY_TYPE == LIBRARY_C
int _fd;
#endif
2018-04-05 12:10:42 -04:00
};
extern LoRaClass LoRa;
2022-02-13 16:58:04 -05:00
#endif