-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcc1200_modem.go
More file actions
818 lines (770 loc) · 21 KB
/
Copy pathcc1200_modem.go
File metadata and controls
818 lines (770 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
package m17
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
"sync"
"time"
"github.com/go-zeromq/zmq4"
"go.bug.st/serial"
"gopkg.in/ini.v1"
)
// CC1200 V2 commands
const (
cc1200CmdPing = iota
//SET
cc1200CmdSetRXFreq
cc1200CmdSetTXFreq
cc1200CmdSetTXPower
cc1200CmdSetReserved
cc1200CmdSetFreqCorr
cc1200CmdSetAFC
cc1200CmdTXStart
cc1200CmdRXStart
cc1200CmdRXData
cc1200CmdTXData
cc1200CmdDbgEnable
cc1200CmdDbgTxt
//GET
cc1200CmdGetIdent = iota + 0x80
cc1200CmdGetCaps
cc1200CmdGetRXFreq
cc1200CmdGetTXFreq
cc1200CmdGetTXPower
cc1200CmdGetFreqCorr
cc1200CmdGetBSBBuff
cc1200CmdGetRSSI
)
const (
cc1200ErrOK = iota //all good
cc1200ErrTrxPLL //TRX PLL lock error
cc1200ErrTrxSPI //TRX SPI comms error
cc1200ErrRange //value out of range
cc1200ErrCmdMalform //malformed command
cc1200ErrBusy //busy!
cc1200ErrBuffFull //buffer full
cc1200ErrNOP //nothing to do
cc1200ErrOther
)
var errBadCmd = errors.New("bad command")
type commandV2 struct {
cmd byte
size uint16
data []byte
}
func newCommandV2FromBytes(buf []byte) (commandV2, error) {
var ret commandV2
ret.cmd = buf[0]
_, err := binary.Decode(buf[1:3], binary.LittleEndian, &ret.size)
if err != nil {
return ret, fmt.Errorf("parse commandV2 size: %v", err)
}
if ret.size < 4 || ret.size > 963 {
return ret, errBadCmd
}
// Heuristics to detect bad commands
switch {
// All commands the firmware sends as of Dec. 2025
case ret.cmd == cc1200CmdPing && ret.size == 7:
fallthrough
case ret.cmd == cc1200CmdSetRXFreq && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdSetTXFreq && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdSetTXPower && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdSetFreqCorr && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdSetAFC && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdTXStart && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdRXStart && ret.size == 4:
fallthrough
case ret.cmd == cc1200CmdRXData && ret.size == 963:
fallthrough
case ret.cmd == cc1200CmdTXData && ret.size == 4:
fallthrough
// case ret.cmd == cc1200CmdDbgEnable && ret.size == 4:
// fallthrough
case ret.cmd == cc1200CmdDbgTxt && ret.size <= 131:
fallthrough
case ret.cmd == cc1200CmdGetIdent && ret.size <= 131:
fallthrough
case ret.cmd == cc1200CmdGetCaps && ret.size <= 4:
fallthrough
case ret.cmd == cc1200CmdGetRXFreq && ret.size <= 7:
fallthrough
case ret.cmd == cc1200CmdGetTXFreq && ret.size <= 7:
fallthrough
// case ret.cmd == cc1200CmdGetTXPower && ret.size <= :
// fallthrough
// case ret.cmd == cc1200CmdGetFreqCorr && ret.size <= :
// fallthrough
case ret.cmd == cc1200CmdGetBSBBuff && ret.size <= 4:
fallthrough
case ret.cmd == cc1200CmdGetRSSI && ret.size <= 4:
ret.data = make([]byte, ret.size-3)
copy(ret.data, buf[3:])
return ret, nil
default:
return ret, errBadCmd
}
}
func newCommandV2(cmd byte, data []byte) commandV2 {
var ret commandV2
ret.cmd = cmd
ret.size = uint16(len(data) + 3)
ret.data = data
// log.Printf("[DEBUG] newCommandV2(%d, [% x]): %v", cmd, data, ret)
return ret
}
func (c commandV2) Bytes() ([]byte, error) {
ret := make([]byte, c.size)
ret[0] = c.cmd
_, err := binary.Encode(ret[1:3], binary.LittleEndian, c.size)
if err == nil {
copy(ret[3:], c.data)
}
return ret, err
}
func (c commandV2) String() string {
return fmt.Sprintf("cmd: %d, size: %d, data: [% x]", c.cmd, c.size, c.data)
}
const (
txIdleCC1200 = iota
txActiveCC1200
)
// txTimeout must be greater than this!
const endTXWait = 8 * FrameTime
const txTimeout = endTXWait + 2*FrameTime
// Values calculated by SP5WWP to apply a 48us pre-emphasis
var iirBParam = []float64{2.8233128196365653, -1.0349763850514728}
var iirAParam = []float64{1.0, 0.7883364345850924}
type gpioLine interface {
SetValue(value int) error
Close() error
}
type CC1200Modem struct {
modem io.ReadWriteCloser
rxSymbols chan float32
s2s SymbolToSample
frameSink func(typ uint16, softBits []SoftBit)
mutex sync.Mutex
txState int // protected by mutex
txTimer *time.Timer
cmdSource chan commandV2
nRST gpioLine
boot0 gpioLine
debugLog *os.File
lastTXData time.Time
}
func NewCC1200Modem(
rxFrequency uint32,
txFrequency uint32,
power int8,
frequencyCorr int16,
afc bool,
modemCfg *ini.Section) (*CC1200Modem, error) {
port := modemCfg.Key("Port").String()
baudRate, baudRateErr := modemCfg.Key("Speed").Int()
nRSTPin, nRSTPinErr := modemCfg.Key("NRSTPin").Int()
boot0Pin, boot0PinErr := modemCfg.Key("Boot0Pin").Int()
zmqPort := modemCfg.Key("ZMQPort").MustInt()
var err error
err = errors.Join(
baudRateErr,
nRSTPinErr,
boot0PinErr,
)
if err != nil {
return nil, err
}
ret := &CC1200Modem{
rxSymbols: make(chan float32, 1),
s2s: NewSymbolToSample(rrcTaps5, TXSymbolScalingCoeff*transmitGain, false, 5),
cmdSource: make(chan commandV2, 1),
lastTXData: time.Now(),
}
ret.txTimer = time.AfterFunc(txTimeout, func() {
log.Printf("[DEBUG] TX timeout")
ret.stopTX()
ret.Start()
})
// Stop it until we transmit
ret.txTimer.Stop()
ret.txState = txIdleCC1200
log.Printf("[DEBUG] Opening modem")
err = ret.gpioSetup(nRSTPin, boot0Pin)
if err != nil {
return nil, err
}
mode := &serial.Mode{
BaudRate: baudRate,
}
ret.modem, err = serial.Open(port, mode)
if err != nil {
return nil, fmt.Errorf("modem open: %w", err)
}
rxSource := make(chan int8, samplesPerSecond)
ret.rxSymbols, err = ret.rxPipeline(rxSource)
if err != nil {
return nil, fmt.Errorf("rx pipeline setup: %w", err)
}
var zmqSource chan byte
if zmqPort > 0 {
zmqSource = make(chan byte, samplesPerSecond)
pub := zmq4.NewPub(context.Background())
// defer pub.Close()
err := pub.Listen(fmt.Sprintf("tcp://*:%d", zmqPort))
if err != nil {
log.Fatalf("Could not listen on ZeroMQ port %d: %v", zmqPort, err)
}
go func() {
buf := make([]byte, 0, 2048)
for {
buf = append(buf, <-zmqSource)
if len(buf) == 2048 {
pub.Send(zmq4.NewMsg(buf))
buf = make([]byte, 0, 2048)
}
}
}()
}
go ret.processReceivedData(rxSource, zmqSource)
log.Printf("[DEBUG] ping()")
_, err = ret.commandWithResponse(newCommandV2(cc1200CmdPing, []byte{}))
if err != nil {
return nil, fmt.Errorf("test PING: %w", err)
}
// ret.debugLog, err = os.OpenFile("/home/jim/debug.sym", os.O_CREATE|os.O_WRONLY, 0644)
// if err != nil {
// log.Printf("[DEBUG] Failure opening debug log: %v", err)
// } else {
// log.Printf("[DEBUG] Opened debug log: %v", ret.debugLog)
// }
err = ret.setRXFreq(rxFrequency)
if err != nil {
return nil, fmt.Errorf("setRXFreq: %w", err)
}
err = ret.setTXFreq(txFrequency)
if err != nil {
return nil, fmt.Errorf("setTXFreq: %w", err)
}
err = ret.setTXPower(power)
if err != nil {
return nil, fmt.Errorf("setTXPower: %w", err)
}
err = ret.setFreqCorrection(frequencyCorr)
if err != nil {
return nil, fmt.Errorf("setFreqCorrection: %w", err)
}
err = ret.setAFC(afc)
if err != nil {
return nil, fmt.Errorf("setAFC: %w", err)
}
return ret, nil
}
func (m *CC1200Modem) StartDecoding(sink func(typ uint16, softBits []SoftBit)) {
m.frameSink = sink
go m.processSymbols()
}
func (m *CC1200Modem) processReceivedData(rxSource chan int8, zmqSource chan byte) {
var buf []byte
var prevCmd commandV2
var badBuf []byte
var badCnt int
for {
var n int
var err error
if badBuf == nil {
buf = make([]byte, 3) // cmd + size
n, err = io.ReadFull(m.modem, buf)
if err != nil {
log.Printf("[ERROR] Error reading cmd from modem: %v", err)
break
}
} else {
// Advance one byte and try again
// Shift the last two bytes to the left and add an empty one to read into
buf = append(buf[1:], 0)
// Read buf[2] so now we have three again
n, err = io.ReadFull(m.modem, buf[2:])
if err != nil {
log.Printf("[ERROR] Error reading cmd from modem: %v", err)
break
}
}
// buf has cmd + size but not data
cmd, err := newCommandV2FromBytes(buf)
if err == errBadCmd {
// save the bad bytes to log them
badBuf = append(badBuf, buf[3-n:]...)
badCnt += n
continue
} else if err != nil {
log.Printf("[ERROR] Error building command: %v", err)
break
}
_, err = io.ReadFull(m.modem, cmd.data)
if err != nil {
log.Printf("[ERROR] Error reading data from modem: %v", err)
break
}
// if badCnt > 0 {
// log.Printf("[ERROR] received %v bad bytes: [% x]\n prev cmd: %v\n next cmd: %v", badCnt, badBuf, prevCmd, cmd)
// }
badBuf = nil
badCnt = 0
prevCmd = cmd
if cmd.cmd == cc1200CmdRXData {
for _, b := range cmd.data {
select {
case rxSource <- int8(b):
// sent
default:
// pipeline is full, so drop it
log.Printf("[DEBUG] processReceivedData dropped rx: %02x", b)
}
if zmqSource != nil {
select {
case zmqSource <- b:
// sent
default:
// pipeline is full, so drop it
}
}
}
} else {
select {
case m.cmdSource <- cmd:
default:
// Channel is full, so drop this cmd
log.Printf("[ERROR] processReceivedData dropped cmd: %d, size: %d, prevCmd: %d, size: %d",
cmd.cmd, cmd.size, prevCmd.cmd, prevCmd.size)
}
}
}
}
func (m *CC1200Modem) processSymbols() {
processSymbolStream(m.rxSymbols, m.frameSink, 5)
}
func (m *CC1200Modem) rxPipeline(sampleSource chan int8) (chan float32, error) {
// modem samples --> to float64 --> IIR filter --> RRC filter & scale
var err error
conv := NewConverter[int8, float64](sampleSource)
// dcf, err := NewDCFilter(sampleSource, 200) //len(rrcTaps5))
// if err != nil {
// return nil, fmt.Errorf("dc filter: %w", err)
// }
iir, err := NewIIRFilter(conv.Source(), iirBParam, iirAParam)
if err != nil {
return nil, fmt.Errorf("iir filter: %w", err)
}
s2s := NewSampleToSymbol(iir.Source(), rrcTaps5, RXSymbolScalingCoeff)
// ds, err := NewDownsampler(s2s.Source(), 5, 0)
// if err != nil {
// return nil, fmt.Errorf("downsampler: %w", err)
// }
return s2s.Source(), nil
}
func (m *CC1200Modem) setNRSTGPIO(set bool) error {
if m.nRST == nil {
// Emulation mode
return nil
}
log.Printf("[DEBUG] setNRSTGPIO(%v)", set)
if set {
return m.nRST.SetValue(1)
}
return m.nRST.SetValue(0)
}
func (m *CC1200Modem) setBoot0GPIO(set bool) error {
if m.boot0 == nil {
// Emulation mode
return nil
}
log.Printf("[DEBUG] setBoot0GPIO(%v)", set)
if set {
return m.boot0.SetValue(1)
}
return m.boot0.SetValue(0)
}
// Reset the modem
func (m *CC1200Modem) Reset() error {
log.Print("[DEBUG] modem Reset()")
err1 := m.setBoot0GPIO(false)
err2 := m.setNRSTGPIO(false)
time.Sleep(50 * time.Millisecond)
err3 := m.setNRSTGPIO(true)
errs := errors.Join(err1, err2, err3)
if errs != nil {
return fmt.Errorf("modem reset: %w", errs)
}
return nil
}
// Close the modem
func (m *CC1200Modem) Close() error {
log.Print("[DEBUG] modem Close()")
m.stopRX()
m.stopTX()
m.nRST.Close()
m.boot0.Close()
if m.debugLog != nil {
m.debugLog.Close()
}
return m.modem.Close()
}
func (m *CC1200Modem) TransmitPacket(p Packet) error {
log.Printf("[DEBUG] TransmitPacket: %v", p)
m.stopRX()
time.Sleep(2 * time.Millisecond)
m.startTX()
time.Sleep(10 * time.Millisecond)
var syms []Symbol
//fill preamble
syms = AppendPreamble(nil, lsfPreamble)
err := m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send preamble: %w", err)
}
// log.Printf("[DEBUG] TransmitPacket: sent preamble")
syms, err = generateLSFSymbols(p.LSF)
if err != nil {
return fmt.Errorf("failed to generate LSF symbols: %w", err)
}
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send LSF: %w", err)
}
// log.Printf("[DEBUG] TransmitPacket: sent LSF")
chunkCnt := 0
packetData := p.PayloadBytes()
for bytesLeft := len(packetData); bytesLeft > 0; bytesLeft -= 25 {
// log.Printf("[DEBUG] TransmitPacket: packetData: [% x], bytesLeft: %d", packetData, bytesLeft)
syms = AppendSyncwordSymbols(nil, PacketSync)
chunk := make([]byte, 25+1) // 25 bytes from the packet plus 6 bits of metadata
if bytesLeft > 25 {
// not the last chunk
copy(chunk, packetData[chunkCnt*25:chunkCnt*25+25])
chunk[25] = byte(chunkCnt << 2)
} else {
// last chunk
copy(chunk, packetData[chunkCnt*25:chunkCnt*25+bytesLeft])
//EOT bit set to 1, set counter to the amount of bytes in this (the last) chunk
if bytesLeft%25 == 0 {
chunk[25] = (1 << 7) | ((25) << 2)
} else {
chunk[25] = uint8((1 << 7) | ((bytesLeft % 25) << 2))
}
}
//encode the packet chunk
// log.Printf("[DEBUG] TransmitPacket: chunk: [% x]", chunk)
b, err := ConvolutionalEncode(chunk, PacketPuncturePattern, PacketModeFinalBit)
if err != nil {
return fmt.Errorf("unable to encode packet: %w", err)
}
encodedBits := NewPayloadBits(b)
rfBits := InterleaveBits(encodedBits)
rfBits = RandomizeBits(rfBits)
// Append chunk to the output
syms = AppendBits(syms, rfBits)
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send: %w", err)
}
// log.Printf("[DEBUG] TransmitPacket: sent payload, bytesLeft: %d", bytesLeft)
chunkCnt++
}
syms = AppendEOT(nil)
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send EOT: %w", err)
}
log.Printf("[DEBUG] Finished TransmitPacket")
time.Sleep(endTXWait)
log.Printf("[DEBUG] Finished TransmitPacket wait")
m.stopTX()
m.Start()
return nil
}
func (m *CC1200Modem) TransmitVoiceStream(sd StreamDatagram) error {
var syms []Symbol
var err error
// log.Printf("[DEBUG] TransmitVoiceStream id: %04x, fn: %04x, last: %v", sd.StreamID, sd.FrameNumber, sd.LastFrame)
m.mutex.Lock()
firstFrame := m.txState != txActiveCC1200
m.mutex.Unlock()
if firstFrame {
// First frame
log.Printf("[DEBUG] Sending LSF for stream %x, lsf: %v", sd.StreamID, sd.LSF)
m.stopRX()
time.Sleep(2 * time.Millisecond)
m.startTX()
time.Sleep(10 * time.Millisecond)
//fill preamble
syms = AppendPreamble(nil, lsfPreamble)
err := m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send preamble: %w", err)
}
syms, err = generateLSFSymbols(sd.LSF)
if err != nil {
return fmt.Errorf("failed to generate LSF symbols: %w", err)
}
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send LSF: %w", err)
}
}
// log.Printf("[DEBUG] Sending frame of stream %x, fn %d", sd.StreamID, sd.FrameNumber)
syms, err = generateStreamSymbols(sd)
if err != nil {
return fmt.Errorf("failed to generate LSF symbols: %w", err)
}
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send stream frame: %w", err)
}
if sd.LastFrame {
// send EOT
log.Printf("[DEBUG] Sending EOT for stream %04x, fn %04x", sd.StreamID, sd.FrameNumber)
syms = AppendEOT(nil)
err = m.writeSymbols(syms)
if err != nil {
return fmt.Errorf("failed to send EOT: %w", err)
}
log.Printf("[DEBUG] Finished TransmitVoiceStream")
time.Sleep(endTXWait)
log.Printf("[DEBUG] Finished TransmitVoiceStream wait")
m.stopTX()
m.Start()
}
return nil
}
func (m *CC1200Modem) startTX() error {
log.Printf("[DEBUG] startTX()")
err := m.commandWithErrResponse(newCommandV2(cc1200CmdTXStart, []byte{1}))
if err != nil {
log.Printf("[ERROR] startTX(): %v", err)
return fmt.Errorf("start TX: %w", err)
}
m.mutex.Lock()
m.txState = txActiveCC1200
m.mutex.Unlock()
// log.Printf("[DEBUG] startTX() txTimer.Reset")
m.txTimer.Reset(txTimeout)
return nil
}
func (m *CC1200Modem) stopTX() {
log.Print("[DEBUG] modem stopTX()")
m.mutex.Lock()
// Only stop if we've started
if m.txState == txActiveCC1200 {
m.mutex.Unlock()
// log.Print("[DEBUG] modem stopping TX")
err := m.commandWithErrResponse(newCommandV2(cc1200CmdTXStart, []byte{0}))
if err != nil {
log.Printf("[ERROR] stopTX(): %v", err)
}
m.mutex.Lock()
m.txState = txIdleCC1200
}
m.mutex.Unlock()
// log.Printf("[DEBUG] stopTX() txTimer.Stop")
m.txTimer.Stop()
}
func (m *CC1200Modem) setTXFreq(freq uint32) error {
log.Printf("[DEBUG] setTXFreq(%v)", freq)
data, err := binary.Append(nil, binary.LittleEndian, freq)
if err != nil {
return fmt.Errorf("encode set TX freq: %w", err)
}
cmd := newCommandV2(cc1200CmdSetTXFreq, data)
err = m.commandWithErrResponse(cmd)
if err != nil {
return fmt.Errorf("send set TX freq: %w", err)
}
return nil
}
func (m *CC1200Modem) setTXPower(dbm int8) error {
log.Printf("[DEBUG] setTXPower(%v)", dbm)
cmd := newCommandV2(cc1200CmdSetTXPower, []byte{byte(dbm)})
err := m.commandWithErrResponse(cmd)
if err != nil {
return fmt.Errorf("send set TX power: %w", err)
}
return nil
}
func (m *CC1200Modem) Start() error {
// log.Printf("[DEBUG] Start()")
m.mutex.Lock()
m.txState = txIdleCC1200
m.mutex.Unlock()
// log.Printf("[DEBUG] sending start cmd")
err := m.commandWithErrResponse(newCommandV2(cc1200CmdRXStart, []byte{1}))
if err != nil {
log.Printf("[ERROR] Start(): %v", err)
return fmt.Errorf("send set RX start error: %w", err)
}
// log.Printf("[DEBUG] end Start()")
return nil
}
func (m *CC1200Modem) stopRX() error {
m.mutex.Lock()
// Only stop if we've started
if m.txState == txIdleCC1200 {
m.mutex.Unlock()
log.Printf("[DEBUG] stopRX()")
err := m.commandWithErrResponse(newCommandV2(cc1200CmdRXStart, []byte{0}))
if err != nil {
log.Printf("[ERROR] stopRX(): %v", err)
return fmt.Errorf("send set RX stop: %w", err)
}
m.mutex.Lock()
}
m.mutex.Unlock()
return nil
}
func (m *CC1200Modem) setRXFreq(freq uint32) error {
log.Printf("[DEBUG] setRXFreq(%v)", freq)
data, err := binary.Append(nil, binary.LittleEndian, freq)
if err != nil {
return fmt.Errorf("encode set RX freq: %w", err)
}
cmd := newCommandV2(cc1200CmdSetRXFreq, data)
err = m.commandWithErrResponse(cmd)
if err != nil {
return fmt.Errorf("send set RX freq: %w", err)
}
return nil
}
func (m *CC1200Modem) setAFC(afc bool) error {
log.Printf("[DEBUG] setAFC(%v)", afc)
var err error
var a byte
if afc {
a = 1
}
cmd := newCommandV2(cc1200CmdSetAFC, []byte{a})
err = m.commandWithErrResponse(cmd)
if err != nil {
return fmt.Errorf("send set AFC: %w", err)
}
return nil
}
func (m *CC1200Modem) setFreqCorrection(corr int16) error {
log.Printf("[DEBUG] setFreqCorrection(%v)", corr)
data, err := binary.Append(nil, binary.LittleEndian, corr)
if err != nil {
return fmt.Errorf("encode set RX freq: %w", err)
}
cmd := newCommandV2(cc1200CmdSetFreqCorr, data)
err = m.commandWithErrResponse(cmd)
if err != nil {
return fmt.Errorf("send set freq corr: %w", err)
}
return nil
}
func (m *CC1200Modem) writeSymbols(symbols []Symbol) error {
buf := m.s2s.Transform(symbols)
if m.debugLog != nil {
_, err := m.debugLog.Write(buf)
if err != nil {
log.Printf("[DEBUG] Failed to write to debug log: %v", err)
}
}
since := time.Since(m.lastTXData)
if since > 4*FrameTime {
log.Printf("[DEBUG] Last TX data sent %v ago", since.Round(time.Millisecond))
}
m.lastTXData = time.Now()
cmd := newCommandV2(cc1200CmdTXData, buf)
resp := byte(cc1200ErrBuffFull)
for resp == cc1200ErrBuffFull {
rc, err := m.commandWithResponse(cmd)
if err != nil {
return fmt.Errorf("commandWithResponse error: %w", err)
}
// log.Printf("[DEBUG] writeSymbols() txTimer.Reset")
m.txTimer.Reset(txTimeout)
resp = rc.data[0]
if resp == cc1200ErrBuffFull {
// log.Printf("[DEBUG] flow control")
// Wait a bit, then retry
time.Sleep(FrameTime)
}
}
if resp != cc1200ErrOK {
return fmt.Errorf("writeSymbols response: %x", resp)
}
return nil
}
func (m *CC1200Modem) command(cmd commandV2) error {
// log.Printf("[DEBUG] command(): %v", cmd)
b, err := cmd.Bytes()
if err != nil {
return fmt.Errorf("command: %w", err)
}
// log.Printf("[DEBUG] modem.Write(): [% x]", b)
_, err = m.modem.Write(b)
if err != nil {
log.Printf("[ERROR] modem.Write([% x]): %v", b, err)
return fmt.Errorf("command: %w", err)
}
return nil
}
func (m *CC1200Modem) commandWithResponse(cmd commandV2) (commandV2, error) {
// log.Printf("[DEBUG] commandWithResponse() sending: %v", cmd)
// clear old responses
for more := true; more; {
select {
case c := <-m.cmdSource:
log.Printf("[DEBUG] old response: %v", c)
more = true
default:
more = false
}
}
err := m.command(cmd)
if err != nil {
return commandV2{}, err
}
var resp commandV2
select {
case resp = <-m.cmdSource:
case <-time.After(txTimeout / 2):
err = fmt.Errorf("response to command %v timed out", cmd)
}
return resp, err
}
func (m *CC1200Modem) commandWithErrResponse(cmd commandV2) error {
// log.Printf("[DEBUG] commandWithErrResponse() sending: %v", cmd)
var err error
var respErr int
respCmd, err := m.commandWithResponse(cmd)
if err != nil {
return fmt.Errorf("commandWithErrResponse error: %w", err)
}
// log.Printf("[DEBUG] respBuf: % x", respBuf)
switch len(respCmd.data) {
case 1:
respErr = int(respCmd.data[0])
case 4:
_, err = binary.Decode(respCmd.data, binary.LittleEndian, respErr)
if err != nil {
return fmt.Errorf("parse modem response: %v", err)
}
default:
return fmt.Errorf("unexpected response: %#v", respCmd)
}
if respErr != 0 {
log.Printf("[ERROR] commandWithErrResponse: %x", respErr)
return fmt.Errorf("modem response: %d", respErr)
}
return nil
}