New SPI Device and main

master
Friedl Ulrich 10 years ago
parent ad23db2039
commit 4aea5666cb

@ -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)
}

@ -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
}

@ -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)
}
}
}

@ -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() {
}
Loading…
Cancel
Save