mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-05 23:43:41 +00:00
Compare commits
38 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 68d4991da1 | |||
| f2637a87d2 | |||
| ecb343b3b3 | |||
| 0b62d4fb4a | |||
| 5b461ec2d1 | |||
| b1c9612158 | |||
| 0b6e1d0e78 | |||
| 49e7a6d6c8 | |||
| be644b36fe | |||
| 409723e496 | |||
| 8aa3530c8b | |||
| 390854ee45 | |||
| ce5dd65dad | |||
| 73e9d5bfec | |||
| 462d2b3315 | |||
| 9e70ecbe64 | |||
| d6d06396b4 | |||
| 80b2a4a569 | |||
| 556aa9e6d3 | |||
| 4bc8ecdbdd | |||
| 999f42dc03 | |||
| a1b47cd778 | |||
| 889536f7f0 | |||
| 301e6bc35b | |||
| 49b390f7bc | |||
| a62fc01d81 | |||
| 36dfdf568e | |||
| 7b5a3970ca | |||
| 3433364586 | |||
| 338be928c9 | |||
| 98943393a5 | |||
| 6b817faed3 | |||
| 9b14ee3ec5 | |||
| 6d51370512 | |||
| 2005d7d483 | |||
| f15eec822f | |||
| dc4f0c974a | |||
| 28ddb2681d |
@@ -249,6 +249,9 @@ endif
|
|||||||
@md5sum ./build/test.uf2
|
@md5sum ./build/test.uf2
|
||||||
tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
|
tinygo build -size short -o ./build/test.uf2 -target=circuitplay-express ./examples/makeybutton/main.go
|
||||||
@md5sum ./build/test.uf2
|
@md5sum ./build/test.uf2
|
||||||
|
tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/lora/lorawan/atcmd/
|
||||||
|
@md5sum ./build/test.hex
|
||||||
|
|
||||||
|
|
||||||
# rwildcard is a recursive version of $(wildcard)
|
# rwildcard is a recursive version of $(wildcard)
|
||||||
# https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
|
# https://blog.jgc.org/2011/07/gnu-make-recursive-wildcard-function.html
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func main() {
|
|||||||
|
|
||||||
## Currently supported devices
|
## Currently supported devices
|
||||||
|
|
||||||
The following 90 devices are supported.
|
The following 91 devices are supported.
|
||||||
|
|
||||||
| Device Name | Interface Type |
|
| Device Name | Interface Type |
|
||||||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
|
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------|
|
||||||
@@ -63,6 +63,7 @@ The following 90 devices are supported.
|
|||||||
| [APA102 RGB LED](https://cdn-shop.adafruit.com/product-files/2343/APA102C.pdf) | SPI |
|
| [APA102 RGB LED](https://cdn-shop.adafruit.com/product-files/2343/APA102C.pdf) | SPI |
|
||||||
| [APDS9960 Digital proximity, ambient light, RGB and gesture sensor](https://cdn.sparkfun.com/assets/learn_tutorials/3/2/1/Avago-APDS-9960-datasheet.pdf) | I2C |
|
| [APDS9960 Digital proximity, ambient light, RGB and gesture sensor](https://cdn.sparkfun.com/assets/learn_tutorials/3/2/1/Avago-APDS-9960-datasheet.pdf) | I2C |
|
||||||
| [AT24CX 2-wire serial EEPROM](https://www.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/24C32-Datasheet.pdf) | I2C |
|
| [AT24CX 2-wire serial EEPROM](https://www.openimpulse.com/blog/wp-content/uploads/wpsc/downloadables/24C32-Datasheet.pdf) | I2C |
|
||||||
|
| [ATECCx08 cryptographic processor](https://datasheet.octopart.com/ATSAMA5D27-WLSOM1-Microchip-datasheet-149595509.pdf) | I2C |
|
||||||
| [AXP192 single Cell Li-Battery and Power System Management](https://github.com/m5stack/M5-Schematic/blob/master/Core/AXP192%20Datasheet_v1.1_en_draft_2211.pdf) | I2C |
|
| [AXP192 single Cell Li-Battery and Power System Management](https://github.com/m5stack/M5-Schematic/blob/master/Core/AXP192%20Datasheet_v1.1_en_draft_2211.pdf) | I2C |
|
||||||
| [BBC micro:bit LED matrix](https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf) | GPIO |
|
| [BBC micro:bit LED matrix](https://github.com/bbcmicrobit/hardware/blob/master/SCH_BBC-Microbit_V1.3B.pdf) | GPIO |
|
||||||
| [BH1750 ambient light sensor](https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf) | I2C |
|
| [BH1750 ambient light sensor](https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf) | I2C |
|
||||||
|
|||||||
@@ -0,0 +1,258 @@
|
|||||||
|
// Package ateccx08 provides a driver for the ATECCx08 I2C cryptographic co-processor.
|
||||||
|
//
|
||||||
|
// Datasheet: https://datasheet.octopart.com/ATSAMA5D27-WLSOM1-Microchip-datasheet-149595509.pdf
|
||||||
|
package ateccx08 // import "tinygo.org/x/drivers/ateccx08"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
maxCommandTime = (200 + 50) * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrWakeup = errors.New("error on wakeup")
|
||||||
|
ErrInvalidCRCCheck = errors.New("invalid CRC check")
|
||||||
|
ErrLockFailed = errors.New("locked failed")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
bus drivers.I2C
|
||||||
|
Address uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns ATECCx08 device for the provided I2C bus using default address.
|
||||||
|
func New(i2c drivers.I2C) *Device {
|
||||||
|
return &Device{
|
||||||
|
bus: i2c,
|
||||||
|
Address: Address,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure the ATECCx08 device.
|
||||||
|
func (d *Device) Configure() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connected returns whether ATECCx08 has been found.
|
||||||
|
func (d *Device) Connected() bool {
|
||||||
|
if err := d.Wakeup(); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := d.Version()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return (v == ATECC508 || v == ATECC608)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wakeup the ATECC by trying to write something to address 0x00
|
||||||
|
func (d *Device) Wakeup() error {
|
||||||
|
d.bus.Tx(uint16(0x0), []byte{0x00}, nil)
|
||||||
|
time.Sleep(1500 * time.Microsecond)
|
||||||
|
d.bus.Tx(uint16(d.Address), []byte{0x00}, nil)
|
||||||
|
time.Sleep(maxCommandTime)
|
||||||
|
|
||||||
|
var status [4]byte
|
||||||
|
if err := d.readResponse(status[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if status[0] != StatusAfterWake {
|
||||||
|
return ErrWakeup
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sleep puts the ATECC to sleep.
|
||||||
|
func (d *Device) Sleep() {
|
||||||
|
d.bus.Tx(uint16(d.Address), []byte{0x01}, nil)
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Idle puts the ATECC in idle mode.
|
||||||
|
func (d *Device) Idle() {
|
||||||
|
d.bus.Tx(uint16(d.Address), []byte{0x02}, nil)
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ATECCVersion uint16
|
||||||
|
|
||||||
|
func (at ATECCVersion) String() string {
|
||||||
|
switch at {
|
||||||
|
case ATECC508:
|
||||||
|
return "ATECC508"
|
||||||
|
case ATECC608:
|
||||||
|
return "ATECC608"
|
||||||
|
case ATECCNone:
|
||||||
|
return "No ATECCx08"
|
||||||
|
default:
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Version returns what version of ATECC is being used.
|
||||||
|
// Either ATECC508, ATECC608, or ATECCNone.
|
||||||
|
func (d *Device) Version() (ATECCVersion, error) {
|
||||||
|
var version [4]byte
|
||||||
|
d.Wakeup()
|
||||||
|
defer d.Idle()
|
||||||
|
|
||||||
|
d.sendCommand(cmdInfo, 0x00, 0, nil)
|
||||||
|
|
||||||
|
time.Sleep(maxCommandTime)
|
||||||
|
if err := d.readResponse(version[:]); err != nil {
|
||||||
|
return ATECCNone, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ATECCVersion(uint16(version[2])<<8 | uint16(version[3])&0xf000), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Random returns an array of 32 byte-sized random numbers.
|
||||||
|
func (d *Device) Random() ([32]byte, error) {
|
||||||
|
var random [32]byte
|
||||||
|
d.Wakeup()
|
||||||
|
defer d.Idle()
|
||||||
|
|
||||||
|
d.sendCommand(cmdRandom, 0x00, 0, nil)
|
||||||
|
time.Sleep(23 * time.Millisecond)
|
||||||
|
|
||||||
|
err := d.readResponse(random[:])
|
||||||
|
return random, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read reads from the device memory.
|
||||||
|
func (d *Device) Read(zone, address int, data []byte) error {
|
||||||
|
d.Wakeup()
|
||||||
|
defer d.Idle()
|
||||||
|
|
||||||
|
d.sendCommand(cmdRead, byte(zone), uint16(address), nil)
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
|
return d.readResponse(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsLocked checks to see if the ATECC is locked.
|
||||||
|
// Config zone (0) must be locked to generate random numbers.
|
||||||
|
func (d *Device) IsLocked() bool {
|
||||||
|
return d.IsZoneLocked(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsZoneLocked checks to see if a specific zone in the ATECC is locked.
|
||||||
|
func (d *Device) IsZoneLocked(zone int) bool {
|
||||||
|
var config [4]byte
|
||||||
|
|
||||||
|
if zone < 0 || zone > 8 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch zone {
|
||||||
|
case 0, 1:
|
||||||
|
if err := d.Read(0, 0x15, config[:]); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// LockConfig
|
||||||
|
loc := 3
|
||||||
|
|
||||||
|
// LockData
|
||||||
|
if zone == 1 {
|
||||||
|
loc = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
if config[loc] == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if err := d.Read(0, 0x16, config[:]); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
slot := byte(zone<<2) | 2
|
||||||
|
|
||||||
|
if (config[0] & slot) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock locks a zone in the device.
|
||||||
|
// Note that you cannot unlock a device zone once locked,
|
||||||
|
// so make sure you know what you are doing!
|
||||||
|
func (d *Device) Lock(zone int) error {
|
||||||
|
var status [1]byte
|
||||||
|
d.Wakeup()
|
||||||
|
defer d.Idle()
|
||||||
|
|
||||||
|
d.sendCommand(cmdLock, byte(zone)|0x80, 0, nil)
|
||||||
|
time.Sleep(32 * time.Millisecond)
|
||||||
|
|
||||||
|
d.readResponse(status[:])
|
||||||
|
|
||||||
|
if status[0] != 0 {
|
||||||
|
return ErrLockFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var cmdBuf [64]byte
|
||||||
|
|
||||||
|
func (d *Device) sendCommand(opcode, param1 byte, param2 uint16, data []byte) error {
|
||||||
|
cmdBuf[0] = 0x03
|
||||||
|
cmdBuf[1] = byte(8 + len(data) - 1)
|
||||||
|
cmdBuf[2] = opcode
|
||||||
|
cmdBuf[3] = param1
|
||||||
|
cmdBuf[4] = byte(param2 & 0xff)
|
||||||
|
cmdBuf[5] = byte(param2 >> 8)
|
||||||
|
copy(cmdBuf[6:], data)
|
||||||
|
|
||||||
|
crc := crc16(cmdBuf[1 : 6+len(data)])
|
||||||
|
cmdBuf[6+len(data)] = crc[0]
|
||||||
|
cmdBuf[6+len(data)+1] = crc[1]
|
||||||
|
|
||||||
|
if err := d.bus.Tx(uint16(d.Address), cmdBuf[:6+len(data)+2], nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Device) readResponse(data []byte) error {
|
||||||
|
var sz [1]byte
|
||||||
|
if err := d.bus.Tx(uint16(d.Address), []byte{cmdAddress}, sz[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rx := make([]byte, sz[0])
|
||||||
|
if err := d.bus.Tx(uint16(d.Address), []byte{cmdAddress}, rx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
size := len(rx) - 2
|
||||||
|
payload := rx[:size]
|
||||||
|
payloaddata := rx[1:size]
|
||||||
|
payloadcrc := rx[size:]
|
||||||
|
|
||||||
|
crcCheck := crc16(payload)
|
||||||
|
if !(crcCheck[0] == payloadcrc[0] &&
|
||||||
|
crcCheck[1] == payloadcrc[1]) {
|
||||||
|
return ErrInvalidCRCCheck
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(data, payloaddata)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
// from https://github.com/usbarmory/armoryctl/blob/master/atecc608/atecc608.go#L104
|
||||||
|
// thank you!
|
||||||
|
package ateccx08
|
||||||
|
|
||||||
|
const (
|
||||||
|
CRC16Poly uint16 = 0x8005
|
||||||
|
)
|
||||||
|
|
||||||
|
func crc16(data []byte) []byte {
|
||||||
|
var crc uint16
|
||||||
|
|
||||||
|
for i := 0; i < len(data); i++ {
|
||||||
|
for shift := uint8(0x01); shift > 0x00; shift <<= 1 {
|
||||||
|
// data and crc bits
|
||||||
|
var d uint8
|
||||||
|
var c uint8
|
||||||
|
|
||||||
|
if uint8(data[i])&uint8(shift) != 0 {
|
||||||
|
d = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
c = uint8(crc >> 15)
|
||||||
|
crc <<= 1
|
||||||
|
|
||||||
|
if d != c {
|
||||||
|
crc ^= CRC16Poly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return []byte{byte(crc & 0xff), byte(crc >> 8)}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package ateccx08
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Address is default I2C address.
|
||||||
|
Address = 0x60
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ATECCNone = 0
|
||||||
|
ATECC508 = 0x5000
|
||||||
|
ATECC608 = 0x6000
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cmdAddress = 0x03
|
||||||
|
cmdCounter = 0x24
|
||||||
|
cmdGenKey = 0x40
|
||||||
|
cmdInfo = 0x30
|
||||||
|
cmdLock = 0x17
|
||||||
|
cmdNonce = 0x16
|
||||||
|
cmdRandom = 0x1B
|
||||||
|
cmdSHA = 0x47
|
||||||
|
cmdSign = 0x41
|
||||||
|
cmdWrite = 0x12
|
||||||
|
cmdRead = 0x02
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
StatusSuccess = 0x00
|
||||||
|
StatusMiscompare = 0x01
|
||||||
|
StatusParseError = 0x03
|
||||||
|
StatusECCFault = 0x05
|
||||||
|
StatusSelfTestError = 0x07
|
||||||
|
StatusHealthTestError = 0x08
|
||||||
|
StatusExecutionError = 0x0f
|
||||||
|
StatusAfterWake = 0x11
|
||||||
|
StatusWatchdogExpire = 0xee
|
||||||
|
StatusCRCError = 0xff
|
||||||
|
)
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"encoding/hex"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/ateccx08"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
println("Looking for ATECCx08...")
|
||||||
|
|
||||||
|
machine.I2C0.Configure(machine.I2CConfig{})
|
||||||
|
|
||||||
|
atecc := ateccx08.New(machine.I2C0)
|
||||||
|
atecc.Configure()
|
||||||
|
|
||||||
|
if !atecc.Connected() {
|
||||||
|
for {
|
||||||
|
println("could not connect to ATECCx08")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
version, _ := atecc.Version()
|
||||||
|
|
||||||
|
println(version.String(), "started")
|
||||||
|
|
||||||
|
if !atecc.IsLocked() {
|
||||||
|
for i := 10; i > 0; i-- {
|
||||||
|
println(version.String(), "is not locked. Locking in", i, "seconds...")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// locks the Configuration zone... PERMANENTLY!
|
||||||
|
atecc.Lock(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
println(version.String(), "locked.")
|
||||||
|
|
||||||
|
for {
|
||||||
|
data, err := atecc.Random()
|
||||||
|
if err != nil {
|
||||||
|
println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
println(hex.EncodeToString(data[:]))
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/ateccx08"
|
||||||
|
)
|
||||||
|
|
||||||
|
var atecc *ateccx08.Device
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
println("Looking for ATECCx08...")
|
||||||
|
|
||||||
|
machine.I2C0.Configure(machine.I2CConfig{})
|
||||||
|
|
||||||
|
atecc = ateccx08.New(machine.I2C0)
|
||||||
|
atecc.Configure()
|
||||||
|
|
||||||
|
if !atecc.Connected() {
|
||||||
|
for {
|
||||||
|
println("could not connect to ATECCx08")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
version, _ := atecc.Version()
|
||||||
|
|
||||||
|
println(version.String(), "started")
|
||||||
|
|
||||||
|
if !atecc.IsLocked() {
|
||||||
|
for {
|
||||||
|
println(version.String(), "is not locked. Random numbers will not actually be random.")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var result [13]byte
|
||||||
|
for {
|
||||||
|
rand.Read(result[:])
|
||||||
|
encodedString := hex.EncodeToString(result[:])
|
||||||
|
println(encodedString)
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// connects the Go crypto/rand package to the random number generation
|
||||||
|
// on the ATECCx08 cryptographic processor.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errNoATECC = errors.New("no ATECCx08")
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Reader = &reader{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type reader struct{}
|
||||||
|
|
||||||
|
func (r *reader) Read(b []byte) (n int, err error) {
|
||||||
|
if len(b) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if atecc == nil {
|
||||||
|
return 0, errNoATECC
|
||||||
|
}
|
||||||
|
|
||||||
|
if !atecc.IsLocked() {
|
||||||
|
panic("ATECCx08 is not locked and cannot produce random numbers!")
|
||||||
|
}
|
||||||
|
|
||||||
|
rnds, err := atecc.Random()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(b); i += 32 {
|
||||||
|
if i+32 > len(b) {
|
||||||
|
copy(b[i:], rnds[:(len(b)-i)])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(b[i:], rnds[:])
|
||||||
|
rnds, err = atecc.Random()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
# AT-CMD implementation of at-lora command set
|
|
||||||
|
|
||||||
```
|
|
||||||
$ tinygo monitor
|
|
||||||
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
|
|
||||||
+AT: OK
|
|
||||||
+VER: 0.0.1 (sx127x v18)
|
|
||||||
```
|
|
||||||
|
|
||||||
# Building
|
|
||||||
|
|
||||||
## Simulator
|
|
||||||
|
|
||||||
```
|
|
||||||
tinygo flash -target pico ./examples/lora/atcmd/
|
|
||||||
```
|
|
||||||
|
|
||||||
## PyBadge with LoRa Featherwing
|
|
||||||
|
|
||||||
```
|
|
||||||
tinygo flash -target pybadge -tags featherwing ./examples/lora/atcmd/
|
|
||||||
```
|
|
||||||
|
|
||||||
## LoRa-E5
|
|
||||||
|
|
||||||
```
|
|
||||||
tinygo flash -target lorae5 ./examples/lora/atcmd/
|
|
||||||
```
|
|
||||||
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
const VERSION = "0.0.1"
|
|
||||||
|
|
||||||
func currentVersion() string {
|
|
||||||
return VERSION
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
//go:build nucleowl55jc
|
|
||||||
|
|
||||||
/*
|
|
||||||
Nucleo WL55JC1
|
|
||||||
RFSwitch
|
|
||||||
|
|
||||||
+-----------+---------+------------+------------+
|
|
||||||
| | FE_CTRL1 | FE_CTRL2 | FE_CTRL3 |
|
|
||||||
| | (PC4) | (PC5) | (PC3) |
|
|
||||||
+-----------+----------+-----------+------------+
|
|
||||||
| TX_HP | LOW | HIGH | HIGH |
|
|
||||||
| TX_LP | HIGH | HIGH | HIGH |
|
|
||||||
| RX | HIGH | LOW | HIGH |
|
|
||||||
+-----------+----------+-----------+------------+
|
|
||||||
*/
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"machine"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/sx126x"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CustomSwitch struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s CustomSwitch) InitRFSwitch() {
|
|
||||||
machine.PC4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
machine.PC5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
machine.PC3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s CustomSwitch) SetRfSwitchMode(mode int) error {
|
|
||||||
switch mode {
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_HP:
|
|
||||||
machine.PC4.Set(false)
|
|
||||||
machine.PC5.Set(true)
|
|
||||||
machine.PC3.Set(true)
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_LP:
|
|
||||||
machine.PC4.Set(true)
|
|
||||||
machine.PC5.Set(true)
|
|
||||||
machine.PC3.Set(true)
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_RX:
|
|
||||||
machine.PC4.Set(true)
|
|
||||||
machine.PC5.Set(false)
|
|
||||||
machine.PC3.Set(true)
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
# AT-CMD implementation of at-lora command set
|
||||||
|
|
||||||
|
This example implements the AT command set as used by Seeed in the LoRa-E5 series of boards, but in the form of a TinyGo program that provides a serial interface.
|
||||||
|
|
||||||
|
See https://files.seeedstudio.com/products/317990687/res/LoRa-E5%20AT%20Command%20Specification_V1.0%20.pdf for more information.
|
||||||
|
|
||||||
|
```
|
||||||
|
$ tinygo monitor
|
||||||
|
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
|
||||||
|
+AT: OK
|
||||||
|
+VER: 0.0.1 (sx127x v18)
|
||||||
|
```
|
||||||
|
|
||||||
|
# Building
|
||||||
|
|
||||||
|
Run the following commands from the main `drivers` directory.
|
||||||
|
|
||||||
|
## Simulator
|
||||||
|
|
||||||
|
Builds/flashes atcmd console application with simulator instead of actual LoRa radio.
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target pico ./examples/lora/lorawan/atcmd/
|
||||||
|
```
|
||||||
|
|
||||||
|
## PyBadge with LoRa Featherwing
|
||||||
|
|
||||||
|
Builds/flashes atcmd console application on PyBadge using LoRa Featherwing (RFM95/SX1276).
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target pybadge -tags featherwing ./examples/lora/lorawan/atcmd/
|
||||||
|
```
|
||||||
|
|
||||||
|
## LoRa-E5
|
||||||
|
|
||||||
|
Builds/flashes atcmd console application on Lora-E5 using onboard SX126x.
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target lorae5 ./examples/lora/lorawan/atcmd/
|
||||||
|
```
|
||||||
|
|
||||||
@@ -3,6 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/examples/lora/lorawan/common"
|
||||||
|
"tinygo.org/x/drivers/lora/lorawan"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Use to test if connection to module is OK.
|
// Use to test if connection to module is OK.
|
||||||
@@ -12,7 +15,7 @@ func quicktest() {
|
|||||||
|
|
||||||
// Check firmware version.
|
// Check firmware version.
|
||||||
func version() {
|
func version() {
|
||||||
writeCommandOutput("VER", currentVersion()+" ("+firmwareVersion()+")")
|
writeCommandOutput("VER", common.CurrentVersion()+" ("+common.FirmwareVersion()+")")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use to check the ID of the LoRaWAN module, or change the ID.
|
// Use to check the ID of the LoRaWAN module, or change the ID.
|
||||||
@@ -23,13 +26,42 @@ func id(args string) error {
|
|||||||
param, val, hasComma := strings.Cut(args, ",")
|
param, val, hasComma := strings.Cut(args, ",")
|
||||||
if hasComma {
|
if hasComma {
|
||||||
// set
|
// set
|
||||||
|
data := strings.Trim(val, "\"'")
|
||||||
|
|
||||||
|
// convert data from hex formatted string
|
||||||
|
data = strings.ReplaceAll(data, " ", "")
|
||||||
|
hexdata, err := hex.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
switch param {
|
switch param {
|
||||||
case "DevAddr":
|
case "DevAddr":
|
||||||
writeCommandOutput(cmd, "DevAddr, "+val)
|
if err := session.SetDevAddr(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
case "DevEui":
|
case "DevEui":
|
||||||
writeCommandOutput(cmd, "DevEui, "+val)
|
if err := otaa.SetDevEUI(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
case "AppEui":
|
case "AppEui":
|
||||||
writeCommandOutput(cmd, "AppEui, "+val)
|
if err := otaa.SetAppEUI(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
default:
|
default:
|
||||||
return errInvalidCommand
|
return errInvalidCommand
|
||||||
}
|
}
|
||||||
@@ -40,15 +72,15 @@ func id(args string) error {
|
|||||||
// get
|
// get
|
||||||
switch param {
|
switch param {
|
||||||
case "DevAddr":
|
case "DevAddr":
|
||||||
writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
case "DevEui":
|
case "DevEui":
|
||||||
writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
case "AppEui":
|
case "AppEui":
|
||||||
writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
default:
|
default:
|
||||||
writeCommandOutput(cmd, "DevAddr, xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
writeCommandOutput(cmd, "DevEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
writeCommandOutput(cmd, "AppEui, xx:xx:xx:xx:xx:xx:xx:xx")
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -208,9 +240,38 @@ func rxwin1(setting string) error {
|
|||||||
|
|
||||||
func key(setting string) error {
|
func key(setting string) error {
|
||||||
cmd := "KEY"
|
cmd := "KEY"
|
||||||
writeCommandOutput(cmd, setting)
|
|
||||||
|
|
||||||
return nil
|
// look for comma in args
|
||||||
|
param, val, hasComma := strings.Cut(setting, ",")
|
||||||
|
if hasComma {
|
||||||
|
// set
|
||||||
|
data := strings.Trim(val, "\"'")
|
||||||
|
|
||||||
|
// convert data from hex formatted string
|
||||||
|
data = strings.ReplaceAll(data, " ", "")
|
||||||
|
hexdata, err := hex.DecodeString(data)
|
||||||
|
if err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch param {
|
||||||
|
case "APPKEY":
|
||||||
|
if err := otaa.SetAppKey(hexdata); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "APPKEY, "+otaa.GetAppKey())
|
||||||
|
default:
|
||||||
|
return errInvalidCommand
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cannot get keys
|
||||||
|
return errInvalidCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
func fdefault(setting string) error {
|
func fdefault(setting string) error {
|
||||||
@@ -228,9 +289,23 @@ func mode(setting string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func join(setting string) error {
|
func join(setting string) error {
|
||||||
|
// TODO: check that DevEUI, AppEUI, and AppKey have values
|
||||||
|
|
||||||
cmd := "JOIN"
|
cmd := "JOIN"
|
||||||
writeCommandOutput(cmd, "Starting")
|
writeCommandOutput(cmd, "Starting")
|
||||||
|
if err := lorawan.Join(otaa, session); err != nil {
|
||||||
|
writeCommandOutput(cmd, err.Error())
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCommandOutput(cmd, "Network joined")
|
||||||
|
writeCommandOutput(cmd, "DevEui, "+otaa.GetDevEUI())
|
||||||
|
writeCommandOutput(cmd, "AppEui, "+otaa.GetAppEUI())
|
||||||
|
writeCommandOutput(cmd, "DevAddr, "+session.GetDevAddr())
|
||||||
|
writeCommandOutput(cmd, "NetID, "+otaa.GetNetID())
|
||||||
|
writeCommandOutput(cmd, "NwkSKey, "+session.GetNwkSKey())
|
||||||
|
writeCommandOutput(cmd, "AppSKey, "+session.GetAppSKey())
|
||||||
writeCommandOutput(cmd, "Done")
|
writeCommandOutput(cmd, "Done")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -376,7 +451,7 @@ func sendhex(data string) error {
|
|||||||
func recv(setting string) error {
|
func recv(setting string) error {
|
||||||
cmd := "RECV"
|
cmd := "RECV"
|
||||||
|
|
||||||
data, err := lorarx()
|
data, err := common.Lorarx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeCommandOutput(cmd, "ERROR "+err.Error())
|
writeCommandOutput(cmd, "ERROR "+err.Error())
|
||||||
return err
|
return err
|
||||||
@@ -389,7 +464,7 @@ func recv(setting string) error {
|
|||||||
func recvhex(setting string) error {
|
func recvhex(setting string) error {
|
||||||
cmd := "RECVHEX"
|
cmd := "RECVHEX"
|
||||||
|
|
||||||
data, err := lorarx()
|
data, err := common.Lorarx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
writeCommandOutput(cmd, "ERROR "+err.Error())
|
writeCommandOutput(cmd, "ERROR "+err.Error())
|
||||||
return err
|
return err
|
||||||
@@ -11,6 +11,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/examples/lora/lorawan/common"
|
||||||
|
"tinygo.org/x/drivers/lora"
|
||||||
|
"tinygo.org/x/drivers/lora/lorawan"
|
||||||
)
|
)
|
||||||
|
|
||||||
// change these to test a different UART or pins if available
|
// change these to test a different UART or pins if available
|
||||||
@@ -20,7 +24,10 @@ var (
|
|||||||
rx = machine.UART_RX_PIN
|
rx = machine.UART_RX_PIN
|
||||||
input = make([]byte, 0, 64)
|
input = make([]byte, 0, 64)
|
||||||
|
|
||||||
radio LoraRadio
|
radio lora.Radio
|
||||||
|
session *lorawan.Session
|
||||||
|
otaa *lorawan.Otaa
|
||||||
|
|
||||||
defaultTimeout uint32 = 1000
|
defaultTimeout uint32 = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,11 +35,15 @@ func main() {
|
|||||||
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
|
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
radio, err = setupLora()
|
radio, err = common.SetupLora()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err.Error())
|
fail(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session = &lorawan.Session{}
|
||||||
|
otaa = &lorawan.Otaa{}
|
||||||
|
lorawan.UseRadio(radio)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if uart.Buffered() > 0 {
|
if uart.Buffered() > 0 {
|
||||||
data, _ := uart.ReadByte()
|
data, _ := uart.ReadByte()
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
# Simple Lorawan example
|
||||||
|
|
||||||
|
This demo code will connect Lorawan network and send sample uplink message
|
||||||
|
|
||||||
|
You may change your Lorawan keys (AppEUI, DevEUI, AppKEY) in key-default.go
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
$ tinygo monitor
|
||||||
|
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
|
||||||
|
Lorawan Simple Demo
|
||||||
|
Start Lorawan Join sequence
|
||||||
|
loraConnect: Connected !
|
||||||
|
```
|
||||||
|
|
||||||
|
# Building
|
||||||
|
|
||||||
|
## Simulator
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target pico ./examples/lora/lorawan/basic-demo
|
||||||
|
```
|
||||||
|
|
||||||
|
## PyBadge with LoRa Featherwing
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target pybadge -tags featherwing ./examples/lora/lorawan/basic-demo
|
||||||
|
```
|
||||||
|
|
||||||
|
## LoRa-E5
|
||||||
|
|
||||||
|
```
|
||||||
|
tinygo flash -target lorae5 ./examples/lora/lorawan/basic-demo
|
||||||
|
```
|
||||||
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
//go:build !customkeys
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
// These are sample keys, so the example builds
|
||||||
|
// Either change here, or create a new go file and use customkeys build tag
|
||||||
|
func setLorawanKeys() {
|
||||||
|
otaa.SetAppEUI([]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
|
||||||
|
otaa.SetDevEUI([]uint8{0xB3, 0xD5, 0x41, 0x00, 0x0A, 0xF1, 0xA4, 0x45})
|
||||||
|
otaa.SetAppKey([]uint8{0x12, 0x22, 0xA3, 0xFF, 0x0C, 0x7B, 0x76, 0x7B, 0x8F, 0xD3, 0x12, 0x4F, 0xCE, 0x7A, 0x32, 0x16})
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
// Simple code for connecting to Lorawan network and uploading sample payload
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/examples/lora/lorawan/common"
|
||||||
|
"tinygo.org/x/drivers/lora"
|
||||||
|
"tinygo.org/x/drivers/lora/lorawan"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LORAWAN_JOIN_TIMEOUT_SEC = 180
|
||||||
|
LORAWAN_RECONNECT_DELAY_SEC = 15
|
||||||
|
LORAWAN_UPLINK_DELAY_SEC = 60
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
radio lora.Radio
|
||||||
|
session *lorawan.Session
|
||||||
|
otaa *lorawan.Otaa
|
||||||
|
)
|
||||||
|
|
||||||
|
func loraConnect() error {
|
||||||
|
start := time.Now()
|
||||||
|
var err error
|
||||||
|
for time.Since(start) < LORAWAN_JOIN_TIMEOUT_SEC*time.Second {
|
||||||
|
println("Trying to join network")
|
||||||
|
err = lorawan.Join(otaa, session)
|
||||||
|
if err == nil {
|
||||||
|
println("Connected to network !")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
println("Join error:", err, "retrying in", LORAWAN_RECONNECT_DELAY_SEC, "sec")
|
||||||
|
time.Sleep(time.Second * LORAWAN_RECONNECT_DELAY_SEC)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = errors.New("Unable to join Lorawan network")
|
||||||
|
println(err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func failMessage(err error) {
|
||||||
|
println("FATAL:", err)
|
||||||
|
for {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
println("*** Lorawan basic join and uplink demo ***")
|
||||||
|
|
||||||
|
// Board specific Lorawan initialization
|
||||||
|
var err error
|
||||||
|
radio, err = common.SetupLora()
|
||||||
|
if err != nil {
|
||||||
|
failMessage(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Required for LoraWan operations
|
||||||
|
session = &lorawan.Session{}
|
||||||
|
otaa = &lorawan.Otaa{}
|
||||||
|
|
||||||
|
// Initial Lora modulation configuration
|
||||||
|
loraConf := lora.Config{
|
||||||
|
Freq: 868100000,
|
||||||
|
Bw: lora.Bandwidth_125_0,
|
||||||
|
Sf: lora.SpreadingFactor9,
|
||||||
|
Cr: lora.CodingRate4_7,
|
||||||
|
HeaderType: lora.HeaderExplicit,
|
||||||
|
Preamble: 12,
|
||||||
|
Ldr: lora.LowDataRateOptimizeOff,
|
||||||
|
Iq: lora.IQStandard,
|
||||||
|
Crc: lora.CRCOn,
|
||||||
|
SyncWord: lora.SyncPublic,
|
||||||
|
LoraTxPowerDBm: 20,
|
||||||
|
}
|
||||||
|
radio.LoraConfig(loraConf)
|
||||||
|
|
||||||
|
// Connect the lorawan with the Lora Radio device.
|
||||||
|
lorawan.UseRadio(radio)
|
||||||
|
|
||||||
|
// Configure AppEUI, DevEUI, APPKey
|
||||||
|
setLorawanKeys()
|
||||||
|
|
||||||
|
// Try to connect Lorawan network
|
||||||
|
if err := loraConnect(); err != nil {
|
||||||
|
failMessage(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to periodicaly send an uplink sample message
|
||||||
|
upCount := 1
|
||||||
|
for {
|
||||||
|
payload := "Hello TinyGo #" + strconv.Itoa(upCount)
|
||||||
|
|
||||||
|
if err := lorawan.SendUplink([]byte(payload), session); err != nil {
|
||||||
|
println("Uplink error:", err)
|
||||||
|
} else {
|
||||||
|
println("Uplink success, msg=", payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
println("Sleeping for", LORAWAN_UPLINK_DELAY_SEC, "sec")
|
||||||
|
time.Sleep(time.Second * LORAWAN_UPLINK_DELAY_SEC)
|
||||||
|
upCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errRadioNotFound = errors.New("radio not found")
|
||||||
|
errRxTimeout = errors.New("radio RX timeout")
|
||||||
|
)
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
//go:build !featherwing && !gnse && !lorae5 && !nucleowl55jc
|
//go:build !featherwing && !stm32wlx && !sx126x
|
||||||
|
|
||||||
package main
|
package common
|
||||||
|
|
||||||
|
import "tinygo.org/x/drivers/lora"
|
||||||
|
|
||||||
// do simulator setup here
|
// do simulator setup here
|
||||||
func setupLora() (LoraRadio, error) {
|
func SetupLora() (lora.Radio, error) {
|
||||||
return &SimLoraRadio{}, nil
|
return &SimLoraRadio{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +29,12 @@ func (sr *SimLoraRadio) SetCodingRate(cr uint8) {}
|
|||||||
func (sr *SimLoraRadio) SetBandwidth(bw uint8) {}
|
func (sr *SimLoraRadio) SetBandwidth(bw uint8) {}
|
||||||
func (sr *SimLoraRadio) SetCrc(enable bool) {}
|
func (sr *SimLoraRadio) SetCrc(enable bool) {}
|
||||||
func (sr *SimLoraRadio) SetSpreadingFactor(sf uint8) {}
|
func (sr *SimLoraRadio) SetSpreadingFactor(sf uint8) {}
|
||||||
|
func (sr *SimLoraRadio) LoraConfig(cnf lora.Config) {}
|
||||||
|
|
||||||
func firmwareVersion() string {
|
func FirmwareVersion() string {
|
||||||
return "simulator " + currentVersion()
|
return "simulator " + CurrentVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
func lorarx() ([]byte, error) {
|
func Lorarx() ([]byte, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
//go:build stm32wlx
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/lora"
|
||||||
|
"tinygo.org/x/drivers/sx126x"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FREQ = 868100000
|
||||||
|
LORA_DEFAULT_RXTIMEOUT_MS = 1000
|
||||||
|
LORA_DEFAULT_TXTIMEOUT_MS = 5000
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
loraRadio *sx126x.Device
|
||||||
|
)
|
||||||
|
|
||||||
|
var spi = machine.SPI3
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl()
|
||||||
|
}
|
||||||
|
|
||||||
|
// do sx126x setup here
|
||||||
|
func SetupLora() (lora.Radio, error) {
|
||||||
|
loraRadio = sx126x.New(spi)
|
||||||
|
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
||||||
|
|
||||||
|
// Create radio controller for target
|
||||||
|
loraRadio.SetRadioController(newRadioControl())
|
||||||
|
|
||||||
|
if state := loraRadio.DetectDevice(); !state {
|
||||||
|
return nil, errRadioNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
loraConf := lora.Config{
|
||||||
|
Freq: FREQ,
|
||||||
|
Bw: lora.Bandwidth_500_0,
|
||||||
|
Sf: lora.SpreadingFactor9,
|
||||||
|
Cr: lora.CodingRate4_7,
|
||||||
|
HeaderType: lora.HeaderExplicit,
|
||||||
|
Preamble: 12,
|
||||||
|
Ldr: lora.LowDataRateOptimizeOff,
|
||||||
|
Iq: lora.IQStandard,
|
||||||
|
Crc: lora.CRCOn,
|
||||||
|
SyncWord: lora.SyncPrivate,
|
||||||
|
LoraTxPowerDBm: 20,
|
||||||
|
}
|
||||||
|
|
||||||
|
loraRadio.LoraConfig(loraConf)
|
||||||
|
|
||||||
|
return loraRadio, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FirmwareVersion() string {
|
||||||
|
return "sx126x"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Lorarx() ([]byte, error) {
|
||||||
|
return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
|
}
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
//go:build gnse || lorae5 || nucleowl55jc
|
//go:build !stm32wlx && sx126x
|
||||||
|
|
||||||
package main
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"device/stm32"
|
|
||||||
"machine"
|
"machine"
|
||||||
"runtime/interrupt"
|
|
||||||
|
|
||||||
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/lora"
|
"tinygo.org/x/drivers/lora"
|
||||||
"tinygo.org/x/drivers/sx126x"
|
"tinygo.org/x/drivers/sx126x"
|
||||||
@@ -23,23 +19,28 @@ var (
|
|||||||
loraRadio *sx126x.Device
|
loraRadio *sx126x.Device
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
spi = machine.SPI0
|
||||||
|
nssPin, busyPin, dio1Pin = machine.GP17, machine.GP10, machine.GP11
|
||||||
|
rxPin, txLowPin, txHighPin = machine.GP13, machine.GP12, machine.GP12
|
||||||
|
)
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl(nssPin, busyPin, dio1Pin, rxPin, txLowPin, txHighPin)
|
||||||
|
}
|
||||||
|
|
||||||
// do sx126x setup here
|
// do sx126x setup here
|
||||||
func setupLora() (LoraRadio, error) {
|
func SetupLora() (lora.Radio, error) {
|
||||||
loraRadio = sx126x.New(machine.SPI3)
|
loraRadio = sx126x.New(spi)
|
||||||
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
||||||
|
|
||||||
// Create RF Switch
|
// Create radio controller for target
|
||||||
var radioSwitch rfswitch.CustomSwitch
|
loraRadio.SetRadioController(newRadioControl())
|
||||||
loraRadio.SetRfSwitch(radioSwitch)
|
|
||||||
|
|
||||||
if state := loraRadio.DetectDevice(); !state {
|
if state := loraRadio.DetectDevice(); !state {
|
||||||
return nil, errRadioNotFound
|
return nil, errRadioNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add interrupt handler for Radio IRQs
|
|
||||||
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, radioIntHandler)
|
|
||||||
intr.Enable()
|
|
||||||
|
|
||||||
loraConf := lora.Config{
|
loraConf := lora.Config{
|
||||||
Freq: FREQ,
|
Freq: FREQ,
|
||||||
Bw: lora.Bandwidth_500_0,
|
Bw: lora.Bandwidth_500_0,
|
||||||
@@ -59,15 +60,10 @@ func setupLora() (LoraRadio, error) {
|
|||||||
return loraRadio, nil
|
return loraRadio, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// radioIntHandler will take care of radio interrupts
|
func FirmwareVersion() string {
|
||||||
func radioIntHandler(intr interrupt.Interrupt) {
|
|
||||||
loraRadio.HandleInterrupt()
|
|
||||||
}
|
|
||||||
|
|
||||||
func firmwareVersion() string {
|
|
||||||
return "sx126x"
|
return "sx126x"
|
||||||
}
|
}
|
||||||
|
|
||||||
func lorarx() ([]byte, error) {
|
func Lorarx() ([]byte, error) {
|
||||||
return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build featherwing
|
//go:build featherwing
|
||||||
|
|
||||||
package main
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -28,7 +28,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// do sx127x setup here
|
// do sx127x setup here
|
||||||
func setupLora() (LoraRadio, error) {
|
func SetupLora() (lora.Radio, error) {
|
||||||
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
dio0Pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
dio0Pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
@@ -55,14 +55,14 @@ func setupLora() (LoraRadio, error) {
|
|||||||
// Prepare for Lora Operation
|
// Prepare for Lora Operation
|
||||||
loraConf := lora.Config{
|
loraConf := lora.Config{
|
||||||
Freq: FREQ,
|
Freq: FREQ,
|
||||||
Bw: lora.Bandwidth_500_0,
|
Bw: lora.Bandwidth_125_0,
|
||||||
Sf: lora.SpreadingFactor9,
|
Sf: lora.SpreadingFactor9,
|
||||||
Cr: lora.CodingRate4_7,
|
Cr: lora.CodingRate4_7,
|
||||||
HeaderType: lora.HeaderExplicit,
|
HeaderType: lora.HeaderExplicit,
|
||||||
Preamble: 12,
|
Preamble: 12,
|
||||||
Iq: lora.IQStandard,
|
Iq: lora.IQStandard,
|
||||||
Crc: lora.CRCOn,
|
Crc: lora.CRCOn,
|
||||||
SyncWord: lora.SyncPrivate,
|
SyncWord: lora.SyncPublic,
|
||||||
LoraTxPowerDBm: 20,
|
LoraTxPowerDBm: 20,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,11 +75,11 @@ func dioIrqHandler(machine.Pin) {
|
|||||||
loraRadio.HandleInterrupt()
|
loraRadio.HandleInterrupt()
|
||||||
}
|
}
|
||||||
|
|
||||||
func firmwareVersion() string {
|
func FirmwareVersion() string {
|
||||||
v := loraRadio.GetVersion()
|
v := loraRadio.GetVersion()
|
||||||
return "sx127x v" + strconv.Itoa(int(v))
|
return "sx127x v" + strconv.Itoa(int(v))
|
||||||
}
|
}
|
||||||
|
|
||||||
func lorarx() ([]byte, error) {
|
func Lorarx() ([]byte, error) {
|
||||||
return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
const VERSION = "0.0.1"
|
||||||
|
|
||||||
|
func CurrentVersion() string {
|
||||||
|
return VERSION
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@ import (
|
|||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/lora"
|
"tinygo.org/x/drivers/lora"
|
||||||
"tinygo.org/x/drivers/sx126x"
|
"tinygo.org/x/drivers/sx126x"
|
||||||
)
|
)
|
||||||
@@ -24,12 +22,11 @@ func main() {
|
|||||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
// Create the driver
|
// Create the driver
|
||||||
loraRadio = sx126x.New(machine.SPI3)
|
loraRadio = sx126x.New(spi)
|
||||||
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
||||||
|
|
||||||
// Create RF Switch
|
// Create radio controller for target
|
||||||
var radioSwitch rfswitch.CustomSwitch
|
loraRadio.SetRadioController(newRadioControl())
|
||||||
loraRadio.SetRfSwitch(radioSwitch)
|
|
||||||
|
|
||||||
state := loraRadio.DetectDevice()
|
state := loraRadio.DetectDevice()
|
||||||
if !state {
|
if !state {
|
||||||
@@ -76,6 +73,5 @@ func main() {
|
|||||||
|
|
||||||
loraRadio.SetStandby()
|
loraRadio.SetStandby()
|
||||||
time.Sleep(60 * time.Second)
|
time.Sleep(60 * time.Second)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//go:build !stm32wlx
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/sx126x"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
spi = machine.SPI0
|
||||||
|
nssPin, busyPin, dio1Pin = machine.GP17, machine.GP10, machine.GP11
|
||||||
|
rxPin, txLowPin, txHighPin = machine.GP13, machine.GP12, machine.GP12
|
||||||
|
)
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl(nssPin, busyPin, dio1Pin, rxPin, txLowPin, txHighPin)
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build stm32wlx
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/sx126x"
|
||||||
|
)
|
||||||
|
|
||||||
|
var spi = machine.SPI3
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl()
|
||||||
|
}
|
||||||
@@ -4,13 +4,9 @@ package main
|
|||||||
// module will be in RX mode between two transmissions
|
// module will be in RX mode between two transmissions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"device/stm32"
|
|
||||||
"machine"
|
"machine"
|
||||||
"runtime/interrupt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/lora"
|
"tinygo.org/x/drivers/lora"
|
||||||
"tinygo.org/x/drivers/sx126x"
|
"tinygo.org/x/drivers/sx126x"
|
||||||
)
|
)
|
||||||
@@ -27,23 +23,19 @@ var (
|
|||||||
txmsg = []byte("Hello TinyGO")
|
txmsg = []byte("Hello TinyGO")
|
||||||
)
|
)
|
||||||
|
|
||||||
// radioIntHandler will take care of radio interrupts
|
|
||||||
func radioIntHandler(intr interrupt.Interrupt) {
|
|
||||||
loraRadio.HandleInterrupt()
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
println("\n# TinyGo Lora RX/TX test")
|
println("\n# TinyGo Lora RX/TX test")
|
||||||
println("# ----------------------")
|
println("# ----------------------")
|
||||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
// Create the driver
|
// Create the driver
|
||||||
loraRadio = sx126x.New(machine.SPI3)
|
loraRadio = sx126x.New(spi)
|
||||||
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
|
||||||
|
|
||||||
// Create RF Switch
|
// Create radio controller for target
|
||||||
var radioSwitch rfswitch.CustomSwitch
|
loraRadio.SetRadioController(newRadioControl())
|
||||||
loraRadio.SetRfSwitch(radioSwitch)
|
|
||||||
|
|
||||||
// Detect the device
|
// Detect the device
|
||||||
state := loraRadio.DetectDevice()
|
state := loraRadio.DetectDevice()
|
||||||
@@ -51,10 +43,6 @@ func main() {
|
|||||||
panic("sx126x not detected.")
|
panic("sx126x not detected.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add interrupt handler for Radio IRQs
|
|
||||||
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, radioIntHandler)
|
|
||||||
intr.Enable()
|
|
||||||
|
|
||||||
loraConf := lora.Config{
|
loraConf := lora.Config{
|
||||||
Freq: FREQ,
|
Freq: FREQ,
|
||||||
Bw: lora.Bandwidth_500_0,
|
Bw: lora.Bandwidth_500_0,
|
||||||
@@ -73,10 +61,10 @@ func main() {
|
|||||||
|
|
||||||
var count uint
|
var count uint
|
||||||
for {
|
for {
|
||||||
tStart := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
println("main: Receiving Lora for 10 seconds")
|
println("main: Receiving Lora for 10 seconds")
|
||||||
for int(time.Now().Sub(tStart).Seconds()) < 10 {
|
for time.Since(start) < 10*time.Second {
|
||||||
buf, err := loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
buf, err := loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("RX Error: ", err)
|
println("RX Error: ", err)
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
//go:build !stm32wlx
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/sx126x"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
spi = machine.SPI1
|
||||||
|
nssPin, busyPin, dio1Pin = machine.GP13, machine.GP6, machine.GP7
|
||||||
|
rxPin, txLowPin, txHighPin = machine.GP9, machine.GP8, machine.GP8
|
||||||
|
)
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl(nssPin, busyPin, dio1Pin, rxPin, txLowPin, txHighPin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
spi.Configure(machine.SPIConfig{
|
||||||
|
Mode: 0,
|
||||||
|
Frequency: 8 * 1e6,
|
||||||
|
SDO: machine.SPI1_SDO_PIN,
|
||||||
|
SDI: machine.SPI1_SDI_PIN,
|
||||||
|
SCK: machine.SPI1_SCK_PIN,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build stm32wlx
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/sx126x"
|
||||||
|
)
|
||||||
|
|
||||||
|
var spi = machine.SPI3
|
||||||
|
|
||||||
|
func newRadioControl() sx126x.RadioController {
|
||||||
|
return sx126x.NewRadioControl()
|
||||||
|
}
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
//go:build gnse
|
|
||||||
|
|
||||||
/*
|
|
||||||
Generic Node Sensor Edition
|
|
||||||
RFSwitch
|
|
||||||
|
|
||||||
Disable Switch : PB8=OFF PA0=OFF PA1=OFF
|
|
||||||
Enable RX : PB8=ON PA0=ON PA1=OFF
|
|
||||||
Enable TX RFO LP : PB8=ON PA0=ON PA1=ON
|
|
||||||
Enable TX RFO HP : PB8=ON PA0=OFF PA1=ON
|
|
||||||
*/
|
|
||||||
package rfswitch
|
|
||||||
|
|
||||||
import (
|
|
||||||
"machine"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/sx126x"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CustomSwitch struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
rfstate int
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s CustomSwitch) InitRFSwitch() {
|
|
||||||
machine.RF_FE_CTRL1.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
machine.RF_FE_CTRL2.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
machine.RF_FE_CTRL3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
rfstate = -1 //Unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s CustomSwitch) SetRfSwitchMode(mode int) error {
|
|
||||||
if mode == rfstate {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch mode {
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_HP:
|
|
||||||
machine.RF_FE_CTRL1.Set(false)
|
|
||||||
machine.RF_FE_CTRL2.Set(true)
|
|
||||||
machine.RF_FE_CTRL3.Set(true)
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_LP:
|
|
||||||
machine.RF_FE_CTRL1.Set(true)
|
|
||||||
machine.RF_FE_CTRL2.Set(true)
|
|
||||||
machine.RF_FE_CTRL3.Set(true)
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_RX:
|
|
||||||
machine.RF_FE_CTRL1.Set(true)
|
|
||||||
machine.RF_FE_CTRL2.Set(false)
|
|
||||||
machine.RF_FE_CTRL3.Set(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
rfstate = mode
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
//go:build lorae5
|
|
||||||
|
|
||||||
package radio
|
|
||||||
|
|
||||||
/*
|
|
||||||
/!\ LoRa-E5 module ONLY transmits through RFO_HP:
|
|
||||||
|
|
||||||
Receive: PA4=1, PA5=0
|
|
||||||
Transmit(high output power, SMPS mode): PA4=0, PA5=1
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"machine"
|
|
||||||
|
|
||||||
"tinygo.org/x/drivers/sx126x"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CustomSwitch struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s CustomSwitch) InitRFSwitch() {
|
|
||||||
machine.PA4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
machine.PB5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s CustomSwitch) SetRfSwitchMode(mode int) error {
|
|
||||||
switch mode {
|
|
||||||
|
|
||||||
case sx126x.RFSWITCH_RX:
|
|
||||||
machine.PA4.Set(true)
|
|
||||||
machine.PB5.Set(false)
|
|
||||||
case sx126x.RFSWITCH_TX_LP:
|
|
||||||
errors.New("RFSWITCH_TX_LP not supported ")
|
|
||||||
case sx126x.RFSWITCH_TX_HP:
|
|
||||||
machine.PA4.Set(false)
|
|
||||||
machine.PB5.Set(true)
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
+46
-6
@@ -13,6 +13,39 @@ import (
|
|||||||
var (
|
var (
|
||||||
errInvalidNMEASentenceLength = errors.New("invalid NMEA sentence length")
|
errInvalidNMEASentenceLength = errors.New("invalid NMEA sentence length")
|
||||||
errInvalidNMEAChecksum = errors.New("invalid NMEA sentence checksum")
|
errInvalidNMEAChecksum = errors.New("invalid NMEA sentence checksum")
|
||||||
|
errEmptyNMEASentence = errors.New("cannot parse empty NMEA sentence")
|
||||||
|
errUnknownNMEASentence = errors.New("unsupported NMEA sentence type")
|
||||||
|
errInvalidGGASentence = errors.New("invalid GGA NMEA sentence")
|
||||||
|
errInvalidRMCSentence = errors.New("invalid RMC NMEA sentence")
|
||||||
|
errInvalidGLLSentence = errors.New("invalid GLL NMEA sentence")
|
||||||
|
)
|
||||||
|
|
||||||
|
type GPSError struct {
|
||||||
|
Err error
|
||||||
|
Info string
|
||||||
|
Sentence string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGPSError(err error, sentence string, info string) GPSError {
|
||||||
|
return GPSError{
|
||||||
|
Info: info,
|
||||||
|
Err: err,
|
||||||
|
Sentence: sentence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ge GPSError) Error() string {
|
||||||
|
return ge.Err.Error() + " " + ge.Info + " " + ge.Sentence
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ge GPSError) Unwrap() error {
|
||||||
|
return ge.Err
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
minimumNMEALength = 7
|
||||||
|
startingDelimiter = '$'
|
||||||
|
checksumDelimiter = '*'
|
||||||
)
|
)
|
||||||
|
|
||||||
// Device wraps a connection to a GPS device.
|
// Device wraps a connection to a GPS device.
|
||||||
@@ -60,11 +93,11 @@ func (gps *Device) readNextSentence() (sentence string) {
|
|||||||
gps.sentence.Reset()
|
gps.sentence.Reset()
|
||||||
var b byte = ' '
|
var b byte = ' '
|
||||||
|
|
||||||
for b != '$' {
|
for b != startingDelimiter {
|
||||||
b = gps.readNextByte()
|
b = gps.readNextByte()
|
||||||
}
|
}
|
||||||
|
|
||||||
for b != '*' {
|
for b != checksumDelimiter {
|
||||||
gps.sentence.WriteByte(b)
|
gps.sentence.WriteByte(b)
|
||||||
b = gps.readNextByte()
|
b = gps.readNextByte()
|
||||||
}
|
}
|
||||||
@@ -126,17 +159,24 @@ func (gps *Device) WriteBytes(bytes []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// validSentence checks if a sentence has been received uncorrupted
|
// validSentence checks if a sentence has been received uncorrupted
|
||||||
|
// For example, a valid NMEA sentence such as this:
|
||||||
|
// $GPGLL,3751.65,S,14507.36,E*77
|
||||||
|
// It has to start with a '$' character.
|
||||||
|
// It has to have a 5 character long sentence identifier.
|
||||||
|
// It has to end with a '*' character following by a checksum.
|
||||||
func validSentence(sentence string) error {
|
func validSentence(sentence string) error {
|
||||||
if len(sentence) < 4 || sentence[0] != '$' || sentence[len(sentence)-3] != '*' {
|
if len(sentence) < minimumNMEALength || sentence[0] != startingDelimiter || sentence[len(sentence)-3] != checksumDelimiter {
|
||||||
return errInvalidNMEASentenceLength
|
return errInvalidNMEASentenceLength
|
||||||
}
|
}
|
||||||
var cs byte = 0
|
var cs byte = 0
|
||||||
for i := 1; i < len(sentence)-3; i++ {
|
for i := 1; i < len(sentence)-3; i++ {
|
||||||
cs ^= sentence[i]
|
cs ^= sentence[i]
|
||||||
}
|
}
|
||||||
checksum := hex.EncodeToString([]byte{cs})
|
checksum := strings.ToUpper(hex.EncodeToString([]byte{cs}))
|
||||||
if (checksum[0] != sentence[len(sentence)-2]) || (checksum[1] != sentence[len(sentence)-1]) {
|
if checksum != sentence[len(sentence)-2:len(sentence)] {
|
||||||
return errInvalidNMEAChecksum
|
return newGPSError(errInvalidNMEAChecksum, sentence,
|
||||||
|
"expected "+sentence[len(sentence)-2:len(sentence)]+
|
||||||
|
" got "+checksum)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
+47
-32
@@ -1,19 +1,11 @@
|
|||||||
package gps
|
package gps
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
errEmptyNMEASentence = errors.New("cannot parse empty NMEA sentence")
|
|
||||||
errUnknownNMEASentence = errors.New("unsupported NMEA sentence type")
|
|
||||||
errInvalidGGASentence = errors.New("invalid GGA NMEA sentence")
|
|
||||||
errInvalidRMCSentence = errors.New("invalid RMC NMEA sentence")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Parser for GPS NMEA sentences.
|
// Parser for GPS NMEA sentences.
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
}
|
}
|
||||||
@@ -51,43 +43,63 @@ func NewParser() Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses a NMEA sentence looking for fix info.
|
// Parse parses a NMEA sentence looking for fix info.
|
||||||
func (parser *Parser) Parse(sentence string) (fix Fix, err error) {
|
func (parser *Parser) Parse(sentence string) (Fix, error) {
|
||||||
|
var fix Fix
|
||||||
if sentence == "" {
|
if sentence == "" {
|
||||||
err = errEmptyNMEASentence
|
return fix, errEmptyNMEASentence
|
||||||
return
|
}
|
||||||
|
if len(sentence) < 6 {
|
||||||
|
return fix, errInvalidNMEASentenceLength
|
||||||
}
|
}
|
||||||
typ := sentence[3:6]
|
typ := sentence[3:6]
|
||||||
switch typ {
|
switch typ {
|
||||||
case "GGA":
|
case "GGA":
|
||||||
|
// https://docs.novatel.com/OEM7/Content/Logs/GPGGA.htm
|
||||||
fields := strings.Split(sentence, ",")
|
fields := strings.Split(sentence, ",")
|
||||||
if len(fields) != 15 {
|
if len(fields) != 15 {
|
||||||
err = errInvalidGGASentence
|
return fix, errInvalidGGASentence
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fix.Altitude = findAltitude(fields[9])
|
|
||||||
fix.Satellites = findSatellites(fields[7])
|
|
||||||
fix.Longitude = findLongitude(fields[4], fields[5])
|
|
||||||
fix.Latitude = findLatitude(fields[2], fields[3])
|
|
||||||
fix.Time = findTime(fields[1])
|
fix.Time = findTime(fields[1])
|
||||||
|
fix.Latitude = findLatitude(fields[2], fields[3])
|
||||||
|
fix.Longitude = findLongitude(fields[4], fields[5])
|
||||||
|
fix.Satellites = findSatellites(fields[7])
|
||||||
|
fix.Altitude = findAltitude(fields[9])
|
||||||
fix.Valid = (fix.Altitude != -99999) && (fix.Satellites > 0)
|
fix.Valid = (fix.Altitude != -99999) && (fix.Satellites > 0)
|
||||||
|
|
||||||
|
return fix, nil
|
||||||
|
case "GLL":
|
||||||
|
// https://docs.novatel.com/OEM7/Content/Logs/GPGLL.htm
|
||||||
|
fields := strings.Split(sentence, ",")
|
||||||
|
if len(fields) != 8 {
|
||||||
|
return fix, errInvalidGLLSentence
|
||||||
|
}
|
||||||
|
|
||||||
|
fix.Latitude = findLatitude(fields[1], fields[2])
|
||||||
|
fix.Longitude = findLongitude(fields[3], fields[4])
|
||||||
|
fix.Time = findTime(fields[5])
|
||||||
|
|
||||||
|
fix.Valid = (fields[6] == "A")
|
||||||
|
|
||||||
|
return fix, nil
|
||||||
case "RMC":
|
case "RMC":
|
||||||
|
// https://docs.novatel.com/OEM7/Content/Logs/GPRMC.htm
|
||||||
fields := strings.Split(sentence, ",")
|
fields := strings.Split(sentence, ",")
|
||||||
if len(fields) != 13 {
|
if len(fields) != 13 {
|
||||||
err = errInvalidRMCSentence
|
return fix, errInvalidRMCSentence
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fix.Longitude = findLongitude(fields[5], fields[6])
|
|
||||||
fix.Latitude = findLatitude(fields[3], fields[4])
|
|
||||||
fix.Time = findTime(fields[1])
|
fix.Time = findTime(fields[1])
|
||||||
|
fix.Valid = (fields[2] == "A")
|
||||||
|
fix.Latitude = findLatitude(fields[3], fields[4])
|
||||||
|
fix.Longitude = findLongitude(fields[5], fields[6])
|
||||||
fix.Speed = findSpeed(fields[7])
|
fix.Speed = findSpeed(fields[7])
|
||||||
fix.Heading = findHeading(fields[8])
|
fix.Heading = findHeading(fields[8])
|
||||||
fix.Valid = (len(fields[2]) > 0 && fields[2][0:1] == "A")
|
|
||||||
default:
|
return fix, nil
|
||||||
err = errUnknownNMEASentence
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
return fix, newGPSError(errUnknownNMEASentence, sentence, typ)
|
||||||
}
|
}
|
||||||
|
|
||||||
// findTime returns the time from an NMEA sentence:
|
// findTime returns the time from an NMEA sentence:
|
||||||
@@ -100,7 +112,10 @@ func findTime(val string) time.Time {
|
|||||||
h, _ := strconv.ParseInt(val[0:2], 10, 8)
|
h, _ := strconv.ParseInt(val[0:2], 10, 8)
|
||||||
m, _ := strconv.ParseInt(val[2:4], 10, 8)
|
m, _ := strconv.ParseInt(val[2:4], 10, 8)
|
||||||
s, _ := strconv.ParseInt(val[4:6], 10, 8)
|
s, _ := strconv.ParseInt(val[4:6], 10, 8)
|
||||||
ms, _ := strconv.ParseInt(val[7:10], 10, 16)
|
ms := int64(0)
|
||||||
|
if len(val) > 6 {
|
||||||
|
ms, _ = strconv.ParseInt(val[7:], 10, 16)
|
||||||
|
}
|
||||||
t := time.Date(0, 0, 0, int(h), int(m), int(s), int(ms), time.UTC)
|
t := time.Date(0, 0, 0, int(h), int(m), int(s), int(ms), time.UTC)
|
||||||
|
|
||||||
return t
|
return t
|
||||||
@@ -120,11 +135,11 @@ func findAltitude(val string) int32 {
|
|||||||
// $--GGA,,ddmm.mmmmm,x,,,,,,,,,,,*hh
|
// $--GGA,,ddmm.mmmmm,x,,,,,,,,,,,*hh
|
||||||
func findLatitude(val, hemi string) float32 {
|
func findLatitude(val, hemi string) float32 {
|
||||||
if len(val) > 8 {
|
if len(val) > 8 {
|
||||||
var dd = val[0:2]
|
dd := val[0:2]
|
||||||
var mm = val[2:]
|
mm := val[2:]
|
||||||
var d, _ = strconv.ParseFloat(dd, 32)
|
d, _ := strconv.ParseFloat(dd, 32)
|
||||||
var m, _ = strconv.ParseFloat(mm, 32)
|
m, _ := strconv.ParseFloat(mm, 32)
|
||||||
var v = float32(d + (m / 60))
|
v := float32(d + (m / 60))
|
||||||
if hemi == "S" {
|
if hemi == "S" {
|
||||||
v *= -1
|
v *= -1
|
||||||
}
|
}
|
||||||
@@ -133,7 +148,7 @@ func findLatitude(val, hemi string) float32 {
|
|||||||
return 0.0
|
return 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
// findLatitude returns the longitude from an NMEA sentence:
|
// findLongitude returns the longitude from an NMEA sentence:
|
||||||
// $--GGA,,,,dddmm.mmmmm,x,,,,,,,,,*hh
|
// $--GGA,,,,dddmm.mmmmm,x,,,,,,,,,*hh
|
||||||
func findLongitude(val, hemi string) float32 {
|
func findLongitude(val, hemi string) float32 {
|
||||||
if len(val) > 8 {
|
if len(val) > 8 {
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package gps
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
qt "github.com/frankban/quicktest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseUnknownSentence(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
p := NewParser()
|
||||||
|
|
||||||
|
val := "$GPGSV,3,1,09,07,14,317,22,08,31,284,25,10,32,133,39,16,85,232,29*7F"
|
||||||
|
_, err := p.Parse(val)
|
||||||
|
c.Assert(err.Error(), qt.Contains, "unsupported NMEA sentence type")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseGGA(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
p := NewParser()
|
||||||
|
|
||||||
|
val := "$GPGGA,115739.00,4158.8441367,N,09147.4416929,"
|
||||||
|
fix, err := p.Parse(val)
|
||||||
|
if err != errInvalidGGASentence {
|
||||||
|
t.Error("should have errInvalidGGASentence error")
|
||||||
|
}
|
||||||
|
|
||||||
|
val = "$GPGGA,115739.00,4158.8441367,N,09147.4416929,W,4,13,0.9,255.747,M,-32.00,M,01,0000*6E"
|
||||||
|
fix, err = p.Parse(val)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should have parsed")
|
||||||
|
}
|
||||||
|
c.Assert(fix.Latitude, qt.Equals, float32(41.980735778808594))
|
||||||
|
c.Assert(fix.Longitude, qt.Equals, float32(-91.79069519042969))
|
||||||
|
c.Assert(fix.Altitude, qt.Equals, int32(255))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseGLL(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
p := NewParser()
|
||||||
|
|
||||||
|
val := "$GPGLL,3953.88008971,N,10506.7531891"
|
||||||
|
_, err := p.Parse(val)
|
||||||
|
if err != errInvalidGLLSentence {
|
||||||
|
t.Error("should have errInvalidGLLSentence error")
|
||||||
|
}
|
||||||
|
|
||||||
|
val = "$GPGLL,5109.0262317,N,11401.8407304,W,202725.00,A,D*79"
|
||||||
|
fix, err := p.Parse(val)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should have parsed")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Assert(fix.Latitude, qt.Equals, float32(51.15043640136719))
|
||||||
|
c.Assert(fix.Longitude, qt.Equals, float32(-114.03067779541016))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseRMC(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
p := NewParser()
|
||||||
|
|
||||||
|
val := "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,"
|
||||||
|
_, err := p.Parse(val)
|
||||||
|
if err != errInvalidRMCSentence {
|
||||||
|
t.Error("should have errInvalidRMCSentence error")
|
||||||
|
}
|
||||||
|
|
||||||
|
val = "$GPRMC,203522.00,A,5109.0262308,N,11401.8407342,W,0.004,133.4,130522,0.0,E,D*2B"
|
||||||
|
fix, err := p.Parse(val)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("should have parsed")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Assert(fix.Latitude, qt.Equals, float32(51.15043640136719))
|
||||||
|
c.Assert(fix.Longitude, qt.Equals, float32(-114.03067779541016))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTime(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
val := ""
|
||||||
|
tm := findTime(val)
|
||||||
|
c.Assert(tm, qt.Equals, time.Time{})
|
||||||
|
|
||||||
|
val = "225446"
|
||||||
|
tm = findTime(val)
|
||||||
|
c.Assert(tm, qt.Equals, time.Date(0, 0, 0, 22, 54, 46, 0, time.UTC))
|
||||||
|
|
||||||
|
val = "124326.02752"
|
||||||
|
tm = findTime(val)
|
||||||
|
c.Assert(tm, qt.Equals, time.Date(0, 0, 0, 12, 43, 26, 2752, time.UTC))
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"tinygo.org/x/drivers/lora"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoJoinAcceptReceived = errors.New("no JoinAccept packet received")
|
||||||
|
ErrNoRadioAttached = errors.New("no LoRa radio attached")
|
||||||
|
ErrInvalidEuiLength = errors.New("invalid EUI length")
|
||||||
|
ErrInvalidAppKeyLength = errors.New("invalid AppKey length")
|
||||||
|
ErrInvalidPacketLength = errors.New("invalid packet length")
|
||||||
|
ErrInvalidDevAddrLength = errors.New("invalid DevAddr length")
|
||||||
|
ErrInvalidMic = errors.New("invalid Mic")
|
||||||
|
ErrFrmPayloadTooLarge = errors.New("FRM payload too large")
|
||||||
|
ErrInvalidNetIDLength = errors.New("invalid NetID length")
|
||||||
|
ErrInvalidNwkSKeyLength = errors.New("invalid NwkSKey length")
|
||||||
|
ErrInvalidAppSKeyLength = errors.New("invalid AppSKey length")
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LORA_TX_TIMEOUT = 2000
|
||||||
|
LORA_RX_TIMEOUT = 5000
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ActiveRadio lora.Radio
|
||||||
|
Retries = 15
|
||||||
|
)
|
||||||
|
|
||||||
|
func UseRadio(r lora.Radio) {
|
||||||
|
if ActiveRadio != nil {
|
||||||
|
panic("lorawan.ActiveRadio is already set")
|
||||||
|
}
|
||||||
|
ActiveRadio = r
|
||||||
|
}
|
||||||
|
|
||||||
|
func Join(otaa *Otaa, session *Session) error {
|
||||||
|
var resp []uint8
|
||||||
|
|
||||||
|
if ActiveRadio == nil {
|
||||||
|
return ErrNoRadioAttached
|
||||||
|
}
|
||||||
|
|
||||||
|
otaa.Init()
|
||||||
|
|
||||||
|
// Send join packet
|
||||||
|
payload, err := otaa.GenerateJoinRequest()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ActiveRadio.SetCrc(true)
|
||||||
|
ActiveRadio.SetIqMode(0) // IQ Standard
|
||||||
|
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for JoinAccept
|
||||||
|
ActiveRadio.SetIqMode(1) // IQ Inverted
|
||||||
|
for i := 0; i < Retries; i++ {
|
||||||
|
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resp != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if resp == nil {
|
||||||
|
return ErrNoJoinAcceptReceived
|
||||||
|
}
|
||||||
|
|
||||||
|
err = otaa.DecodeJoinAccept(resp, session)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendUplink(data []uint8, session *Session) error {
|
||||||
|
payload, err := session.GenMessage(0, []byte(data))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ActiveRadio.SetCrc(true)
|
||||||
|
ActiveRadio.SetIqMode(0) // IQ Standard
|
||||||
|
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListenDownlink() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import "crypto/rand"
|
||||||
|
|
||||||
|
// reverseBytes reverses order of a given byte slice
|
||||||
|
func reverseBytes(s []byte) []byte {
|
||||||
|
result := make([]byte, len(s))
|
||||||
|
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
result[i], result[j] = s[j], s[i]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRand16 returns 2 random bytes
|
||||||
|
func GetRand16() ([2]uint8, error) {
|
||||||
|
var randomBytes [2]byte
|
||||||
|
_, err := rand.Read(randomBytes[:])
|
||||||
|
|
||||||
|
return randomBytes, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,253 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"hash"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type cmacHash struct {
|
||||||
|
ciph cipher.Block
|
||||||
|
k1 []byte
|
||||||
|
k2 []byte
|
||||||
|
data []byte
|
||||||
|
x []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Size = aes.BlockSize
|
||||||
|
blockSize = Size
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
subkeyZero []byte
|
||||||
|
subkeyRb []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
subkeyZero = bytes.Repeat([]byte{0x00}, blockSize)
|
||||||
|
subkeyRb = append(bytes.Repeat([]byte{0x00}, blockSize-1), 0x87)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns an AES-CMAC hash using the supplied key. The key must be 16, 24,
|
||||||
|
// or 32 bytes long.
|
||||||
|
func NewCmac(key []byte) (hash.Hash, error) {
|
||||||
|
// Create a cipher.
|
||||||
|
ciph, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Set up the hash object.
|
||||||
|
h := &cmacHash{ciph: ciph}
|
||||||
|
h.k1, h.k2 = generateSubkeys(ciph)
|
||||||
|
h.Reset()
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Xor(dst []byte, a []byte, b []byte) error {
|
||||||
|
if len(dst) != len(a) || len(a) != len(b) {
|
||||||
|
panic("crypto/Xor: bad length")
|
||||||
|
}
|
||||||
|
for i, _ := range a {
|
||||||
|
dst[i] = a[i] ^ b[i]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Reset() {
|
||||||
|
h.data = h.data[:0]
|
||||||
|
h.x = make([]byte, blockSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) BlockSize() int {
|
||||||
|
return h.ciph.BlockSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Size() int {
|
||||||
|
return h.ciph.BlockSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Sum(b []byte) []byte {
|
||||||
|
dataLen := len(h.data)
|
||||||
|
|
||||||
|
// We should have at most one block left.
|
||||||
|
if dataLen > blockSize {
|
||||||
|
panic("cmacHash err1")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate M_last.
|
||||||
|
mLast := make([]byte, blockSize)
|
||||||
|
if dataLen == blockSize {
|
||||||
|
Xor(mLast, h.data, h.k1)
|
||||||
|
} else {
|
||||||
|
// TODO(jacobsa): Accept a destination buffer in common.PadBlock and
|
||||||
|
// simplify this code.
|
||||||
|
Xor(mLast, PadBlock(h.data), h.k2)
|
||||||
|
}
|
||||||
|
|
||||||
|
y := make([]byte, blockSize)
|
||||||
|
Xor(y, mLast, h.x)
|
||||||
|
|
||||||
|
result := make([]byte, blockSize)
|
||||||
|
h.ciph.Encrypt(result, y)
|
||||||
|
|
||||||
|
b = append(b, result...)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) Write(p []byte) (n int, err error) {
|
||||||
|
n = len(p)
|
||||||
|
|
||||||
|
// First step: consume enough data to expand h.data to a full block, if
|
||||||
|
// possible.
|
||||||
|
{
|
||||||
|
toConsume := blockSize - len(h.data)
|
||||||
|
if toConsume > len(p) {
|
||||||
|
toConsume = len(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.data = append(h.data, p[:toConsume]...)
|
||||||
|
p = p[toConsume:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's no data left in p, it means h.data might not be a full block.
|
||||||
|
// Even if it is, we're not sure it's the final block, which we must treat
|
||||||
|
// specially. So we must stop here.
|
||||||
|
if len(p) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// h.data is a full block and is not the last; process it.
|
||||||
|
h.writeBlocks(h.data)
|
||||||
|
h.data = h.data[:0]
|
||||||
|
|
||||||
|
// Consume any further full blocks in p that we're sure aren't the last. Note
|
||||||
|
// that we're sure that len(p) is greater than zero here.
|
||||||
|
blocksToProcess := (len(p) - 1) / blockSize
|
||||||
|
bytesToProcess := blocksToProcess * blockSize
|
||||||
|
|
||||||
|
h.writeBlocks(p[:bytesToProcess])
|
||||||
|
p = p[bytesToProcess:]
|
||||||
|
|
||||||
|
// Store the rest for later.
|
||||||
|
h.data = append(h.data, p...)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *cmacHash) writeBlocks(p []byte) {
|
||||||
|
y := make([]byte, blockSize)
|
||||||
|
|
||||||
|
for off := 0; off < len(p); off += blockSize {
|
||||||
|
block := p[off : off+blockSize]
|
||||||
|
|
||||||
|
xorBlock(
|
||||||
|
unsafe.Pointer(&y[0]),
|
||||||
|
unsafe.Pointer(&h.x[0]),
|
||||||
|
unsafe.Pointer(&block[0]))
|
||||||
|
|
||||||
|
h.ciph.Encrypt(h.x, y)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func xorBlock(
|
||||||
|
dstPtr unsafe.Pointer,
|
||||||
|
aPtr unsafe.Pointer,
|
||||||
|
bPtr unsafe.Pointer) {
|
||||||
|
// Check assumptions. (These are compile-time constants, so this should
|
||||||
|
// compile out.)
|
||||||
|
const wordSize = unsafe.Sizeof(uintptr(0))
|
||||||
|
if blockSize != 4*wordSize {
|
||||||
|
panic("xorBlock err1")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert.
|
||||||
|
a := (*[4]uintptr)(aPtr)
|
||||||
|
b := (*[4]uintptr)(bPtr)
|
||||||
|
dst := (*[4]uintptr)(dstPtr)
|
||||||
|
|
||||||
|
// Compute.
|
||||||
|
dst[0] = a[0] ^ b[0]
|
||||||
|
dst[1] = a[1] ^ b[1]
|
||||||
|
dst[2] = a[2] ^ b[2]
|
||||||
|
dst[3] = a[3] ^ b[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
func PadBlock(block []byte) []byte {
|
||||||
|
blockLen := len(block)
|
||||||
|
if blockLen >= aes.BlockSize {
|
||||||
|
panic("PadBlock input must be less than 16 bytes.")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]byte, aes.BlockSize)
|
||||||
|
copy(result, block)
|
||||||
|
result[blockLen] = 0x80
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given the supplied cipher, whose block size must be 16 bytes, return two
|
||||||
|
// subkeys that can be used in MAC generation. See section 5.3 of NIST SP
|
||||||
|
// 800-38B. Note that the other NIST-approved block size of 8 bytes is not
|
||||||
|
// supported by this function.
|
||||||
|
func generateSubkeys(ciph cipher.Block) (k1 []byte, k2 []byte) {
|
||||||
|
if ciph.BlockSize() != blockSize {
|
||||||
|
panic("generateSubkeys requires a cipher with a block size of 16 bytes.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1
|
||||||
|
l := make([]byte, blockSize)
|
||||||
|
ciph.Encrypt(l, subkeyZero)
|
||||||
|
|
||||||
|
// Step 2: Derive the first subkey.
|
||||||
|
if Msb(l) == 0 {
|
||||||
|
// TODO(jacobsa): Accept a destination buffer in ShiftLeft and then hoist
|
||||||
|
// the allocation in the else branch below.
|
||||||
|
k1 = ShiftLeft(l)
|
||||||
|
} else {
|
||||||
|
k1 = make([]byte, blockSize)
|
||||||
|
Xor(k1, ShiftLeft(l), subkeyRb)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 3: Derive the second subkey.
|
||||||
|
if Msb(k1) == 0 {
|
||||||
|
k2 = ShiftLeft(k1)
|
||||||
|
} else {
|
||||||
|
k2 = make([]byte, blockSize)
|
||||||
|
Xor(k2, ShiftLeft(k1), subkeyRb)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShiftLeft(b []byte) []byte {
|
||||||
|
l := len(b)
|
||||||
|
if l == 0 {
|
||||||
|
panic("shiftLeft requires a non-empty buffer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
output := make([]byte, l)
|
||||||
|
|
||||||
|
overflow := byte(0)
|
||||||
|
for i := int(l - 1); i >= 0; i-- {
|
||||||
|
output[i] = b[i] << 1
|
||||||
|
output[i] |= overflow
|
||||||
|
overflow = (b[i] & 0x80) >> 7
|
||||||
|
}
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
// Msb returns the most significant bit of the supplied data (which must be
|
||||||
|
// non-empty). This is the MSB(L) function of RFC 4493.
|
||||||
|
func Msb(buf []byte) uint8 {
|
||||||
|
if len(buf) == 0 {
|
||||||
|
panic("msb requires non-empty buffer.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[0] >> 7
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
|
// genPayloadMIC computes MIC given the payload and the key
|
||||||
|
func genPayloadMIC(payload []uint8, key [16]uint8) [4]uint8 {
|
||||||
|
var mic [4]uint8
|
||||||
|
hash, _ := NewCmac(key[:])
|
||||||
|
hash.Write(payload)
|
||||||
|
hb := hash.Sum([]byte{})
|
||||||
|
copy(mic[:], hb[0:4])
|
||||||
|
return mic
|
||||||
|
}
|
||||||
|
|
||||||
|
func calcMessageMIC(payload []uint8, key [16]uint8, dir uint8, addr []byte, fCnt uint32, lenMessage uint8) [4]uint8 {
|
||||||
|
var b0 []byte
|
||||||
|
b0 = append(b0, 0x49, 0x00, 0x00, 0x00, 0x00)
|
||||||
|
b0 = append(b0, dir)
|
||||||
|
b0 = append(b0, addr[:]...)
|
||||||
|
var b [4]byte
|
||||||
|
binary.LittleEndian.PutUint32(b[:], fCnt)
|
||||||
|
b0 = append(b0, b[:]...)
|
||||||
|
b0 = append(b0, 0x00)
|
||||||
|
b0 = append(b0, lenMessage)
|
||||||
|
|
||||||
|
var full []byte
|
||||||
|
full = append(full, b0...)
|
||||||
|
full = append(full, payload...)
|
||||||
|
|
||||||
|
var mic [4]uint8
|
||||||
|
hash, _ := NewCmac(key[:])
|
||||||
|
hash.Write(full)
|
||||||
|
hb := hash.Sum([]byte{})
|
||||||
|
copy(mic[:], hb[0:4])
|
||||||
|
return mic
|
||||||
|
}
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Otaa is used to store Over The Air Activation data of a LoRaWAN session
|
||||||
|
type Otaa struct {
|
||||||
|
DevEUI [8]uint8
|
||||||
|
AppEUI [8]uint8
|
||||||
|
AppKey [16]uint8
|
||||||
|
devNonce [2]uint8
|
||||||
|
appNonce [3]uint8
|
||||||
|
NetID [3]uint8
|
||||||
|
buf []uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize DevNonce
|
||||||
|
func (o *Otaa) Init() {
|
||||||
|
o.buf = make([]uint8, 0)
|
||||||
|
|
||||||
|
o.generateDevNonce()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) generateDevNonce() {
|
||||||
|
// TODO: handle error
|
||||||
|
rnd, _ := GetRand16()
|
||||||
|
o.devNonce[0] = rnd[0]
|
||||||
|
o.devNonce[1] = rnd[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) incrementDevNonce() {
|
||||||
|
nonce := uint16(o.devNonce[1])<<8 | uint16(o.devNonce[0]) + 1
|
||||||
|
o.devNonce[0] = uint8(nonce)
|
||||||
|
o.devNonce[1] = uint8((nonce >> 8))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set configures the Otaa AppEUI, DevEUI, AppKey for the device
|
||||||
|
func (o *Otaa) Set(appEUI []uint8, devEUI []uint8, appKey []uint8) {
|
||||||
|
o.SetAppEUI(appEUI)
|
||||||
|
o.SetDevEUI(devEUI)
|
||||||
|
o.SetAppKey(appKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAppEUI configures the Otaa AppEUI
|
||||||
|
func (o *Otaa) SetAppEUI(appEUI []uint8) error {
|
||||||
|
if len(appEUI) != 8 {
|
||||||
|
return ErrInvalidEuiLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(o.AppEUI[:], appEUI)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) GetAppEUI() string {
|
||||||
|
return hex.EncodeToString(o.AppEUI[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDevEUI configures the Otaa DevEUI
|
||||||
|
func (o *Otaa) SetDevEUI(devEUI []uint8) error {
|
||||||
|
if len(devEUI) != 8 {
|
||||||
|
return ErrInvalidEuiLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(o.DevEUI[:], devEUI)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) GetDevEUI() string {
|
||||||
|
return hex.EncodeToString(o.DevEUI[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAppKey configures the Otaa AppKey
|
||||||
|
func (o *Otaa) SetAppKey(appKey []uint8) error {
|
||||||
|
if len(appKey) != 16 {
|
||||||
|
return ErrInvalidAppKeyLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(o.AppKey[:], appKey)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) GetAppKey() string {
|
||||||
|
return hex.EncodeToString(o.AppKey[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) GetNetID() string {
|
||||||
|
return hex.EncodeToString(o.NetID[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *Otaa) SetNetID(netID []uint8) error {
|
||||||
|
if len(netID) != 3 {
|
||||||
|
return ErrInvalidNetIDLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(o.NetID[:], netID)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateJoinRequest Generates a LoraWAN Join request
|
||||||
|
func (o *Otaa) GenerateJoinRequest() ([]uint8, error) {
|
||||||
|
o.incrementDevNonce()
|
||||||
|
|
||||||
|
// TODO: Add checks
|
||||||
|
o.buf = o.buf[:0]
|
||||||
|
o.buf = append(o.buf, 0x00)
|
||||||
|
o.buf = append(o.buf, reverseBytes(o.AppEUI[:])...)
|
||||||
|
o.buf = append(o.buf, reverseBytes(o.DevEUI[:])...)
|
||||||
|
o.buf = append(o.buf, o.devNonce[:]...)
|
||||||
|
mic := genPayloadMIC(o.buf, o.AppKey)
|
||||||
|
o.buf = append(o.buf, mic[:]...)
|
||||||
|
|
||||||
|
return o.buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeJoinAccept Decodes a Lora Join Accept packet
|
||||||
|
func (o *Otaa) DecodeJoinAccept(phyPload []uint8, s *Session) error {
|
||||||
|
if len(phyPload) < 12 {
|
||||||
|
return ErrInvalidPacketLength
|
||||||
|
}
|
||||||
|
data := phyPload[1:] // Remove trailing 0x20
|
||||||
|
|
||||||
|
// Prepare AES Cipher
|
||||||
|
block, err := aes.NewCipher(o.AppKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
buf := make([]byte, len(data))
|
||||||
|
for k := 0; k < len(data)/aes.BlockSize; k++ {
|
||||||
|
block.Encrypt(buf[k*aes.BlockSize:], data[k*aes.BlockSize:])
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(o.appNonce[:], buf[0:3])
|
||||||
|
copy(o.NetID[:], buf[3:6])
|
||||||
|
copy(s.DevAddr[:], buf[6:10])
|
||||||
|
s.DLSettings = buf[10]
|
||||||
|
s.RXDelay = buf[11]
|
||||||
|
|
||||||
|
if len(buf) > 16 {
|
||||||
|
copy(s.CFList[:], buf[12:28])
|
||||||
|
}
|
||||||
|
rxMic := buf[len(buf)-4:]
|
||||||
|
|
||||||
|
dataMic := []byte{}
|
||||||
|
dataMic = append(dataMic, phyPload[0])
|
||||||
|
dataMic = append(dataMic, o.appNonce[:]...)
|
||||||
|
dataMic = append(dataMic, o.NetID[:]...)
|
||||||
|
dataMic = append(dataMic, s.DevAddr[:]...)
|
||||||
|
dataMic = append(dataMic, s.DLSettings)
|
||||||
|
dataMic = append(dataMic, s.RXDelay)
|
||||||
|
dataMic = append(dataMic, s.CFList[:]...)
|
||||||
|
computedMic := genPayloadMIC(dataMic[:], o.AppKey)
|
||||||
|
if !bytes.Equal(computedMic[:], rxMic[:]) {
|
||||||
|
return ErrInvalidMic
|
||||||
|
}
|
||||||
|
// Generate NwkSKey
|
||||||
|
// NwkSKey = aes128_encrypt(AppKey, 0x01|AppNonce|NetID|DevNonce|pad16)
|
||||||
|
sKey := []byte{}
|
||||||
|
sKey = append(sKey, 0x01)
|
||||||
|
sKey = append(sKey, o.appNonce[:]...)
|
||||||
|
sKey = append(sKey, o.NetID[:]...)
|
||||||
|
sKey = append(sKey, o.devNonce[:]...)
|
||||||
|
for i := 0; i < 7; i++ {
|
||||||
|
sKey = append(sKey, 0x00) // PAD to 16
|
||||||
|
}
|
||||||
|
block.Encrypt(buf, sKey)
|
||||||
|
copy(s.NwkSKey[:], buf[0:16])
|
||||||
|
|
||||||
|
// Generate AppSKey
|
||||||
|
// AppSKey = aes128_encrypt(AppKey, 0x02|AppNonce|NetID|DevNonce|pad16)
|
||||||
|
sKey[0] = 0x02
|
||||||
|
block.Encrypt(buf, sKey)
|
||||||
|
copy(s.AppSKey[:], buf[0:16])
|
||||||
|
|
||||||
|
// Reset counters
|
||||||
|
s.FCntDown = 0
|
||||||
|
s.FCntUp = 0
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
package lorawan
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Session is used to store session data of a LoRaWAN session
|
||||||
|
type Session struct {
|
||||||
|
NwkSKey [16]uint8
|
||||||
|
AppSKey [16]uint8
|
||||||
|
DevAddr [4]uint8
|
||||||
|
FCntDown uint32
|
||||||
|
FCntUp uint32
|
||||||
|
CFList [16]uint8
|
||||||
|
RXDelay uint8
|
||||||
|
DLSettings uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetDevAddr configures the Session DevAddr
|
||||||
|
func (s *Session) SetDevAddr(devAddr []uint8) error {
|
||||||
|
if len(devAddr) != 4 {
|
||||||
|
return ErrInvalidDevAddrLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(s.DevAddr[:], devAddr)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDevAddr returns the Session DevAddr
|
||||||
|
func (s *Session) GetDevAddr() string {
|
||||||
|
return hex.EncodeToString(s.DevAddr[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNwkSKey configures the Session NwkSKey
|
||||||
|
func (s *Session) SetNwkSKey(nwkSKey []uint8) error {
|
||||||
|
if len(nwkSKey) != 16 {
|
||||||
|
return ErrInvalidNwkSKeyLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(s.NwkSKey[:], nwkSKey)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetNwkSKey returns the Session NwkSKey
|
||||||
|
func (s *Session) GetNwkSKey() string {
|
||||||
|
return hex.EncodeToString(s.NwkSKey[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAppSKey configures the Session AppSKey
|
||||||
|
func (s *Session) SetAppSKey(appSKey []uint8) error {
|
||||||
|
if len(appSKey) != 16 {
|
||||||
|
return ErrInvalidAppSKeyLength
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(s.AppSKey[:], appSKey)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAppSKey returns the Session AppSKey
|
||||||
|
func (s *Session) GetAppSKey() string {
|
||||||
|
return hex.EncodeToString(s.AppSKey[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenMessage generates an uplink message.
|
||||||
|
func (s *Session) GenMessage(dir uint8, payload []uint8) ([]uint8, error) {
|
||||||
|
var buf []uint8
|
||||||
|
buf = append(buf, 0b01000000) // FHDR Unconfirmed up
|
||||||
|
buf = append(buf, s.DevAddr[:]...)
|
||||||
|
|
||||||
|
// FCtl : No ADR, No RFU, No ACK, No FPending, No FOpt
|
||||||
|
buf = append(buf, 0x00)
|
||||||
|
|
||||||
|
// FCnt Up
|
||||||
|
buf = append(buf, uint8(s.FCntUp&0xFF), uint8((s.FCntUp>>8)&0xFF))
|
||||||
|
|
||||||
|
// FPort=1
|
||||||
|
buf = append(buf, 0x01)
|
||||||
|
|
||||||
|
fCnt := uint32(0)
|
||||||
|
if dir == 0 {
|
||||||
|
fCnt = s.FCntUp
|
||||||
|
s.FCntUp++
|
||||||
|
} else {
|
||||||
|
fCnt = s.FCntDown
|
||||||
|
}
|
||||||
|
data, err := s.genFRMPayload(dir, fCnt, payload, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
buf = append(buf, data[:]...)
|
||||||
|
|
||||||
|
mic := calcMessageMIC(buf, s.NwkSKey, dir, s.DevAddr[:], fCnt, uint8(len(buf)))
|
||||||
|
buf = append(buf, mic[:]...)
|
||||||
|
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) genFRMPayload(dir uint8, fCnt uint32, payload []byte, isFOpts bool) ([]byte, error) {
|
||||||
|
k := len(payload) / aes.BlockSize
|
||||||
|
if len(payload)%aes.BlockSize != 0 {
|
||||||
|
k++
|
||||||
|
}
|
||||||
|
if k > math.MaxUint8 {
|
||||||
|
return nil, ErrFrmPayloadTooLarge
|
||||||
|
}
|
||||||
|
encrypted := make([]byte, 0, k*16)
|
||||||
|
cipher, err := aes.NewCipher(s.AppSKey[:])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var a [aes.BlockSize]byte
|
||||||
|
a[0] = 0x01
|
||||||
|
a[5] = dir
|
||||||
|
copy(a[6:10], s.DevAddr[:])
|
||||||
|
binary.LittleEndian.PutUint32(a[10:14], fCnt)
|
||||||
|
var ss [aes.BlockSize]byte
|
||||||
|
var b [aes.BlockSize]byte
|
||||||
|
for i := uint8(0); i < uint8(k); i++ {
|
||||||
|
copy(b[:], payload[i*aes.BlockSize:])
|
||||||
|
if !isFOpts {
|
||||||
|
a[15] = i + 1
|
||||||
|
}
|
||||||
|
cipher.Encrypt(ss[:], a[:])
|
||||||
|
for j := 0; j < aes.BlockSize; j++ {
|
||||||
|
b[j] = b[j] ^ ss[j]
|
||||||
|
}
|
||||||
|
encrypted = append(encrypted, b[:]...)
|
||||||
|
}
|
||||||
|
return encrypted[:len(payload)], nil
|
||||||
|
}
|
||||||
@@ -1,15 +1,6 @@
|
|||||||
package main
|
package lora
|
||||||
|
|
||||||
import (
|
type Radio interface {
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
errRadioNotFound = errors.New("radio not found")
|
|
||||||
errRxTimeout = errors.New("radio RX timeout")
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoraRadio interface {
|
|
||||||
Reset()
|
Reset()
|
||||||
Tx(pkt []uint8, timeoutMs uint32) error
|
Tx(pkt []uint8, timeoutMs uint32) error
|
||||||
Rx(timeoutMs uint32) ([]uint8, error)
|
Rx(timeoutMs uint32) ([]uint8, error)
|
||||||
@@ -19,4 +10,5 @@ type LoraRadio interface {
|
|||||||
SetBandwidth(bw uint8)
|
SetBandwidth(bw uint8)
|
||||||
SetCrc(enable bool)
|
SetCrc(enable bool)
|
||||||
SetSpreadingFactor(sf uint8)
|
SetSpreadingFactor(sf uint8)
|
||||||
|
LoraConfig(cnf Config)
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -211,3 +213,41 @@ func (c *Client) Post(url, contentType string, body io.Reader) (resp *Response,
|
|||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
return c.Do(req)
|
return c.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostForm issues a POST to the specified URL, with data's keys and
|
||||||
|
// values URL-encoded as the request body.
|
||||||
|
//
|
||||||
|
// The Content-Type header is set to application/x-www-form-urlencoded.
|
||||||
|
// To set other headers, use NewRequest and DefaultClient.Do.
|
||||||
|
//
|
||||||
|
// When err is nil, resp always contains a non-nil resp.Body.
|
||||||
|
// Caller should close resp.Body when done reading from it.
|
||||||
|
//
|
||||||
|
// PostForm is a wrapper around DefaultClient.PostForm.
|
||||||
|
//
|
||||||
|
// See the Client.Do method documentation for details on how redirects
|
||||||
|
// are handled.
|
||||||
|
//
|
||||||
|
// To make a request with a specified context.Context, use NewRequestWithContext
|
||||||
|
// and DefaultClient.Do.
|
||||||
|
func PostForm(url string, data url.Values) (resp *Response, err error) {
|
||||||
|
return DefaultClient.PostForm(url, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PostForm issues a POST to the specified URL,
|
||||||
|
// with data's keys and values URL-encoded as the request body.
|
||||||
|
//
|
||||||
|
// The Content-Type header is set to application/x-www-form-urlencoded.
|
||||||
|
// To set other headers, use NewRequest and Client.Do.
|
||||||
|
//
|
||||||
|
// When err is nil, resp always contains a non-nil resp.Body.
|
||||||
|
// Caller should close resp.Body when done reading from it.
|
||||||
|
//
|
||||||
|
// See the Client.Do method documentation for details on how redirects
|
||||||
|
// are handled.
|
||||||
|
//
|
||||||
|
// To make a request with a specified context.Context, use NewRequestWithContext
|
||||||
|
// and Client.Do.
|
||||||
|
func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) {
|
||||||
|
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||||
|
}
|
||||||
|
|||||||
+4
-1
@@ -357,7 +357,10 @@ func ParseIP(s string) IP {
|
|||||||
|
|
||||||
// String returns the string form of the IP address ip.
|
// String returns the string form of the IP address ip.
|
||||||
func (ip IP) String() string {
|
func (ip IP) String() string {
|
||||||
return string(ip)
|
if len(ip) < 4 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strconv.Itoa(int(ip[0])) + "." + strconv.Itoa(int(ip[1])) + "." + strconv.Itoa(int(ip[2])) + "." + strconv.Itoa(int(ip[3]))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Conn is a generic stream-oriented network connection.
|
// Conn is a generic stream-oriented network connection.
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
qt "github.com/frankban/quicktest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIPAddressString(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
ipaddr := ParseIP("127.0.0.1")
|
||||||
|
|
||||||
|
c.Assert(ipaddr.String(), qt.Equals, "127.0.0.1")
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package sx126x
|
||||||
|
|
||||||
|
// SX126X radio transceiver has several pins that control
|
||||||
|
// RF_IN, RF_OUT, NSS, and BUSY.
|
||||||
|
// This interface allows the creation of struct
|
||||||
|
// that can drive the RF Switch (Used in Lora RX and Lora Tx)
|
||||||
|
type RadioController interface {
|
||||||
|
Init() error
|
||||||
|
SetRfSwitchMode(mode int) error
|
||||||
|
SetNss(state bool) error
|
||||||
|
WaitWhileBusy() error
|
||||||
|
SetupInterrupts(handler func()) error
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//go:build gnse
|
||||||
|
|
||||||
|
/*
|
||||||
|
Generic Node Sensor Edition
|
||||||
|
RFSwitch
|
||||||
|
|
||||||
|
Disable Switch : PB8=OFF PA0=OFF PA1=OFF
|
||||||
|
Enable RX : PB8=ON PA0=ON PA1=OFF
|
||||||
|
Enable TX RFO LP : PB8=ON PA0=ON PA1=ON
|
||||||
|
Enable TX RFO HP : PB8=ON PA0=OFF PA1=ON
|
||||||
|
*/
|
||||||
|
package sx126x
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RadioControl for GNSE board.
|
||||||
|
type RadioControl struct {
|
||||||
|
STM32RadioControl
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRadioControl() *RadioControl {
|
||||||
|
return &RadioControl{STM32RadioControl{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init pins needed for controlling rx/tx
|
||||||
|
func (rc *RadioControl) Init() error {
|
||||||
|
machine.PA0.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
machine.PA1.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
machine.PB8.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||||
|
switch mode {
|
||||||
|
|
||||||
|
case RFSWITCH_TX_HP:
|
||||||
|
machine.PA0.Set(false)
|
||||||
|
machine.PA1.Set(true)
|
||||||
|
machine.PB8.Set(true)
|
||||||
|
|
||||||
|
case RFSWITCH_TX_LP:
|
||||||
|
machine.PA0.Set(true)
|
||||||
|
machine.PA1.Set(true)
|
||||||
|
machine.PB8.Set(true)
|
||||||
|
|
||||||
|
case RFSWITCH_RX:
|
||||||
|
machine.PA0.Set(true)
|
||||||
|
machine.PA1.Set(false)
|
||||||
|
machine.PB8.Set(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
//go:build lorae5
|
||||||
|
|
||||||
|
/*
|
||||||
|
LoRa-E5 module ONLY transmits through RFO_HP:
|
||||||
|
|
||||||
|
Receive: PA4=1, PA5=0
|
||||||
|
Transmit(high output power, SMPS mode): PA4=0, PA5=1
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sx126x
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RadioControl for LoRa-E5 board.
|
||||||
|
type RadioControl struct {
|
||||||
|
STM32RadioControl
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRadioControl() *RadioControl {
|
||||||
|
return &RadioControl{STM32RadioControl: STM32RadioControl{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init pins needed for controlling rx/tx
|
||||||
|
func (rc *RadioControl) Init() error {
|
||||||
|
machine.PA4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
machine.PB5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||||
|
switch mode {
|
||||||
|
|
||||||
|
case RFSWITCH_RX:
|
||||||
|
machine.PA4.Set(true)
|
||||||
|
machine.PB5.Set(false)
|
||||||
|
case RFSWITCH_TX_LP:
|
||||||
|
return errLowPowerTxNotSupported
|
||||||
|
case RFSWITCH_TX_HP:
|
||||||
|
machine.PA4.Set(false)
|
||||||
|
machine.PB5.Set(true)
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
//go:build !stm32wlx
|
||||||
|
|
||||||
|
package sx126x
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RadioControl for boards that are connected using normal pins.
|
||||||
|
type RadioControl struct {
|
||||||
|
nssPin, busyPin, dio1Pin machine.Pin
|
||||||
|
rxPin, txLowPin, txHighPin machine.Pin
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRadioControl(nssPin, busyPin, dio1Pin,
|
||||||
|
rxPin, txLowPin, txHighPin machine.Pin) *RadioControl {
|
||||||
|
return &RadioControl{
|
||||||
|
nssPin: nssPin,
|
||||||
|
busyPin: busyPin,
|
||||||
|
dio1Pin: dio1Pin,
|
||||||
|
rxPin: rxPin,
|
||||||
|
txLowPin: txLowPin,
|
||||||
|
txHighPin: txHighPin,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNss sets the NSS line aka chip select for SPI.
|
||||||
|
func (rc *RadioControl) SetNss(state bool) error {
|
||||||
|
rc.nssPin.Set(state)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WaitWhileBusy wait until the radio is no longer busy
|
||||||
|
func (rc *RadioControl) WaitWhileBusy() error {
|
||||||
|
count := 100
|
||||||
|
for count > 0 {
|
||||||
|
if !rc.busyPin.Get() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
count--
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
return errWaitWhileBusyTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init() configures whatever needed for sx126x radio control
|
||||||
|
func (rc *RadioControl) Init() error {
|
||||||
|
rc.nssPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
rc.busyPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// add interrupt handler for Radio IRQs for pins
|
||||||
|
func (rc *RadioControl) SetupInterrupts(handler func()) error {
|
||||||
|
irqHandler = handler
|
||||||
|
|
||||||
|
rc.dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||||
|
if err := rc.dio1Pin.SetInterrupt(machine.PinRising, handleInterrupt); err != nil {
|
||||||
|
return errRadioNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var irqHandler func()
|
||||||
|
|
||||||
|
func handleInterrupt(machine.Pin) {
|
||||||
|
irqHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||||
|
switch mode {
|
||||||
|
|
||||||
|
case RFSWITCH_RX:
|
||||||
|
rc.rxPin.Set(true)
|
||||||
|
rc.txLowPin.Set(false)
|
||||||
|
rc.txHighPin.Set(false)
|
||||||
|
case RFSWITCH_TX_LP:
|
||||||
|
rc.rxPin.Set(false)
|
||||||
|
rc.txLowPin.Set(true)
|
||||||
|
rc.txHighPin.Set(false)
|
||||||
|
case RFSWITCH_TX_HP:
|
||||||
|
rc.rxPin.Set(false)
|
||||||
|
rc.txLowPin.Set(false)
|
||||||
|
rc.txHighPin.Set(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -4,40 +4,28 @@ package sx126x
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"device/stm32"
|
"device/stm32"
|
||||||
"errors"
|
|
||||||
"machine"
|
"runtime/interrupt"
|
||||||
"tinygo.org/x/drivers"
|
|
||||||
"tinygo.org/x/drivers/lora"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new SX126x connection.
|
// STM32RadioControl helps implement the RadioController interface
|
||||||
func New(spi drivers.SPI) *Device {
|
type STM32RadioControl struct {
|
||||||
c := make(chan lora.RadioEvent, 10)
|
irqHandler func()
|
||||||
d := Device{
|
|
||||||
spi: spi,
|
|
||||||
radioEventChan: c,
|
|
||||||
}
|
|
||||||
if d.spi == machine.SPI3 {
|
|
||||||
d.SubGhzInit()
|
|
||||||
d.SetDeviceType(DEVICE_TYPE_SX1262)
|
|
||||||
} else {
|
|
||||||
panic("Driver only support SUBGHZSPI (SPI3) on stm32wlx targets")
|
|
||||||
}
|
|
||||||
return &d
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpiSetNss Sets the NSS line
|
// SetNss sets the NSS line aka chip select for SPI.
|
||||||
func (d *Device) SpiSetNss(state bool) {
|
func (rc *STM32RadioControl) SetNss(state bool) error {
|
||||||
if state {
|
if state {
|
||||||
stm32.PWR.SUBGHZSPICR.SetBits(stm32.PWR_SUBGHZSPICR_NSS)
|
stm32.PWR.SUBGHZSPICR.SetBits(stm32.PWR_SUBGHZSPICR_NSS)
|
||||||
} else {
|
} else {
|
||||||
stm32.PWR.SUBGHZSPICR.ClearBits(stm32.PWR_SUBGHZSPICR_NSS)
|
stm32.PWR.SUBGHZSPICR.ClearBits(stm32.PWR_SUBGHZSPICR_NSS)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitBusy sleep until all busy flags clears
|
// WaitWhileBusy wait until the radio is no longer busy
|
||||||
func (d *Device) WaitBusy() error {
|
func (rc *STM32RadioControl) WaitWhileBusy() error {
|
||||||
count := 100
|
count := 100
|
||||||
var rfbusyms, rfbusys bool
|
var rfbusyms, rfbusys bool
|
||||||
for count > 0 {
|
for count > 0 {
|
||||||
@@ -49,12 +37,11 @@ func (d *Device) WaitBusy() error {
|
|||||||
}
|
}
|
||||||
count--
|
count--
|
||||||
}
|
}
|
||||||
return errors.New("WaitBusy Timeout")
|
return errWaitWhileBusyTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubGhzInit() configures internal SX1262's SPI bus.
|
// init() configures whatever needed for sx126x radio control
|
||||||
func (d *Device) SubGhzInit() {
|
func init() {
|
||||||
|
|
||||||
// Enable APB3 Periph clock and delay
|
// Enable APB3 Periph clock and delay
|
||||||
stm32.RCC.APB3ENR.SetBits(stm32.RCC_APB3ENR_SUBGHZSPIEN)
|
stm32.RCC.APB3ENR.SetBits(stm32.RCC_APB3ENR_SUBGHZSPIEN)
|
||||||
_ = stm32.RCC.APB3ENR.Get()
|
_ = stm32.RCC.APB3ENR.Get()
|
||||||
@@ -81,5 +68,18 @@ func (d *Device) SubGhzInit() {
|
|||||||
stm32.SPI3.CR1.Set(stm32.SPI_CR1_MSTR | stm32.SPI_CR1_SSI | (0b010 << 3) | stm32.SPI_CR1_SSM)
|
stm32.SPI3.CR1.Set(stm32.SPI_CR1_MSTR | stm32.SPI_CR1_SSI | (0b010 << 3) | stm32.SPI_CR1_SSM)
|
||||||
stm32.SPI3.CR2.Set(stm32.SPI_CR2_FRXTH | (0b111 << 8))
|
stm32.SPI3.CR2.Set(stm32.SPI_CR2_FRXTH | (0b111 << 8))
|
||||||
stm32.SPI3.CR1.SetBits(stm32.SPI_CR1_SPE)
|
stm32.SPI3.CR1.SetBits(stm32.SPI_CR1_SPE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *STM32RadioControl) SetupInterrupts(handler func()) error {
|
||||||
|
irqHandler = handler
|
||||||
|
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, handleInterrupt)
|
||||||
|
intr.Enable()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var irqHandler func()
|
||||||
|
|
||||||
|
func handleInterrupt(interrupt.Interrupt) {
|
||||||
|
irqHandler()
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
//go:build nucleowl55jc
|
//go:build nucleowl55jc
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Nucleo WL55JC1
|
Nucleo WL55JC1
|
||||||
RFSwitch
|
RFSwitch
|
||||||
|
|
||||||
+-----------+---------+------------+------------+
|
+-----------+---------+------------+------------+
|
||||||
| | FE_CTRL1 | FE_CTRL2 | FE_CTRL3 |
|
| | FE_CTRL1 | FE_CTRL2 | FE_CTRL3 |
|
||||||
@@ -13,42 +13,48 @@
|
|||||||
| RX | HIGH | LOW | HIGH |
|
| RX | HIGH | LOW | HIGH |
|
||||||
+-----------+----------+-----------+------------+
|
+-----------+----------+-----------+------------+
|
||||||
*/
|
*/
|
||||||
package rfswitch
|
package sx126x
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
|
|
||||||
"tinygo.org/x/drivers/sx126x"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CustomSwitch struct {
|
// RadioControl for Nucleo WL55JC1 board.
|
||||||
|
type RadioControl struct {
|
||||||
|
STM32RadioControl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s CustomSwitch) InitRFSwitch() {
|
func NewRadioControl() *RadioControl {
|
||||||
|
return &RadioControl{STM32RadioControl{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init pins needed for controlling rx/tx
|
||||||
|
func (rc *RadioControl) Init() error {
|
||||||
machine.PC4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
machine.PC4.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
machine.PC5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
machine.PC5.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
machine.PC3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
machine.PC3.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s CustomSwitch) SetRfSwitchMode(mode int) error {
|
func (rc *RadioControl) SetRfSwitchMode(mode int) error {
|
||||||
switch mode {
|
switch mode {
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_HP:
|
case RFSWITCH_TX_HP:
|
||||||
machine.PC4.Set(false)
|
machine.PC4.Set(false)
|
||||||
machine.PC5.Set(true)
|
machine.PC5.Set(true)
|
||||||
machine.PC3.Set(true)
|
machine.PC3.Set(true)
|
||||||
|
|
||||||
case sx126x.RFSWITCH_TX_LP:
|
case RFSWITCH_TX_LP:
|
||||||
machine.PC4.Set(true)
|
machine.PC4.Set(true)
|
||||||
machine.PC5.Set(true)
|
machine.PC5.Set(true)
|
||||||
machine.PC3.Set(true)
|
machine.PC3.Set(true)
|
||||||
|
|
||||||
case sx126x.RFSWITCH_RX:
|
case RFSWITCH_RX:
|
||||||
machine.PC4.Set(true)
|
machine.PC4.Set(true)
|
||||||
machine.PC5.Set(false)
|
machine.PC5.Set(false)
|
||||||
machine.PC3.Set(true)
|
machine.PC3.Set(true)
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
+53
-40
@@ -13,13 +13,13 @@ import (
|
|||||||
"tinygo.org/x/drivers/lora"
|
"tinygo.org/x/drivers/lora"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SX126X radio transceiver RF_IN and RF_OUT may be connected
|
var (
|
||||||
// to RF Switch. This interface allows the creation of struct
|
errWaitWhileBusyTimeout = errors.New("WaitWhileBusy Timeout")
|
||||||
// that can drive the RF Switch (Used in Lora RX and Lora Tx)
|
errLowPowerTxNotSupported = errors.New("RFSWITCH_TX_LP not supported")
|
||||||
type RFSwitch interface {
|
errRadioNotFound = errors.New("LoRa radio not found")
|
||||||
InitRFSwitch()
|
errUnexpectedRxRadioEvent = errors.New("Unexpected Radio Event during RX")
|
||||||
SetRfSwitchMode(mode int) error
|
errUnexpectedTxRadioEvent = errors.New("Unexpected Radio Event during TX")
|
||||||
}
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DEVICE_TYPE_SX1261 = iota
|
DEVICE_TYPE_SX1261 = iota
|
||||||
@@ -38,18 +38,26 @@ const (
|
|||||||
SPI_BUFFER_SIZE = 256
|
SPI_BUFFER_SIZE = 256
|
||||||
)
|
)
|
||||||
|
|
||||||
// Device wraps an SPI connection to a SX127x device.
|
// Device wraps an SPI connection to a SX126x device.
|
||||||
type Device struct {
|
type Device struct {
|
||||||
spi drivers.SPI // SPI bus for module communication
|
spi drivers.SPI // SPI bus for module communication
|
||||||
rstPin, csPin machine.Pin // GPIOs for reset and chip select
|
rstPin machine.Pin // GPIO for reset pin
|
||||||
radioEventChan chan lora.RadioEvent // Channel for Receiving events
|
radioEventChan chan lora.RadioEvent // Channel for Receiving events
|
||||||
loraConf lora.Config // Current Lora configuration
|
loraConf lora.Config // Current Lora configuration
|
||||||
rfswitch RFSwitch // RF Switch, if any
|
controller RadioController // to manage interactions with the radio
|
||||||
deepSleep bool // Internal Sleep state
|
deepSleep bool // Internal Sleep state
|
||||||
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
|
||||||
spiBuffer [SPI_BUFFER_SIZE]uint8
|
spiBuffer [SPI_BUFFER_SIZE]uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new SX126x connection.
|
||||||
|
func New(spi drivers.SPI) *Device {
|
||||||
|
return &Device{
|
||||||
|
spi: spi,
|
||||||
|
radioEventChan: make(chan lora.RadioEvent, 10),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SX126X_RTC_FREQ_IN_HZ uint32 = 64000
|
SX126X_RTC_FREQ_IN_HZ uint32 = 64000
|
||||||
)
|
)
|
||||||
@@ -79,10 +87,15 @@ func (d *Device) SetDeviceType(devType int) {
|
|||||||
d.deviceType = devType
|
d.deviceType = devType
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRfSwitch let you define a custom RF Switch driver if needed
|
// SetRadioControl let you define the RadioController
|
||||||
func (d *Device) SetRfSwitch(rfswitch RFSwitch) {
|
func (d *Device) SetRadioController(rc RadioController) error {
|
||||||
d.rfswitch = rfswitch
|
d.controller = rc
|
||||||
d.rfswitch.InitRFSwitch()
|
if err := d.controller.Init(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.controller.SetupInterrupts(d.HandleInterrupt)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
@@ -126,8 +139,8 @@ func (d *Device) SetFs() {
|
|||||||
|
|
||||||
// SetTxContinuousWave set device in test mode to generate a continuous wave (RF tone)
|
// SetTxContinuousWave set device in test mode to generate a continuous wave (RF tone)
|
||||||
func (d *Device) SetTxContinuousWave() {
|
func (d *Device) SetTxContinuousWave() {
|
||||||
if d.rfswitch != nil {
|
if d.controller != nil {
|
||||||
d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||||
}
|
}
|
||||||
d.ExecSetCommand(SX126X_CMD_SET_TX_CONTINUOUS_WAVE, []uint8{})
|
d.ExecSetCommand(SX126X_CMD_SET_TX_CONTINUOUS_WAVE, []uint8{})
|
||||||
}
|
}
|
||||||
@@ -136,8 +149,8 @@ func (d *Device) SetTxContinuousWave() {
|
|||||||
// Take care to initialize all Lora settings like it's done in Tx before calling this function
|
// Take care to initialize all Lora settings like it's done in Tx before calling this function
|
||||||
// If you don't init properly all the settings, it'll fail
|
// If you don't init properly all the settings, it'll fail
|
||||||
func (d *Device) SetTxContinuousPreamble() {
|
func (d *Device) SetTxContinuousPreamble() {
|
||||||
if d.rfswitch != nil {
|
if d.controller != nil {
|
||||||
d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||||
}
|
}
|
||||||
d.ExecSetCommand(SX126X_CMD_SET_TX_INFINITE_PREAMBLE, []uint8{})
|
d.ExecSetCommand(SX126X_CMD_SET_TX_INFINITE_PREAMBLE, []uint8{})
|
||||||
}
|
}
|
||||||
@@ -235,25 +248,25 @@ func (d *Device) SetRxTxFallbackMode(fallbackMode uint8) {
|
|||||||
// ReadRegister reads register value
|
// ReadRegister reads register value
|
||||||
func (d *Device) ReadRegister(addr, size uint16) ([]uint8, error) {
|
func (d *Device) ReadRegister(addr, size uint16) ([]uint8, error) {
|
||||||
d.CheckDeviceReady()
|
d.CheckDeviceReady()
|
||||||
d.SpiSetNss(false)
|
d.controller.SetNss(false)
|
||||||
// Send command
|
// Send command
|
||||||
cmd := []uint8{SX126X_CMD_READ_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF), 0x00}
|
cmd := []uint8{SX126X_CMD_READ_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF), 0x00}
|
||||||
d.spi.Tx(cmd, nil)
|
d.spi.Tx(cmd, nil)
|
||||||
ret := d.spiBuffer[0:size]
|
ret := d.spiBuffer[0:size]
|
||||||
d.spi.Tx(nil, ret)
|
d.spi.Tx(nil, ret)
|
||||||
d.SpiSetNss(true)
|
d.controller.SetNss(true)
|
||||||
d.WaitBusy()
|
d.controller.WaitWhileBusy()
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteRegister writes value to register
|
// WriteRegister writes value to register
|
||||||
func (d *Device) WriteRegister(addr uint16, data []uint8) {
|
func (d *Device) WriteRegister(addr uint16, data []uint8) {
|
||||||
d.CheckDeviceReady()
|
d.CheckDeviceReady()
|
||||||
d.SpiSetNss(false)
|
d.controller.SetNss(false)
|
||||||
cmd := []uint8{SX126X_CMD_WRITE_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF)}
|
cmd := []uint8{SX126X_CMD_WRITE_REGISTER, uint8((addr & 0xFF00) >> 8), uint8(addr & 0x00FF)}
|
||||||
d.spi.Tx(append(cmd, data...), nil)
|
d.spi.Tx(append(cmd, data...), nil)
|
||||||
d.SpiSetNss(true)
|
d.controller.SetNss(true)
|
||||||
d.WaitBusy()
|
d.controller.WaitWhileBusy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteBuffer write data from current buffer position
|
// WriteBuffer write data from current buffer position
|
||||||
@@ -466,12 +479,12 @@ func (d *Device) SetModulationParams(spreadingFactor, bandwidth, codingRate, low
|
|||||||
// CheckDeviceReady sleep until all busy flags clears
|
// CheckDeviceReady sleep until all busy flags clears
|
||||||
func (d *Device) CheckDeviceReady() error {
|
func (d *Device) CheckDeviceReady() error {
|
||||||
if d.deepSleep == true {
|
if d.deepSleep == true {
|
||||||
d.SpiSetNss(false)
|
d.controller.SetNss(false)
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
d.SpiSetNss(true)
|
d.controller.SetNss(true)
|
||||||
d.deepSleep = false
|
d.deepSleep = false
|
||||||
}
|
}
|
||||||
return d.WaitBusy()
|
return d.controller.WaitWhileBusy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecSetCommand send a command to configure the peripheral
|
// ExecSetCommand send a command to configure the peripheral
|
||||||
@@ -482,24 +495,24 @@ func (d *Device) ExecSetCommand(cmd uint8, buf []uint8) {
|
|||||||
} else {
|
} else {
|
||||||
d.deepSleep = false
|
d.deepSleep = false
|
||||||
}
|
}
|
||||||
d.SpiSetNss(false)
|
d.controller.SetNss(false)
|
||||||
// Send command and params
|
// Send command and params
|
||||||
d.spi.Tx(append([]uint8{cmd}, buf...), nil)
|
d.spi.Tx(append([]uint8{cmd}, buf...), nil)
|
||||||
d.SpiSetNss(true)
|
d.controller.SetNss(true)
|
||||||
if cmd != SX126X_CMD_SET_SLEEP {
|
if cmd != SX126X_CMD_SET_SLEEP {
|
||||||
d.WaitBusy()
|
d.controller.WaitWhileBusy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExecGetCommand queries the peripheral the peripheral
|
// ExecGetCommand queries the peripheral the peripheral
|
||||||
func (d *Device) ExecGetCommand(cmd uint8, size uint8) []uint8 {
|
func (d *Device) ExecGetCommand(cmd uint8, size uint8) []uint8 {
|
||||||
d.CheckDeviceReady()
|
d.CheckDeviceReady()
|
||||||
d.SpiSetNss(false)
|
d.controller.SetNss(false)
|
||||||
// Send the command and flush first status byte (as not used)
|
// Send the command and flush first status byte (as not used)
|
||||||
d.spi.Tx([]uint8{cmd, 0x00}, nil)
|
d.spi.Tx([]uint8{cmd, 0x00}, nil)
|
||||||
d.spi.Tx(nil, d.spiBuffer[:size])
|
d.spi.Tx(nil, d.spiBuffer[:size])
|
||||||
d.SpiSetNss(true)
|
d.controller.SetNss(true)
|
||||||
d.WaitBusy()
|
d.controller.WaitWhileBusy()
|
||||||
return d.spiBuffer[:size]
|
return d.spiBuffer[:size]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -510,7 +523,7 @@ func (d *Device) ExecGetCommand(cmd uint8, size uint8) []uint8 {
|
|||||||
// SetFrequency() Sets current Lora Frequency
|
// SetFrequency() Sets current Lora Frequency
|
||||||
// NB: Change will be applied at next RX / TX
|
// NB: Change will be applied at next RX / TX
|
||||||
func (d *Device) SetFrequency(freq uint32) {
|
func (d *Device) SetFrequency(freq uint32) {
|
||||||
d.loraConf.Freq = d.loraConf.Freq
|
d.loraConf.Freq = freq
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetIqMode() defines the current IQ Mode (Standard/Inverted)
|
// SetIqMode() defines the current IQ Mode (Standard/Inverted)
|
||||||
@@ -582,8 +595,8 @@ func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error {
|
|||||||
return lora.ErrUndefinedLoraConf
|
return lora.ErrUndefinedLoraConf
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.rfswitch != nil {
|
if d.controller != nil {
|
||||||
err := d.rfswitch.SetRfSwitchMode(RFSWITCH_TX_HP)
|
err := d.controller.SetRfSwitchMode(RFSWITCH_TX_HP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -605,7 +618,7 @@ func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error {
|
|||||||
|
|
||||||
msg := <-d.GetRadioEventChan()
|
msg := <-d.GetRadioEventChan()
|
||||||
if msg.EventType != lora.RadioEventTxDone {
|
if msg.EventType != lora.RadioEventTxDone {
|
||||||
return errors.New("Unexpected Radio Event while TX")
|
return errUnexpectedTxRadioEvent
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -616,8 +629,8 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
return nil, lora.ErrUndefinedLoraConf
|
return nil, lora.ErrUndefinedLoraConf
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.rfswitch != nil {
|
if d.controller != nil {
|
||||||
err := d.rfswitch.SetRfSwitchMode(RFSWITCH_RX)
|
err := d.controller.SetRfSwitchMode(RFSWITCH_RX)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -637,7 +650,7 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
if msg.EventType == lora.RadioEventTimeout {
|
if msg.EventType == lora.RadioEventTimeout {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if msg.EventType != lora.RadioEventRxDone {
|
} else if msg.EventType != lora.RadioEventRxDone {
|
||||||
return nil, errors.New("Unexpected Radio Event while RX")
|
return nil, errUnexpectedRxRadioEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
pLen, pStart := d.GetRxBufferStatus()
|
pLen, pStart := d.GetRxBufferStatus()
|
||||||
|
|||||||
Reference in New Issue
Block a user