|
14 | 14 | """
|
15 | 15 | import random
|
16 | 16 | import time
|
17 |
| - |
18 | 17 | import adafruit_bus_device.spi_device as spidev
|
19 | 18 | from micropython import const
|
20 | 19 |
|
| 20 | +try: |
| 21 | + import supervisor |
| 22 | + |
| 23 | + HAS_SUPERVISOR = True |
| 24 | +except ImportError: |
| 25 | + HAS_SUPERVISOR = False |
| 26 | + |
21 | 27 | __version__ = "0.0.0-auto.0"
|
22 | 28 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM9x.git"
|
23 | 29 |
|
|
99 | 105 | TX_MODE = 0b011
|
100 | 106 | FS_RX_MODE = 0b100
|
101 | 107 | RX_MODE = 0b101
|
102 |
| - |
| 108 | +# supervisor.ticks_ms() contants |
| 109 | +_TICKS_PERIOD = const(1 << 29) |
| 110 | +_TICKS_MAX = const(_TICKS_PERIOD - 1) |
| 111 | +_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2) |
103 | 112 |
|
104 | 113 | # Disable the too many instance members warning. Pylint has no knowledge
|
105 | 114 | # of the context and is merely guessing at the proper amount of members. This
|
|
109 | 118 | # pylint: disable=too-many-statements
|
110 | 119 |
|
111 | 120 |
|
| 121 | +def ticks_diff(ticks1, ticks2): |
| 122 | + """Compute the signed difference between two ticks values |
| 123 | + assuming that they are within 2**28 ticks |
| 124 | + """ |
| 125 | + diff = (ticks1 - ticks2) & _TICKS_MAX |
| 126 | + diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD |
| 127 | + return diff |
| 128 | + |
| 129 | + |
112 | 130 | class RFM9x:
|
113 | 131 | """Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and
|
114 | 132 | receivng bytes of data in long range LoRa mode at a support board frequency
|
@@ -641,6 +659,7 @@ def crc_error(self):
|
641 | 659 | """crc status"""
|
642 | 660 | return (self._read_u8(_RH_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5
|
643 | 661 |
|
| 662 | + # pylint: disable=too-many-branches |
644 | 663 | def send(
|
645 | 664 | self,
|
646 | 665 | data,
|
@@ -701,11 +720,17 @@ def send(
|
701 | 720 | self.transmit()
|
702 | 721 | # Wait for tx done interrupt with explicit polling (not ideal but
|
703 | 722 | # best that can be done right now without interrupts).
|
704 |
| - start = time.monotonic() |
705 | 723 | timed_out = False
|
706 |
| - while not timed_out and not self.tx_done(): |
707 |
| - if (time.monotonic() - start) >= self.xmit_timeout: |
708 |
| - timed_out = True |
| 724 | + if HAS_SUPERVISOR: |
| 725 | + start = supervisor.ticks_ms() |
| 726 | + while not timed_out and not self.tx_done(): |
| 727 | + if ticks_diff(supervisor.ticks_ms(), start) >= self.xmit_timeout * 1000: |
| 728 | + timed_out = True |
| 729 | + else: |
| 730 | + start = time.monotonic() |
| 731 | + while not timed_out and not self.tx_done(): |
| 732 | + if time.monotonic() - start >= self.xmit_timeout: |
| 733 | + timed_out = True |
709 | 734 | # Listen again if necessary and return the result packet.
|
710 | 735 | if keep_listening:
|
711 | 736 | self.listen()
|
@@ -753,7 +778,6 @@ def send_with_ack(self, data):
|
753 | 778 | self.flags = 0 # clear flags
|
754 | 779 | return got_ack
|
755 | 780 |
|
756 |
| - # pylint: disable=too-many-branches |
757 | 781 | def receive(
|
758 | 782 | self, *, keep_listening=True, with_header=False, with_ack=False, timeout=None
|
759 | 783 | ):
|
@@ -781,11 +805,17 @@ def receive(
|
781 | 805 | # interrupt supports.
|
782 | 806 | # Make sure we are listening for packets.
|
783 | 807 | self.listen()
|
784 |
| - start = time.monotonic() |
785 | 808 | timed_out = False
|
786 |
| - while not timed_out and not self.rx_done(): |
787 |
| - if (time.monotonic() - start) >= timeout: |
788 |
| - timed_out = True |
| 809 | + if HAS_SUPERVISOR: |
| 810 | + start = supervisor.ticks_ms() |
| 811 | + while not timed_out and not self.rx_done(): |
| 812 | + if ticks_diff(supervisor.ticks_ms(), start) >= timeout * 1000: |
| 813 | + timed_out = True |
| 814 | + else: |
| 815 | + start = time.monotonic() |
| 816 | + while not timed_out and not self.rx_done(): |
| 817 | + if time.monotonic() - start >= timeout: |
| 818 | + timed_out = True |
789 | 819 | # Payload ready is set, a packet is in the FIFO.
|
790 | 820 | packet = None
|
791 | 821 | # save last RSSI reading
|
|
0 commit comments