Adafruit Tiny LoRa Library
TinyLoRa.h
Go to the documentation of this file.
1 
36 #ifndef TINY_LORA_H
37 #define TINY_LORA_H
38 
39 #include <Arduino.h>
40 #if defined(ARDUINO_ARCH_AVR)
41 #include <avr/pgmspace.h>
42 #elif defined(ARDUINO_ARCH_ESP32)
43 #include <pgmspace.h>
44 #endif
45 
46 // uncomment for debug output
47 // #define DEBUG
48 
50 typedef enum rfm_channels {
51  CH0,
52  CH1,
53  CH2,
54  CH3,
55  CH4,
56  CH5,
57  CH6,
58  CH7,
59  MULTI,
61 
63 typedef enum rfm_datarates {
64  SF7BW125,
65  SF7BW250,
66  SF8BW125,
67  SF9BW125,
68  SF10BW125,
69  SF11BW125,
70  SF12BW125,
72 
74 #if !defined(EU863) && !defined(AU915) && !defined(AS920)
75 #define US902
76 #endif
77 //#define EU863 ///< Used in Europe
78 //#define AU915 ///< Used in Australia
79 //#define AS920 ///< Used in Asia
80 
81 #define RFM9x_VER 0x12
82 
83 /* RFM Modes */
84 #define MODE_SLEEP 0x00
85 #define MODE_LORA 0x80
86 #define MODE_STDBY 0x01
87 #define MODE_TX 0x83
88 
89 /* RFM Registers */
90 #define REG_PA_CONFIG 0x09
91 #define REG_PA_DAC 0x4D
92 #define REG_PREAMBLE_MSB 0x20
93 #define REG_PREAMBLE_LSB 0x21
94 #define REG_FRF_MSB 0x06
95 #define REG_FRF_MID 0x07
96 #define REG_FRF_LSB 0x08
97 #define REG_FEI_LSB 0x1E
98 #define REG_FEI_MSB 0x1D
99 #define REG_MODEM_CONFIG 0x26
100 #define REG_VER 0x42
101 
102 /**************************************************************************/
106 /**************************************************************************/
107 class TinyLoRa {
108 public:
109  uint8_t txrandomNum;
110  uint16_t frameCounter;
111  void setChannel(rfm_channels_t channel);
112  void setDatarate(rfm_datarates_t datarate);
113  void setPower(int8_t Tx_Power = 17);
114  TinyLoRa(int8_t rfm_dio0, int8_t rfm_nss, int8_t rfm_rst);
115  bool begin(void);
116  void sendData(unsigned char *Data, unsigned char Data_Length,
117  unsigned int Frame_Counter_Tx, uint8_t Frame_Port = 1);
118 
119 private:
120  uint8_t randomNum;
121  int8_t _cs, _irq, _rst;
122  bool _isMultiChan;
123  unsigned char _rfmMSB, _rfmMID, _rfmLSB, _sf, _bw, _modemcfg;
124  static const unsigned char LoRa_Frequency[8][3];
125  static const unsigned char S_Table[16][16];
126  void RFM_Send_Package(unsigned char *RFM_Tx_Package,
127  unsigned char Package_Length);
128  void RFM_Write(unsigned char RFM_Address, unsigned char RFM_Data);
129  uint8_t RFM_Read(uint8_t RFM_Address);
130  void Encrypt_Payload(unsigned char *Data, unsigned char Data_Length,
131  unsigned int Frame_Counter, unsigned char Direction);
132  void Calculate_MIC(unsigned char *Data, unsigned char *Final_MIC,
133  unsigned char Data_Length, unsigned int Frame_Counter,
134  unsigned char Direction);
135  void Generate_Keys(unsigned char *K1, unsigned char *K2);
136  void Shift_Left(unsigned char *Data);
137  void XOR(unsigned char *New_Data, unsigned char *Old_Data);
138  void AES_Encrypt(unsigned char *Data, unsigned char *Key);
139  void AES_Add_Round_Key(unsigned char *Round_Key, unsigned char (*State)[4]);
140  unsigned char AES_Sub_Byte(unsigned char Byte);
141  void AES_Shift_Rows(unsigned char (*State)[4]);
142  void AES_Mix_Collums(unsigned char (*State)[4]);
143  void AES_Calculate_Round_Key(unsigned char Round, unsigned char *Round_Key);
144 };
145 
146 #endif
void setDatarate(rfm_datarates_t datarate)
Sets the RFM datarate.
Definition: TinyLoRa.cpp:198
rfm_datarates
Definition: TinyLoRa.h:63
uint8_t txrandomNum
random number for AES
Definition: TinyLoRa.h:109
rfm_channels
Definition: TinyLoRa.h:50
void sendData(unsigned char *Data, unsigned char Data_Length, unsigned int Frame_Counter_Tx, uint8_t Frame_Port=1)
Function to assemble and send a LoRaWAN package.
Definition: TinyLoRa.cpp:609
uint16_t frameCounter
frame counter
Definition: TinyLoRa.h:110
enum rfm_datarates rfm_datarates_t
TinyLoRa(int8_t rfm_dio0, int8_t rfm_nss, int8_t rfm_rst)
Instanciates a new TinyLoRa class, including assigning irq and cs pins to the RFM breakout...
Definition: TinyLoRa.cpp:323
void setChannel(rfm_channels_t channel)
Sets the RFM channel.
Definition: TinyLoRa.cpp:249
TinyLoRa Class.
Definition: TinyLoRa.h:107
void setPower(int8_t Tx_Power=17)
Sets the TX power.
Definition: TinyLoRa.cpp:426
bool begin(void)
Initializes the RFM, including configuring SPI, configuring the frameCounter and txrandomNum.
Definition: TinyLoRa.cpp:340
enum rfm_channels rfm_channels_t