Skip to content

Commit a35786b

Browse files
committed
sx127x: add functions used for FSK radio communication
Signed-off-by: deadprogram <[email protected]>
1 parent 2a42fa7 commit a35786b

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

smoketest.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/xpt2046/mai
124124
tinygo build -size short -o ./build/test.elf -target=m5stack-core2 ./examples/ft6336/basic/
125125
tinygo build -size short -o ./build/test.elf -target=m5stack-core2 ./examples/ft6336/touchpaint/
126126
tinygo build -size short -o ./build/test.hex -target=nucleo-wl55jc ./examples/sx126x/lora_rxtx/
127+
tinygo build -size short -o ./build/test.hex -target=pybadge ./examples/sx127x/lora_rxtx/
127128
tinygo build -size short -o ./build/test.uf2 -target=pico ./examples/ssd1289/main.go
128129
tinygo build -size short -o ./build/test.hex -target=pico ./examples/irremote/main.go
129130
tinygo build -size short -o ./build/test.hex -target=badger2040 ./examples/uc8151/main.go

sx127x/registers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ const (
9696
SX127X_OPMODE_RX_SINGLE = uint8(0x06)
9797
SX127X_OPMODE_CAD = uint8(0x07)
9898

99+
SX127X_OPMODE_LOW_FREQUENCY = uint8(0x4)
100+
101+
SX127X_OPMODE_MODULATION_MASK = uint8(0x60)
102+
SX127X_OPMODE_MODULATION_FSK = uint8(0x0)
103+
SX127X_OPMODE_MODULATION_OOK = uint8(0x20)
104+
99105
SX127X_LORA_MAC_PUBLIC_SYNCWORD = 0x34
100106
SX127X_LORA_MAC_PRIVATE_SYNCWORD = 0x14
101107
)

sx127x/sx127x.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ type Device struct {
2525
rstPin machine.Pin // GPIO for reset
2626
radioEventChan chan lora.RadioEvent // Channel for Receiving events
2727
loraConf lora.Config // Current Lora configuration
28-
controller RadioController // to manage interactions with the radio
28+
controller RadioController // to manage interrupts with the radio
2929
deepSleep bool // Internal Sleep state
30-
deviceType int // sx1261,sx1262,sx1268 (defaults sx1261)
30+
deviceType int // sx1272, sx1273, sx1276, sx1279 (defaults sx1276)
3131
spiTxBuf []byte // global Tx buffer to avoid heap allocations in interrupt
3232
spiRxBuf []byte // global Rx buffer to avoid heap allocations in interrupt
3333
}
@@ -65,6 +65,11 @@ func (d *Device) SetRadioController(rc RadioController) error {
6565
return nil
6666
}
6767

68+
// Specify device type (sx1272, sx1273, sx1276, sx1279)
69+
func (d *Device) SetDeviceType(devType int) {
70+
d.deviceType = devType
71+
}
72+
6873
// Reset re-initialize the sx127x device
6974
func (d *Device) Reset() {
7075
d.rstPin.Low()
@@ -81,23 +86,31 @@ func (d *Device) DetectDevice() bool {
8186

8287
// ReadRegister reads register value
8388
func (d *Device) ReadRegister(reg uint8) uint8 {
84-
d.controller.SetNss(false)
89+
if d.controller != nil {
90+
d.controller.SetNss(false)
91+
}
92+
8593
// Send register
86-
//d.spiTxBuf = []byte{reg & 0x7f}
8794
d.spiTxBuf = d.spiTxBuf[:0]
8895
d.spiTxBuf = append(d.spiTxBuf, byte(reg&0x7f))
8996
d.spi.Tx(d.spiTxBuf, nil)
9097
// Read value
9198
d.spiRxBuf = d.spiRxBuf[:0]
9299
d.spiRxBuf = append(d.spiRxBuf, 0)
93100
d.spi.Tx(nil, d.spiRxBuf)
94-
d.controller.SetNss(true)
101+
if d.controller != nil {
102+
d.controller.SetNss(true)
103+
}
104+
95105
return d.spiRxBuf[0]
96106
}
97107

98108
// WriteRegister writes value to register
99109
func (d *Device) WriteRegister(reg uint8, value uint8) uint8 {
100-
d.controller.SetNss(false)
110+
if d.controller != nil {
111+
d.controller.SetNss(false)
112+
}
113+
101114
// Send register
102115
d.spiTxBuf = d.spiTxBuf[:0]
103116
d.spiTxBuf = append(d.spiTxBuf, byte(reg|0x80))
@@ -108,7 +121,10 @@ func (d *Device) WriteRegister(reg uint8, value uint8) uint8 {
108121
d.spiRxBuf = d.spiRxBuf[:0]
109122
d.spiRxBuf = append(d.spiRxBuf, 0)
110123
d.spi.Tx(d.spiTxBuf, d.spiRxBuf)
111-
d.controller.SetNss(true)
124+
if d.controller != nil {
125+
d.controller.SetNss(true)
126+
}
127+
112128
return d.spiRxBuf[0]
113129
}
114130

@@ -119,9 +135,20 @@ func (d *Device) SetOpMode(mode uint8) {
119135
d.WriteRegister(SX127X_REG_OP_MODE, new)
120136
}
121137

122-
// SetOpMode changes the sx1276 mode
138+
// SetOpModeLora changes the sx1276 mode to lora.
123139
func (d *Device) SetOpModeLora() {
124-
d.WriteRegister(SX127X_REG_OP_MODE, SX127X_OPMODE_LORA)
140+
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)|SX127X_OPMODE_LORA)
141+
}
142+
143+
// SetOpModeFsk changes the sx1276 mode to fsk/ook.
144+
func (d *Device) SetOpModeFsk() {
145+
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)&^SX127X_OPMODE_LORA)
146+
}
147+
148+
// SetModulationType changes the modulation type (SX127X_OPMODE_MODULATION_FSK, SX127X_OPMODE_MODULATION_OOK)
149+
func (d *Device) SetModulationType(typ uint8) {
150+
cleared := d.ReadRegister(SX127X_REG_OP_MODE) &^ SX127X_OPMODE_MODULATION_MASK
151+
d.WriteRegister(SX127X_REG_OP_MODE, cleared|typ)
125152
}
126153

127154
// GetVersion returns hardware version of sx1276 chipset
@@ -244,9 +271,9 @@ func (d *Device) SetLowDataRateOptim(val uint8) {
244271
// SetLowFrequencyModeOn enables Low Data Rate Optimization
245272
func (d *Device) SetLowFrequencyModeOn(val bool) {
246273
if val {
247-
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)|0x04)
274+
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)|SX127X_OPMODE_LOW_FREQUENCY)
248275
} else {
249-
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)&0xfb)
276+
d.WriteRegister(SX127X_REG_OP_MODE, d.ReadRegister(SX127X_REG_OP_MODE)&^SX127X_OPMODE_LOW_FREQUENCY)
250277
}
251278
}
252279

0 commit comments

Comments
 (0)