From 4aea5666cbe45820be3b32bc3dfd23b1baa14c99 Mon Sep 17 00:00:00 2001 From: Friedl Ulrich Date: Fri, 6 Feb 2015 21:38:03 +0100 Subject: [PATCH] New SPI Device and main --- rfm69d/rfm69d.go | 43 +++++++++++++++++++++++++++++++++++++ rfm69device.go | 29 ++++++++++++++++++------- rfm69handler.go | 34 ++++++++++++++++++++++++++++- rfm69spi.go => spi_linux.go | 0 spidummy.go | 23 ++++++++++++++++++++ 5 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 rfm69d/rfm69d.go rename rfm69spi.go => spi_linux.go (100%) create mode 100644 spidummy.go diff --git a/rfm69d/rfm69d.go b/rfm69d/rfm69d.go new file mode 100644 index 0000000..7804868 --- /dev/null +++ b/rfm69d/rfm69d.go @@ -0,0 +1,43 @@ +package main + +import ( + "log" + + "github.com/fulr/rfm69" + "github.com/kidoman/embd" + + _ "github.com/kidoman/embd/host/rpi" +) + +func main() { + log.Print("Start") + + if err := embd.InitGPIO(); err != nil { + panic(err) + } + defer embd.CloseGPIO() + + gpio, err := embd.NewDigitalPin(25) + if err != nil { + panic(err) + } + defer gpio.Close() + + if err := gpio.SetDirection(embd.In); err != nil { + panic(err) + } + gpio.ActiveLow(false) + + spiBus, err := rfm69.NewSPIDevice() + if err != nil { + panic(err) + } + defer spiBus.Close() + + rfm, err := rfm69.NewDevice(spiBus, gpio, 1, 10, true) + if err != nil { + t.Error(err) + } + t.Log(rfm) + +} diff --git a/rfm69device.go b/rfm69device.go index bec32d7..48af00f 100644 --- a/rfm69device.go +++ b/rfm69device.go @@ -24,6 +24,14 @@ const ( MaxDataLen = 66 ) +// Data is the data structure for the protocol +type Data struct { + ToAddress byte + Data []byte + RequestAck bool + SendAck bool +} + // NewDevice creates a new device func NewDevice(spi *SPIDevice, gpio embd.DigitalPin, nodeID, networkID byte, isRfm69HW bool) (*Device, error) { ret := &Device{ @@ -134,7 +142,7 @@ func (r *Device) setup() error { // Encryption is persistent between resets and can trip you up during debugging. // Disable it during initialization so we always start from a known state. - err := r.encrypt([]byte{}) + err := r.Encrypt([]byte{}) if err != nil { return err } @@ -171,7 +179,8 @@ func (r *Device) waitForMode() error { return nil } -func (r *Device) encrypt(key []byte) error { +// Encrypt sets the encryption key and enables AES encryption +func (r *Device) Encrypt(key []byte) error { var turnOn byte if len(key) == 16 { turnOn = 1 @@ -334,8 +343,8 @@ func (r *Device) readWriteReg(reg, andMask, orMask byte) error { return r.writeReg(reg, regValue) } -func (r *Device) writeFifo(toAddress byte, buffer []byte, requestACK, sendACK bool) error { - buffersize := len(buffer) +func (r *Device) writeFifo(data *Data) error { + buffersize := len(data.Data) if buffersize > MaxDataLen { buffersize = MaxDataLen } @@ -343,18 +352,22 @@ func (r *Device) writeFifo(toAddress byte, buffer []byte, requestACK, sendACK bo // write to FIFO tx[0] = REG_FIFO | 0x80 tx[1] = byte(buffersize + 3) - tx[2] = toAddress + tx[2] = data.ToAddress tx[3] = r.address - if requestACK { + if data.RequestAck { tx[4] = 0x40 } - if sendACK { + if data.SendAck { tx[4] = 0x80 } - copy(tx[5:], buffer[:buffersize]) + copy(tx[5:], data.Data[:buffersize]) _, err := r.SpiDevice.Xfer(tx) return err } + +func (r *Device) readFifo() (Data, error) { + return Data{}, nil +} diff --git a/rfm69handler.go b/rfm69handler.go index 4517243..bbb9d03 100644 --- a/rfm69handler.go +++ b/rfm69handler.go @@ -1,8 +1,40 @@ package rfm69 +import ( + "log" + + "github.com/kidoman/embd" +) + // Loop is the main receive and transmit handling loop func (r *Device) Loop() error { - for { + c := make(chan Data) + irq := make(chan int) + r.gpio.Watch(embd.EdgeRising, func(pin embd.DigitalPin) { + irq <- 1 + }) + + r.SetMode(RF_OPMODE_RECEIVER) + + for { + select { + case dataToTransmit := <-c: + // can send? + r.SetMode(RF_OPMODE_STANDBY) + r.writeFifo(&dataToTransmit) + r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00) + r.SetMode(RF_OPMODE_TRANSMITTER) + <-irq + r.SetMode(RF_OPMODE_RECEIVER) + r.writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01) + case <-irq: + data, err := r.readFifo() + if err != nil { + log.Print(err) + return err + } + log.Print(data) + } } } diff --git a/rfm69spi.go b/spi_linux.go similarity index 100% rename from rfm69spi.go rename to spi_linux.go diff --git a/spidummy.go b/spidummy.go new file mode 100644 index 0000000..cdd7bac --- /dev/null +++ b/spidummy.go @@ -0,0 +1,23 @@ +// +build !linux + +package rfm69 + +// SPIDevice is a device +type SPIDevice struct { +} + +// NewSPIDevice creates a new device +func NewSPIDevice() (*SPIDevice, error) { + return &SPIDevice{}, nil +} + +// Xfer cross transfer +func (d *SPIDevice) Xfer(tx []byte) ([]byte, error) { + length := len(tx) + rx := make([]byte, length) + return rx, nil +} + +// Close closes the fd +func (d *SPIDevice) Close() { +}