From 706955976c7f40a76f1c6e0bf94f1986980d75a3 Mon Sep 17 00:00:00 2001 From: adminpete Date: Sun, 16 Nov 2014 11:27:44 +0000 Subject: [PATCH] drivers, nrf24: Nonblocking send now done by generator. --- drivers/nrf24l01/nrf24l01.py | 64 +++++++++++++++--------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/drivers/nrf24l01/nrf24l01.py b/drivers/nrf24l01/nrf24l01.py index 2db3546814..93bfc2e7cb 100644 --- a/drivers/nrf24l01/nrf24l01.py +++ b/drivers/nrf24l01/nrf24l01.py @@ -1,10 +1,4 @@ """NRF24L01 driver for Micro Python - -Support for nonblocking send added. Minor fixes: -Timeout now uses pyb.elapsed_millis(). -Channel numbers constrained to 125 as per datasheet. -Status register read with reg_read() - reg_read_ret_status() removed. -Default speed 250K for improved range/error rate. """ import pyb @@ -145,7 +139,7 @@ class NRF24L01: self.reg_write(CONFIG, config) def set_channel(self, channel): - self.reg_write(RF_CH, min(channel, 125)) # Changed from 127 + self.reg_write(RF_CH, min(channel, 125)) # address should be a bytes object 5 bytes long def open_tx_pipe(self, address): @@ -207,44 +201,38 @@ class NRF24L01: start = pyb.millis() result = None while result is None and (pyb.elapsed_millis(start) < timeout): - result = send_nonblock() # 1 == success 2 == fail + result = next(send_nonblock) # 1 == success 2 == fail if result == 2: raise OSError("send failed") def send_nonblocking(self, buf): ''' - Support for nonblocking transmission. Returns a function instance. - The first call to a function instance sends the data and returns None. - Subsequent calls test TX status returning not ready None, ready 1, error 2. + Support for nonblocking transmission. Returns a generator instance. + First use sends the data and returns None. Subsequently tests TX status + returning not ready None, ready 1, error 2. ''' - init = True - def make_snb(): - nonlocal init - if init: - # power up - self.reg_write(CONFIG, (self.reg_read(CONFIG) | PWR_UP) & ~PRIM_RX) - pyb.udelay(150) + # power up + self.reg_write(CONFIG, (self.reg_read(CONFIG) | PWR_UP) & ~PRIM_RX) + pyb.udelay(150) - # send the data - self.cs.low() - self.spi.send(W_TX_PAYLOAD) - self.spi.send(buf) - if len(buf) < self.payload_size: - self.spi.send(b'\x00' * (self.payload_size - len(buf))) # pad out data - self.cs.high() + # send the data + self.cs.low() + self.spi.send(W_TX_PAYLOAD) + self.spi.send(buf) + if len(buf) < self.payload_size: + self.spi.send(b'\x00' * (self.payload_size - len(buf))) # pad out data + self.cs.high() - # enable the chip so it can send the data - self.ce.high() - pyb.udelay(15) # needs to be >10us - self.ce.low() - init = False - return None # Not ready + # enable the chip so it can send the data + self.ce.high() + pyb.udelay(15) # needs to be >10us + self.ce.low() + yield None # Not ready - if not (self.reg_read(STATUS) & (TX_DS | MAX_RT)): - return None # Not ready - # Either ready or failed: get and clear status flags, power down - status = self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT) - self.reg_write(CONFIG, self.reg_read(CONFIG) & ~PWR_UP) - return 1 if status & TX_DS else 2 - return make_snb + while not (self.reg_read(STATUS) & (TX_DS | MAX_RT)): + yield None # Not ready + # Either ready or failed: get and clear status flags, power down + status = self.reg_write(STATUS, RX_DR | TX_DS | MAX_RT) + self.reg_write(CONFIG, self.reg_read(CONFIG) & ~PWR_UP) + yield 1 if status & TX_DS else 2