Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions examples/afsk/si5351.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@ import (
)

func initRadio() *afsk.AFSK {
machine.I2C0.Configure(machine.I2CConfig{})
dev := si5351.New(machine.I2C0)
dev.Configure()
if err := dev.Configure(si5351.Config{}); err != nil {
panic(err)
}

return afsk.NewAFSK(&Si5351Radio{device: &dev})
return afsk.NewAFSK(&Si5351Radio{device: dev})
}

type Si5351Radio struct {
device *si5351.Device
}

func (r *Si5351Radio) Transmit(freq uint64) error {
r.device.SetFrequency(freq, 0, si5351.PLL_A)
if err := r.device.SetRawFrequency(si5351.Clock0, si5351.Frequency(freq)); err != nil {
return err
}

r.device.EnableOutput(si5351.Clock0, true)

return nil
}

func (r *Si5351Radio) Standby() error {
r.device.OutputEnable(0, false)
r.device.EnableOutput(si5351.Clock0, false)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion examples/fsk4/featherwing.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initRadio() *fsk4.FSK4 {
println("setting OOK modulation")
dev.SetModulationType(sx127x.SX127X_OPMODE_MODULATION_OOK)

return fsk4.NewFSK4(&sx127xRadio{device: dev}, 10140956, 270, 100)
return fsk4.NewFSK4(&sx127xRadio{device: dev}, 14_097_060, 270, 100*time.Millisecond)
}

type sx127xRadio struct {
Expand Down
4 changes: 2 additions & 2 deletions examples/fsk4/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func main() {
println("Transmitting on frequency", frequency, "Hz")

// transmit some data
for range 5 {
for range 50 {
println("Transmitting data:", hex.EncodeToString(horusPacket))
radio.Write(horusPacket)

time.Sleep(3 * time.Second)
time.Sleep(30 * time.Second)
}

time.Sleep(2 * time.Second)
Expand Down
4 changes: 3 additions & 1 deletion examples/fsk4/noradio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
package main

import (
"time"

"tinygo.org/x/wireless/fsk4"
)

func initRadio() *fsk4.FSK4 {
return fsk4.NewFSK4(&NoRadio{}, 144800000, 5000, 1200)
return fsk4.NewFSK4(&NoRadio{}, 144800000, 5000, 1200*time.Millisecond)
}

type NoRadio struct{}
Expand Down
17 changes: 13 additions & 4 deletions examples/fsk4/si5351.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ package main

import (
"machine"
"time"

"tinygo.org/x/drivers/si5351"
"tinygo.org/x/wireless/fsk4"
)

func initRadio() *fsk4.FSK4 {
machine.I2C0.Configure(machine.I2CConfig{})
dev := si5351.New(machine.I2C0)
dev.Configure()
if err := dev.Configure(si5351.Config{}); err != nil {
panic(err)
}

f := fsk4.NewFSK4(&Si5351Radio{device: &dev}, 10140956, 270, 100)
f := fsk4.NewFSK4(&Si5351Radio{device: dev}, 14_097_060, 146, 682*time.Millisecond)
f.Configure()

return f
Expand All @@ -24,13 +28,18 @@ type Si5351Radio struct {
}

func (r *Si5351Radio) Transmit(freq uint64) error {
r.device.SetFrequency(freq, 0, si5351.PLL_A)
if err := r.device.SetRawFrequency(si5351.Clock0, si5351.Frequency(freq)); err != nil {
return err
}

r.device.EnableOutput(si5351.Clock0, true)

return nil

}

func (r *Si5351Radio) Standby() error {
r.device.OutputEnable(0, false)
r.device.EnableOutput(si5351.Clock0, false)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion examples/wspr/featherwing.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func initRadio() *fsk4.FSK4 {
println("setting OOK modulation")
dev.SetModulationType(sx127x.SX127X_OPMODE_MODULATION_OOK)

return fsk4.NewFSK4(&sx127xRadio{device: dev}, 10140956, 270, 100)
return fsk4.NewFSK4(&sx127xRadio{device: dev}, 14_097_060, 270, 100*time.Millisecond)
}

type sx127xRadio struct {
Expand Down
15 changes: 9 additions & 6 deletions examples/wspr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ func main() {
println("error writing WSPR message")
return
}
data = data[:n]

// transmit some data
for range 5 {
println("Transmitting WSPR message with", len(data), "symbols")
radio.WriteSymbols(data)

time.Sleep(3 * time.Second)
for range 50 {
println("Transmitting WSPR message with", n, "symbols")
if err := radio.WriteSymbols(data[:n]); err != nil {
println("error transmitting WSPR message:", err.Error())
return
}

println("Waiting for next transmission...")
time.Sleep(15 * time.Second)
}

time.Sleep(2 * time.Second)
Expand Down
8 changes: 6 additions & 2 deletions examples/wspr/noradio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

package main

import "tinygo.org/x/wireless/fsk4"
import (
"time"

"tinygo.org/x/wireless/fsk4"
)

func initRadio() *fsk4.FSK4 {
return fsk4.NewFSK4(&NoRadio{}, 144800000, 5000, 1200)
return fsk4.NewFSK4(&NoRadio{}, 144800000, 5000, 1200*time.Millisecond)
}

type NoRadio struct{}
Expand Down
18 changes: 14 additions & 4 deletions examples/wspr/si5351.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ package main

import (
"machine"
"time"

"tinygo.org/x/drivers/si5351"
"tinygo.org/x/wireless/fsk4"
)

func initRadio() *fsk4.FSK4 {
machine.I2C0.Configure(machine.I2CConfig{})

dev := si5351.New(machine.I2C0)
dev.Configure()
cnf := si5351.Config{}
if err := dev.Configure(cnf); err != nil {
panic(err)
}

f := fsk4.NewFSK4(&Si5351Radio{device: &dev}, 10140956, 270, 100)
f := fsk4.NewFSK4(&Si5351Radio{device: dev}, 14_097_060, 146, 682*time.Millisecond)
f.Configure()

return f
Expand All @@ -24,13 +30,17 @@ type Si5351Radio struct {
}

func (r *Si5351Radio) Transmit(freq uint64) error {
r.device.SetFrequency(freq, 0, si5351.PLL_A)
if err := r.device.SetRawFrequency(si5351.Clock0, si5351.Frequency(freq)); err != nil {
return err
}

r.device.EnableOutput(si5351.Clock0, true)

return nil
}

func (r *Si5351Radio) Standby() error {
r.device.OutputEnable(0, false)
r.device.EnableOutput(si5351.Clock0, false)

return nil
}
Expand Down
49 changes: 37 additions & 12 deletions fsk4/fsk4.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package fsk4

import (
"errors"
"time"
)

var (
baudRateTooHighError = errors.New("Baud rate too high, cannot keep up with transmission")
)

// FSK4 represents an FSK4 modem.
type FSK4 struct {
radio Radio
base uint64
shift uint32
rate uint32
rate time.Duration
tones [4]uint32
}

// NewFSK4 creates a new FSK4 modem instance.
func NewFSK4(radio Radio, base uint64, shift, rate uint32) *FSK4 {
// radio: the Radio interface implementation
// base: the base frequency in Hz
// shift: the frequency shift in Hz*100 eg. 270 = 2.7 Hz
// rate: the send rate
func NewFSK4(radio Radio, base uint64, shift uint32, rate time.Duration) *FSK4 {
return &FSK4{
radio: radio,
base: base,
Expand Down Expand Up @@ -51,8 +60,8 @@ func (r *FSK4) GetBaseFrequency() uint64 {
return r.base
}

// GetRate returns the current sample rate.
func (r *FSK4) GetRate() uint32 {
// GetRate returns the current transfer rate.
func (r *FSK4) GetRate() time.Duration {
return r.rate
}

Expand All @@ -61,8 +70,8 @@ func (r *FSK4) SetBaseFrequency(freq uint64) {
r.base = freq
}

// SetSampleRate sets the sample rate.
func (r *FSK4) SetSampleRate(rate uint32) {
// SetSampleRate sets the transfer rate.
func (r *FSK4) SetRate(rate time.Duration) {
r.rate = rate
}

Expand All @@ -80,8 +89,11 @@ func (r *FSK4) Standby() error {
// This is useful for protocols like WSPR that provide pre-encoded symbols.
func (r *FSK4) WriteSymbols(symbols []byte) error {
for _, symbol := range symbols {
r.tone(symbol & 0x03)
if err := r.tone(symbol & 0x03); err != nil {
return err
}
}

return r.Standby()
}

Expand All @@ -106,7 +118,9 @@ func (r *FSK4) writeByte(data byte) error {
symbol := (data & 0xC0) >> 6

// Modulate
r.tone(symbol)
if err := r.tone(symbol); err != nil {
return err
}

// Shift to next symbol
data = data << 2
Expand All @@ -115,9 +129,20 @@ func (r *FSK4) writeByte(data byte) error {
return nil
}

func (r *FSK4) tone(symbol byte) {
freq := r.base + uint64(r.tones[symbol])
r.radio.Transmit(freq)
func (r *FSK4) tone(symbol byte) error {
start := time.Now()
freq := r.base*100 + uint64(r.tones[symbol])
if err := r.radio.Transmit(freq); err != nil {
return err
}

// added delay to account for transmitter startup time
transmitStartupTime := time.Since(start)
if transmitStartupTime > r.rate {
return baudRateTooHighError
}
// hold for one symbol period
time.Sleep(time.Duration(1000000/r.rate) * time.Microsecond)
time.Sleep(r.rate - transmitStartupTime)

return nil
}
Loading