adafruit-circuitpython-rfm 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
adafruit_rfm/rfm9x.py ADDED
@@ -0,0 +1,534 @@
1
+ # SPDX-FileCopyrightText: 2024 Jerry Needell for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """
6
+ `adafruit_rfm.rfm9x`
7
+ ====================================================
8
+
9
+ CircuitPython module for the RFM95/6/7/8 LoRa 433/915mhz radio modules.
10
+
11
+ * Author(s): Jerry Needell
12
+ """
13
+
14
+ import time
15
+
16
+ from micropython import const
17
+
18
+ from adafruit_rfm.rfm_common import RFMSPI
19
+
20
+ try:
21
+ import busio
22
+ import digitalio
23
+ from circuitpython_typing import ReadableBuffer
24
+
25
+ try:
26
+ from typing import Literal, Optional
27
+ except ImportError:
28
+ from typing_extensions import Literal
29
+
30
+ except ImportError:
31
+ pass
32
+
33
+ __version__ = "1.0.4"
34
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RFM.git"
35
+
36
+ # pylint: disable=duplicate-code
37
+
38
+ # Internal constants:
39
+ # Register names (FSK Mode even though we use LoRa instead, from table 85)
40
+ _RF95_REG_00_FIFO = const(0x00)
41
+ _RF95_REG_01_OP_MODE = const(0x01)
42
+ _RF95_REG_06_FRF_MSB = const(0x06)
43
+ _RF95_REG_07_FRF_MID = const(0x07)
44
+ _RF95_REG_08_FRF_LSB = const(0x08)
45
+ _RF95_REG_09_PA_CONFIG = const(0x09)
46
+ _RF95_REG_0A_PA_RAMP = const(0x0A)
47
+ _RF95_REG_0B_OCP = const(0x0B)
48
+ _RF95_REG_0C_LNA = const(0x0C)
49
+ _RF95_REG_0D_FIFO_ADDR_PTR = const(0x0D)
50
+ _RF95_REG_0E_FIFO_TX_BASE_ADDR = const(0x0E)
51
+ _RF95_REG_0F_FIFO_RX_BASE_ADDR = const(0x0F)
52
+ _RF95_REG_10_FIFO_RX_CURRENT_ADDR = const(0x10)
53
+ _RF95_REG_11_IRQ_FLAGS_MASK = const(0x11)
54
+ _RF95_REG_12_IRQ_FLAGS = const(0x12)
55
+ _RF95_REG_13_RX_NB_BYTES = const(0x13)
56
+ _RF95_REG_14_RX_HEADER_CNT_VALUE_MSB = const(0x14)
57
+ _RF95_REG_15_RX_HEADER_CNT_VALUE_LSB = const(0x15)
58
+ _RF95_REG_16_RX_PACKET_CNT_VALUE_MSB = const(0x16)
59
+ _RF95_REG_17_RX_PACKET_CNT_VALUE_LSB = const(0x17)
60
+ _RF95_REG_18_MODEM_STAT = const(0x18)
61
+ _RF95_REG_19_PKT_SNR_VALUE = const(0x19)
62
+ _RF95_REG_1A_PKT_RSSI_VALUE = const(0x1A)
63
+ _RF95_REG_1B_RSSI_VALUE = const(0x1B)
64
+ _RF95_REG_1C_HOP_CHANNEL = const(0x1C)
65
+ _RF95_REG_1D_MODEM_CONFIG1 = const(0x1D)
66
+ _RF95_REG_1E_MODEM_CONFIG2 = const(0x1E)
67
+ _RF95_REG_1F_SYMB_TIMEOUT_LSB = const(0x1F)
68
+ _RF95_REG_20_PREAMBLE_MSB = const(0x20)
69
+ _RF95_REG_21_PREAMBLE_LSB = const(0x21)
70
+ _RF95_REG_22_PAYLOAD_LENGTH = const(0x22)
71
+ _RF95_REG_23_MAX_PAYLOAD_LENGTH = const(0x23)
72
+ _RF95_REG_24_HOP_PERIOD = const(0x24)
73
+ _RF95_REG_25_FIFO_RX_BYTE_ADDR = const(0x25)
74
+ _RF95_REG_26_MODEM_CONFIG3 = const(0x26)
75
+
76
+ _RF95_REG_40_DIO_MAPPING1 = const(0x40)
77
+ _RF95_REG_41_DIO_MAPPING2 = const(0x41)
78
+ _RF95_REG_42_VERSION = const(0x42)
79
+
80
+ _RF95_REG_4B_TCXO = const(0x4B)
81
+ _RF95_REG_4D_PA_DAC = const(0x4D)
82
+ _RF95_REG_5B_FORMER_TEMP = const(0x5B)
83
+ _RF95_REG_61_AGC_REF = const(0x61)
84
+ _RF95_REG_62_AGC_THRESH1 = const(0x62)
85
+ _RF95_REG_63_AGC_THRESH2 = const(0x63)
86
+ _RF95_REG_64_AGC_THRESH3 = const(0x64)
87
+
88
+ _RF95_DETECTION_OPTIMIZE = const(0x31)
89
+ _RF95_DETECTION_THRESHOLD = const(0x37)
90
+
91
+ _RF95_PA_DAC_DISABLE = const(0x04)
92
+ _RF95_PA_DAC_ENABLE = const(0x07)
93
+
94
+ # The crystal oscillator frequency of the module
95
+ _RF95_FXOSC = 32000000.0
96
+
97
+ # The Frequency Synthesizer step = RH_RF95_FXOSC / 2^^19
98
+ _RF95_FSTEP = _RF95_FXOSC / 524288
99
+
100
+ # RadioHead specific compatibility constants.
101
+ _RH_BROADCAST_ADDRESS = const(0xFF)
102
+
103
+ # The acknowledgement bit in the FLAGS
104
+ # The top 4 bits of the flags are reserved for RadioHead. The lower 4 bits are reserved
105
+ # for application layer use.
106
+ _RH_FLAGS_ACK = const(0x80)
107
+ _RH_FLAGS_RETRY = const(0x40)
108
+
109
+ # User facing constants:
110
+ SLEEP_MODE = 0b000
111
+ STANDBY_MODE = 0b001
112
+ FS_TX_MODE = 0b010
113
+ TX_MODE = 0b011
114
+ FS_RX_MODE = 0b100
115
+ RX_MODE = 0b101
116
+
117
+
118
+ # pylint: disable=too-many-instance-attributes
119
+ class RFM9x(RFMSPI):
120
+ """Interface to a RFM95/6/7/8 LoRa radio module. Allows sending and
121
+ receiving bytes of data in long range LoRa mode at a support board frequency
122
+ (433/915mhz).
123
+
124
+ You must specify the following parameters:
125
+ - spi: The SPI bus connected to the radio.
126
+ - cs: The CS pin DigitalInOut connected to the radio.
127
+ - reset: The reset/RST pin DigialInOut connected to the radio.
128
+ - frequency: The frequency (in mhz) of the radio module (433/915mhz typically).
129
+
130
+ You can optionally specify:
131
+ - preamble_length: The length in bytes of the packet preamble (default 8).
132
+ - high_power: Boolean to indicate a high power board (RFM95, etc.). Default
133
+ is True for high power.
134
+ - baudrate: Baud rate of the SPI connection, default is 5mhz but you might
135
+ choose to lower to 1mhz if using long wires or a breadboard.
136
+ - agc: Boolean to Enable/Disable Automatic Gain Control - Default=False (AGC off)
137
+ - crc: Boolean to Enable/Disable Cyclic Redundancy Check - Default=True (CRC Enabled)
138
+ Remember this library makes a best effort at receiving packets with pure
139
+ Python code. Trying to receive packets too quickly will result in lost data
140
+ so limit yourself to simple scenarios of sending and receiving single
141
+ packets at a time.
142
+
143
+ Also note this library tries to be compatible with raw RadioHead Arduino
144
+ library communication. This means the library sets up the radio modulation
145
+ to match RadioHead's defaults.
146
+ Advanced RadioHead features like address/node specific packets
147
+ or "reliable datagram" delivery are supported however due to the
148
+ limitations noted, "reliable datagram" is still subject to missed packets.
149
+ """
150
+
151
+ operation_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, bits=3)
152
+
153
+ low_frequency_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=3, bits=1)
154
+
155
+ modulation_type = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=5, bits=2)
156
+
157
+ # Long range/LoRa mode can only be set in sleep mode!
158
+ long_range_mode = RFMSPI.RegisterBits(_RF95_REG_01_OP_MODE, offset=7, bits=1)
159
+
160
+ output_power = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, bits=4)
161
+
162
+ max_power = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, offset=4, bits=3)
163
+
164
+ pa_select = RFMSPI.RegisterBits(_RF95_REG_09_PA_CONFIG, offset=7, bits=1)
165
+
166
+ pa_dac = RFMSPI.RegisterBits(_RF95_REG_4D_PA_DAC, bits=3)
167
+
168
+ dio0_mapping = RFMSPI.RegisterBits(_RF95_REG_40_DIO_MAPPING1, offset=6, bits=2)
169
+
170
+ auto_agc = RFMSPI.RegisterBits(_RF95_REG_26_MODEM_CONFIG3, offset=2, bits=1)
171
+
172
+ low_datarate_optimize = RFMSPI.RegisterBits(_RF95_REG_26_MODEM_CONFIG3, offset=3, bits=1)
173
+
174
+ lna_boost_hf = RFMSPI.RegisterBits(_RF95_REG_0C_LNA, offset=0, bits=2)
175
+
176
+ auto_ifon = RFMSPI.RegisterBits(_RF95_DETECTION_OPTIMIZE, offset=7, bits=1)
177
+
178
+ detection_optimize = RFMSPI.RegisterBits(_RF95_DETECTION_OPTIMIZE, offset=0, bits=3)
179
+
180
+ bw_bins = (7800, 10400, 15600, 20800, 31250, 41700, 62500, 125000, 250000)
181
+
182
+ def __init__( # noqa: PLR0913
183
+ self,
184
+ spi: busio.SPI,
185
+ cs: digitalio.DigitalInOut, # pylint: disable=invalid-name
186
+ rst: digitalio.DigitalInOut,
187
+ frequency: int,
188
+ *,
189
+ preamble_length: int = 8,
190
+ high_power: bool = True,
191
+ baudrate: int = 5000000,
192
+ agc: bool = False,
193
+ crc: bool = True,
194
+ ) -> None:
195
+ super().__init__(spi, cs, baudrate=baudrate)
196
+ self.module = "RFM9X"
197
+ self.max_packet_length = 252
198
+ self.high_power = high_power
199
+ # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
200
+ # Set Default Baudrate to 5MHz to avoid problems
201
+ # self._device = spidev.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=0)
202
+ # Setup reset as a digital output - initially High
203
+ # This line is pulled low as an output quickly to trigger a reset.
204
+ self._rst = rst
205
+ # initialize Reset High
206
+ self._rst.switch_to_output(value=True)
207
+ self.reset()
208
+ # No device type check! Catch an error from the very first request and
209
+ # throw a nicer message to indicate possible wiring problems.
210
+ version = self.read_u8(address=_RF95_REG_42_VERSION)
211
+ if version != 18:
212
+ raise RuntimeError(
213
+ "Failed to find rfm9x with expected version -- check wiring. Version found:",
214
+ hex(version),
215
+ )
216
+
217
+ # Set sleep mode, wait 10s and confirm in sleep mode (basic device check).
218
+ # Also set long range mode (LoRa mode) as it can only be done in sleep.
219
+ self.sleep()
220
+ time.sleep(0.01)
221
+ self.long_range_mode = True
222
+ if self.operation_mode != SLEEP_MODE or not self.long_range_mode:
223
+ raise RuntimeError("Failed to configure radio for LoRa mode, check wiring!")
224
+ # clear default setting for access to LF registers if frequency > 525MHz
225
+ if frequency > 525:
226
+ self.low_frequency_mode = 0
227
+ # Setup entire 256 byte FIFO
228
+ self.write_u8(_RF95_REG_0E_FIFO_TX_BASE_ADDR, 0x00)
229
+ self.write_u8(_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0x00)
230
+ # Set mode idle
231
+ self.idle()
232
+ # Set frequency
233
+ self.frequency_mhz = frequency
234
+ # Set preamble length (default 8 bytes to match radiohead).
235
+ self.preamble_length = preamble_length
236
+ # Defaults set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
237
+ self.signal_bandwidth = 125000
238
+ self.coding_rate = 5
239
+ self.spreading_factor = 7
240
+ # Default to enable CRC checking on incoming packets.
241
+ self.enable_crc = crc
242
+ """CRC Enable state"""
243
+ # set AGC - Default = False
244
+ self.auto_agc = agc
245
+ """Automatic Gain Control state"""
246
+ # Set transmit power to 13 dBm, a safe value any module supports.
247
+ self.tx_power = 13
248
+
249
+ def reset(self) -> None:
250
+ """Perform a reset of the chip."""
251
+ # See section 7.2.2 of the datasheet for reset description.
252
+ self._rst.value = False # Set Reset Low
253
+ time.sleep(0.0001) # 100 us
254
+ self._rst.value = True # set Reset High
255
+ time.sleep(0.005) # 5 ms
256
+
257
+ def idle(self) -> None:
258
+ """Enter idle standby mode."""
259
+ self.operation_mode = STANDBY_MODE
260
+
261
+ def sleep(self) -> None:
262
+ """Enter sleep mode."""
263
+ self.operation_mode = SLEEP_MODE
264
+
265
+ def listen(self) -> None:
266
+ """Listen for packets to be received by the chip. Use :py:func:`receive`
267
+ to listen, wait and retrieve packets as they're available.
268
+ """
269
+ self.operation_mode = RX_MODE
270
+ self.dio0_mapping = 0b00 # Interrupt on rx done.
271
+
272
+ def transmit(self) -> None:
273
+ """Transmit a packet which is queued in the FIFO. This is a low level
274
+ function for entering transmit mode and more. For generating and
275
+ transmitting a packet of data use :py:func:`send` instead.
276
+ """
277
+ self.operation_mode = TX_MODE
278
+ self.dio0_mapping = 0b01 # Interrupt on tx done.
279
+
280
+ @property
281
+ def preamble_length(self) -> int:
282
+ """The length of the preamble for sent and received packets, an unsigned
283
+ 16-bit value. Received packets must match this length or they are
284
+ ignored! Set to 8 to match the RadioHead RFM95 library.
285
+ """
286
+ msb = self.read_u8(_RF95_REG_20_PREAMBLE_MSB)
287
+ lsb = self.read_u8(_RF95_REG_21_PREAMBLE_LSB)
288
+ return ((msb << 8) | lsb) & 0xFFFF
289
+
290
+ @preamble_length.setter
291
+ def preamble_length(self, val: int) -> None:
292
+ assert 0 <= val <= 65535
293
+ self.write_u8(_RF95_REG_20_PREAMBLE_MSB, (val >> 8) & 0xFF)
294
+ self.write_u8(_RF95_REG_21_PREAMBLE_LSB, val & 0xFF)
295
+
296
+ @property
297
+ def frequency_mhz(self) -> Literal[433.0, 915.0]:
298
+ """The frequency of the radio in Megahertz. Only the allowed values for
299
+ your radio must be specified (i.e. 433 vs. 915 mhz)!
300
+ """
301
+ msb = self.read_u8(_RF95_REG_06_FRF_MSB)
302
+ mid = self.read_u8(_RF95_REG_07_FRF_MID)
303
+ lsb = self.read_u8(_RF95_REG_08_FRF_LSB)
304
+ frf = ((msb << 16) | (mid << 8) | lsb) & 0xFFFFFF
305
+ frequency = (frf * _RF95_FSTEP) / 1000000.0
306
+ return frequency
307
+
308
+ @frequency_mhz.setter
309
+ def frequency_mhz(self, val: Literal[433.0, 915.0]) -> None:
310
+ if val < 240 or val > 960:
311
+ raise RuntimeError("frequency_mhz must be between 240 and 960")
312
+ # Calculate FRF register 24-bit value.
313
+ frf = int((val * 1000000.0) / _RF95_FSTEP) & 0xFFFFFF
314
+ # Extract byte values and update registers.
315
+ msb = frf >> 16
316
+ mid = (frf >> 8) & 0xFF
317
+ lsb = frf & 0xFF
318
+ self.write_u8(_RF95_REG_06_FRF_MSB, msb)
319
+ self.write_u8(_RF95_REG_07_FRF_MID, mid)
320
+ self.write_u8(_RF95_REG_08_FRF_LSB, lsb)
321
+
322
+ @property
323
+ def tx_power(self) -> int:
324
+ """The transmit power in dBm. Can be set to a value from 5 to 23 for
325
+ high power devices (RFM95/96/97/98, high_power=True) or -1 to 14 for low
326
+ power devices. Only integer power levels are actually set (i.e. 12.5
327
+ will result in a value of 12 dBm).
328
+ The actual maximum setting for high_power=True is 20dBm but for values > 20
329
+ the PA_BOOST will be enabled resulting in an additional gain of 3dBm.
330
+ The actual setting is reduced by 3dBm.
331
+ The reported value will reflect the reduced setting.
332
+ """
333
+ if self.high_power:
334
+ return self.output_power + 5
335
+ return self.output_power - 1
336
+
337
+ @tx_power.setter
338
+ def tx_power(self, val: int) -> None:
339
+ val = int(val)
340
+ if self.high_power:
341
+ if val < 5 or val > 23:
342
+ raise RuntimeError("tx_power must be between 5 and 23")
343
+ # Enable power amp DAC if power is above 20 dB.
344
+ # Lower setting by 3db when PA_BOOST enabled - see Data Sheet Section 6.4
345
+ if val > 20:
346
+ self.pa_dac = _RF95_PA_DAC_ENABLE
347
+ val -= 3
348
+ else:
349
+ self.pa_dac = _RF95_PA_DAC_DISABLE
350
+ self.pa_select = True
351
+ self.output_power = (val - 5) & 0x0F
352
+ else:
353
+ assert -1 <= val <= 14
354
+ self.pa_select = False
355
+ self.max_power = 0b111 # Allow max power output.
356
+ self.output_power = (val + 1) & 0x0F
357
+
358
+ @property
359
+ def rssi(self) -> float:
360
+ """The received strength indicator (in dBm) of the last received message."""
361
+ # Read RSSI register and convert to value using formula in datasheet.
362
+ # Remember in LoRa mode the payload register changes function to RSSI!
363
+ raw_rssi = self.read_u8(_RF95_REG_1A_PKT_RSSI_VALUE)
364
+ if self.low_frequency_mode:
365
+ raw_rssi -= 157
366
+ else:
367
+ raw_rssi -= 164
368
+ return float(raw_rssi)
369
+
370
+ @property
371
+ def snr(self) -> float:
372
+ """The SNR (in dB) of the last received message."""
373
+ # Read SNR 0x19 register and convert to value using formula in datasheet.
374
+ # SNR(dB) = PacketSnr [twos complement] / 4
375
+ snr_byte = self.read_u8(_RF95_REG_19_PKT_SNR_VALUE)
376
+ if snr_byte > 127:
377
+ snr_byte = (256 - snr_byte) * -1
378
+ return snr_byte / 4
379
+
380
+ @property
381
+ def signal_bandwidth(self) -> int:
382
+ """The signal bandwidth used by the radio (try setting to a higher
383
+ value to increase throughput or to a lower value to increase the
384
+ likelihood of successfully received payloads). Valid values are
385
+ listed in RFM9x.bw_bins."""
386
+ bw_id = (self.read_u8(_RF95_REG_1D_MODEM_CONFIG1) & 0xF0) >> 4
387
+ if bw_id >= len(self.bw_bins):
388
+ current_bandwidth = 500000
389
+ else:
390
+ current_bandwidth = self.bw_bins[bw_id]
391
+ return current_bandwidth
392
+
393
+ @signal_bandwidth.setter
394
+ def signal_bandwidth(self, val: int) -> None:
395
+ # Set signal bandwidth (set to 125000 to match RadioHead Bw125).
396
+ for bw_id, cutoff in enumerate(self.bw_bins):
397
+ if val <= cutoff:
398
+ break
399
+ else:
400
+ bw_id = 9
401
+ self.write_u8(
402
+ _RF95_REG_1D_MODEM_CONFIG1,
403
+ (self.read_u8(_RF95_REG_1D_MODEM_CONFIG1) & 0x0F) | (bw_id << 4),
404
+ )
405
+ if val >= 500000:
406
+ # see Semtech SX1276 errata note 2.3
407
+ self.auto_ifon = True
408
+ # see Semtech SX1276 errata note 2.1
409
+ if self.low_frequency_mode:
410
+ self.write_u8(0x36, 0x02)
411
+ self.write_u8(0x3A, 0x7F)
412
+ else:
413
+ self.write_u8(0x36, 0x02)
414
+ self.write_u8(0x3A, 0x64)
415
+ else:
416
+ # see Semtech SX1276 errata note 2.3
417
+ self.auto_ifon = False
418
+ self.write_u8(0x36, 0x03)
419
+ if val == 7800:
420
+ self.write_u8(0x2F, 0x48)
421
+ elif val >= 62500:
422
+ # see Semtech SX1276 errata note 2.3
423
+ self.write_u8(0x2F, 0x40)
424
+ else:
425
+ self.write_u8(0x2F, 0x44)
426
+ self.write_u8(0x30, 0)
427
+
428
+ @property
429
+ def coding_rate(self) -> Literal[5, 6, 7, 8]:
430
+ """The coding rate used by the radio to control forward error
431
+ correction (try setting to a higher value to increase tolerance of
432
+ short bursts of interference or to a lower value to increase bit
433
+ rate). Valid values are limited to 5, 6, 7, or 8."""
434
+ cr_id = (self.read_u8(_RF95_REG_1D_MODEM_CONFIG1) & 0x0E) >> 1
435
+ denominator = cr_id + 4
436
+ return denominator
437
+
438
+ @coding_rate.setter
439
+ def coding_rate(self, val: Literal[5, 6, 7, 8]) -> None:
440
+ # Set coding rate (set to 5 to match RadioHead Cr45).
441
+ denominator = min(max(val, 5), 8)
442
+ cr_id = denominator - 4
443
+ self.write_u8(
444
+ _RF95_REG_1D_MODEM_CONFIG1,
445
+ (self.read_u8(_RF95_REG_1D_MODEM_CONFIG1) & 0xF1) | (cr_id << 1),
446
+ )
447
+
448
+ @property
449
+ def spreading_factor(self) -> Literal[6, 7, 8, 9, 10, 11, 12]:
450
+ """The spreading factor used by the radio (try setting to a higher
451
+ value to increase the receiver's ability to distinguish signal from
452
+ noise or to a lower value to increase the data transmission rate).
453
+ Valid values are limited to 6, 7, 8, 9, 10, 11, or 12."""
454
+ sf_id = (self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) & 0xF0) >> 4
455
+ return sf_id
456
+
457
+ @spreading_factor.setter
458
+ def spreading_factor(self, val: Literal[6, 7, 8, 9, 10, 11, 12]) -> None:
459
+ # Set spreading factor (set to 7 to match RadioHead Sf128).
460
+ val = min(max(val, 6), 12)
461
+
462
+ if val == 6:
463
+ self.detection_optimize = 0x5
464
+ else:
465
+ self.detection_optimize = 0x3
466
+
467
+ self.write_u8(_RF95_DETECTION_THRESHOLD, 0x0C if val == 6 else 0x0A)
468
+ self.write_u8(
469
+ _RF95_REG_1E_MODEM_CONFIG2,
470
+ ((self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) & 0x0F) | ((val << 4) & 0xF0)),
471
+ )
472
+
473
+ @property
474
+ def enable_crc(self) -> bool:
475
+ """Set to True to enable hardware CRC checking of incoming packets.
476
+ Incoming packets that fail the CRC check are not processed. Set to
477
+ False to disable CRC checking and process all incoming packets."""
478
+ return (self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) & 0x04) == 0x04
479
+
480
+ @enable_crc.setter
481
+ def enable_crc(self, val: bool) -> None:
482
+ # Optionally enable CRC checking on incoming packets.
483
+ if val:
484
+ self.write_u8(
485
+ _RF95_REG_1E_MODEM_CONFIG2,
486
+ self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) | 0x04,
487
+ )
488
+ else:
489
+ self.write_u8(
490
+ _RF95_REG_1E_MODEM_CONFIG2,
491
+ self.read_u8(_RF95_REG_1E_MODEM_CONFIG2) & 0xFB,
492
+ )
493
+
494
+ @property
495
+ def crc_error(self) -> bool:
496
+ """crc status"""
497
+ return (self.read_u8(_RF95_REG_12_IRQ_FLAGS) & 0x20) >> 5
498
+
499
+ def packet_sent(self) -> bool:
500
+ """Transmit status"""
501
+ return (self.read_u8(_RF95_REG_12_IRQ_FLAGS) & 0x8) >> 3
502
+
503
+ def payload_ready(self) -> bool:
504
+ """Receive status"""
505
+ return (self.read_u8(_RF95_REG_12_IRQ_FLAGS) & 0x40) >> 6
506
+
507
+ def clear_interrupt(self) -> None:
508
+ """Clear Interrupt flags"""
509
+ self.write_u8(_RF95_REG_12_IRQ_FLAGS, 0xFF)
510
+
511
+ def fill_fifo(self, payload: ReadableBuffer) -> None:
512
+ """len_data is not used but is here for compatibility with rfm69
513
+ Fill the FIFO with a packet to send"""
514
+ self.write_u8(_RF95_REG_0D_FIFO_ADDR_PTR, 0x00) # FIFO starts at 0.
515
+ # Write payload.
516
+ self.write_from(_RF95_REG_00_FIFO, payload)
517
+ # Write payload and header length.
518
+ self.write_u8(_RF95_REG_22_PAYLOAD_LENGTH, len(payload))
519
+
520
+ def read_fifo(self) -> Optional[bytearray]:
521
+ """Read the data from the FIFO."""
522
+ # Read the length of the FIFO.
523
+ fifo_length = self.read_u8(_RF95_REG_13_RX_NB_BYTES)
524
+ packet = None # return None if FIFO empty
525
+ if fifo_length > 0: # read and clear the FIFO if anything in it
526
+ packet = bytearray(fifo_length)
527
+ current_addr = self.read_u8(_RF95_REG_10_FIFO_RX_CURRENT_ADDR)
528
+ self.write_u8(_RF95_REG_0D_FIFO_ADDR_PTR, current_addr)
529
+ # read the packet
530
+ self.read_into(_RF95_REG_00_FIFO, packet)
531
+
532
+ # clear interrupt
533
+ self.write_u8(_RF95_REG_12_IRQ_FLAGS, 0xFF)
534
+ return packet