RadioLib
Universal wireless communication library for Arduino
PhysicalLayer.h
1 #if !defined(_RADIOLIB_PHYSICAL_LAYER_H)
2 #define _RADIOLIB_PHYSICAL_LAYER_H
3 
4 #include "../../TypeDef.h"
5 #include "../../Module.h"
6 
7 // common IRQ flags
8 #define RADIOLIB_IRQ_TX_DONE 0x00
9 #define RADIOLIB_IRQ_RX_DONE 0x01
10 #define RADIOLIB_IRQ_PREAMBLE_DETECTED 0x02
11 #define RADIOLIB_IRQ_SYNC_WORD_VALID 0x03
12 #define RADIOLIB_IRQ_HEADER_VALID 0x04
13 #define RADIOLIB_IRQ_HEADER_ERR 0x05
14 #define RADIOLIB_IRQ_CRC_ERR 0x06
15 #define RADIOLIB_IRQ_CAD_DONE 0x07
16 #define RADIOLIB_IRQ_CAD_DETECTED 0x08
17 #define RADIOLIB_IRQ_TIMEOUT 0x09
18 
23 struct LoRaRate_t {
25  uint8_t spreadingFactor;
26 
28  float bandwidth;
29 
31  uint8_t codingRate;
32 };
33 
38 struct FSKRate_t {
40  float bitRate;
41 
43  float freqDev;
44 };
45 
50 union DataRate_t {
53 
56 };
57 
67  public:
68 
69  // constructor
70 
76  PhysicalLayer(float step, size_t maxLen);
77 
78  // basic methods
79 
80  #if defined(RADIOLIB_BUILD_ARDUINO)
87  int16_t transmit(__FlashStringHelper* fstr, uint8_t addr = 0);
88 
95  int16_t transmit(String& str, uint8_t addr = 0);
96  #endif
97 
104  int16_t transmit(const char* str, uint8_t addr = 0);
105 
113  virtual int16_t transmit(const uint8_t* data, size_t len, uint8_t addr = 0);
114 
115  #if defined(RADIOLIB_BUILD_ARDUINO)
122  int16_t receive(String& str, size_t len = 0);
123  #endif
124 
129  virtual int16_t sleep();
130 
135  virtual int16_t standby();
136 
141  virtual int16_t standby(uint8_t mode);
142 
147  virtual int16_t startReceive();
148 
159  virtual int16_t startReceive(uint32_t timeout, uint32_t irqFlags, uint32_t irqMask, size_t len);
160 
167  virtual int16_t receive(uint8_t* data, size_t len);
168 
169  #if defined(RADIOLIB_BUILD_ARDUINO)
177  int16_t startTransmit(String& str, uint8_t addr = 0);
178  #endif
179 
187  int16_t startTransmit(const char* str, uint8_t addr = 0);
188 
196  virtual int16_t startTransmit(const uint8_t* data, size_t len, uint8_t addr = 0);
197 
202  virtual int16_t finishTransmit();
203 
204  #if defined(RADIOLIB_BUILD_ARDUINO)
212  int16_t readData(String& str, size_t len = 0);
213  #endif
214 
222  virtual int16_t readData(uint8_t* data, size_t len);
223 
230  virtual int16_t transmitDirect(uint32_t frf = 0);
231 
237  virtual int16_t receiveDirect();
238 
239  // configuration methods
240 
246  virtual int16_t setFrequency(float freq);
247 
253  virtual int16_t setBitRate(float br);
254 
261  virtual int16_t setFrequencyDeviation(float freqDev);
262 
268  virtual int16_t setDataShaping(uint8_t sh);
269 
275  virtual int16_t setEncoding(uint8_t encoding);
276 
282  virtual int16_t invertIQ(bool enable);
283 
289  virtual int16_t setOutputPower(int8_t power);
290 
297  virtual int16_t checkOutputPower(int8_t power, int8_t* clipped);
298 
305  virtual int16_t setSyncWord(uint8_t* sync, size_t len);
306 
312  virtual int16_t setPreambleLength(size_t len);
313 
319  virtual int16_t setDataRate(DataRate_t dr);
320 
326  virtual int16_t checkDataRate(DataRate_t dr);
327 
332  float getFreqStep() const;
333 
339  virtual size_t getPacketLength(bool update = true);
340 
345  virtual float getRSSI();
346 
351  virtual float getSNR();
352 
358  virtual RadioLibTime_t getTimeOnAir(size_t len);
359 
367 
374  virtual int16_t irqRxDoneRxTimeout(uint32_t &irqFlags, uint32_t &irqMask);
375 
380  virtual int16_t checkIrq(uint8_t irq);
381 
387  virtual int16_t startChannelScan();
388 
393  virtual int16_t getChannelScanResult();
394 
401  virtual int16_t scanChannel();
402 
408  int32_t random(int32_t max);
409 
416  int32_t random(int32_t min, int32_t max);
417 
422  virtual uint8_t randomByte();
423 
429  int16_t startDirect();
430 
431  #if !RADIOLIB_EXCLUDE_DIRECT_RECEIVE
438  int16_t setDirectSyncWord(uint32_t syncWord, uint8_t len);
439 
445  virtual void setDirectAction(void (*func)(void));
446 
451  virtual void readBit(uint32_t pin);
452 
457  int16_t available();
458 
462  void dropSync();
463 
470  uint8_t read(bool drop = true);
471  #endif
472 
479  virtual int16_t setDIOMapping(uint32_t pin, uint32_t value);
480 
485  virtual void setPacketReceivedAction(void (*func)(void));
486 
490  virtual void clearPacketReceivedAction();
491 
496  virtual void setPacketSentAction(void (*func)(void));
497 
501  virtual void clearPacketSentAction();
502 
507  virtual void setChannelScanAction(void (*func)(void));
508 
512  virtual void clearChannelScanAction();
513 
514  #if RADIOLIB_INTERRUPT_TIMING
515 
521  void setInterruptSetup(void (*func)(uint32_t));
522 
527  void setTimerFlag();
528 
529  #endif
530 
531 #if !RADIOLIB_GODMODE
532  protected:
533 #endif
534 #if !RADIOLIB_EXCLUDE_DIRECT_RECEIVE
535  void updateDirectBuffer(uint8_t bit);
536 #endif
537 
538 #if !RADIOLIB_GODMODE
539  private:
540 #endif
541  float freqStep;
542  size_t maxPacketLength;
543 
544  #if !RADIOLIB_EXCLUDE_DIRECT_RECEIVE
545  uint8_t bufferBitPos = 0;
546  uint8_t bufferWritePos = 0;
547  uint8_t bufferReadPos = 0;
548  uint8_t buffer[RADIOLIB_STATIC_ARRAY_SIZE] = { 0 };
549  uint32_t syncBuffer = 0;
550  uint32_t directSyncWord = 0;
551  uint8_t directSyncWordLen = 0;
552  uint32_t directSyncWordMask = 0;
553  bool gotSync = false;
554  #endif
555 
556  virtual Module* getMod() = 0;
557 
558  // allow specific classes access the private getMod method
559  friend class AFSKClient;
560  friend class RTTYClient;
561  friend class MorseClient;
562  friend class HellClient;
563  friend class SSTVClient;
564  friend class AX25Client;
565  friend class FSK4Client;
566  friend class PagerClient;
567  friend class BellClient;
568  friend class FT8Client;
569  friend class LoRaWANNode;
570 };
571 
572 #endif
Client for audio-based transmissions. Requires Arduino tone() function, and a module capable of direc...
Definition: AFSK.h:16
Client for AX25 communication.
Definition: AX25.h:233
Client for Bell modem communication. The public interface is the same as Arduino Serial.
Definition: BellModem.h:57
Client for FSK-4 communication. The public interface is the same as Arduino Serial.
Definition: FSK4.h:15
Client for Hellschreiber transmissions.
Definition: Hellschreiber.h:90
LoRaWAN-compatible node (class A device).
Definition: LoRaWAN.h:524
Implements all common low-level methods to control the wireless module. Every module class contains o...
Definition: Module.h:65
Client for Morse Code communication. The public interface is the same as Arduino Serial.
Definition: Morse.h:93
Client for Pager communication.
Definition: Pager.h:62
Provides common interface for protocols that run on LoRa/FSK modules, such as RTTY or LoRaWAN....
Definition: PhysicalLayer.h:66
virtual int16_t invertIQ(bool enable)
Set IQ inversion. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:249
virtual void clearChannelScanAction()
Clears interrupt service routine to call when a channel scan is finished.
Definition: PhysicalLayer.cpp:488
virtual int16_t checkOutputPower(int8_t power, int8_t *clipped)
Check if output power is configurable. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:259
virtual int16_t startReceive()
Sets module to received mode using its default configuration.
Definition: PhysicalLayer.cpp:131
virtual uint8_t randomByte()
Get one truly random byte from RSSI noise. Must be implemented in module class.
Definition: PhysicalLayer.cpp:363
virtual int16_t finishTransmit()
Clean up after transmission is done.
Definition: PhysicalLayer.cpp:160
virtual int16_t setFrequencyDeviation(float freqDev)
Sets FSK frequency deviation from carrier frequency. Only available in FSK mode. Must be implemented ...
Definition: PhysicalLayer.cpp:234
virtual size_t getPacketLength(bool update=true)
Query modem for the packet length of received payload. Must be implemented in module class.
Definition: PhysicalLayer.cpp:290
virtual void setPacketSentAction(void(*func)(void))
Sets interrupt service routine to call when a packet is sent.
Definition: PhysicalLayer.cpp:476
virtual int16_t setDIOMapping(uint32_t pin, uint32_t value)
Configure DIO pin mapping to get a given signal on a DIO pin (if available).
Definition: PhysicalLayer.cpp:462
virtual int16_t readData(uint8_t *data, size_t len)
Reads data that was received after calling startReceive method.
Definition: PhysicalLayer.cpp:209
void dropSync()
Forcefully drop synchronization.
Definition: PhysicalLayer.cpp:386
int16_t transmit(const char *str, uint8_t addr=0)
C-string transmit method.
Definition: PhysicalLayer.cpp:52
uint8_t read(bool drop=true)
Get data from direct mode buffer.
Definition: PhysicalLayer.cpp:393
virtual RadioLibTime_t getTimeOnAir(size_t len)
Get expected time-on-air for a given size of payload.
Definition: PhysicalLayer.cpp:303
virtual int16_t checkDataRate(DataRate_t dr)
Check the data rate can be configured by this module. Must be implemented in module class if the modu...
Definition: PhysicalLayer.cpp:281
virtual int16_t getChannelScanResult()
Read the channel scan result.
Definition: PhysicalLayer.cpp:328
virtual void clearPacketSentAction()
Clears interrupt service routine to call when a packet is sent.
Definition: PhysicalLayer.cpp:480
virtual int16_t setSyncWord(uint8_t *sync, size_t len)
Set sync word. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:265
virtual int16_t setDataRate(DataRate_t dr)
Set data. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:276
virtual void clearPacketReceivedAction()
Clears interrupt service routine to call when a packet is received.
Definition: PhysicalLayer.cpp:472
virtual RadioLibTime_t calculateRxTimeout(RadioLibTime_t timeoutUs)
Calculate the timeout value for this specific module / series (in number of symbols or units of time)...
Definition: PhysicalLayer.cpp:308
virtual int16_t setPreambleLength(size_t len)
Set preamble length. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:271
int16_t startDirect()
Configure module parameters for direct modes. Must be called prior to "ham" modes like RTTY or AX....
Definition: PhysicalLayer.cpp:367
virtual int16_t setFrequency(float freq)
Sets carrier frequency. Must be implemented in module class.
Definition: PhysicalLayer.cpp:224
int16_t setDirectSyncWord(uint32_t syncWord, uint8_t len)
Set sync word to be used to determine start of packet in direct reception mode.
Definition: PhysicalLayer.cpp:401
int16_t startTransmit(const char *str, uint8_t addr=0)
Interrupt-driven Arduino String transmit method. Unlike the standard transmit method,...
Definition: PhysicalLayer.cpp:149
float getFreqStep() const
Gets the module frequency step size that was set in constructor.
Definition: PhysicalLayer.cpp:286
virtual int16_t transmitDirect(uint32_t frf=0)
Enables direct transmission mode on pins DIO1 (clock) and DIO2 (data). Must be implemented in module ...
Definition: PhysicalLayer.cpp:215
virtual float getRSSI()
Gets RSSI (Recorded Signal Strength Indicator) of the last received packet.
Definition: PhysicalLayer.cpp:295
int16_t available()
Get the number of direct mode bytes currently available in buffer.
Definition: PhysicalLayer.cpp:382
virtual int16_t receive(uint8_t *data, size_t len)
Binary receive method. Must be implemented in module class.
Definition: PhysicalLayer.cpp:112
virtual int16_t scanChannel()
Check whether the current communication channel is free or occupied. Performs CAD for LoRa modules,...
Definition: PhysicalLayer.cpp:332
virtual void setDirectAction(void(*func)(void))
Set interrupt service routine function to call when data bit is received in direct mode....
Definition: PhysicalLayer.cpp:452
virtual int16_t setEncoding(uint8_t encoding)
Sets FSK data encoding. Only available in FSK mode. Must be implemented in module class.
Definition: PhysicalLayer.cpp:244
virtual int16_t standby()
Sets module to standby.
Definition: PhysicalLayer.cpp:122
int32_t random(int32_t max)
Get truly random number in range 0 - max.
Definition: PhysicalLayer.cpp:336
virtual void readBit(uint32_t pin)
Function to read and process data bit in direct reception mode. Must be implemented in module class.
Definition: PhysicalLayer.cpp:456
virtual int16_t setOutputPower(int8_t power)
Set output power. Must be implemented in module class if the module supports it.
Definition: PhysicalLayer.cpp:254
virtual int16_t receiveDirect()
Enables direct reception mode on pins DIO1 (clock) and DIO2 (data). Must be implemented in module cla...
Definition: PhysicalLayer.cpp:220
PhysicalLayer(float step, size_t maxLen)
Default constructor.
Definition: PhysicalLayer.cpp:4
virtual void setChannelScanAction(void(*func)(void))
Sets interrupt service routine to call when a channel scan is finished.
Definition: PhysicalLayer.cpp:484
virtual int16_t startChannelScan()
Interrupt-driven channel activity detection method. Interrupt will be activated when packet is detect...
Definition: PhysicalLayer.cpp:324
virtual int16_t setDataShaping(uint8_t sh)
Sets GFSK data shaping. Only available in FSK mode. Must be implemented in module class.
Definition: PhysicalLayer.cpp:239
virtual void setPacketReceivedAction(void(*func)(void))
Sets interrupt service routine to call when a packet is received.
Definition: PhysicalLayer.cpp:468
virtual int16_t setBitRate(float br)
Sets FSK bit rate. Only available in FSK mode. Must be implemented in module class.
Definition: PhysicalLayer.cpp:229
virtual float getSNR()
Gets SNR (Signal to Noise Ratio) of the last received packet. Only available for LoRa modem.
Definition: PhysicalLayer.cpp:299
virtual int16_t sleep()
Sets module to sleep.
Definition: PhysicalLayer.cpp:118
virtual int16_t irqRxDoneRxTimeout(uint32_t &irqFlags, uint32_t &irqMask)
Create the flags that make up RxDone and RxTimeout used for receiving downlinks.
Definition: PhysicalLayer.cpp:313
virtual int16_t checkIrq(uint8_t irq)
Check whether a specific IRQ bit is set (e.g. RxTimeout, CadDone).
Definition: PhysicalLayer.cpp:319
Client for RTTY communication. The public interface is the same as Arduino Serial.
Definition: RTTY.h:17
Client for SSTV transmissions.
Definition: SSTV.h:121
unsigned long RadioLibTime_t
Type used for durations in RadioLib.
Definition: TypeDef.h:616
Data rate structure interpretation in case FSK is used.
Definition: PhysicalLayer.h:38
float bitRate
FSK bit rate in kbps.
Definition: PhysicalLayer.h:40
float freqDev
FS frequency deviation in kHz.
Definition: PhysicalLayer.h:43
Data rate structure interpretation in case LoRa is used.
Definition: PhysicalLayer.h:23
uint8_t spreadingFactor
LoRa spreading factor.
Definition: PhysicalLayer.h:25
float bandwidth
LoRa bandwidth in kHz.
Definition: PhysicalLayer.h:28
uint8_t codingRate
LoRa coding rate.
Definition: PhysicalLayer.h:31
Common data rate structure.
Definition: PhysicalLayer.h:50
LoRaRate_t lora
Interpretation for LoRa modems.
Definition: PhysicalLayer.h:52
FSKRate_t fsk
Interpretation for FSK modems.
Definition: PhysicalLayer.h:55