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 // list of persistent parameters
10 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_VERSION_ID (0) // this is NOT the LoRaWAN version, but version of this table
11 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_MAGIC_ID (1)
12 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_DEV_ADDR_ID (2)
13 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_APP_S_KEY_ID (3)
14 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_FNWK_SINT_KEY_ID (4)
15 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_SNWK_SINT_KEY_ID (5)
16 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_NWK_SENC_KEY_ID (6)
17 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_DEV_NONCE_ID (7)
18 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_JOIN_NONCE_ID (8)
19 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_HOME_NET_ID (9)
20 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_DL_SETTINGS_ID (10)
21 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_CF_LIST_ID (11)
22 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_RX_DELAY_ID (12)
23 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_FCNT_UP_ID (13)
24 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_A_FCNT_DOWN_ID (14)
25 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_N_FCNT_DOWN_ID (15)
26 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_CONF_FCNT_ID (16)
27 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_ADR_ID (17)
28 #define RADIOLIB_PERSISTENT_PARAM_LORAWAN_FOPTS_ID (18)
29
30 static const uint32_t RadioLibPersistentParamTable[] = {
31 0x00, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_VERSION
32 0x08, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_MAGIC_ID
33 0x0C, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_DEV_ADDR_ID
34 0x10, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_APP_S_KEY_ID
35 0x20, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_FNWK_SINT_KEY_ID
36 0x30, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_SNWK_SINT_KEY_ID
37 0x40, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_NWK_SENC_KEY_ID
38 0x50, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_DEV_NONCE_ID
39 0x54, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_JOIN_NONCE_ID
40 0x58, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_HOME_NET_ID
41 0x5C, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_DL_SETTINGS_ID
42 0x60, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_CF_LIST
43 0x70, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_RX_DELAY_ID
44 0x74, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_FCNT_UP_ID
45 0x78, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_AFCNT_DOWN_ID
46 0x7C, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_NFCNT_DOWN_ID
47 0x80, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_CONF_FCNT_ID
48 0x84, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_ADR_ID
49 0x88, // RADIOLIB_PERSISTENT_PARAM_LORAWAN_FOPTS_ID
50 0xD0, // end
51 };
52
57 class RadioLibHal {
58 public:
59
60 // values for pin modes, levels and change directions
61 // these tell RadioLib how are different logic states represented on a given platform
62
66 const uint32_t GpioModeInput;
67
71 const uint32_t GpioModeOutput;
72
76 const uint32_t GpioLevelLow;
77
81 const uint32_t GpioLevelHigh;
82
86 const uint32_t GpioInterruptRising;
87
91 const uint32_t GpioInterruptFalling;
92
102 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);
103
104 // pure virtual methods - these must be implemented by the hardware abstraction for RadioLib to function
105
112 virtual void pinMode(uint32_t pin, uint32_t mode) = 0;
113
120 virtual void digitalWrite(uint32_t pin, uint32_t value) = 0;
121
128 virtual uint32_t digitalRead(uint32_t pin) = 0;
129
137 virtual void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) = 0;
138
144 virtual void detachInterrupt(uint32_t interruptNum) = 0;
145
151 virtual void delay(unsigned long ms) = 0;
152
158 virtual void delayMicroseconds(unsigned long us) = 0;
159
165 virtual unsigned long millis() = 0;
166
172 virtual unsigned long micros() = 0;
173
182 virtual long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) = 0;
183
187 virtual void spiBegin() = 0;
188
192 virtual void spiBeginTransaction() = 0;
193
200 virtual void spiTransfer(uint8_t* out, size_t len, uint8_t* in) = 0;
201
205 virtual void spiEndTransaction() = 0;
206
210 virtual void spiEnd() = 0;
211
212 // virtual methods - these may or may not exists on a given platform
213 // they exist in this implementation, but do nothing
214
220 virtual void init();
221
227 virtual void term();
228
235 virtual void tone(uint32_t pin, unsigned int frequency, unsigned long duration = 0);
236
241 virtual void noTone(uint32_t pin);
242
246 virtual void yield();
247
253 virtual uint32_t pinToInterrupt(uint32_t pin);
254
261 virtual void readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len);
262
269 virtual void writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len);
270
275 void wipePersistentStorage();
276
282 uint32_t getPersistentAddr(uint32_t id);
283
291 template<typename T>
292 void setPersistentParameter(uint32_t id, T val);
293
301 template<typename T>
302 T getPersistentParameter(uint32_t id);
303 };
304
305 #endif
RadioLibHal
Hardware abstraction library base interface.
Definition: Hal.h:57
RadioLibHal::GpioInterruptFalling
const uint32_t GpioInterruptFalling
Value to be used as the "falling" GPIO level change direction.
Definition: Hal.h:91
RadioLibHal::digitalRead
virtual uint32_t digitalRead(uint32_t pin)=0
Digital read method. Must be implemented by the platform-specific hardware abstraction!
RadioLibHal::GpioModeOutput
const uint32_t GpioModeOutput
Value to be used as the "output" GPIO direction.
Definition: Hal.h:71
RadioLibHal::yield
virtual void yield()
Yield method, called from long loops in multi-threaded environment (to prevent blocking other threads...
Definition: Hal.cpp:29
RadioLibHal::getPersistentParameter
T getPersistentParameter(uint32_t id)
Method to get arbitrary parameter from persistent storage. This method DOES NOT perform any endiannes...
Definition: Hal.cpp:73
RadioLibHal::millis
virtual unsigned long millis()=0
Get number of milliseconds since start. Must be implemented by the platform-specific hardware abstrac...
RadioLibHal::GpioModeInput
const uint32_t GpioModeInput
Value to be used as the "input" GPIO direction.
Definition: Hal.h:66
RadioLibHal::detachInterrupt
virtual void detachInterrupt(uint32_t interruptNum)=0
Method to detach function from an external interrupt. Must be implemented by the platform-specific ha...
RadioLibHal::pinToInterrupt
virtual uint32_t pinToInterrupt(uint32_t pin)
Function to convert from pin number to interrupt number.
Definition: Hal.cpp:33
RadioLibHal::setPersistentParameter
void setPersistentParameter(uint32_t id, T val)
Method to set arbitrary parameter to persistent storage. This method DOES NOT perform any endianness ...
Definition: Hal.cpp:63
RadioLibHal::pulseIn
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...
RadioLibHal::GpioLevelLow
const uint32_t GpioLevelLow
Value to be used as the "low" GPIO level.
Definition: Hal.h:76
RadioLibHal::GpioLevelHigh
const uint32_t GpioLevelHigh
Value to be used as the "high" GPIO level.
Definition: Hal.h:81
RadioLibHal::spiEnd
virtual void spiEnd()=0
SPI termination method.
RadioLibHal::init
virtual void init()
Module initialization method. This will be called by all radio modules at the beginning of startup....
Definition: Hal.cpp:11
RadioLibHal::readPersistentStorage
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
RadioLibHal::digitalWrite
virtual void digitalWrite(uint32_t pin, uint32_t value)=0
Digital write method. Must be implemented by the platform-specific hardware abstraction!
RadioLibHal::spiEndTransaction
virtual void spiEndTransaction()=0
Method to end SPI transaction.
RadioLibHal::noTone
virtual void noTone(uint32_t pin)
Method to stop producing a tone.
Definition: Hal.cpp:25
RadioLibHal::GpioInterruptRising
const uint32_t GpioInterruptRising
Value to be used as the "rising" GPIO level change direction.
Definition: Hal.h:86
RadioLibHal::delay
virtual void delay(unsigned long ms)=0
Blocking wait function. Must be implemented by the platform-specific hardware abstraction!
RadioLibHal::writePersistentStorage
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
RadioLibHal::spiBegin
virtual void spiBegin()=0
SPI initialization method.
RadioLibHal::term
virtual void term()
Module termination method. This will be called by all radio modules when the destructor is called....
Definition: Hal.cpp:15
RadioLibHal::tone
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
RadioLibHal::spiBeginTransaction
virtual void spiBeginTransaction()=0
Method to start SPI transaction.
RadioLibHal::wipePersistentStorage
void wipePersistentStorage()
Method to wipe the persistent storage by writing to 0. Will write at most RADIOLIB_HAL_PERSISTENT_STO...
Definition: Hal.cpp:51
RadioLibHal::delayMicroseconds
virtual void delayMicroseconds(unsigned long us)=0
Blocking microsecond wait function. Must be implemented by the platform-specific hardware abstraction...
RadioLibHal::RadioLibHal
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
RadioLibHal::getPersistentAddr
uint32_t getPersistentAddr(uint32_t id)
Method to convert from persistent parameter ID to its physical address.
Definition: Hal.cpp:58
RadioLibHal::spiTransfer
virtual void spiTransfer(uint8_t *out, size_t len, uint8_t *in)=0
Method to transfer buffer over SPI.
RadioLibHal::micros
virtual unsigned long micros()=0
Get number of microseconds since start. Must be implemented by the platform-specific hardware abstrac...
RadioLibHal::pinMode
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...
RadioLibHal::attachInterrupt
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...