@@ -91,7 +91,7 @@ def master(count=1, size=32): # count = 5 will transmit the list 5 times
9191 # most for payloads that fail to transmit.
9292 result = nrf .send (buffers , force_retry = 2 ) # result is a list
9393 end_timer = time .monotonic_ns () # end timer
94- print ("Transmission took" , (end_timer - start_timer ) / 1000 , "us " )
94+ print ("Transmission took" , (end_timer - start_timer ) / 1000000 , "ms " )
9595 for r in result : # tally the resulting success rate
9696 successful += 1 if r else 0
9797 print (
@@ -103,39 +103,42 @@ def master(count=1, size=32): # count = 5 will transmit the list 5 times
103103def master_fifo (count = 1 , size = 32 ):
104104 """Similar to the `master()` above except this function uses the full
105105 TX FIFO and `RF24.write()` instead of `RF24.send()`"""
106- buf = make_buffers (size ) # make a list of payloads
107- nrf .listen = False # ensures the nRF24L01 is in TX mode
106+ payloads = make_buffers (size ) # make a list of payloads
107+ nrf .listen = False # enter inactive TX mode
108108 for cnt in range (count ): # transmit the same payloads this many times
109109 nrf .flush_tx () # clear the TX FIFO so we can use all 3 levels
110- # NOTE the write_only parameter does not initiate sending
111- buf_iter = 0 # iterator of payloads for the while loop
112110 failures = 0 # keep track of manual retries
113111 start_timer = time .monotonic_ns () # start timer
114- while buf_iter < size : # cycle through all the payloads
115- nrf .ce_pin = False
116- while buf_iter < size and nrf .write (buf [buf_iter ], write_only = 1 ):
112+ for buf in payloads :
113+ while not nrf .write (buf ):
117114 # NOTE write() returns False if TX FIFO is already full
118- buf_iter += 1 # increment iterator of payloads
119- nrf .ce_pin = True
120- while not nrf .fifo (True , True ): # updates irq_df flag
121- if nrf .irq_df :
122- # reception failed; we need to reset the irq_rf flag
123- nrf .ce_pin = False # fall back to Standby-I mode
115+ if nrf .irq_df : # reception failed; TX FIFO is locked
124116 failures += 1 # increment manual retries
125- nrf .clear_status_flags () # clear the irq_df flag
126- if failures > 99 and buf_iter < 7 and cnt < 2 :
127- # we need to prevent an infinite loop
128- print (
129- "Make sure slave() node is listening. Quiting master_fifo()"
130- )
131- buf_iter = size + 1 # be sure to exit the while loop
132- nrf .flush_tx () # discard all payloads in TX FIFO
133- else :
134- nrf .ce_pin = True # start re-transmitting
135- nrf .ce_pin = False
117+ # we need to reset the irq_rf flag and the radio's CE pin
118+ nrf .ce_pin = False # fall back to Standby-I mode
119+ # NOTE next call to nrf.write() will
120+ # nrf.clear_status_flags() # clear the irq_df flag
121+ # nrf.ce_pin = True # restart transmissions
122+ if failures > 99 :
123+ break # prevent infinite loop
124+ if failures > 99 :
125+ # exit early because RX side seems absent
126+ nrf .flush_tx () # discard all payloads in TX FIFO
127+ print ("Make sure slave() node is listening. Quitting master_fifo()" )
128+ break
129+ # wait for radio to finish transmitting everything in the TX FIFO
130+ while failures < 99 and not nrf .fifo (about_tx = True , check_empty = True ):
131+ # fifo() also update()s the StatusFlags
132+ if nrf .irq_df :
133+ failures += 1
134+ # manually restart transmissions because where done write()ing
135+ nrf .ce_pin = False
136+ nrf .clear_status_flags ()
137+ nrf .ce_pin = True
136138 end_timer = time .monotonic_ns () # end timer
139+ nrf .ce_pin = False # enter inactive TX mode
137140 print (
138- "Transmission took {} us " .format ((end_timer - start_timer ) / 1000 ),
141+ "Transmission took {} ms " .format ((end_timer - start_timer ) / 1000000 ),
139142 "with {} failures detected." .format (failures ),
140143 )
141144
@@ -148,7 +151,7 @@ def slave(timeout=5):
148151 while time .monotonic () < start_timer + timeout :
149152 if nrf .available ():
150153 count += 1
151- # retreive the received packet's payload
154+ # retrieve the received packet's payload
152155 buffer = nrf .read () # clears flags & empties RX FIFO
153156 print ("Received:" , buffer , "-" , count )
154157 start_timer = time .monotonic () # reset timer on every RX payload
@@ -164,7 +167,7 @@ def set_role():
164167 user_input = (
165168 input (
166169 "*** Enter 'R' for receiver role.\n "
167- "*** Enter 'T' for transmitter role (using 1 level of the TX FIFO).\n "
170+ "*** Enter 'T' for transmitter role (using 1 level of the TX FIFO).\n "
168171 "*** Enter 'F' for transmitter role (using all 3 levels of the TX FIFO).\n "
169172 "*** Enter 'Q' to quit example.\n "
170173 )
0 commit comments