summaryrefslogtreecommitdiff
path: root/drivers/nrf24l01/nrf24l01.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-02-27 15:36:53 +1100
committerDamien George <damien.p.george@gmail.com>2020-02-28 10:33:03 +1100
commit69661f3343bedf86e514337cff63d96cc42f8859 (patch)
treeaf5dfb380ffdb75dda84828f63cf9d840d992f0f /drivers/nrf24l01/nrf24l01.py
parent3f39d18c2b884d32f0443e2e8114ff9d7a14d718 (diff)
all: Reformat C and Python source code with tools/codeformat.py.
This is run with uncrustify 0.70.1, and black 19.10b0.
Diffstat (limited to 'drivers/nrf24l01/nrf24l01.py')
-rw-r--r--drivers/nrf24l01/nrf24l01.py75
1 files changed, 38 insertions, 37 deletions
diff --git a/drivers/nrf24l01/nrf24l01.py b/drivers/nrf24l01/nrf24l01.py
index a95d2b5ca..76d55312f 100644
--- a/drivers/nrf24l01/nrf24l01.py
+++ b/drivers/nrf24l01/nrf24l01.py
@@ -5,49 +5,50 @@ from micropython import const
import utime
# nRF24L01+ registers
-CONFIG = const(0x00)
-EN_RXADDR = const(0x02)
-SETUP_AW = const(0x03)
-SETUP_RETR = const(0x04)
-RF_CH = const(0x05)
-RF_SETUP = const(0x06)
-STATUS = const(0x07)
-RX_ADDR_P0 = const(0x0a)
-TX_ADDR = const(0x10)
-RX_PW_P0 = const(0x11)
+CONFIG = const(0x00)
+EN_RXADDR = const(0x02)
+SETUP_AW = const(0x03)
+SETUP_RETR = const(0x04)
+RF_CH = const(0x05)
+RF_SETUP = const(0x06)
+STATUS = const(0x07)
+RX_ADDR_P0 = const(0x0A)
+TX_ADDR = const(0x10)
+RX_PW_P0 = const(0x11)
FIFO_STATUS = const(0x17)
-DYNPD = const(0x1c)
+DYNPD = const(0x1C)
# CONFIG register
-EN_CRC = const(0x08) # enable CRC
-CRCO = const(0x04) # CRC encoding scheme; 0=1 byte, 1=2 bytes
-PWR_UP = const(0x02) # 1=power up, 0=power down
-PRIM_RX = const(0x01) # RX/TX control; 0=PTX, 1=PRX
+EN_CRC = const(0x08) # enable CRC
+CRCO = const(0x04) # CRC encoding scheme; 0=1 byte, 1=2 bytes
+PWR_UP = const(0x02) # 1=power up, 0=power down
+PRIM_RX = const(0x01) # RX/TX control; 0=PTX, 1=PRX
# RF_SETUP register
-POWER_0 = const(0x00) # -18 dBm
-POWER_1 = const(0x02) # -12 dBm
-POWER_2 = const(0x04) # -6 dBm
-POWER_3 = const(0x06) # 0 dBm
-SPEED_1M = const(0x00)
-SPEED_2M = const(0x08)
-SPEED_250K = const(0x20)
+POWER_0 = const(0x00) # -18 dBm
+POWER_1 = const(0x02) # -12 dBm
+POWER_2 = const(0x04) # -6 dBm
+POWER_3 = const(0x06) # 0 dBm
+SPEED_1M = const(0x00)
+SPEED_2M = const(0x08)
+SPEED_250K = const(0x20)
# STATUS register
-RX_DR = const(0x40) # RX data ready; write 1 to clear
-TX_DS = const(0x20) # TX data sent; write 1 to clear
-MAX_RT = const(0x10) # max retransmits reached; write 1 to clear
+RX_DR = const(0x40) # RX data ready; write 1 to clear
+TX_DS = const(0x20) # TX data sent; write 1 to clear
+MAX_RT = const(0x10) # max retransmits reached; write 1 to clear
# FIFO_STATUS register
-RX_EMPTY = const(0x01) # 1 if RX FIFO is empty
+RX_EMPTY = const(0x01) # 1 if RX FIFO is empty
# constants for instructions
-R_RX_PL_WID = const(0x60) # read RX payload width
-R_RX_PAYLOAD = const(0x61) # read RX payload
-W_TX_PAYLOAD = const(0xa0) # write TX payload
-FLUSH_TX = const(0xe1) # flush TX FIFO
-FLUSH_RX = const(0xe2) # flush RX FIFO
-NOP = const(0xff) # use to read STATUS register
+R_RX_PL_WID = const(0x60) # read RX payload width
+R_RX_PAYLOAD = const(0x61) # read RX payload
+W_TX_PAYLOAD = const(0xA0) # write TX payload
+FLUSH_TX = const(0xE1) # flush TX FIFO
+FLUSH_RX = const(0xE2) # flush RX FIFO
+NOP = const(0xFF) # use to read STATUS register
+
class NRF24L01:
def __init__(self, spi, cs, ce, channel=46, payload_size=16):
@@ -84,7 +85,7 @@ class NRF24L01:
self.reg_write(SETUP_RETR, (6 << 4) | 8)
# set rf power and speed
- self.set_power_speed(POWER_3, SPEED_250K) # Best for point to point links
+ self.set_power_speed(POWER_3, SPEED_250K) # Best for point to point links
# init CRC
self.set_crc(2)
@@ -218,7 +219,7 @@ class NRF24L01:
start = utime.ticks_ms()
result = None
while result is None and utime.ticks_diff(utime.ticks_ms(), start) < timeout:
- result = self.send_done() # 1 == success, 2 == fail
+ result = self.send_done() # 1 == success, 2 == fail
if result == 2:
raise OSError("send failed")
@@ -232,18 +233,18 @@ class NRF24L01:
self.spi.readinto(self.buf, W_TX_PAYLOAD)
self.spi.write(buf)
if len(buf) < self.payload_size:
- self.spi.write(b'\x00' * (self.payload_size - len(buf))) # pad out data
+ self.spi.write(b"\x00" * (self.payload_size - len(buf))) # pad out data
self.cs(1)
# enable the chip so it can send the data
self.ce(1)
- utime.sleep_us(15) # needs to be >10us
+ utime.sleep_us(15) # needs to be >10us
self.ce(0)
# returns None if send still in progress, 1 for success, 2 for fail
def send_done(self):
if not (self.reg_read(STATUS) & (TX_DS | MAX_RT)):
- return None # tx not finished
+ return None # tx not finished
# either finished or failed: get and clear status flags, power down
status = self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT)