RadioHead
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
RH_LORA Class Reference

Driver to send and receive unaddressed, unreliable datagrams via a LoRa capable radio transceiver. More...

#include <RH_LORA.h>

Inheritance diagram for RH_LORA:
RHSPIDriver RHGenericDriver

Classes

struct  ModemConfig
 Defines register values for a set of modem configuration registers. More...
 

Public Types

enum  ModemConfigChoice { Bw125Cr45Sf128 = 0, Bw500Cr45Sf128 }
 
- Public Types inherited from RHGenericDriver
enum  RHMode { RHModeInitialising = 0, RHModeIdle, RHModeTx, RHModeRx }
 Defines different operating modes for the transport hardware. More...
 

Public Member Functions

 RH_LORA (uint8_t slaveSelectPin=SS, uint8_t interruptPin=2, RHGenericSPI &spi=hardware_spi)
 
virtual bool init ()
 
bool printRegisters ()
 
void setModemRegisters (const ModemConfig *config)
 
bool setModemConfig (ModemConfigChoice index)
 
virtual bool available ()
 
virtual bool recv (uint8_t *buf, uint8_t *len)
 
virtual bool send (const uint8_t *data, uint8_t len)
 
void setPreambleLength (uint16_t bytes)
 
virtual uint8_t maxMessageLength ()
 
bool setFrequency (float centre)
 
void setModeIdle ()
 
void setModeRx ()
 
void setModeTx ()
 
void setTxPower (int8_t power)
 
- Public Member Functions inherited from RHSPIDriver
 RHSPIDriver (uint8_t slaveSelectPin=SS, RHGenericSPI &spi=hardware_spi)
 
bool init ()
 
uint8_t spiRead (uint8_t reg)
 
uint8_t spiWrite (uint8_t reg, uint8_t val)
 
uint8_t spiBurstRead (uint8_t reg, uint8_t *dest, uint8_t len)
 
uint8_t spiBurstWrite (uint8_t reg, const uint8_t *src, uint8_t len)
 
- Public Member Functions inherited from RHGenericDriver
 RHGenericDriver ()
 Constructor.
 
virtual void waitAvailable ()
 
virtual bool waitPacketSent ()
 
virtual bool waitPacketSent (uint16_t timeout)
 
virtual bool waitAvailableTimeout (uint16_t timeout)
 
virtual void setThisAddress (uint8_t thisAddress)
 
virtual void setHeaderTo (uint8_t to)
 
virtual void setHeaderFrom (uint8_t from)
 
virtual void setHeaderId (uint8_t id)
 
virtual void setHeaderFlags (uint8_t set, uint8_t clear=RH_FLAGS_NONE)
 
virtual void setPromiscuous (bool promiscuous)
 
virtual uint8_t headerTo ()
 
virtual uint8_t headerFrom ()
 
virtual uint8_t headerId ()
 
virtual uint8_t headerFlags ()
 
int8_t lastRssi ()
 
RHMode mode ()
 
void setMode (RHMode mode)
 Sets the operating mode of the transport.
 

Protected Member Functions

void handleInterrupt ()
 
void validateRxBuf ()
 Examine the revceive buffer to determine whether the message is for this node.
 
void clearRxBuf ()
 Clear our local receive buffer.
 

Additional Inherited Members

- Static Public Member Functions inherited from RHGenericDriver
static void printBuffer (const char *prompt, const uint8_t *buf, uint8_t len)
 
- Protected Attributes inherited from RHSPIDriver
RHGenericSPI_spi
 Reference to the RHGenericSPI instance to use to trasnfer data with teh SPI device.
 
uint8_t _slaveSelectPin
 The pin number of the Slave Selct pin that is used to select the desired device.
 
- Protected Attributes inherited from RHGenericDriver
volatile RHMode _mode
 The current transport operating mode.
 
uint8_t _thisAddress
 This node id.
 
bool _promiscuous
 Whether the transport is in promiscuous mode.
 
volatile uint8_t _rxHeaderTo
 TO header in the last received mesasge.
 
volatile uint8_t _rxHeaderFrom
 FROM header in the last received mesasge.
 
volatile uint8_t _rxHeaderId
 ID header in the last received mesasge.
 
volatile uint8_t _rxHeaderFlags
 FLAGS header in the last received mesasge.
 
uint8_t _txHeaderTo
 TO header to send in all messages.
 
uint8_t _txHeaderFrom
 FROM header to send in all messages.
 
uint8_t _txHeaderId
 ID header to send in all messages.
 
uint8_t _txHeaderFlags
 FLAGS header to send in all messages.
 
volatile int8_t _lastRssi
 The value of the last received RSSI value, in some transport specific units.
 
volatile uint16_t _rxBad
 Count of the number of bad messages (eg bad checksum etc) received.
 
volatile uint16_t _rxGood
 Count of the number of successfully transmitted messaged.
 
volatile uint16_t _txGood
 Count of the number of bad messages (correct checksum etc) received.
 

Detailed Description

Driver to send and receive unaddressed, unreliable datagrams via a LoRa capable radio transceiver.

For Semtech SX1272/73, HopeRF RFM95/96/97/98 and other similar LoRa capable radios based on http://www.semtech.com/images/datasheet/sx1272.pdf and http://www.semtech.com/images/datasheet/LoraDesignGuide_STD.pdf and http://www.hoperf.com/upload/rf/RFM95_96_97_98W.pdf and http://www.hoperf.cn/upload/rfchip/RF96_97_98.pdf

Works with

Overview

This class provides basic functions for sending and receiving unaddressed, unreliable datagrams of arbitrary length to 251 octets per packet.

Manager classes may use this class to implement reliable, addressed datagrams and streams, mesh routers, repeaters, translators etc.

Naturally, for any 2 radios to communicate that must be configured to use the same frequency and modulation scheme.

This Driver provides an object-oriented interface for sending and receiving data messages with Hope-RF RFM95/96/97/98(W) and compatible radio modules on LoRa mode.

The Hope-RF (http://www.hoperf.com) RFM95/96/97/98(W) is a low-cost ISM transceiver chip. It supports FSK, GFSK, OOK over a wide range of frequencies and programmable data rates, and it also supports the proprietary LoRA (Long Range) mode, which is what we use in this RadioHead driver.

This Driver provides functions for sending and receiving messages of up to 251 octets on any frequency supported by the radio, in a range of predefined Bandwidths, Spreading Factors and Coding Rates. Frequency can be set with 61Hz precision to any frequency from 240.0MHz to 960.0MHz. Caution: most modules only support a more limited range of frequencies due to antenna tuning.

Up to 2 RFM95/96/97/98(W) modules can be connected to an Arduino (3 on a Mega), permitting the construction of translators and frequency changers, etc.

Support for other RF69 features such as transmitter power control etc is also provided.

Tested on MinWirelessLoRa with arduino-1.0.5 on OpenSuSE 13.1

Packet Format

All messages sent and received by this RH_LORA Driver conform to this packet format:

Connecting RFM95/96/97/98 to Arduino

We tested with Anarduino MiniWirelessLoRA, which is an Arduino Duemilanove compatible with a RFM96W module on-board. Therefore it needs no connections other than the USB programming connection and an antenna to make it work.

If you have a bare RFM95/96/97/98 that you want to connect to an Arduino, you might use these connections (untested): CAUTION: you must use a 3.3V type Arduino, otherwise you will also need voltage level shifters between the Arduino and the RFM69. CAUTION, you must also ensure you connect an antenna

* Arduino RFM95/96/97/98
* GND----------GND (ground in)
* 3V3----------3.3V (3.3V in)
* interrupt 0 pin D2-----------DIO0 (interrupt request out)
* SS pin D10----------NSS (chip select in)
* SCK pin D13----------SCK (SPI clock in)
* MOSI pin D11----------MOSI (SPI Data in)
* MISO pin D12----------MISO (SPI Data out)
*

With these connections, you can then use the default constructor RH_LORA(). You can override the default settings for the SS pin and the interrupt in the RH_LORA constructor if you wish to connect the slave select SS to other than the normal one for your Arduino (D10 for Diecimila, Uno etc and D53 for Mega) or the interrupt request to other than pin D2 (Caution, different processors have different constraints as to the pins available for interrupts).

It is possible to have 2 or more radios connected to one Arduino, provided each radio has its own SS and interrupt line (SCK, SDI and SDO are common to all radios)

Caution: on some Arduinos such as the Mega 2560, if you set the slave select pin to be other than the usual SS pin (D53 on Mega 2560), you may need to set the usual SS pin to be an output to force the Arduino into SPI master mode.

Caution: Power supply requirements of the RFM module may be relevant in some circumstances: RFM95/96/97/98 modules are capable of pulling 120mA+ at full power, where Arduino's 3.3V line can give 50mA. You may need to make provision for alternate power supply for the RFM module, especially if you wish to use full transmit power, and/or you have other shields demanding power. Inadequate power for the RFM is likely to cause symptoms such as: -reset's/bootups terminate with "init failed" messages -random termination of communication after 5-30 packets sent/received -"fake ok" state, where initialization passes fluently, but communication doesn't happen -shields hang Arduino boards, especially during the flashing

Interrupts

The RH_LORA driver uses interrupts to react to events in the RFM module, such as the reception of a new packet, or the completion of transmission of a packet. The RH_LORA driver interrupt service routine reads status from and writes data to the the RFM module via the SPI interface. It is very important therefore, that if you are using the RH_LORA driver with another SPI based deviced, that you disable interrupts while you transfer data to and from that other device. Use cli() to disable interrupts and sei() to reenable them.

Memory

The RH_LORA driver requires non-trivial amounts of memory. The sample programs all compile to about 8kbytes each, which will fit in the flash proram memory of most Arduinos. However, the RAM requirements are more critical. Therefore, you should be vary sparing with RAM use in programs that use the RH_LORA driver.

It is often hard to accurately identify when you are hitting RAM limits on Arduino. The symptoms can include:

Transmitter Power

You can control the transmitter power on the RF transceiver with the RH_LORA::setTxPower() function. The argument can be any of +5 to +20 The default is 13. Eg:

* driver.setTxPower(10);
*

We have made some actual power measurements against programmed power for Anarduino MiniWirelessLoRa (which has RFM96W-433Mhz installed)

Member Enumeration Documentation

Choices for setModemConfig() for a selected subset of common data rates. If you need another configuration, determine the necessary settings and call setModemRegisters() with your desired settings. It might be helpful to use the LoRa calculator mentioned in http://www.semtech.com/images/datasheet/LoraDesignGuide_STD.pdf These are indexes into MODEM_CONFIG_TABLE. We strongly recommend you use these symbolic definitions and not their integer equivalents: its possible that new values will be introduced in later versions (though we will try to avoid it).

Enumerator
Bw125Cr45Sf128 

Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on.

Bw500Cr45Sf128 

Bw = 500 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on.

Constructor & Destructor Documentation

RH_LORA::RH_LORA ( uint8_t  slaveSelectPin = SS,
uint8_t  interruptPin = 2,
RHGenericSPI spi = hardware_spi 
)

Constructor. You can have multiple instances, but each instance must have its own interrupt and slave select pin. After constructing, you must call init() to initialise the interface and the radio module. A maximum of 3 instances can co-exist on one processor, provided there are sufficient distinct interrupt lines, one for each instance.

Parameters
[in]slaveSelectPinthe Arduino pin number of the output to use to select the RH_RF22 before accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega, D10 for Maple)
[in]interruptPinThe interrupt Pin number that is connected to the RFM DIO0 interrupt line. Defaults to pin 2, as required by Anardion MinWirelessLoRa module. Caution: You must specify an interrupt capable pin. On many Arduino boards, there are limitations as to which pins may be used as interrupts. On Leonardo pins 0, 1, 2 or 3. On Mega2560 pins 2, 3, 18, 19, 20, 21. On Due and Teensy, any digital pin. On other Arduinos pins 2 or 3. See http://arduino.cc/en/Reference/attachInterrupt for more details. On Chipkit Uno32, pins 38, 2, 7, 8, 35. On other boards, any digital pin may be used.
[in]spiPointer to the SPI interface object to use. Defaults to the standard Arduino hardware SPI interface

Member Function Documentation

bool RH_LORA::available ( )
virtual

Tests whether a new message is available from the Driver. On most drivers, this will also put the Driver into RHModeRx mode until a message is actually received by the transport, when it wil be returned to RHModeIdle. This can be called multiple times in a timeout loop

Returns
true if a new, complete, error-free uncollected message is available to be retreived by recv()

Implements RHGenericDriver.

References setModeRx().

Referenced by recv().

void RH_LORA::handleInterrupt ( )
protected

This is a low level function to handle the interrupts for one instance of RF69. Called automatically by isr*() Should not need to be called by user code.

References RHGenericDriver::_lastRssi, RHGenericDriver::_mode, RHGenericDriver::_rxBad, RHGenericDriver::_txGood, RHGenericDriver::RHModeRx, RHGenericDriver::RHModeTx, setModeIdle(), RHSPIDriver::spiBurstRead(), RHSPIDriver::spiRead(), RHSPIDriver::spiWrite(), and validateRxBuf().

bool RH_LORA::init ( )
virtual

Initialise the Driver transport hardware and software. Make sure the Driver is properly configured before calling init().

Returns
true if initialisation succeeded.

Reimplemented from RHGenericDriver.

References Bw125Cr45Sf128, RHSPIDriver::init(), setFrequency(), setModeIdle(), setModemConfig(), setPreambleLength(), setTxPower(), RHSPIDriver::spiRead(), and RHSPIDriver::spiWrite().

uint8_t RH_LORA::maxMessageLength ( )
virtual

Returns the maximum message length available in this Driver.

Returns
The maximum legal message length

Implements RHGenericDriver.

bool RH_LORA::printRegisters ( )

Prints the value of all chip registers for debugging purposes

Returns
true on success

References RHSPIDriver::spiRead().

bool RH_LORA::recv ( uint8_t *  buf,
uint8_t *  len 
)
virtual

Turns the receiver on if it not already on. If there is a valid message available, copy it to buf and return true else return false. If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). You should be sure to call this function frequently enough to not miss any messages It is recommended that you call it in your main loop.

Parameters
[in]bufLocation to copy the received message
[in,out]lenPointer to available space in buf. Set to the actual number of octets copied.
Returns
true if a valid message was copied to buf

Implements RHGenericDriver.

References available(), and clearRxBuf().

bool RH_LORA::send ( const uint8_t *  data,
uint8_t  len 
)
virtual

Waits until any previous transmit packet is finished being transmitted with waitPacketSent(). Then loads a message into the transmitter and starts the transmitter. Note that a message length of 0 is permitted.

Parameters
[in]dataArray of data to be sent
[in]lenNumber of bytes of data to send
Returns
true if the message length was valid and it was correctly queued for transmit

Implements RHGenericDriver.

References RHGenericDriver::_txHeaderFlags, RHGenericDriver::_txHeaderFrom, RHGenericDriver::_txHeaderId, RHGenericDriver::_txHeaderTo, setModeIdle(), setModeTx(), RHSPIDriver::spiBurstWrite(), RHSPIDriver::spiWrite(), and RHGenericDriver::waitPacketSent().

bool RH_LORA::setFrequency ( float  centre)

Sets the transmitter and receiver centre frequency

Parameters
[in]centreFrequency in MHz. 137.0 to 1020.0. Caution,RFM95/96/97/98 comes in several different frequency ranges, and setting a frequency outside that range of your radio will probably not work
Returns
true if the selected frquency centre is within range

References RHSPIDriver::spiWrite().

Referenced by init().

void RH_LORA::setModeIdle ( )

If current mode is Rx or Tx changes it to Idle. If the transmitter or receiver is running, disables them.

References RHGenericDriver::_mode, RHGenericDriver::RHModeIdle, and RHSPIDriver::spiWrite().

Referenced by handleInterrupt(), init(), and send().

bool RH_LORA::setModemConfig ( ModemConfigChoice  index)

Select one of the predefined modem configurations. If you need a modem configuration not provided here, use setModemRegisters() with your own ModemConfig.

Parameters
[in]indexThe configuration choice.
Returns
true if index is a valid choice.

References setModemRegisters().

Referenced by init().

void RH_LORA::setModemRegisters ( const ModemConfig config)

Sets all the registered required to configure the data modem in the LORA, including the bandwidth, spreading factor etc. You can use this to configure the modem with custom configurations if none of the canned configurations in ModemConfigChoice suit you.

Parameters
[in]configA ModemConfig structure containing values for the modem configuration registers.

References RH_LORA::ModemConfig::reg_1d, RH_LORA::ModemConfig::reg_1e, RH_LORA::ModemConfig::reg_26, and RHSPIDriver::spiWrite().

Referenced by setModemConfig().

void RH_LORA::setModeRx ( )

If current mode is Tx or Idle, changes it to Rx. Starts the receiver in the LORA.

References RHGenericDriver::_mode, RHGenericDriver::RHModeRx, and RHSPIDriver::spiWrite().

Referenced by available().

void RH_LORA::setModeTx ( )

If current mode is Rx or Idle, changes it to Rx. F Starts the transmitter in the LORA.

References RHGenericDriver::_mode, RHGenericDriver::RHModeTx, and RHSPIDriver::spiWrite().

Referenced by send().

void RH_LORA::setPreambleLength ( uint16_t  bytes)

Sets the length of the preamble in bytes. Caution: this should be set to the same value on all nodes in your network. Default is 8. Sets the message preamble length in RH_LORA_REG_??_PREAMBLE_?SB

Parameters
[in]bytesPreamble length in bytes.

References RHSPIDriver::spiWrite().

Referenced by init().

void RH_LORA::setTxPower ( int8_t  power)

Sets the transmitter power output level. Be a good neighbour and set the lowest power level you need. Caution: legal power limits may apply in certain countries. After init(), the power will be set to 13dBm.

Parameters
[in]powerTransmitter power level in dBm. For RFM95/96/97/98 LORA, valid values are from +5 to +20

References RHSPIDriver::spiWrite().

Referenced by init().


The documentation for this class was generated from the following files: