RadioLib
Universal wireless communication library for Arduino
Hal.h
1 #if !defined(_RADIOLIB_HAL_H)
2 #define _RADIOLIB_HAL_H
3 
4 #include <stdint.h>
5 #include <stddef.h>
6 
7 #include "BuildOpt.h"
8 
9 #define RADIOLIB_EEPROM_TABLE_VERSION (0x0002)
10 
11 // list of persistent parameters
12 enum RADIOLIB_EEPROM_PARAMS {
13  RADIOLIB_EEPROM_TABLE_VERSION_ID, // table layout version
14  RADIOLIB_EEPROM_LORAWAN_CLASS_ID, // class A, B or C
15  RADIOLIB_EEPROM_LORAWAN_MODE_ID, // none, OTAA or ABP
16  RADIOLIB_EEPROM_LORAWAN_CHECKSUM_ID, // checksum of keys used for device activation
17  RADIOLIB_EEPROM_LORAWAN_VERSION_ID, // LoRaWAN version
18  RADIOLIB_EEPROM_LORAWAN_LAST_TIME_ID, // last heard time through DeviceTimeReq or Beacon
19  RADIOLIB_EEPROM_LORAWAN_DEV_ADDR_ID,
20  RADIOLIB_EEPROM_LORAWAN_APP_S_KEY_ID,
21  RADIOLIB_EEPROM_LORAWAN_FNWK_SINT_KEY_ID,
22  RADIOLIB_EEPROM_LORAWAN_SNWK_SINT_KEY_ID,
23  RADIOLIB_EEPROM_LORAWAN_NWK_SENC_KEY_ID,
24  RADIOLIB_EEPROM_LORAWAN_DEV_NONCE_ID,
25  RADIOLIB_EEPROM_LORAWAN_JOIN_NONCE_ID,
26  RADIOLIB_EEPROM_LORAWAN_HOME_NET_ID,
27  RADIOLIB_EEPROM_LORAWAN_A_FCNT_DOWN_ID,
28  RADIOLIB_EEPROM_LORAWAN_N_FCNT_DOWN_ID,
29  RADIOLIB_EEPROM_LORAWAN_CONF_FCNT_UP_ID,
30  RADIOLIB_EEPROM_LORAWAN_CONF_FCNT_DOWN_ID,
31  RADIOLIB_EEPROM_LORAWAN_ADR_FCNT_ID,
32  RADIOLIB_EEPROM_LORAWAN_RJ_COUNT0_ID,
33  RADIOLIB_EEPROM_LORAWAN_RJ_COUNT1_ID,
34  RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID,
35  RADIOLIB_EEPROM_LORAWAN_LINK_ADR_ID,
36  RADIOLIB_EEPROM_LORAWAN_DUTY_CYCLE_ID,
37  RADIOLIB_EEPROM_LORAWAN_RX_PARAM_SETUP_ID,
38  RADIOLIB_EEPROM_LORAWAN_RX_TIMING_SETUP_ID,
39  RADIOLIB_EEPROM_LORAWAN_TX_PARAM_SETUP_ID,
40  RADIOLIB_EEPROM_LORAWAN_ADR_PARAM_SETUP_ID,
41  RADIOLIB_EEPROM_LORAWAN_REJOIN_PARAM_SETUP_ID,
42  RADIOLIB_EEPROM_LORAWAN_BEACON_FREQ_ID,
43  RADIOLIB_EEPROM_LORAWAN_PING_SLOT_CHANNEL_ID,
44  RADIOLIB_EEPROM_LORAWAN_PERIODICITY_ID,
45  RADIOLIB_EEPROM_LORAWAN_NUM_ADR_MASKS_ID,
46  RADIOLIB_EEPROM_LORAWAN_MAC_QUEUE_UL_ID,
47  RADIOLIB_EEPROM_LORAWAN_UL_CHANNELS_ID,
48  RADIOLIB_EEPROM_LORAWAN_DL_CHANNELS_ID
49 };
50 
51 static const uint32_t RadioLibPersistentParamTable[] = {
52  0x00, // RADIOLIB_EEPROM_TABLE_VERSION_ID
53  0x02, // RADIOLIB_EEPROM_LORAWAN_CLASS_ID
54  0x03, // RADIOLIB_EEPROM_LORAWAN_MODE_ID
55  0x05, // RADIOLIB_EEPROM_LORAWAN_CHECKSUM_ID
56  0x07, // RADIOLIB_EEPROM_LORAWAN_VERSION_ID
57  0x08, // RADIOLIB_EEPROM_LORAWAN_LAST_TIME_ID
58  0x0C, // RADIOLIB_EEPROM_LORAWAN_DEV_ADDR_ID
59  0x10, // RADIOLIB_EEPROM_LORAWAN_APP_S_KEY_ID
60  0x20, // RADIOLIB_EEPROM_LORAWAN_FNWK_SINT_KEY_ID
61  0x30, // RADIOLIB_EEPROM_LORAWAN_SNWK_SINT_KEY_ID
62  0x40, // RADIOLIB_EEPROM_LORAWAN_NWK_SENC_KEY_ID
63  0x50, // RADIOLIB_EEPROM_LORAWAN_DEV_NONCE_ID
64  0x54, // RADIOLIB_EEPROM_LORAWAN_JOIN_NONCE_ID
65  0x58, // RADIOLIB_EEPROM_LORAWAN_HOME_NET_ID
66  0x5C, // RADIOLIB_EEPROM_LORAWAN_A_FCNT_DOWN_ID
67  0x60, // RADIOLIB_EEPROM_LORAWAN_N_FCNT_DOWN_ID
68  0x64, // RADIOLIB_EEPROM_LORAWAN_CONF_FCNT_UP_ID
69  0x68, // RADIOLIB_EEPROM_LORAWAN_CONF_FCNT_DOWN_ID
70  0x6C, // RADIOLIB_EEPROM_LORAWAN_ADR_FCNT_ID
71  0x70, // RADIOLIB_EEPROM_LORAWAN_RJ_COUNT0_ID
72  0x72, // RADIOLIB_EEPROM_LORAWAN_RJ_COUNT1_ID
73  0x74, // RADIOLIB_EEPROM_LORAWAN_FCNT_UP_ID
74  0xA0, // RADIOLIB_EEPROM_LORAWAN_LINK_ADR_ID
75  0xA4, // RADIOLIB_EEPROM_LORAWAN_DUTY_CYCLE_ID
76  0xA5, // RADIOLIB_EEPROM_LORAWAN_RX_PARAM_SETUP_ID
77  0xA9, // RADIOLIB_EEPROM_LORAWAN_RX_TIMING_SETUP_ID
78  0xAA, // RADIOLIB_EEPROM_LORAWAN_TX_PARAM_SETUP_ID
79  0xAB, // RADIOLIB_EEPROM_LORAWAN_ADR_PARAM_SETUP_ID
80  0xAC, // RADIOLIB_EEPROM_LORAWAN_REJOIN_PARAM_SETUP_ID
81  0xAD, // RADIOLIB_EEPROM_LORAWAN_BEACON_FREQ_ID
82  0xB0, // RADIOLIB_EEPROM_LORAWAN_PING_SLOT_CHANNEL_ID
83  0xB4, // RADIOLIB_EEPROM_LORAWAN_PERIODICITY_ID
84  0xB5, // RADIOLIB_EEPROM_LORAWAN_NUM_ADR_MASKS_ID
85  0xB6, // RADIOLIB_EEPROM_LORAWAN_MAC_QUEUE_UL_ID
86  0x0100, // RADIOLIB_EEPROM_LORAWAN_UL_CHANNELS_ID
87  0x0180, // RADIOLIB_EEPROM_LORAWAN_DL_CHANNELS_ID
88  0x01C0, // end
89 };
90 
95 class RadioLibHal {
96  public:
97 
98  // values for pin modes, levels and change directions
99  // these tell RadioLib how are different logic states represented on a given platform
100 
104  const uint32_t GpioModeInput;
105 
109  const uint32_t GpioModeOutput;
110 
114  const uint32_t GpioLevelLow;
115 
119  const uint32_t GpioLevelHigh;
120 
124  const uint32_t GpioInterruptRising;
125 
129  const uint32_t GpioInterruptFalling;
130 
140  RadioLibHal(const uint32_t input, const uint32_t output, const uint32_t low, const uint32_t high, const uint32_t rising, const uint32_t falling);
141 
142  // pure virtual methods - these must be implemented by the hardware abstraction for RadioLib to function
143 
150  virtual void pinMode(uint32_t pin, uint32_t mode) = 0;
151 
158  virtual void digitalWrite(uint32_t pin, uint32_t value) = 0;
159 
166  virtual uint32_t digitalRead(uint32_t pin) = 0;
167 
175  virtual void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) = 0;
176 
182  virtual void detachInterrupt(uint32_t interruptNum) = 0;
183 
189  virtual void delay(unsigned long ms) = 0;
190 
196  virtual void delayMicroseconds(unsigned long us) = 0;
197 
203  virtual unsigned long millis() = 0;
204 
210  virtual unsigned long micros() = 0;
211 
220  virtual long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) = 0;
221 
225  virtual void spiBegin() = 0;
226 
230  virtual void spiBeginTransaction() = 0;
231 
238  virtual void spiTransfer(uint8_t* out, size_t len, uint8_t* in) = 0;
239 
243  virtual void spiEndTransaction() = 0;
244 
248  virtual void spiEnd() = 0;
249 
250  // virtual methods - these may or may not exists on a given platform
251  // they exist in this implementation, but do nothing
252 
258  virtual void init();
259 
265  virtual void term();
266 
273  virtual void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0);
274 
279  virtual void noTone(uint32_t pin);
280 
284  virtual void yield();
285 
291  virtual uint32_t pinToInterrupt(uint32_t pin);
292 
299  virtual void readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len);
300 
307  virtual void writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len);
308 
313  void wipePersistentStorage();
314 
320  uint32_t getPersistentAddr(uint32_t id);
321 
330  template<typename T>
331  void setPersistentParameter(uint32_t id, T val, uint32_t offset = 0);
332 
340  template<typename T>
341  T getPersistentParameter(uint32_t id);
342 };
343 
344 #endif
Hardware abstraction library base interface.
Definition: Hal.h:95
const uint32_t GpioInterruptFalling
Value to be used as the "falling" GPIO level change direction.
Definition: Hal.h:129
virtual uint32_t digitalRead(uint32_t pin)=0
Digital read method. Must be implemented by the platform-specific hardware abstraction!
const uint32_t GpioModeOutput
Value to be used as the "output" GPIO direction.
Definition: Hal.h:109
virtual void yield()
Yield method, called from long loops in multi-threaded environment (to prevent blocking other threads...
Definition: Hal.cpp:29
T getPersistentParameter(uint32_t id)
Method to get arbitrary parameter from persistent storage. This method DOES NOT perform any endiannes...
Definition: Hal.cpp:73
void setPersistentParameter(uint32_t id, T val, uint32_t offset=0)
Method to set arbitrary parameter to persistent storage. This method DOES NOT perform any endianness ...
Definition: Hal.cpp:63
virtual unsigned long millis()=0
Get number of milliseconds since start. Must be implemented by the platform-specific hardware abstrac...
const uint32_t GpioModeInput
Value to be used as the "input" GPIO direction.
Definition: Hal.h:104
virtual void detachInterrupt(uint32_t interruptNum)=0
Method to detach function from an external interrupt. Must be implemented by the platform-specific ha...
virtual uint32_t pinToInterrupt(uint32_t pin)
Function to convert from pin number to interrupt number.
Definition: Hal.cpp:33
virtual long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout)=0
Measure the length of incoming digital pulse in microseconds. Must be implemented by the platform-spe...
const uint32_t GpioLevelLow
Value to be used as the "low" GPIO level.
Definition: Hal.h:114
const uint32_t GpioLevelHigh
Value to be used as the "high" GPIO level.
Definition: Hal.h:119
virtual void spiEnd()=0
SPI termination method.
virtual void init()
Module initialization method. This will be called by all radio modules at the beginning of startup....
Definition: Hal.cpp:11
virtual void readPersistentStorage(uint32_t addr, uint8_t *buff, size_t len)
Method to read from persistent storage (e.g. EEPROM).
Definition: Hal.cpp:37
virtual void digitalWrite(uint32_t pin, uint32_t value)=0
Digital write method. Must be implemented by the platform-specific hardware abstraction!
virtual void spiEndTransaction()=0
Method to end SPI transaction.
virtual void noTone(uint32_t pin)
Method to stop producing a tone.
Definition: Hal.cpp:25
const uint32_t GpioInterruptRising
Value to be used as the "rising" GPIO level change direction.
Definition: Hal.h:124
virtual void delay(unsigned long ms)=0
Blocking wait function. Must be implemented by the platform-specific hardware abstraction!
virtual void writePersistentStorage(uint32_t addr, uint8_t *buff, size_t len)
Method to write to persistent storage (e.g. EEPROM).
Definition: Hal.cpp:44
virtual void spiBegin()=0
SPI initialization method.
virtual void term()
Module termination method. This will be called by all radio modules when the destructor is called....
Definition: Hal.cpp:15
virtual void tone(uint32_t pin, unsigned int frequency, unsigned long duration=0)
Method to produce a square-wave with 50% duty cycle ("tone") of a given frequency at some pin.
Definition: Hal.cpp:19
virtual void spiBeginTransaction()=0
Method to start SPI transaction.
void wipePersistentStorage()
Method to wipe the persistent storage by writing to 0. Will write at most RADIOLIB_HAL_PERSISTENT_STO...
Definition: Hal.cpp:51
virtual void delayMicroseconds(unsigned long us)=0
Blocking microsecond wait function. Must be implemented by the platform-specific hardware abstraction...
RadioLibHal(const uint32_t input, const uint32_t output, const uint32_t low, const uint32_t high, const uint32_t rising, const uint32_t falling)
Default constructor.
Definition: Hal.cpp:3
uint32_t getPersistentAddr(uint32_t id)
Method to convert from persistent parameter ID to its physical address.
Definition: Hal.cpp:58
virtual void spiTransfer(uint8_t *out, size_t len, uint8_t *in)=0
Method to transfer buffer over SPI.
virtual unsigned long micros()=0
Get number of microseconds since start. Must be implemented by the platform-specific hardware abstrac...
virtual void pinMode(uint32_t pin, uint32_t mode)=0
GPIO pin mode (input/output/...) configuration method. Must be implemented by the platform-specific h...
virtual void attachInterrupt(uint32_t interruptNum, void(*interruptCb)(void), uint32_t mode)=0
Method to attach function to an external interrupt. Must be implemented by the platform-specific hard...