Tidy up rfmtest.py to fix all flake8 errors
This commit is contained in:
parent
1932e05c7d
commit
7ad9f0dfa5
1 changed files with 154 additions and 130 deletions
|
@ -1,159 +1,183 @@
|
||||||
# rfmtest.py - performs SPI comms on raspberry pi to RFM95 IC. Paul Richarsd pi@sosgez.co.uk
|
# rfmtest.py - performs SPI comms on raspberry pi to RFM95 IC. Paul Richarsd pi@sosgez.co.uk
|
||||||
#
|
|
||||||
# NB this is python2 code. SPI data transfer does not use external driver.
|
# NB this is python2 code. SPI data transfer does not use external driver.
|
||||||
#
|
#
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
from time import sleep
|
from time import sleep
|
||||||
#define GPIO board pin assignments for comms
|
# define GPIO board pin assignments for comms
|
||||||
|
|
||||||
|
|
||||||
|
def swait(): # just a short delay.
|
||||||
|
sleep(0.001)
|
||||||
|
|
||||||
def swait(): # just a short delay.
|
|
||||||
sleep(0.001)
|
|
||||||
|
|
||||||
def initports():
|
def initports():
|
||||||
global gclk,gout,gin,ginit,gen
|
global gclk, gout, gin, ginit, gen
|
||||||
ginit=15 # was 3 # chip reset
|
ginit = 15 # wassyn 3 # chip reset
|
||||||
gclk=23 # CLOCK
|
gclk = 23 # CLOCK
|
||||||
gout=19 # MOSI
|
gout = 19 # MOSI
|
||||||
gin=21 # MISO
|
gin = 21 # MISO
|
||||||
gen=24 # chip enable (active low)
|
gen = 24 # chip enable (active low)
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
GPIO.setup(ginit,GPIO.OUT)
|
GPIO.setup(ginit, GPIO.OUT)
|
||||||
GPIO.setup(gclk,GPIO.OUT)
|
GPIO.setup(gclk, GPIO.OUT)
|
||||||
GPIO.setup(gout,GPIO.OUT)
|
GPIO.setup(gout, GPIO.OUT)
|
||||||
GPIO.setup(gen,GPIO.OUT)
|
GPIO.setup(gen, GPIO.OUT)
|
||||||
GPIO.setup(gin,GPIO.IN,pull_up_down = GPIO.PUD_UP)
|
GPIO.setup(gin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
swait()
|
swait()
|
||||||
# set default state
|
# set default state
|
||||||
GPIO.output(ginit,0)
|
GPIO.output(ginit, 0)
|
||||||
GPIO.output(gclk,0)
|
GPIO.output(gclk, 0)
|
||||||
GPIO.output(gout,0)
|
GPIO.output(gout, 0)
|
||||||
GPIO.output(gen,1)
|
GPIO.output(gen, 1)
|
||||||
GPIO.output(ginit,1)
|
GPIO.output(ginit, 1)
|
||||||
# READY FOR 1 TRANSFER
|
# READY FOR 1 TRANSFER
|
||||||
|
|
||||||
def xfr1(iswrite,address,data1): # SPI data transfer main routine. Single byte is transferred out and in
|
|
||||||
global gen,gout,gin,gclk
|
def xfr1(iswrite, address, data1):
|
||||||
GPIO.output(gen,0)
|
"""Main SPI data transfer routine. A single byte is transferred out and in."""
|
||||||
swait()
|
global gen, gout, gin, gclk
|
||||||
swait()
|
GPIO.output(gen, 0)
|
||||||
d1 = address & 127
|
swait()
|
||||||
if iswrite>0:
|
swait()
|
||||||
d1 = d1 | 128
|
d1 = address & 127
|
||||||
swait()
|
if iswrite > 0:
|
||||||
m=128 # send MSB first
|
d1 = d1 | 128
|
||||||
for bit in xrange(8):
|
swait()
|
||||||
if((d1 & m) >0):
|
m = 128 # send MSB first
|
||||||
GPIO.output(gout,1)
|
for bit in range(8):
|
||||||
else:
|
if((d1 & m) > 0):
|
||||||
GPIO.output(gout,0)
|
GPIO.output(gout, 1)
|
||||||
swait()
|
else:
|
||||||
GPIO.output(gclk,1)
|
GPIO.output(gout, 0)
|
||||||
swait()
|
swait()
|
||||||
GPIO.output(gclk,0)
|
GPIO.output(gclk, 1)
|
||||||
swait()
|
swait()
|
||||||
m = (m >> 1) & 127
|
GPIO.output(gclk, 0)
|
||||||
m = 128
|
swait()
|
||||||
d2 = data1
|
m = (m >> 1) & 127
|
||||||
res = 0
|
m = 128
|
||||||
for bit in xrange(8):
|
d2 = data1
|
||||||
if((d2 & m)>0):
|
res = 0
|
||||||
GPIO.output(gout,1)
|
for bit in range(8):
|
||||||
else:
|
if((d2 & m) > 0):
|
||||||
GPIO.output(gout,0)
|
GPIO.output(gout, 1)
|
||||||
swait()
|
else:
|
||||||
res = res << 1
|
GPIO.output(gout, 0)
|
||||||
GPIO.output(gclk,1)
|
swait()
|
||||||
if(GPIO.input(gin)>0):
|
res = res << 1
|
||||||
res = res | 1
|
GPIO.output(gclk, 1)
|
||||||
swait()
|
if(GPIO.input(gin) > 0):
|
||||||
GPIO.output(gclk,0)
|
res = res | 1
|
||||||
swait()
|
swait()
|
||||||
m = m >> 1
|
GPIO.output(gclk, 0)
|
||||||
GPIO.output(gen,1)
|
swait()
|
||||||
swait()
|
m = m >> 1
|
||||||
return res
|
GPIO.output(gen, 1)
|
||||||
|
swait()
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def writeReg(where,data):
|
def writeReg(where, data):
|
||||||
xfr1(1,where,data)
|
xfr1(1, where, data)
|
||||||
|
|
||||||
|
|
||||||
def readReg(where):
|
def readReg(where):
|
||||||
return xfr1(0,where,0)
|
return xfr1(0, where, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def ex1(): # just exercising a pin so I can see it on scope
|
||||||
|
global gclk, gout, gin, gen, ginit
|
||||||
|
dt = 0.01
|
||||||
|
port = gen
|
||||||
|
for x in range(100):
|
||||||
|
print(1)
|
||||||
|
GPIO.output(port, 1)
|
||||||
|
sleep(dt)
|
||||||
|
print(0)
|
||||||
|
GPIO.output(port, 0)
|
||||||
|
sleep(dt)
|
||||||
|
|
||||||
def ex1(): # just exercising a pin so I can see it on scope
|
|
||||||
global gclk,gout,gin,gen,ginit
|
|
||||||
dt = 0.01
|
|
||||||
port = gen
|
|
||||||
for x in xrange(100):
|
|
||||||
print 1
|
|
||||||
GPIO.output(port,1)
|
|
||||||
sleep(dt)
|
|
||||||
print 0
|
|
||||||
GPIO.output(port,0)
|
|
||||||
sleep(dt)
|
|
||||||
|
|
||||||
def readregs():
|
def readregs():
|
||||||
print "Reg Value"
|
print("Reg Value")
|
||||||
for x in xrange(0x44):
|
for x in range(0x44):
|
||||||
res = -1
|
# res = -1
|
||||||
v = xfr1(0,x,0)
|
v = xfr1(0, x, 0)
|
||||||
print '@',format(x,'02X'),
|
print('@', format(x, '02X'))
|
||||||
print ' ',' $',format(v,'02X')
|
print(' ', ' $', format(v, '02X'))
|
||||||
|
|
||||||
|
|
||||||
def GetOpMode():
|
def GetOpMode():
|
||||||
x = xfr1(0,1,0) # register 1 controls the RFM IC mode. eg FSK/LORA/SLEEP/STANDBY/TX/RX
|
# register 1 controls the RFM IC mode. eg FSK/LORA/SLEEP/STANDBY/TX/RX
|
||||||
return x
|
x = xfr1(0, 1, 0)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
def GetRegBitRate():
|
def GetRegBitRate():
|
||||||
h = xfr1(0,2,0)
|
h = xfr1(0, 2, 0)
|
||||||
l = xfr1(0,3,0)
|
l_c = xfr1(0, 3, 0)
|
||||||
return h*256 + l
|
return h * 256 + l_c
|
||||||
|
|
||||||
|
|
||||||
def GetCarrierFreq():
|
def GetCarrierFreq():
|
||||||
h = readReg(6) # MSB
|
h = readReg(6) # MSB
|
||||||
m = readReg(7)
|
m = readReg(7)
|
||||||
l = readReg(8) # LSB
|
l_a = readReg(8) # LSB
|
||||||
return ((h*256.0+m)*256.0 + l)*61.035 # in Hz
|
return ((h * 256.0 + m) * 256.0 + l_a) * 61.035 # in Hz
|
||||||
|
|
||||||
|
|
||||||
def SetFreq(mhz):
|
def SetFreq(mhz):
|
||||||
f = int(mhz*1e6/61.035)
|
f = int(mhz * 1e6 / 61.035)
|
||||||
h = int(f/65536.0)
|
h = int(f / 65536.0)
|
||||||
r = f-h*65536
|
r = f - h * 65536
|
||||||
m = int(r/256)
|
m = int(r / 256)
|
||||||
l = r-256*m
|
l_b = r - 256 * m
|
||||||
writeReg(6,h) # MSB
|
writeReg(6, h) # MSB
|
||||||
writeReg(7,m)
|
writeReg(7, m)
|
||||||
writeReg(8,l) # LSB
|
writeReg(8, l_b) # LSB
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def GetAGC():
|
def GetAGC():
|
||||||
return xfr1(0,0x61,0) # or use readReg(0x61) etc
|
return xfr1(0, 0x61, 0) # or use readReg(0x61) etc
|
||||||
|
|
||||||
|
|
||||||
def GetAGC1():
|
def GetAGC1():
|
||||||
return xfr1(0,0x62,0)
|
return xfr1(0, 0x62, 0)
|
||||||
|
|
||||||
|
|
||||||
def GetAGC2():
|
def GetAGC2():
|
||||||
return xfr1(0,0x63,0)
|
return xfr1(0, 0x63, 0)
|
||||||
|
|
||||||
|
|
||||||
def GetAGC3():
|
def GetAGC3():
|
||||||
return xfr1(0,0x64,0)
|
return xfr1(0, 0x64, 0)
|
||||||
|
|
||||||
|
|
||||||
def ShowRegs():
|
def ShowRegs():
|
||||||
print' Some regsiter examples'
|
print('Some regsiter examples')
|
||||||
print 'Op Mode ',GetOpMode()
|
print('Op Mode ', GetOpMode())
|
||||||
print 'Bit Rate ', GetRegBitRate()
|
print('Bit Rate ', GetRegBitRate())
|
||||||
print 'Default carrier freq ',GetCarrierFreq()/1e6, ' MHz'
|
print('Default carrier freq ', GetCarrierFreq() / 1e6, 'MHz')
|
||||||
print 'AGC regs 61-64 : ',format(GetAGC(),'02X'),format(GetAGC1(),'02X'),format(GetAGC2(),'02X'),format(GetAGC3(),'02X')
|
print(
|
||||||
|
'AGC regs 61-64 : ',
|
||||||
|
format(GetAGC(), '02X'),
|
||||||
|
format(GetAGC1(), '02X'),
|
||||||
|
format(GetAGC2(), '02X'),
|
||||||
|
format(GetAGC3(), '02X')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print ('Simple GPIO SPI without drivers')
|
print('Simple GPIO SPI without drivers')
|
||||||
sleep(0.5) # secs
|
sleep(0.5) # secs
|
||||||
initports()
|
initports()
|
||||||
readregs() # shows massive list, before any changes are made
|
readregs() # shows massive list, before any changes are made
|
||||||
ShowRegs() # shows a few interesting registers
|
ShowRegs() # shows a few interesting registers
|
||||||
SetFreq(868.1) # now write to registers 6,7,8
|
SetFreq(868.1) # now write to registers 6, 7, 8
|
||||||
print 'New carrier freq ',GetCarrierFreq()/1e6, ' MHz'
|
print('New carrier freq ', GetCarrierFreq() / 1e6, ' MHz')
|
||||||
print ("*** Finished")
|
print("*** Finished")
|
||||||
GPIO.cleanup()
|
GPIO.cleanup()
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
Reference in a new issue