-
Notifications
You must be signed in to change notification settings - Fork 1
/
RF-Waterfall-CC1101.spin2
290 lines (238 loc) · 8.71 KB
/
RF-Waterfall-CC1101.spin2
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
{
--------------------------------------------
Filename: RF-Waterfall-CC1101.spin2
Description: Display received power levels in the frequency domain
as a "waterfall"
Utilizes ISM-band transceiver IC's (CC1101 version)
Author: Jesse Burt
Copyright (c) 2020
Started: Apr 6, 2020
Updated: Apr 28, 2020
See end of file for terms of use.
--------------------------------------------
}
CON
_clkfreq = 250_000_000
_xtlfreq = 20_000_000
LABEL_COL = 0
DATA_COL = 11
VAR
long _center, _basefreq, _span, _start, _end
long _dly
byte _offset
OBJ
ser: "com.serial.terminal.ansi" | SER_BAUD=2_000_000
vga: "display.vga.bitmap-8bpp" | BASEPIN=0, VF=10, VB=33, HF=16, HS=89, HB=85, HV=640
cc1101: "wireless.transceiver.cc1101" | CS=32+3, SCK=32+2, MOSI=32+0, MISO=32+1, SPI_FREQ=5_000_000
str: "string"
fnt: "font.5x8"
wfall: "metrology.waterfall"
PUB main() | lna, dvga, cmd, rxbw, intfreq
setup()
' set initial RF parameters
_center := 434_375_000 ' 300_000_000..348_000_000, 387_000_000..464_000_000, 779_000_000..928_000_000
_span := 1_000_000 ' Freq span from base freq
_start := _center - (_span/2)
_end := _center + (_span/2)
_basefreq := _start
cc1101.gpio0(cc1101.IO_HI_Z)
cc1101.idle() ' Change to Idle while changing settings
cc1101.auto_cal_mode(cc1101.IDLE_RXTX)
cc1101.carrier_freq(_basefreq)
cc1101.syncwd_mode(cc1101.SYNCMODE_3032_CS)
cc1101.carrier_sense_thresh(14)
cc1101.magn_target(24)
cc1101.dvga_gain(-3)
cc1101.lna_gain(0)
cc1101.rx_bw(58)
cc1101.agc_mode(cc1101.AGC_OFF)
cc1101.dc_block_ena(false)
cc1101.rx_mode()
lna := dvga := rxbw := 0
intfreq := 380859
_offset := 138 #> 138
' set up the waterfall initial parameters
wfall.init( 0, vga.font_height()*6, ... ' x,y: leftmost, six text rows down from the top
300, vga.font_height()*12, ... ' w,h: 300px, 12 text rows
@vga, ... ' attach to the vga object
@_rssi, ... ' point to the table of acquired RSSI values
300) ' 300 entries long
wfall.set_color_scale(1)
display_settings()
repeat
read_rssi()
wfall.draw()
if lookdown(cmd := ser.getchar_noblock(): 32..127)
case cmd
"=":
lna := (lna + 1) <# 7
cc1101.lna_gain(lookupz(lna: -17, -14, -11, -9, -7, -6, -2, 0))
"-":
lna := (lna - 1) #> 0
cc1101.lna_gain(lookupz(lna: -17, -14, -11, -9, -7, -6, -2, 0))
"]":
dvga := (dvga + 1) <# 7
cc1101.dvga_gain(lookupz(dvga: -3, -2, -1, 0))
"[":
dvga := (dvga - 1) #> 0
cc1101.dvga_gain(lookupz(dvga: -3, -2, -1, 0))
";":
_offset := (_offset - 1) #> 138
"'":
_offset := (_offset + 1) <# 255
".":
rxbw := (rxbw + 1) <# 15
cc1101.rx_bw(lookupz(rxbw: 58, 68, 81, 102, 116, 135, 162, 203, 232, 270, 325, 406, 464, 541, 650, 812))
",":
rxbw := (rxbw - 1) #> 0
cc1101.rx_bw(lookupz(rxbw: 58, 68, 81, 102, 116, 135, 162, 203, 232, 270, 325, 406, 464, 541, 650, 812))
"p":
intfreq := (intfreq + 25390) <# 787109
cc1101.interm_freq(intfreq)
"o":
intfreq := (intfreq - 25390) #> 25390
cc1101.interm_freq(intfreq)
"s":
_span := (_span - 10_000) #> 10_000
"S":
_span := (_span + 10_000) <# 5_000_000
"b":
_basefreq := (_basefreq - 50_000) #> 300_000_000
"B":
_basefreq := (_basefreq + 50_000) <# 928_000_000
"d":
_dly := (_dly-1) #> 0
ser.printf("%d\n\r", _dly)
"D":
_dly := (_dly+1) <# 1000
ser.printf("%d\n\r", _dly)
other:
display_settings() ' Update the settings display
PUB display_settings() | basefreq, span, lna, dvga, rxbw, ifreq, startrow
vga.clear()
lna := cc1101.lna_gain()
dvga := cc1101.dvga_gain()
rxbw := cc1101.rx_bw()
ifreq := cc1101.interm_freq()
vga.fgcolor(255)
vga.pos_xy(0, 2)
vga.printf("%sHz", dec_thousands(_basefreq, ",") )
vga.printf(" Span: %sHz ", dec_thousands(_span, ",") )
startrow := ( (wfall._sy + wfall._height+4) / vga.fontheight() )
vga.pos_xy(LABEL_COL, startrow)
vga.str(@"LNA Gain: ")
vga.pos_xy(DATA_COL, startrow)
vga.printf("%ddB ", lna)
vga.pos_xy(LABEL_COL, startrow+1)
vga.str(@"DVGA Gain: ")
vga.pos_xy(DATA_COL, startrow+1)
vga.printf("%ddB ", dvga)
vga.pos_xy(LABEL_COL, startrow+2)
vga.str(@"RX BW: ")
vga.pos_xy(DATA_COL, startrow+2)
vga.printf("%dkHz ", rxbw)
vga.pos_xy(LABEL_COL, startrow+3)
vga.str(@"IF: ")
vga.pos_xy(DATA_COL, startrow+3)
ifnot ( ifreq/1000 > (rxbw/2) )
vga.fgcolor(190) ' current IF is bad; color it red
else
vga.fgcolor(255)
vga.printf("%sHz ", dec_thousands(ifreq, ",") )
vga.fgcolor(255)
draw_scale(wfall._right+5, wfall._sy, wfall._height)
var long _rssi[300]
PUB read_rssi() | i, freqstep, f
' Acquire RSSI measurement for each frequency within the currently set span
freqstep := _span/wfall._width ' calc step from ratio of freq span:spec plot width
cc1101.idle()
cc1101.cal_freq_synth()
cc1101.rx_mode()
waitus(2000) ' wait for receiver to settle
i := 0
repeat f from _basefreq to _basefreq+_span step freqstep
cc1101.carrier_freq(f)
waitus(_dly) ' give RSSI time to settle
_rssi[i++] := (cc1101.rssi()+_offset)
PRI draw_scale(x, y, ht) | i, c, w, scl, ci
' Draw the color scale used by the waterfall
w := 5
scl := (vga.MAX_COLOR+1)/ht ' scale color range to waterfall height
ci := 0
repeat i from (y+ht) to y ' draw from bottom to the top
vga.line(x, i, x+w, i, (ci*scl) )
ci++
var byte _tmp[15]
pri dec_thousands(n, c): p
' Convert an integer to a string representation with thousands separators
' n: integer
' c: character to use for separator (e.g., "," or ".")
bytefill(@_tmp, 0, 15)
if ( n >= 1_000_000 )
str.sprintf5(@_tmp, @"%d%c%03.3d%c%03.3d", (n/1_000_000), c, ...
(n//1_000_000)/1000, c, ...
(n//1_000) )
elseif ( n >= 1_000 )
str.sprintf3(@_tmp, @"%d%c%03.3d", (n/1_000), c, ...
(n//1_000) )
return @_tmp
PUB setup()
ser.start()
ser.clear()
ser.printf("Serial terminal started - p2 @ %dMHz\n\r", clkfreq/1000000)
vga.defaults()
fullspecpalette()
vga.start()
vga.set_font(fnt.ptr, fnt.setup)
vga.char_attrs(vga.TERMINAL)
vga.clear()
ser.strln(@"VGA 8bpp driver started")
vga.fgcolor(255)
if ( cc1101.start() )
ser.strln(@"CC1101 driver started")
else
ser.strln(@"CC1101 driver failed to start - halting")
repeat
PUB greyscalepalette() | i, p
p := vga.palette_ptr()
repeat i from 0 to 255
long[p][i] := 0 | (i << 16) | (i << 8) | i
PUB fullspecpalette() | i, r, g, b, c, p
' Set up palette
r := g := b := c := 0
p := vga.palette_ptr()
repeat i from 0 to 255
case i
0..31:
r += 4
g := 0
b += 4
32..63:
r -= 4
g := 0
b := b
64..95:
r := 0
g += 4
b := b
96..127:
r := 0
g := g
b -= 4
128..159:
r += 4
g := g
b := b
160..191:
r := r
g -= 4
b := 0
192..254:
r := r
g += 4
b += 4
255:
r := g := b := 255
c := 0 | (r << 16) | (g << 8) | b
long[p][i] := c
long[p][0] := $00_00_00_00