tinygorfm9x

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2023 License: GPL-3.0 Imports: 5 Imported by: 1

README

tinygorfm9x

A tinygo library to run an rfm9x module. Based off of code by @cyraxx and this adafruit article.

See the example for usage.

Documentation

Index

Constants

View Source
const (
	REGISTERS_FIFO              = 0x00
	REGISTERS_OP_MODE           = 0x01
	REGISTERS_FRF_MSB           = 0x06
	REGISTERS_FRF_MID           = 0x07
	REGISTERS_FRF_LSB           = 0x08
	REGISTERS_PA_CONFIG         = 0x09
	REGISTERS_FIFO_ADDR_PTR     = 0x0D
	REGISTERS_FIFO_TX_BASE_ADDR = 0x0E
	REGISTERS_FIFO_RX_BASE_ADDR = 0x0F
	REGISTERS_IRQ_FLAGS         = 0x12
	REGISTERS_RX_NB_BYTES       = 0x13
	REGISTERS_PKT_SNR_VALUE     = 0x19
	REGISTERS_PKT_RSSI_VALUE    = 0x1A
	REGISTERS_MODEM_CONFIG_1    = 0x1D
	REGISTERS_MODEM_CONFIG_2    = 0x1E
	REGISTERS_PREAMBLE_MSB      = 0x20
	REGISTERS_PREAMBLE_LSB      = 0x21
	REGISTERS_PAYLOAD_LENGTH    = 0x22
	REGISTERS_MODEM_CONFIG_3    = 0x26
	REGISTERS_DIO_MAPPING_1     = 0x40
	REGISTERS_DIO_MAPPING_2     = 0x41
	REGISTERS_VERSION           = 0x42
	REGISTERS_PA_DAC            = 0x4D
	OP_MODES_SLEEP              = 0b000
	OP_MODES_STANDBY            = 0b001
	OP_MODES_TRANSMIT           = 0b011
	OP_MODES_RXCONT             = 0b101
	OP_MODES_RXSINGLE           = 0b110
	OP_MODES_CAD                = 0b111
	DIO0_MAPPINGS_RX_DONE       = 0b00
	DIO0_MAPPINGS_TX_DONE       = 0b01
	DIO0_MAPPINGS_CAD_DONE      = 0b10
	RF95_FXOSC                  = 32000000
	RF95_FSTEP                  = RF95_FXOSC / 524288
)

Define constants.

Variables

View Source
var (
	BANDWIDTHS        = []int{7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000}
	BW_REG_2F_OFFSETS = []byte{0x48, 0x44, 0x44, 0x44, 0x44, 0x44, 0x40, 0x40, 0x40}
	BITMASKS          = []byte{0b00000001, 0b00000011, 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111}
)

Define lists and bitmasks.

View Source
var (
	ErrRFM9xNotDetected               = errors.New("RFM9x module not detected")
	ErrRFM9xNotSupported              = errors.New("RFM9x version not supported")
	ErrRFM9xOPModeReadbackIncorrect   = errors.New("communication error: Readback of operating mode configuration failed")
	ErrRFM9xLoRaModeReadbackIncorrect = errors.New("communication error: Readback of LoRa mode configuration failed")
	ErrSendPayloadEmpty               = errors.New("empty payload supplied")
	ErrSendPayloadTooBig              = errors.New("payload too long")
	ErrSendTimeOut                    = errors.New("timeout while sending")
	ErrInvalidSpreadingFactor         = errors.New("invalid spreading factor")
	ErrInvalidCodingRate              = errors.New("invalid coding rate")
	ErrInvalidTXPower                 = errors.New("invalid TX power")
)

Define errors.

Functions

This section is empty.

Types

type Options

type Options struct {
	FrequencyMHz      int
	PreambleLength    uint16
	BandwidthHz       int
	CodingRate        byte
	SpreadingFactor   byte
	EnableCRCChecking bool
	TxPowerDb         int
	EnableAGC         bool
	ResetPin          machine.Pin
	CSPin             machine.Pin
	DIO0Pin           machine.Pin
	DIO1Pin           machine.Pin // Currently not used
	DIO2Pin           machine.Pin // Currently not used
	SPISpeedHz        uint32
	TxTimeoutMs       int
}

Options are the options for the radio.

type Packet

type Packet struct {
	// Payload is the bytes received by the radio
	Payload []byte
	// RSSIDb is the Received Signal Strength Indication of the Packet
	RSSIDb byte
	// SNRDb is the signal-to-noise ratio of the Packet
	SNRDb byte
}

Packet is a packet received from the radio.

type RFM9x

type RFM9x struct {
	// Options is an instance of the Options type
	Options Options
	// SPIDevice is the SPI device used to communicate with the RFM9X
	SPIDevice machine.SPI
	// OnReceivedPacket is a function set by the user that is executed when a packet is received
	OnReceivedPacket func(Packet)
}

RFM9x is a driver for the RFM9x LoRa radio module.

func (*RFM9x) ClearDIO0Interrupt

func (rfm *RFM9x) ClearDIO0Interrupt() (err error)

ClearDIO0Interrupt deletes the function set to be run when the DIO0 pin is changed.

func (*RFM9x) GetLoRaMode

func (rfm *RFM9x) GetLoRaMode() (isLoRa bool, err error)

GetLoRaMode returns the current LoRa mode of the device.

func (*RFM9x) GetOperatingMode

func (rfm *RFM9x) GetOperatingMode() (mode byte, err error)

GetOperatingMode returns the current operating mode of the device.

func (*RFM9x) GetVersion

func (rfm *RFM9x) GetVersion() (version byte, err error)

GetVersion returns the version of the device. It should be 0x12.

func (*RFM9x) Init

func (rfm *RFM9x) Init(opts Options) (err error)

Init sets up the RFM9x with the options provided, and uses the defaultOptions for fields not specified by the options provided.

func (*RFM9x) ReadBits

func (rfm *RFM9x) ReadBits(address, bits, offset byte) (value byte, err error)

ReadBits reads a bit from a byte address using an offset.

func (*RFM9x) ReadBuffer

func (rfm *RFM9x) ReadBuffer(address byte, length byte) (value []byte, err error)

ReadBuffer uses SPI to read multiple bytes from the device using the address and length provided.

func (*RFM9x) ReadByteFromAddress

func (rfm *RFM9x) ReadByteFromAddress(address byte) (value byte, err error)

ReadByteFromAddress reads a single byte from the device at the address provided.

func (*RFM9x) Reset

func (rfm *RFM9x) Reset() (err error)

Reset resets the RFM9x by toggling the ResetPin.

func (*RFM9x) Send

func (rfm *RFM9x) Send(payload []byte) (err error)

Send a payload using the radio.

func (*RFM9x) SetAGC added in v1.0.2

func (rfm *RFM9x) SetAGC(enableAGC bool) (err error)

SetAGC enables or disables the Automatic Gain Control of the device.

func (*RFM9x) SetCodingRate

func (rfm *RFM9x) SetCodingRate(codingRate byte) (err error)

SetCodingRate sets the coding rate of the device.

func (*RFM9x) SetDIO0Interrupt

func (rfm *RFM9x) SetDIO0Interrupt(interrupt func(machine.Pin)) (err error)

SetDIO0Interrupt sets the function to be run when the DIO0 pin is changed.

func (*RFM9x) SetFIFOBaseAddress

func (rfm *RFM9x) SetFIFOBaseAddress(txBaseAddress byte, rxBaseAddress byte) (err error)

SetFIFOBaseAddress sets the TX and RX base address of the device.

func (*RFM9x) SetFrequencyAndBandwidth

func (rfm *RFM9x) SetFrequencyAndBandwidth(frequencyMHz int, bandwidthHz int) (err error)

SetFrequencyAndBandwidth sets the frequency (in MHz) and the bandwidth (in Hz) of the device.

func (*RFM9x) SetLoRaMode

func (rfm *RFM9x) SetLoRaMode(isLoRa bool) (err error)

SetLoRaMode sets the current LoRa mode of the device.

func (*RFM9x) SetLowFrequencyMode

func (rfm *RFM9x) SetLowFrequencyMode(isLowFrequency bool) (err error)

SetLowFrequencyMode sets the low frequency mode of the device.

func (*RFM9x) SetOperatingMode

func (rfm *RFM9x) SetOperatingMode(mode byte) (err error)

SetOperatingMode sets the current operating mode of the device.

func (*RFM9x) SetPreambleLength

func (rfm *RFM9x) SetPreambleLength(preambleLength uint16) (err error)

SetPreambleLength sets the preamble length of the device.

func (*RFM9x) SetRxCRC added in v1.0.2

func (rfm *RFM9x) SetRxCRC(enableCRC bool) (err error)

SetRxCRC enables or disables the CRC checking functionality of the device.

func (*RFM9x) SetSpreadingFactor

func (rfm *RFM9x) SetSpreadingFactor(sf byte) (err error)

SetSpreadingFactor sets the spreading factor of the device.

func (*RFM9x) SetTxPower

func (rfm *RFM9x) SetTxPower(txPowerDb int) (err error)

SetTxPower sets the TX power of the device in db.

func (*RFM9x) StartReceive added in v1.0.2

func (rfm *RFM9x) StartReceive() (err error)

StartReceive tells the RFM9X to start waiting for a packet. When it receives a packet, it will run OnReceivedPacket().

func (*RFM9x) StopReceive

func (rfm *RFM9x) StopReceive() (err error)

StopReceive puts the device into standby mode and stops waiting for packets.

func (*RFM9x) WriteBits

func (rfm *RFM9x) WriteBits(address, bits, offset, val byte) (err error)

WriteBits writes a bit in a byte using an offset.

func (*RFM9x) WriteBuffer

func (rfm *RFM9x) WriteBuffer(address byte, buffer []byte) (err error)

WriteBuffer uses SPI to write multiple bytes to the device using the address and buffer provided.

func (*RFM9x) WriteByteToAddress

func (rfm *RFM9x) WriteByteToAddress(address byte, val byte) (err error)

WriteByteToAddress writes a single byte to the device at the address provided.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL