@James46717 Hi James, thank you for uploading such a cool project! I felt compelled to start a similar project that also utilizes 2x nRF905, Rasp pi 4 and a pico. But I am currently stuck on the most basic of tasks; getting the two devices to talk to each other…
Your nRF905 driver worked for the raspberry pi 4. I assume it works because the config got applied,
nRF905 = 0x6c 0xc 0x44 0x20 0x20 0x55 0xaa 0x55 0xaa 0x18
, furthermore the register commands of reading and writing addresses work (printing the first few bytes actually gave back something other than 00s).
However, the problem lays on the rasp pico. The config does not get applied nRF905 = 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
and the register commands does not reach because whenever I do spi.read(10) after writing to it, it always return 00s. I attached the code below (which is your code minus the humidity date check etc).
I think those are the reasons why the fault is on pico’s side? I doubled, and tripled check the pins ensuring they are correctly placed. I hope that you could provide me with some pointers as to where did I not apply your work correctly, thanks a ton!
One more thing I would also like to note is that the Address Match and Data Ready checks at the main loop always run true, even when I’m not running the Rasp 4’s nRF905…
import time
from machine import SPI, Pin
############################## nRF905 func lib
############### Radio TX RX
def read_cfg_reg(spi, cs):
cs.value(0)
spi.write(b"\x10")
data = bytearray(spi.read(10))
print(f'data1: {data}') # all zeros?
cs.value(1)
return data
def write_cfg_reg(spi, cs, data):
cs.value(0)
spi.write(data)
cs.value(1)
return
def read_TX_address(spi, cs):
cs.value(0)
spi.write(b"\x23")
data = bytearray(spi.read(4))
cs.value(1)
return data
def write_TX_address(spi, cs, data):
cs.value(0)
spi.write(b"\x22")
spi.write(data)
cs.value(1)
return
def read_TX_payload(spi, cs, size):
cs.value(0)
spi.write(b"\x21")
data = bytearray(spi.read(size))
cs.value(1)
return data
def write_TX_payload(spi, cs, data):
cs.value(0)
spi.write(b"\x20")
spi.write(data)
cs.value(1)
return
def read_RX_payload(spi, cs, size):
cs.value(0)
spi.write(b"\x24")
data = bytearray(spi.read(size))
cs.value(1)
return data
############### nRf905 config
def print_Config_Reg(msg,Config_Reg):
print(msg,hex(Config_Reg[0]), hex(Config_Reg[1]), hex(Config_Reg[2]), hex(Config_Reg[3]), hex(Config_Reg[4]), hex(Config_Reg[5]),
hex(Config_Reg[6]), hex(Config_Reg[7]), hex(Config_Reg[8]), hex(Config_Reg[9]))
return
def set_cfg_register(spi,cs,cfg):
Config_Reg = bytearray((0,0,0,0,0,0,0,0,0,0))
print(f'cfg_reg1: {Config_Reg}')
Config_Reg = read_cfg_reg(spi, cs)
print(f'cfg_reg2: {Config_Reg}')
Config_Reg[0] = cfg['CH_NO']
Config_Reg[1] = cfg['AUTO_RETRAN'] << 5 | cfg['RX_RED_PWR'] << 4 | cfg['PA_PWR'] << 2 | cfg['HFREQ_PLL'] << 1 | cfg['CH_NO_MSB']
Config_Reg[2] = cfg['TX_AFW'] << 4 | cfg['RX_AFW']
Config_Reg[3] = cfg['TX_PW']
Config_Reg[4] = cfg['RX_PW']
Config_Reg[5] = cfg['RX_Address0']
Config_Reg[6] = cfg['RX_Address1']
Config_Reg[7] = cfg['RX_Address2']
Config_Reg[8] = cfg['RX_Address3']
Config_Reg[9] = cfg['CRC_MODE'] << 7 | cfg['CRC_EN'] << 6 | cfg['XOF'] << 3 | cfg['UP_CLK_EN'] << 2 | cfg['UP_CLK_FREQ']
write_cfg_reg(spi, cs, Config_Reg)
Config_Reg = read_cfg_reg(spi, cs)
print_Config_Reg("nRF905 Config =",Config_Reg)
return
############################## GPIO PINS
LED = Pin(25, Pin.OUT)
TXEN = Pin(11, Pin.OUT)
PWR = Pin(12, Pin.OUT)
CE = Pin(13, Pin.OUT)
DR = Pin(14, Pin.IN, Pin.PULL_UP)
AM = Pin(15, Pin.IN, Pin.PULL_UP)
CSN = Pin(17, Pin.OUT)
############### GPIO STATE SETUP
LED.value(1)
PWR.value(1)
CE.value(0) # disable radio
TXEN.value(0) # RX
CSN.value(1) # disable comms
############### nRF905 Config Var
spi = SPI(0, baudrate=1000000, polarity=0, phase=0, bits=8,
firstbit=SPI.MSB, sck=Pin(18), mosi=Pin(19), miso=Pin(16))
# TX_Ad of pico is RX of rasp 4
TX_Address = bytearray((0x55,0xAA,0x55,0xAA), 'UTF-8')
RX_Address = bytearray((0xAA,0x55,0xAA,0x55), 'UTF-8')
bufsize = 32
nRF905_Config = {'AUTO_RETRAN':0, 'RX_RED_PWR':0, 'PA_PWR':3, 'HFREQ_PLL':0, 'CH_NO_MSB':0,
'CH_NO':0x6C,
'TX_AFW':4,'RX_AFW':4,
'TX_PW':bufsize,
'RX_PW':bufsize,
'RX_Address0':RX_Address[0],
'RX_Address1':RX_Address[1],
'RX_Address2':RX_Address[2],
'RX_Address3':RX_Address[3],
'CRC_MODE':0, 'CRC_EN':0, 'XOF':3, 'UP_CLK_EN':0, 'UP_CLK_FREQ':0}
############### TX message
def print_b_arr(x):
s = ""
for i in x:
s += chr(i)
# print(s)
return
def TX_Message(address, payload):
TXEN.value(0)
CE.value(0)
# checks for valid len of 32, pad with space if not
if len(payload) < 32:
for i in range(len(payload), 32):
payload += ' '
else:
if len(payload) > 32:
payload = payload[0:32]
write_TX_address(spi, CSN, address)
write_TX_payload(spi, CSN, address)
TXEN.value(1)
CE.value(1)
# confirm delivery
while not DR.value():
pass
print_b_arr(read_TX_payload(spi, CSN, bufsize))
CE.value(0)
TXEN.value(0)
return
############### Main
set_cfg_register(spi, CSN, nRF905_Config)
time.sleep(0.1)
while True:
TXEN.value(0)
CE. value(1)
LED.value(0)
TX_payload = bytearray('pico says hi')
TX_Message(TX_Address, TX_payload)
if AM.value():
if DR.value():
RX_payload = read_RX_payload(spi, CSN, bufsize)
print(f'payload: {RX_payload}')
time.sleep(0.1)
TX_Message(TX_Address, TX_payload)