-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspitest
executable file
·247 lines (212 loc) · 8.31 KB
/
spitest
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
#!/usr/bin/python
import sys
from time import sleep
import spidev
import Adafruit_BBIO.GPIO as GPIO
SPI = spidev.SpiDev()
myDev = "/dev/spidev1.0"
myAddr = [0xca, 0xfe, 0xfa, 0xce]
remAddr = [0xd0, 0x0d, 0xd0, 0x0d]
confReg = {'chan', 'conf_0', 'addr_fws', 'rx_pw', 'tx_pw', 'rx_addr_0', 'rx_addr_1', 'rx_addr_2', 'rx_addr_3', 'conf_1'}
testFreq = 0b01101010
def main():
if len(sys.argv) > 1:
if (sys.argv[1]=="tx")or(sys.argv[1]=="rx")or(sys.argv[1]=="cd")or(sys.argv[1]=="am"):
init()
try:
print_config()
if (sys.argv[1] == "tx"):
for txcount in range (0, 1):
test_tx(testFreq)
elif (sys.argv[1] == "rx"):
test_rx()
elif (sys.argv[1] == "cd"):
test_cd()
elif (sys.argv[1] == "am"):
test_am()
finally:
SPI.close()
set_power_down()
GPIO.cleanup()
else:
usage()
sys.exit()
else:
usage()
sys.exit()
## Functions:
def usage():
print "usage: "+'\033[93m'+"scriptname "+'\033[94m'+"tx"+'\033[91m'+"|"+'\033[94m'+"rx"+'\033[91m'+"|"+'\033[94m'+"cd"+'\033[91m'+"|"+'\033[94m'+"am"
print '\033[94m'+"\ttx"+'\033[0m'+": send tx test payload from "+'\033[92m'+"myAddr"+'\033[0m'+" to "+'\033[92m'+"remAddr"+'\033[0m'+" at "+'\033[92m'+"testFreq"
print '\033[94m'+"\trx"+'\033[0m'+": listen for packet addressed to "+'\033[92m'+"myAddr"+'\033[0m'+" at "+'\033[92m'+"testFreq"+'\033[0m'+" and display payload"
print '\033[94m'+"\tam"+'\033[0m'+": listen for packet addressed to "+'\033[92m'+"myAddr"+'\033[0m'+" at "+'\033[92m'+"testFreq"
print '\033[94m'+"\tcd"+'\033[0m'+": listen for carrier at "+'\033[92m'+"testFreq"+'\033[0m'
def init():
sys.stdout.write('\033[94m'+"[*]"+'\033[0m'+" Initializing radio... ")
sys.stdout.flush()
GPIO.setup("P9_11", GPIO.OUT) # PWR - Power up chip. Active high.
GPIO.setup("P9_13", GPIO.OUT) # TXEN - Transmit mode. Low for Recieve mode.
GPIO.setup("P9_15", GPIO.OUT) # CE - Enable transciever. Active high.
GPIO.setup("P9_12", GPIO.IN) # CD - Carrier detect. High when carrier present on tuned freq at greater than -95dBm.
GPIO.setup("P9_14", GPIO.IN) # AM - Address match. High when packet header address field matches transciever address.
GPIO.setup("P9_16", GPIO.IN) # DR - Data ready. High when data packet is recieved
GPIO.output("P9_11", GPIO.LOW)
GPIO.output("P9_13", GPIO.LOW)
GPIO.output("P9_15", GPIO.LOW)
set_stand_by()
try:
SPI.open(1, 0)
SPI.xfer2([0b00000000, testFreq, 0b00011100, 0b01000100, 0b01000100, 0b00000100, myAddr[0], myAddr[1], myAddr[2], myAddr[3], 0b11011111])
print "done.\n"
except:
print '\033[91m'+"Failsauce! "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def set_config(addr, val1, val2):
try:
sys.stdout.write('\033[94m'+"[*]"+'\033[0m'+" Changing config register[s]... ")
sys.stdout.flush()
set_stand_by()
SPI.open(1, 0)
SPI.xfer2([0b00000000^addr, val1, val2])
print "done.\n"
except:
print '\033[91m'+"Failmobile! Could not write register. "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def test_am():
try:
count = 0
sys.stdout.write('\033[94m'+"[*]"+'\033[0m'+" Listening for own address in header... ")
sys.stdout.flush()
while True:
set_stand_by()
SPI.open(1, 0)
SPI.xfer2([0b00000000, testFreq, 0b00001100, 0b01000100, 0b01000100, 0b00000100, myAddr[0], myAddr[1], myAddr[2], myAddr[3], 0b11011111])
set_rx()
while (not GPIO.input("P9_14")):
continue
count+=1
print "["+str(count)+"] address matched... "
except:
print '\033[91m'+"Failgasm! Could not detect packet with our address. "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def test_cd():
try:
count = 0
sys.stdout.write('\033[94m'+"[*]"+'\033[0m'+" Listening for carrier... ")
sys.stdout.flush()
while True:
set_stand_by()
SPI.open(1, 0)
SPI.xfer2([0b00000000, testFreq, 0b00001100, 0b01000100, 0b01000100, 0b00000100, myAddr[0], myAddr[1], myAddr[2], myAddr[3], 0b11011111])
set_rx()
while (not GPIO.input("P9_12")):
continue
count+=1
print "["+str(count)+"] carrier found. "
except:
print '\033[91m'+"Failgasm! Could not detect carrier on our frequency. "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def test_rx():
try:
sys.stdout.write('\033[94m'+"[*]"+'\033[0m'+" Listening... ")
sys.stdout.flush()
set_stand_by()
SPI.open(1, 0)
SPI.xfer2([0b00000000, testFreq, 0b00001100, 0b01000100, 0b01000100, 0b00000100, myAddr[0], myAddr[1], myAddr[2], myAddr[3], 0b11011111])
SPI.close()
set_rx()
sleep(0.01)
while (not GPIO.input("P9_12")):
continue
sys.stdout.write("\ncarrier found... ")
sys.stdout.flush()
while (not GPIO.input("P9_14")):
continue
sys.stdout.write("\naddress match found... ")
sys.stdout.flush()
set_stand_by()
while (not GPIO.input("P9_16")):
continue
SPI.open(1, 0)
res = SPI.xfer([0b00100100])
print '{:08b}'.format(res[0])
res = SPI.readbytes(4)
sys.stdout.write("RX Buffer: "+'{:08b}'.format(res[0])+" "+'{:08b}'.format(res[1])+" "+'{:08b}'.format(res[2])+" "+'{:08b}'.format(res[3])+"\n")
sys.stdout.flush()
except:
print '\033[91m'+"Failgasm! Could not recieve payload. "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def test_tx(freq):
try:
set_tx()
# Writing address, payload and config:
SPI.open(1, 0)
SPI.xfer2([0b00100010, remAddr[0], remAddr[1], remAddr[2], remAddr[3]])
SPI.close()
SPI.open(1, 0)
SPI.xfer2([0b00100000, 11, 110, 22, 220])
SPI.close()
SPI.open(1, 0)
SPI.xfer2([0b00000000, freq, 0b00011100, 0b01000100, 0b01000100, 0b00000100, myAddr[0], myAddr[1], myAddr[2], myAddr[3], 0b11011111])
sys.stdout.write('\033[94m'+"\n[*]"+'\033[0m'+" Trying send payload...")
sys.stdout.flush()
while (not GPIO.input("P9_16")):
continue
print " done.\n"
except:
sys.stdout.write('\033[91m'+"Failmobile! Could not send payload "+'\033[0m', sys.exc_info()[0])
sys.stdout.flush()
finally:
SPI.close()
def print_config():
try:
set_stand_by()
SPI.open(1, 0)
print '\033[94m'+"[*]"+'\033[0m'+" Reading config registers:"
for offset in range (0b0000, 0b1010):
bytes = SPI.xfer2([0b00010000^offset, 0b00010000^offset])
sys.stdout.write(str("\t"+hex(offset)+": "+'\033[93m'+'{:08b}'.format(bytes[1]))+'\033[0m'+": "+'\033[92m'+hex(bytes[1])+'\033[0m'+" \n")
sys.stdout.flush()
except:
print '\033[91m'+"Could not read byte. "+'\033[0m', sys.exc_info()[0]
finally:
SPI.close()
def set_power_down():
GPIO.setup("P9_11", GPIO.OUT)
GPIO.output("P9_11", GPIO.LOW)
GPIO.setup("P9_13", GPIO.IN)
GPIO.setup("P9_15", GPIO.IN)
def set_stand_by():
GPIO.setup("P9_11", GPIO.OUT)
GPIO.output("P9_11", GPIO.HIGH)
GPIO.setup("P9_13", GPIO.IN)
GPIO.setup("P9_15", GPIO.OUT)
GPIO.output("P9_15", GPIO.LOW)
def set_read_rx():
GPIO.setup("P9_11", GPIO.OUT)
GPIO.output("P9_11", GPIO.HIGH)
GPIO.setup("P9_13", GPIO.OUT)
GPIO.output("P9_13", GPIO.LOW)
GPIO.setup("P9_15", GPIO.IN)
def set_rx():
GPIO.setup("P9_11", GPIO.OUT)
GPIO.output("P9_11", GPIO.HIGH)
GPIO.setup("P9_13", GPIO.OUT)
GPIO.output("P9_13", GPIO.LOW)
GPIO.setup("P9_15", GPIO.OUT)
GPIO.output("P9_15", GPIO.HIGH)
def set_tx():
GPIO.setup("P9_11", GPIO.OUT)
GPIO.output("P9_11", GPIO.HIGH)
GPIO.setup("P9_13", GPIO.OUT)
GPIO.output("P9_13", GPIO.HIGH)
GPIO.setup("P9_15", GPIO.OUT)
GPIO.output("P9_15", GPIO.HIGH)
############################
if __name__ == "__main__":
main()