Skip to content

Instantly share code, notes, and snippets.

@mossmann
Last active September 5, 2019 15:03
Show Gist options
  • Save mossmann/13187d2cbe73e7ad288298ead383714f to your computer and use it in GitHub Desktop.
Save mossmann/13187d2cbe73e7ad288298ead383714f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
from greatfet import GreatFET
gf=GreatFET()
# incomplete list of SPI commands
NOP = 0xFF
SPI_MEM_WR = 0x18
SPI_MEM_RD = 0x38
RC_SLEEP = 0xB1
RC_IDLE = 0xB2
RC_PHY_RDY = 0xB3
RC_RX = 0xB4
RC_TX = 0xB5
RC_MEAS = 0xB6
RC_CCA = 0xB7
RC_PC_RESET = 0xB8
RC_RESET = 0xC8
RC_STATUS = (
"reserved",
"idle",
"MEAS",
"PHY_RDY",
"RX",
"TX",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved",
"reserved"
)
# very incomplete list of registers
buffercfg = 0x107
pkt_cfg = 0x108
ch_freq0 = 0x300
ch_freq1 = 0x301
ch_freq2 = 0x302
vco_band_ovrw = 0x353
vco_idac_ovrw = 0x354
vco_ovrw_cfg = 0x355
vco_cal_cfg = 0x36f
vco_band_rb = 0x380
vco_idac_rb = 0x381
tx_fsk_test = 0x3f0
def readreg(reg):
return(gf.spi.transmit([SPI_MEM_RD | (reg>>8), reg&0xff, 0xff, 0xff])[3])
def writereg(reg, val):
return(gf.spi.transmit([SPI_MEM_WR | (reg>>8), reg&0xff, val]))
def reset():
gf.spi.transmit([RC_RESET])
def status():
# Send a NOP and print the status byte response in hex.
s = gf.spi.transmit([NOP])[0]
print(s)
print("SPI is ready for access : ", (s>>6)&0x1)
print("pending interrupt condition: ", (s>>5)&0x1)
print("radio controller is ready : ", (s>>4)&0x1)
print("radio controller is ready : ", (s>>3)&0x1)
print("channel is idle : ", (s>>2)&0x1)
print("radio controller status : ", s&0x7, RC_STATUS[s&0x7])
def idle():
gf.spi.transmit([RC_IDLE])
def ready():
# undo set_override() to ensure that system calibration is not skipped
writereg(vco_cal_cfg, 9)
writereg(vco_ovrw_cfg, 0x08)
gf.spi.transmit([RC_PHY_RDY])
def tx():
gf.spi.transmit([RC_TX])
def config_test():
# See Transmit Test Modes on page 71 of data sheet.
writereg(tx_fsk_test, 0x21) # Set tx_fsk_test to unmodulated carrier
writereg(buffercfg, 0xb0)
writereg(pkt_cfg, 0x0c) # Set skip_synth_settle in pkt_cfg
def set_freq(frequency_mhz):
units = frequency_mhz * 100
writereg(ch_freq0, units & 0xff)
writereg(ch_freq1, (units>>8) & 0xff)
writereg(ch_freq2, (units>>16) & 0xff)
def set_override():
writereg(vco_cal_cfg, 15)
writereg(vco_ovrw_cfg, 0x0b)
# Set up GPIO pins used to configure the TX RF switch.
v1=gf.gpio.get_pin('J2_P19')
v2=gf.gpio.get_pin('J2_P23')
v3=gf.gpio.get_pin('J2_P25')
v4=gf.gpio.get_pin('J2_P27')
v1.set_direction(v1.DIRECTION_OUT)
v2.set_direction(v2.DIRECTION_OUT)
v3.set_direction(v3.DIRECTION_OUT)
v4.set_direction(v4.DIRECTION_OUT)
# To select the U1 TX path, write 1 to v1 and 0 to the others.
# For U2, write 1 to v2 and 0 to the others, etc.
v1.write(1)
v2.write(0)
v3.write(0)
v4.write(0)
# U1 chip select: J2_P9
# U2 chip select: J1_P14
# U3 chip select: J2_P7
# U4 chip select: J1_P34
#
# I've just been testing one chip at a time, so I have connected a jumper wire
# from one of the above pins to J1_P37 (SSEL). Alternatively you can wrap
# spi.transmit() in a function that pulls down the appropriate chip select.
status()
reset()
status()
status()
config_test()
set_freq(int(sys.argv[1]))
ready()
tx()
status()
# Status should now be 0xe5 which indicates TX is active. If it doesn't go to
# 0xe5, try again. (For reliability we probably need to confirm prior state
# changes before requesting another.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment