├── VERSION
├── SX127x
├── __init__.py
├── LoRaArgumentParser.py
├── board_config.py
├── constants.py
└── LoRa.py
├── .gitignore
├── socket_client.py
├── lora_util.py
├── rx_cont.py
├── socket_transceiver.py
├── tx_beacon.py
├── test_lora.py
├── README.md
└── LICENSE
/VERSION:
--------------------------------------------------------------------------------
1 | 0.1
--------------------------------------------------------------------------------
/SX127x/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ['SX127x']
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.txt
2 | *.out
3 | *.pyc
4 | .DS_Store
5 | .ssh
6 |
--------------------------------------------------------------------------------
/socket_client.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | # used for testing socket_transceiver.py
4 | # connects to socket and allows user to send ascii payload
5 |
6 | import socket
7 |
8 | def sock_client():
9 | host = '127.0.0.1'
10 | port = 20000
11 |
12 | sock = socket.socket()
13 | sock.connect((host,port))
14 |
15 | message = input('>> ')
16 |
17 | while message != 'quit':
18 | sock.send(bytearray(message,'utf-8'))
19 |
20 | data = bytearray(sock.recv(1024)).decode('ascii')
21 |
22 | print ('From LoRa: ' + data)
23 |
24 | message = input('>> ')
25 |
26 | sock.close()
27 |
28 | if __name__ == '__main__':
29 | sock_client()
30 |
--------------------------------------------------------------------------------
/lora_util.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python2.7
2 |
3 | """ This is a utility script for the SX127x (LoRa mode). It dumps all registers. """
4 |
5 | # Copyright 2015 Mayer Analytics Ltd.
6 | #
7 | # This file is part of pySX127x.
8 | #
9 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
10 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
11 | # version.
12 | #
13 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15 | # details.
16 | #
17 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
18 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
19 | # own applications, or shipping pySX127x with a closed source product.
20 | #
21 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
22 | # .
23 |
24 |
25 | from SX127x.LoRa import *
26 | from SX127x.board_config import BOARD
27 | import argparse
28 |
29 | BOARD.setup()
30 |
31 | parser = argparse.ArgumentParser(description='LoRa utility functions')
32 | parser.add_argument('--dump', '-d', dest='dump', default=False, action="store_true", help="dump all registers")
33 | args = parser.parse_args()
34 |
35 | lora = LoRa(verbose=False)
36 |
37 | if args.dump:
38 |
39 | print("LoRa register dump:\n")
40 | print("%02s %18s %2s %8s" % ('i', 'reg_name', 'v', 'v'))
41 | print("-- ------------------ -- --------")
42 | for reg_i, reg_name, val in lora.dump_registers():
43 | print("%02X %18s %02X %s" % (reg_i, reg_name, val, format(val, '#010b')[2:]))
44 | print("")
45 |
46 | else:
47 | print(lora)
48 |
49 | BOARD.teardown()
--------------------------------------------------------------------------------
/SX127x/LoRaArgumentParser.py:
--------------------------------------------------------------------------------
1 | """ Defines LoRaArgumentParser which extends argparse.ArgumentParser with standard config parameters for the SX127x. """
2 | # -*- coding: utf-8 -*-
3 |
4 | # Copyright 2018 Mayer Analytics Ltd.
5 | #
6 | # This file is part of pySX127x.
7 | #
8 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
9 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
10 | # version.
11 | #
12 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 | # details.
15 | #
16 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
17 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
18 | # own applications, or shipping pySX127x with a closed source product.
19 | #
20 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
21 | # .
22 |
23 |
24 | import argparse
25 |
26 |
27 | class LoRaArgumentParser(argparse.ArgumentParser):
28 | """ This class extends argparse.ArgumentParser.
29 | Some commonly used LoRa config parameters are defined
30 | * ocp
31 | * spreading factor
32 | * frequency
33 | * bandwidth
34 | * preamble
35 | Call the parse_args with an additional parameter referencing a LoRa object. The args will be used to configure
36 | the LoRa.
37 | """
38 |
39 | bw_lookup = dict(BW7_8=0, BW10_4=1, BW15_6=2, BW20_8=3, BW31_25=4, BW41_7=5, BW62_5=6, BW125=7, BW250=8, BW500=9)
40 | cr_lookup = dict(CR4_5=1, CR4_6=2,CR4_7=3,CR4_8=4)
41 |
42 | def __init__(self, description):
43 | argparse.ArgumentParser.__init__(self, description=description)
44 | self.add_argument('--ocp', '-c', dest='ocp', default=100, action="store", type=float,
45 | help="Over current protection in mA (45 .. 240 mA)")
46 | self.add_argument('--sf', '-s', dest='sf', default=7, action="store", type=int,
47 | help="Spreading factor (6...12). Default is 7.")
48 | self.add_argument('--freq', '-f', dest='freq', default=869., action="store", type=float,
49 | help="Frequency")
50 | self.add_argument('--bw', '-b', dest='bw', default='BW125', action="store", type=str,
51 | help="Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25 BW41_7 BW62_5 BW125 BW250 BW500).\nDefault is BW125.")
52 | self.add_argument('--cr', '-r', dest='coding_rate', default='CR4_5', action="store", type=str,
53 | help="Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8).\nDefault is CR4_5.")
54 | self.add_argument('--preamble', '-p', dest='preamble', default=8, action="store", type=int,
55 | help="Preamble length. Default is 8.")
56 |
57 | def parse_args(self, lora):
58 | """ Parse the args, perform some sanity checks and configure the LoRa accordingly.
59 | :param lora: Reference to LoRa object
60 | :return: args
61 | """
62 | args = argparse.ArgumentParser.parse_args(self)
63 | args.bw = self.bw_lookup.get(args.bw, None)
64 | args.coding_rate = self.cr_lookup.get(args.coding_rate, None)
65 | # some sanity checks
66 | assert(args.bw is not None)
67 | assert(args.coding_rate is not None)
68 | assert(args.sf >=6 and args.sf <= 12)
69 | # set the LoRa object
70 | lora.set_freq(args.freq)
71 | lora.set_preamble(args.preamble)
72 | lora.set_spreading_factor(args.sf)
73 | lora.set_bw(args.bw)
74 | lora.set_coding_rate(args.coding_rate)
75 | lora.set_ocp_trim(args.ocp)
76 | return args
77 |
--------------------------------------------------------------------------------
/rx_cont.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | """ A simple continuous receiver class. """
4 |
5 | # Copyright 2015 Mayer Analytics Ltd.
6 | #
7 | # This file is part of pySX127x.
8 | #
9 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
10 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
11 | # version.
12 | #
13 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15 | # details.
16 | #
17 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
18 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
19 | # own applications, or shipping pySX127x with a closed source product.
20 | #
21 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
22 | # .
23 |
24 |
25 | from time import sleep
26 | from SX127x.LoRa import *
27 | from SX127x.LoRaArgumentParser import LoRaArgumentParser
28 | from SX127x.board_config import BOARD
29 |
30 | BOARD.setup()
31 |
32 | parser = LoRaArgumentParser("Continous LoRa receiver.")
33 |
34 |
35 | class LoRaRcvCont(LoRa):
36 | def __init__(self, verbose=False):
37 | super(LoRaRcvCont, self).__init__(verbose)
38 | self.set_mode(MODE.SLEEP)
39 | self.set_dio_mapping([0] * 6)
40 |
41 | def on_rx_done(self):
42 | BOARD.led_on()
43 | print("\nRxDone")
44 | self.clear_irq_flags(RxDone=1)
45 | payload = self.read_payload(nocheck=True)
46 | print(bytes(payload).decode())
47 | self.set_mode(MODE.SLEEP)
48 | self.reset_ptr_rx()
49 | BOARD.led_off()
50 | self.set_mode(MODE.RXCONT)
51 |
52 | def on_tx_done(self):
53 | print("\nTxDone")
54 | print(self.get_irq_flags())
55 |
56 | def on_cad_done(self):
57 | print("\non_CadDone")
58 | print(self.get_irq_flags())
59 |
60 | def on_rx_timeout(self):
61 | print("\non_RxTimeout")
62 | print(self.get_irq_flags())
63 |
64 | def on_valid_header(self):
65 | print("\non_ValidHeader")
66 | print(self.get_irq_flags())
67 |
68 | def on_payload_crc_error(self):
69 | print("\non_PayloadCrcError")
70 | print(self.get_irq_flags())
71 |
72 | def on_fhss_change_channel(self):
73 | print("\non_FhssChangeChannel")
74 | print(self.get_irq_flags())
75 |
76 | def start(self):
77 | self.reset_ptr_rx()
78 | self.set_mode(MODE.RXCONT)
79 | while True:
80 | sleep(.5)
81 | rssi_value = self.get_rssi_value()
82 | status = self.get_modem_status()
83 | sys.stdout.flush()
84 | sys.stdout.write("\r%d %d %d" % (rssi_value, status['rx_ongoing'], status['modem_clear']))
85 |
86 |
87 | lora = LoRaRcvCont(verbose=False)
88 | args = parser.parse_args(lora)
89 |
90 | lora.set_mode(MODE.STDBY)
91 | lora.set_pa_config(pa_select=1)
92 | #lora.set_rx_crc(True)
93 | #lora.set_coding_rate(CODING_RATE.CR4_6)
94 | #lora.set_pa_config(max_power=0, output_power=0)
95 | #lora.set_lna_gain(GAIN.G1)
96 | #lora.set_implicit_header_mode(False)
97 | #lora.set_low_data_rate_optim(True)
98 | #lora.set_pa_ramp(PA_RAMP.RAMP_50_us)
99 | #lora.set_agc_auto_on(True)
100 |
101 | print(lora)
102 | assert(lora.get_agc_auto_on() == 1)
103 |
104 | try: input("Press enter to start...")
105 | except: pass
106 |
107 | try:
108 | lora.start()
109 | except KeyboardInterrupt:
110 | sys.stdout.flush()
111 | print("")
112 | sys.stderr.write("KeyboardInterrupt\n")
113 | finally:
114 | sys.stdout.flush()
115 | print("")
116 | lora.set_mode(MODE.SLEEP)
117 | print(lora)
118 | BOARD.teardown()
119 |
--------------------------------------------------------------------------------
/socket_transceiver.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | """ An asynchronous socket <-> LoRa interface """
4 |
5 | # MIT License
6 | #
7 | # Copyright (c) 2016 bjcarne
8 | #
9 | # Permission is hereby granted, free of charge, to any person obtaining a copy
10 | # of this software and associated documentation files (the "Software"), to deal
11 | # in the Software without restriction, including without limitation the rights
12 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | # copies of the Software, and to permit persons to whom the Software is
14 | # furnished to do so, subject to the following conditions:
15 | #
16 | # The above copyright notice and this permission notice shall be included in all
17 | # copies or substantial portions of the Software.
18 | #
19 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 | # SOFTWARE.
26 |
27 |
28 | import sys, asyncore
29 | from time import time
30 | from SX127x.LoRa import *
31 | from SX127x.board_config import BOARD
32 |
33 | BOARD.setup()
34 |
35 | class Server(asyncore.dispatcher):
36 | def __init__(self, host, port):
37 | asyncore.dispatcher.__init__(self)
38 | self.create_socket()
39 | self.set_reuse_addr()
40 | self.bind((host, port))
41 | self.listen(1)
42 |
43 | def handle_accepted(self, sock, addr):
44 | print("Connection from %s:%s" % sock.getpeername())
45 | self.conn = Handler(sock)
46 |
47 | class Handler(asyncore.dispatcher):
48 | def __init__(self, sock):
49 | asyncore.dispatcher.__init__(self, sock)
50 | self.databuffer = b""
51 | self.tx_wait = 0
52 |
53 | # when data is available on socket send to LoRa
54 | def handle_read(self):
55 | if not self.tx_wait:
56 | data = self.recv(127)
57 | print('Send:' + str(data))
58 | lora.write_payload(list(data))
59 | lora.set_dio_mapping([1,0,0,0,0,0]) # set DIO0 for txdone
60 | lora.set_mode(MODE.TX)
61 | self.tx_wait = 1
62 |
63 | # when data for the socket, send
64 | def handle_write(self):
65 | if self.databuffer:
66 | self.send(self.databuffer)
67 | self.databuffer = b""
68 |
69 | def handle_close(self):
70 | print("Client disconnected")
71 | self.close()
72 |
73 | class LoRaSocket(LoRa):
74 |
75 | def __init__(self, verbose=False):
76 | super(LoRaSocket, self).__init__(verbose)
77 | self.set_mode(MODE.SLEEP)
78 | self.set_pa_config(pa_select=1)
79 | self.set_max_payload_length(128) # set max payload to max fifo buffer length
80 | self.payload = []
81 | self.set_dio_mapping([0] * 6) #initialise DIO0 for rxdone
82 |
83 | # when LoRa receives data send to socket conn
84 | def on_rx_done(self):
85 | payload = self.read_payload(nocheck=True)
86 |
87 | if len(payload) == 127:
88 | self.payload[len(self.payload):] = payload
89 | else:
90 | self.payload[len(self.payload):] = payload
91 | print('Recv:' + str(bytes(self.payload)))
92 |
93 | server.conn.databuffer = bytes(self.payload) #send to socket conn
94 | self.payload = []
95 |
96 | self.clear_irq_flags(RxDone=1) # clear rxdone IRQ flag
97 | self.reset_ptr_rx()
98 | self.set_mode(MODE.RXCONT)
99 |
100 | # after data sent by LoRa reset to receive mode
101 | def on_tx_done(self):
102 | self.clear_irq_flags(TxDone=1) # clear txdone IRQ flag
103 | self.set_dio_mapping([0] * 6)
104 |
105 | self.set_mode(MODE.RXCONT)
106 | server.conn.tx_wait = 0
107 |
108 |
109 | if __name__ == '__main__':
110 |
111 | server = Server('localhost', 20000)
112 |
113 | lora = LoRaSocket(verbose=False)
114 |
115 | print(lora)
116 |
117 | try:
118 | asyncore.loop()
119 |
120 | except KeyboardInterrupt:
121 | sys.stderr.write("\nKeyboardInterrupt\n")
122 |
123 | finally:
124 | lora.set_mode(MODE.SLEEP)
125 | print("Closing socket connection")
126 | server.close()
127 | BOARD.teardown()
128 |
--------------------------------------------------------------------------------
/tx_beacon.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | """ A simple beacon transmitter class to send a 1-byte message (0x0f) in regular time intervals. """
4 |
5 | # Copyright 2015 Mayer Analytics Ltd.
6 | #
7 | # This file is part of pySX127x.
8 | #
9 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
10 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
11 | # version.
12 | #
13 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15 | # details.
16 | #
17 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
18 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
19 | # own applications, or shipping pySX127x with a closed source product.
20 | #
21 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
22 | # .
23 |
24 |
25 | import sys
26 | from time import sleep
27 | from SX127x.LoRa import *
28 | from SX127x.LoRaArgumentParser import LoRaArgumentParser
29 | from SX127x.board_config import BOARD
30 |
31 | BOARD.setup()
32 |
33 | parser = LoRaArgumentParser("A simple LoRa beacon")
34 | parser.add_argument('--single', '-S', dest='single', default=False, action="store_true", help="Single transmission")
35 | parser.add_argument('--wait', '-w', dest='wait', default=1, action="store", type=float, help="Waiting time between transmissions (default is 0s)")
36 |
37 |
38 | class LoRaBeacon(LoRa):
39 |
40 | tx_counter = 0
41 |
42 | def __init__(self, verbose=False):
43 | super(LoRaBeacon, self).__init__(verbose)
44 | self.set_mode(MODE.SLEEP)
45 | self.set_dio_mapping([1,0,0,0,0,0])
46 |
47 | def on_rx_done(self):
48 | print("\nRxDone")
49 | print(self.get_irq_flags())
50 | print(map(hex, self.read_payload(nocheck=True)))
51 | self.set_mode(MODE.SLEEP)
52 | self.reset_ptr_rx()
53 | self.set_mode(MODE.RXCONT)
54 |
55 | def on_tx_done(self):
56 | global args
57 | self.set_mode(MODE.STDBY)
58 | self.clear_irq_flags(TxDone=1)
59 | sys.stdout.flush()
60 | self.tx_counter += 1
61 | sys.stdout.write("\rtx #%d" % self.tx_counter)
62 | if args.single:
63 | print
64 | sys.exit(0)
65 | BOARD.led_off()
66 | sleep(args.wait)
67 | self.write_payload([0x0f])
68 | BOARD.led_on()
69 | self.set_mode(MODE.TX)
70 |
71 | def on_cad_done(self):
72 | print("\non_CadDone")
73 | print(self.get_irq_flags())
74 |
75 | def on_rx_timeout(self):
76 | print("\non_RxTimeout")
77 | print(self.get_irq_flags())
78 |
79 | def on_valid_header(self):
80 | print("\non_ValidHeader")
81 | print(self.get_irq_flags())
82 |
83 | def on_payload_crc_error(self):
84 | print("\non_PayloadCrcError")
85 | print(self.get_irq_flags())
86 |
87 | def on_fhss_change_channel(self):
88 | print("\non_FhssChangeChannel")
89 | print(self.get_irq_flags())
90 |
91 | def start(self):
92 | global args
93 | sys.stdout.write("\rstart")
94 | self.tx_counter = 0
95 | BOARD.led_on()
96 | self.write_payload([0x0f])
97 | self.set_mode(MODE.TX)
98 | while True:
99 | sleep(1)
100 |
101 | lora = LoRaBeacon(verbose=False)
102 | args = parser.parse_args(lora)
103 |
104 | lora.set_pa_config(pa_select=1)
105 | #lora.set_rx_crc(True)
106 | #lora.set_agc_auto_on(True)
107 | #lora.set_lna_gain(GAIN.NOT_USED)
108 | #lora.set_coding_rate(CODING_RATE.CR4_6)
109 | #lora.set_implicit_header_mode(False)
110 | #lora.set_pa_config(max_power=0x04, output_power=0x0F)
111 | #lora.set_pa_config(max_power=0x04, output_power=0b01000000)
112 | #lora.set_low_data_rate_optim(True)
113 | #lora.set_pa_ramp(PA_RAMP.RAMP_50_us)
114 |
115 |
116 | print(lora)
117 | #assert(lora.get_lna()['lna_gain'] == GAIN.NOT_USED)
118 | assert(lora.get_agc_auto_on() == 1)
119 |
120 | print("Beacon config:")
121 | print(" Wait %f s" % args.wait)
122 | print(" Single tx = %s" % args.single)
123 | print("")
124 | try: input("Press enter to start...")
125 | except: pass
126 |
127 | try:
128 | lora.start()
129 | except KeyboardInterrupt:
130 | sys.stdout.flush()
131 | print("")
132 | sys.stderr.write("KeyboardInterrupt\n")
133 | finally:
134 | sys.stdout.flush()
135 | print("")
136 | lora.set_mode(MODE.SLEEP)
137 | print(lora)
138 | BOARD.teardown()
139 |
--------------------------------------------------------------------------------
/SX127x/board_config.py:
--------------------------------------------------------------------------------
1 | """ Defines the BOARD class that contains the board pin mappings and RF module HF/LF info. """
2 | # -*- coding: utf-8 -*-
3 |
4 | # Copyright 2015-2022 Mayer Analytics Ltd.
5 | #
6 | # This file is part of pySX127x.
7 | #
8 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
9 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
10 | # version.
11 | #
12 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 | # details.
15 | #
16 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
17 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
18 | # own applications, or shipping pySX127x with a closed source product.
19 | #
20 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
21 | # .
22 |
23 |
24 | import RPi.GPIO as GPIO
25 | import spidev
26 |
27 | import time
28 |
29 |
30 | class BOARD:
31 | """ Board initialisation/teardown and pin configuration is kept here.
32 | Also, information about the RF module is kept here.
33 | This is the Raspberry Pi board with one LED and a modtronix inAir9B.
34 | """
35 | # Note that the BCOM numbering for the GPIOs is used.
36 | DIO0 = 22 # RaspPi GPIO 22
37 | DIO1 = 23 # RaspPi GPIO 23
38 | DIO2 = 24 # RaspPi GPIO 24
39 | DIO3 = 25 # RaspPi GPIO 25
40 | LED = 18 # RaspPi GPIO 18 connects to the LED on the proto shield
41 | SWITCH = 4 # RaspPi GPIO 4 connects to a switch connected to the board which was used to trigger TXs
42 |
43 | # The spi object is kept here
44 | spi = None
45 |
46 | # tell pySX127x here whether the attached RF module uses low-band (RF*_LF pins) or high-band (RF*_HF pins).
47 | # low band (called band 1&2) are 137-175 and 410-525
48 | # high band (called band 3) is 862-1020
49 | low_band = True
50 |
51 | @staticmethod
52 | def setup():
53 | """ Configure the Raspberry GPIOs
54 | :rtype : None
55 | """
56 | GPIO.setmode(GPIO.BCM)
57 | # LED
58 | GPIO.setup(BOARD.LED, GPIO.OUT)
59 | GPIO.output(BOARD.LED, 0)
60 | # switch
61 | GPIO.setup(BOARD.SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
62 | # DIOx
63 | for gpio_pin in [BOARD.DIO0, BOARD.DIO1, BOARD.DIO2, BOARD.DIO3]:
64 | GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
65 | # blink 2 times to signal the board is set up
66 | BOARD.blink(.1, 2)
67 |
68 | @staticmethod
69 | def teardown():
70 | """ Cleanup GPIO and SpiDev """
71 | GPIO.cleanup()
72 | BOARD.spi.close()
73 |
74 | @staticmethod
75 | def SpiDev(spi_bus=0, spi_cs=0):
76 | """ Init and return the SpiDev object
77 | :return: SpiDev object
78 | :param spi_bus: The RPi SPI bus to use: 0 or 1
79 | :param spi_cs: The RPi SPI chip select to use: 0 or 1
80 | :rtype: SpiDev
81 | """
82 | BOARD.spi = spidev.SpiDev()
83 | BOARD.spi.open(spi_bus, spi_cs)
84 | BOARD.spi.max_speed_hz = 5000000 # SX127x can go up to 10MHz, pick half that to be safe
85 | return BOARD.spi
86 |
87 | @staticmethod
88 | def add_event_detect(dio_number, callback):
89 | """ Wraps around the GPIO.add_event_detect function
90 | :param dio_number: DIO pin 0...5
91 | :param callback: The function to call when the DIO triggers an IRQ.
92 | :return: None
93 | """
94 | GPIO.add_event_detect(dio_number, GPIO.RISING, callback=callback)
95 |
96 | @staticmethod
97 | def add_events(cb_dio0, cb_dio1, cb_dio2, cb_dio3, cb_dio4, cb_dio5, switch_cb=None):
98 | BOARD.add_event_detect(BOARD.DIO0, callback=cb_dio0)
99 | BOARD.add_event_detect(BOARD.DIO1, callback=cb_dio1)
100 | BOARD.add_event_detect(BOARD.DIO2, callback=cb_dio2)
101 | BOARD.add_event_detect(BOARD.DIO3, callback=cb_dio3)
102 | # the modtronix inAir9B does not expose DIO4 and DIO5
103 | if switch_cb is not None:
104 | GPIO.add_event_detect(BOARD.SWITCH, GPIO.RISING, callback=switch_cb, bouncetime=300)
105 |
106 | @staticmethod
107 | def led_on(value=1):
108 | """ Switch the proto shields LED
109 | :param value: 0/1 for off/on. Default is 1.
110 | :return: value
111 | :rtype : int
112 | """
113 | GPIO.output(BOARD.LED, value)
114 | return value
115 |
116 | @staticmethod
117 | def led_off():
118 | """ Switch LED off
119 | :return: 0
120 | """
121 | GPIO.output(BOARD.LED, 0)
122 | return 0
123 |
124 | @staticmethod
125 | def blink(time_sec, n_blink):
126 | if n_blink == 0:
127 | return
128 | BOARD.led_on()
129 | for i in range(n_blink):
130 | time.sleep(time_sec)
131 | BOARD.led_off()
132 | time.sleep(time_sec)
133 | BOARD.led_on()
134 | BOARD.led_off()
135 |
--------------------------------------------------------------------------------
/test_lora.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python2.7
2 |
3 | """ This script runs a small number of unit tests. """
4 |
5 | # Copyright 2015 Mayer Analytics Ltd.
6 | #
7 | # This file is part of pySX127x.
8 | #
9 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
10 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
11 | # version.
12 | #
13 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15 | # details.
16 | #
17 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
18 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
19 | # own applications, or shipping pySX127x with a closed source product.
20 | #
21 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
22 | # .
23 |
24 |
25 | from SX127x.LoRa import *
26 | from SX127x.board_config import BOARD
27 | import unittest
28 |
29 |
30 | def get_reg(reg_addr):
31 | return lora.get_register(reg_addr)
32 |
33 |
34 | def SaveState(reg_addr, n_registers=1):
35 | """ This decorator wraps a get/set_register around the function (unittest) call.
36 | :param reg_addr: Start of register addresses
37 | :param n_registers: Number of registers to save. (Useful for MSB/LSB register pairs, etc.)
38 | :return:
39 | """
40 | def decorator(func):
41 | def wrapper(self):
42 | reg_bkup = lora.get_register(reg_addr)
43 | func(self)
44 | lora.set_register(reg_addr, reg_bkup)
45 | return wrapper
46 | return decorator
47 |
48 |
49 | class TestSX127x(unittest.TestCase):
50 |
51 | def test_setter_getter(self):
52 | bkup = lora.get_payload_length()
53 | for l in [1,50, 128, bkup]:
54 | lora.set_payload_length(l)
55 | self.assertEqual(lora.get_payload_length(), l)
56 |
57 | @SaveState(REG.LORA.OP_MODE)
58 | def test_mode(self):
59 | mode = lora.get_mode()
60 | for m in [MODE.STDBY, MODE.SLEEP, mode]:
61 | lora.set_mode(m)
62 | self.assertEqual(lora.get_mode(), m)
63 |
64 | @SaveState(REG.LORA.FR_MSB, n_registers=3)
65 | def test_set_freq(self):
66 | freq = lora.get_freq()
67 | for f in [433.5, 434.5, 434.0, freq]:
68 | lora.set_freq(f)
69 | self.assertEqual(lora.get_freq(), f)
70 |
71 | @SaveState(REG.LORA.MODEM_CONFIG_3)
72 | def test_set_agc_on(self):
73 | lora.set_agc_auto_on(True)
74 | self.assertEqual((get_reg(REG.LORA.MODEM_CONFIG_3) & 0b100) >> 2, 1)
75 | lora.set_agc_auto_on(False)
76 | self.assertEqual((get_reg(REG.LORA.MODEM_CONFIG_3) & 0b100) >> 2, 0)
77 |
78 | @SaveState(REG.LORA.MODEM_CONFIG_3)
79 | def test_set_low_data_rate_optim(self):
80 | lora.set_low_data_rate_optim(True)
81 | self.assertEqual((get_reg(REG.LORA.MODEM_CONFIG_3) & 0b1000) >> 3, 1)
82 | lora.set_low_data_rate_optim(False)
83 | self.assertEqual((get_reg(REG.LORA.MODEM_CONFIG_3) & 0b1000) >> 3, 0)
84 |
85 | @SaveState(REG.LORA.DIO_MAPPING_1, 2)
86 | def test_set_dio_mapping(self):
87 |
88 | dio_mapping = [1] * 6
89 | lora.set_dio_mapping(dio_mapping)
90 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b01010101)
91 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b01010000)
92 | self.assertEqual(lora.get_dio_mapping(), dio_mapping)
93 |
94 | dio_mapping = [2] * 6
95 | lora.set_dio_mapping(dio_mapping)
96 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b10101010)
97 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b10100000)
98 | self.assertEqual(lora.get_dio_mapping(), dio_mapping)
99 |
100 | dio_mapping = [0] * 6
101 | lora.set_dio_mapping(dio_mapping)
102 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b00000000)
103 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b00000000)
104 | self.assertEqual(lora.get_dio_mapping(), dio_mapping)
105 |
106 | dio_mapping = [0,1,2,0,1,2]
107 | lora.set_dio_mapping(dio_mapping)
108 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b00011000)
109 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b01100000)
110 | self.assertEqual(lora.get_dio_mapping(), dio_mapping)
111 |
112 | dio_mapping = [1,2,0,1,2,0]
113 | lora.set_dio_mapping(dio_mapping)
114 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_1), 0b01100001)
115 | self.assertEqual(get_reg(REG.LORA.DIO_MAPPING_2), 0b10000000)
116 | self.assertEqual(lora.get_dio_mapping(), dio_mapping)
117 |
118 | # def test_set_lna_gain(self):
119 | # bkup_lna_gain = lora.get_lna()['lna_gain']
120 | # for target_gain in [GAIN.NOT_USED, GAIN.G1, GAIN.G2, GAIN.G6, GAIN.NOT_USED, bkup_lna_gain]:
121 | # print(target_gain)
122 | # lora.set_lna_gain(target_gain)
123 | # actual_gain = lora.get_lna()['lna_gain']
124 | # self.assertEqual(GAIN.lookup[actual_gain], GAIN.lookup[target_gain])
125 |
126 |
127 | if __name__ == '__main__':
128 |
129 | BOARD.setup()
130 | lora = LoRa(verbose=False)
131 | unittest.main()
132 | BOARD.teardown()
133 |
--------------------------------------------------------------------------------
/SX127x/constants.py:
--------------------------------------------------------------------------------
1 | """ Defines constants (modes, bandwidths, registers, etc.) needed by SX127x. """
2 | # -*- coding: utf-8 -*-
3 |
4 | # Copyright 2015-2018 Mayer Analytics Ltd.
5 | #
6 | # This file is part of pySX127x.
7 | #
8 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
9 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
10 | # version.
11 | #
12 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 | # details.
15 | #
16 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
17 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
18 | # own applications, or shipping pySX127x with a closed source product.
19 | #
20 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
21 | # .
22 |
23 |
24 | def add_lookup(cls):
25 | """ A decorator that adds a lookup dictionary to the class.
26 | The lookup dictionary maps the codes back to the names. This is used for pretty-printing. """
27 | varnames = filter(str.isupper, cls.__dict__.keys())
28 | lookup = dict(map(lambda varname: (cls.__dict__.get(varname, None), varname), varnames))
29 | setattr(cls, 'lookup', lookup)
30 | return cls
31 |
32 |
33 | @add_lookup
34 | class MODE:
35 | SLEEP = 0x80
36 | STDBY = 0x81
37 | FSTX = 0x82
38 | TX = 0x83
39 | FSRX = 0x84
40 | RXCONT = 0x85
41 | RXSINGLE = 0x86
42 | CAD = 0x87
43 | FSK_STDBY= 0x01 # needed for calibration
44 |
45 |
46 | @add_lookup
47 | class BW:
48 | BW7_8 = 0
49 | BW10_4 = 1
50 | BW15_6 = 2
51 | BW20_8 = 3
52 | BW31_25 = 4
53 | BW41_7 = 5
54 | BW62_5 = 6
55 | BW125 = 7
56 | BW250 = 8
57 | BW500 = 9
58 |
59 |
60 | @add_lookup
61 | class CODING_RATE:
62 | CR4_5 = 1
63 | CR4_6 = 2
64 | CR4_7 = 3
65 | CR4_8 = 4
66 |
67 |
68 | @add_lookup
69 | class GAIN:
70 | NOT_USED = 0b000
71 | G1 = 0b001
72 | G2 = 0b010
73 | G3 = 0b011
74 | G4 = 0b100
75 | G5 = 0b101
76 | G6 = 0b110
77 |
78 |
79 | @add_lookup
80 | class PA_SELECT:
81 | RFO = 0
82 | PA_BOOST = 1
83 |
84 |
85 | @add_lookup
86 | class PA_RAMP:
87 | RAMP_3_4_ms = 0
88 | RAMP_2_ms = 1
89 | RAMP_1_ms = 2
90 | RAMP_500_us = 3
91 | RAMP_250_us = 4
92 | RAMP_125_us = 5
93 | RAMP_100_us = 6
94 | RAMP_62_us = 7
95 | RAMP_50_us = 8
96 | RAMP_40_us = 9
97 | RAMP_31_us = 10
98 | RAMP_25_us = 11
99 | RAMP_20_us = 12
100 | RAMP_15_us = 13
101 | RAMP_12_us = 14
102 | RAMP_10_us = 15
103 |
104 |
105 | class MASK:
106 | class IRQ_FLAGS:
107 | RxTimeout = 7
108 | RxDone = 6
109 | PayloadCrcError = 5
110 | ValidHeader = 4
111 | TxDone = 3
112 | CadDone = 2
113 | FhssChangeChannel = 1
114 | CadDetected = 0
115 |
116 |
117 | class REG:
118 |
119 | @add_lookup
120 | class LORA:
121 | FIFO = 0x00
122 | OP_MODE = 0x01
123 | FR_MSB = 0x06
124 | FR_MID = 0x07
125 | FR_LSB = 0x08
126 | PA_CONFIG = 0x09
127 | PA_RAMP = 0x0A
128 | OCP = 0x0B
129 | LNA = 0x0C
130 | FIFO_ADDR_PTR = 0x0D
131 | FIFO_TX_BASE_ADDR = 0x0E
132 | FIFO_RX_BASE_ADDR = 0x0F
133 | FIFO_RX_CURR_ADDR = 0x10
134 | IRQ_FLAGS_MASK = 0x11
135 | IRQ_FLAGS = 0x12
136 | RX_NB_BYTES = 0x13
137 | RX_HEADER_CNT_MSB = 0x14
138 | RX_PACKET_CNT_MSB = 0x16
139 | MODEM_STAT = 0x18
140 | PKT_SNR_VALUE = 0x19
141 | PKT_RSSI_VALUE = 0x1A
142 | RSSI_VALUE = 0x1B
143 | HOP_CHANNEL = 0x1C
144 | MODEM_CONFIG_1 = 0x1D
145 | MODEM_CONFIG_2 = 0x1E
146 | SYMB_TIMEOUT_LSB = 0x1F
147 | PREAMBLE_MSB = 0x20
148 | PAYLOAD_LENGTH = 0x22
149 | MAX_PAYLOAD_LENGTH = 0x23
150 | HOP_PERIOD = 0x24
151 | FIFO_RX_BYTE_ADDR = 0x25
152 | MODEM_CONFIG_3 = 0x26
153 | PPM_CORRECTION = 0x27
154 | FEI_MSB = 0x28
155 | DETECT_OPTIMIZE = 0X31
156 | INVERT_IQ = 0x33
157 | DETECTION_THRESH = 0X37
158 | SYNC_WORD = 0X39
159 | DIO_MAPPING_1 = 0x40
160 | DIO_MAPPING_2 = 0x41
161 | VERSION = 0x42
162 | TCXO = 0x4B
163 | PA_DAC = 0x4D
164 | AGC_REF = 0x61
165 | AGC_THRESH_1 = 0x62
166 | AGC_THRESH_2 = 0x63
167 | AGC_THRESH_3 = 0x64
168 | PLL = 0x70
169 |
170 | @add_lookup
171 | class FSK:
172 | LNA = 0x0C
173 | RX_CONFIG = 0x0D
174 | RSSI_CONFIG = 0x0E
175 | PREAMBLE_DETECT = 0x1F
176 | OSC = 0x24
177 | SYNC_CONFIG = 0x27
178 | SYNC_VALUE_1 = 0x28
179 | SYNC_VALUE_2 = 0x29
180 | SYNC_VALUE_3 = 0x2A
181 | SYNC_VALUE_4 = 0x2B
182 | SYNC_VALUE_5 = 0x2C
183 | SYNC_VALUE_6 = 0x2D
184 | SYNC_VALUE_7 = 0x2E
185 | SYNC_VALUE_8 = 0x2F
186 | PACKET_CONFIG_1 = 0x30
187 | FIFO_THRESH = 0x35
188 | IMAGE_CAL = 0x3B
189 | DIO_MAPPING_1 = 0x40
190 | DIO_MAPPING_2 = 0x41
191 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Overview
2 |
3 | This is a python interface to the [Semtech SX1276/7/8/9](http://www.semtech.com/wireless-rf/rf-transceivers/)
4 | long range, low power transceiver family.
5 |
6 | The SX127x have both LoRa and FSK capabilities. Here the focus lies on the
7 | LoRa spread spectrum modulation hence only the LoRa modem interface is implemented so far
8 | (but see the [roadmap](#roadmap) below for future plans).
9 |
10 | Spread spectrum modulation has a number of intriguing features:
11 | * High interference immunity
12 | * Up to 20dBm link budget advantage (for the SX1276/7/8/9)
13 | * High Doppler shift immunity
14 |
15 | More information about LoRa can be found on the [LoRa Alliance website](https://lora-alliance.org).
16 | Links to some LoRa performance reports can be found in the [references](#references) section below.
17 |
18 |
19 | # Motivation
20 |
21 | Transceiver modules are usually interfaced with microcontroller boards such as the
22 | Arduino and there are already many fine C/C++ libraries for the SX127x family available on
23 | [github](https://github.com/search?q=sx127x) and [mbed.org](https://developer.mbed.org/search/?q=sx127x).
24 |
25 | Although C/C++ is the de facto standard for development on microcontrollers, [python](https://www.python.org)
26 | running on a Raspberry Pi (NanoPi, BananaPi, UDOO Neo, BeagleBoard, etc. etc.) is becoming a viable alternative for rapid prototyping.
27 |
28 | High level programming languages like python require a full-blown OS such as Linux. (There are some exceptions like
29 | [MicroPython](https://micropython.org) and its fork [CircuitPython](https://www.adafruit.com/circuitpython).)
30 | But using hardware capable of running Linux contradicts, to some extent, the low power specification of the SX127x family.
31 | Therefore it is clear that this approach aims mostly at prototyping and technology testing.
32 |
33 | Prototyping on a full-blown OS using high level programming languages has several clear advantages:
34 | * Working prototypes can be built quickly
35 | * Technology testing ist faster
36 | * Proof of concept is easier to achieve
37 | * The application development phase is reached quicker
38 |
39 |
40 | # Hardware
41 |
42 | The transceiver module is a SX1276 based Modtronix [inAir9B](https://web.archive.org/web/20200926024317/https://modtronix.com/inair9.html).
43 | It is mounted on a prototyping board to a Raspberry Pi rev 2 model B.
44 |
45 | | Proto board pin | RaspPi GPIO | Direction |
46 | |:----------------|:-----------:|:---------:|
47 | | inAir9B DIO0 | GPIO 22 | IN |
48 | | inAir9B DIO1 | GPIO 23 | IN |
49 | | inAir9B DIO2 | GPIO 24 | IN |
50 | | inAir9B DIO3 | GPIO 25 | IN |
51 | | inAir9b Reset | GPIO ? | OUT |
52 | | LED | GPIO 18 | OUT |
53 | | Switch | GPIO 4 | IN |
54 |
55 | Todo:
56 | - [ ] Add picture(s)
57 | - [ ] Wire the SX127x reset to a GPIO?
58 |
59 |
60 | # Code Examples
61 |
62 | ### Overview
63 | First import the modules
64 | ```python
65 | from SX127x.LoRa import *
66 | from SX127x.board_config import BOARD
67 | ```
68 | then set up the board GPIOs
69 | ```python
70 | BOARD.setup()
71 | ```
72 | The LoRa object is instantiated and put into the standby mode
73 | ```python
74 | lora = LoRa()
75 | lora.set_mode(MODE.STDBY)
76 | ```
77 | Registers are queried like so:
78 | ```python
79 | print(lora.version()) # this prints the sx127x chip version
80 | print(lora.get_freq()) # this prints the frequency setting
81 | ```
82 | and setting registers is easy, too
83 | ```python
84 | lora.set_freq(433.0) # Set the frequency to 433 MHz
85 | ```
86 | In applications the `LoRa` class should be subclassed while overriding one or more of the callback functions that
87 | are invoked on successful RX or TX operations, for example.
88 | ```python
89 | class MyLoRa(LoRa):
90 |
91 | def __init__(self, verbose=False):
92 | super(MyLoRa, self).__init__(verbose)
93 | # setup registers etc.
94 |
95 | def on_rx_done(self):
96 | payload = self.read_payload(nocheck=True)
97 | # etc.
98 | ```
99 |
100 | In the end the resources should be freed properly
101 | ```python
102 | BOARD.teardown()
103 | ```
104 |
105 | ### More details
106 | Most functions of `SX127x.Lora` are setter and getter functions. For example, the setter and getter for
107 | the coding rate are demonstrated here
108 | ```python
109 | print(lora.get_coding_rate()) # print the current coding rate
110 | lora.set_coding_rate(CODING_RATE.CR4_6) # set it to CR4_6
111 | ```
112 |
113 | @todo
114 |
115 |
116 | # Installation
117 |
118 | Make sure SPI is activated on you RaspberryPi: [SPI](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#spi-overview)
119 | **pySX127x** requires these two python packages:
120 | * [RPi.GPIO](https://pypi.python.org/pypi/RPi.GPIO) for accessing the GPIOs, it should be already installed on
121 | a standard Raspian Linux image
122 | * [spidev](https://pypi.python.org/pypi/spidev) for controlling SPI
123 |
124 | In order to install spidev download the source code and run setup.py manually:
125 | ```bash
126 | wget https://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz
127 | tar xfvz spidev-3.1.tar.gz
128 | cd spidev-3.1
129 | sudo python setup.py install
130 | ```
131 |
132 | At this point you may want to confirm that the unit tests pass. See the section [Tests](#tests) below.
133 |
134 | You can now run the scripts. For example dump the registers with `lora_util.py`:
135 | ```bash
136 | rasp$ sudo ./lora_util.py
137 | SX127x LoRa registers:
138 | mode SLEEP
139 | freq 434.000000 MHz
140 | coding_rate CR4_5
141 | bw BW125
142 | spreading_factor 128 chips/symb
143 | implicit_hdr_mode OFF
144 | ... and so on ....
145 | ```
146 |
147 |
148 | # Class Reference
149 |
150 | The interface to the SX127x LoRa modem is implemented in the class `SX127x.LoRa.LoRa`.
151 | The most important modem configuration parameters are:
152 |
153 | | Function | Description |
154 | |------------------|---------------------------------------------|
155 | | set_mode | Change OpMode, use the constants.MODE class |
156 | | set_freq | Set the frequency |
157 | | set_bw | Set the bandwidth 7.8kHz ... 500kHz |
158 | | set_coding_rate | Set the coding rate 4/5, 4/6, 4/7, 4/8 |
159 | | | |
160 | | @todo | |
161 |
162 | Most set_* functions have a mirror get_* function, but beware that the getter return types do not necessarily match
163 | the setter input types.
164 |
165 | ### Register naming convention
166 | The register addresses are defined in class `SX127x.constants.REG` and we use a specific naming convention which
167 | is best illustrated by a few examples:
168 |
169 | | Register | Modem | Semtech doc. | pySX127x |
170 | |----------|-------|-------------------| ---------------------------|
171 | | 0x0E | LoRa | RegFifoTxBaseAddr | REG.LORA.FIFO_TX_BASE_ADDR |
172 | | 0x0E | FSK | RegRssiCOnfig | REG.FSK.RSSI_CONFIG |
173 | | 0x1D | LoRa | RegModemConfig1 | REG.LORA.MODEM_CONFIG_1 |
174 | | etc. | | | |
175 |
176 | ### Hardware
177 | Hardware related definition and initialisation are located in `SX127x.board_config.BOARD`.
178 | If you use a SBC other than the Raspberry Pi you'll have to adapt the BOARD class.
179 |
180 |
181 | # Script references
182 |
183 | ### Continuous receiver `rx_cont.py`
184 | The SX127x is put in RXCONT mode and continuously waits for transmissions. Upon a successful read the
185 | payload and the irq flags are printed to screen.
186 | ```
187 | usage: rx_cont.py [-h] [--ocp OCP] [--sf SF] [--freq FREQ] [--bw BW]
188 | [--cr CODING_RATE] [--preamble PREAMBLE]
189 |
190 | Continous LoRa receiver
191 |
192 | optional arguments:
193 | -h, --help show this help message and exit
194 | --ocp OCP, -c OCP Over current protection in mA (45 .. 240 mA)
195 | --sf SF, -s SF Spreading factor (6...12). Default is 7.
196 | --freq FREQ, -f FREQ Frequency
197 | --bw BW, -b BW Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25
198 | BW41_7 BW62_5 BW125 BW250 BW500). Default is BW125.
199 | --cr CODING_RATE, -r CODING_RATE
200 | Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8). Default
201 | is CR4_5.
202 | --preamble PREAMBLE, -p PREAMBLE
203 | Preamble length. Default is 8.
204 | ```
205 |
206 | ### Simple LoRa beacon `tx_beacon.py`
207 | A small payload is transmitted in regular intervals.
208 | ```
209 | usage: tx_beacon.py [-h] [--ocp OCP] [--sf SF] [--freq FREQ] [--bw BW]
210 | [--cr CODING_RATE] [--preamble PREAMBLE] [--single]
211 | [--wait WAIT]
212 |
213 | A simple LoRa beacon
214 |
215 | optional arguments:
216 | -h, --help show this help message and exit
217 | --ocp OCP, -c OCP Over current protection in mA (45 .. 240 mA)
218 | --sf SF, -s SF Spreading factor (6...12). Default is 7.
219 | --freq FREQ, -f FREQ Frequency
220 | --bw BW, -b BW Bandwidth (one of BW7_8 BW10_4 BW15_6 BW20_8 BW31_25
221 | BW41_7 BW62_5 BW125 BW250 BW500). Default is BW125.
222 | --cr CODING_RATE, -r CODING_RATE
223 | Coding rate (one of CR4_5 CR4_6 CR4_7 CR4_8). Default
224 | is CR4_5.
225 | --preamble PREAMBLE, -p PREAMBLE
226 | Preamble length. Default is 8.
227 | --single, -S Single transmission
228 | --wait WAIT, -w WAIT Waiting time between transmissions (default is 0s)
229 | ```
230 |
231 |
232 | # Tests
233 |
234 | Execute `test_lora.py` to run a few unit tests.
235 |
236 |
237 | # Contributors
238 |
239 | Please feel free to comment, report issues, or contribute!
240 |
241 | Contact me via my company website [Mayer Analytics](http://mayeranalytics.com) and my private blog
242 | [mcmayer.net](http://mcmayer.net).
243 |
244 | Follow me on twitter [@markuscmayer](https://twitter.com/markuscmayer) and
245 | [@mayeranalytics](https://twitter.com/mayeranalytics).
246 |
247 |
248 | # Roadmap
249 |
250 | 95% of functions for the Sx127x LoRa capabilities are implemented. Functions will be added when necessary.
251 | The test coverage is rather low but we intend to change that soon.
252 |
253 | ### Semtech SX1272/3 vs. SX1276/7/8/9
254 | **pySX127x** is not entirely compatible with the 1272.
255 | The 1276 and 1272 chips are different and the interfaces not 100% identical. For example registers 0x26/27.
256 | But the pySX127x library should get you pretty far if you use it with care. Here are the two datasheets:
257 | * [Semtech - SX1276/77/78/79 - 137 MHz to 1020 MHz Low Power Long Range Transceiver](https://www.semtech.com/products/wireless-rf/lora-connect)
258 | * [Semtech SX1272/73 - 860 MHz to 1020 MHz Low Power Long Range Transceiver](https://www.semtech.com/products/wireless-rf/lora-connect)
259 |
260 | ### HopeRF transceiver ICs ###
261 | HopeRF has a family of LoRa capable transceiver chips [RFM92/95/96/98](http://www.hoperf.com/)
262 | that have identical or almost identical SPI interface as the Semtech SX1276/7/8/9 family.
263 |
264 | ### Microchip transceiver IC ###
265 | Likewise Microchip has the chip [RN2483](https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/RN2483-Low-Power-Long-Range-LoRa-Technology-Transceiver-Module-DS50002346F.pdf)
266 |
267 | The [pySX127x](https://github.com/mayeranalytics/pySX127x) project will therefore be renamed to pyLoRa at some point.
268 |
269 | # LoRaWAN
270 | LoRaWAN is a LPWAN (low power WAN) and, and **pySX127x** has almost no relationship with LoRaWAN. Here we only deal with the interface into the chip(s) that enable the physical layer of LoRaWAN networks. If you need a LoRaWAN implementation have a look at [Jeroennijhof](https://github.com/jeroennijhof)s [LoRaWAN](https://github.com/jeroennijhof/LoRaWAN) which is based on pySX127x.
271 |
272 | By the way, LoRaWAN is what you need when you want to talk to the [TheThingsNetwork](https://www.thethingsnetwork.org/), a "global open LoRaWAN network". The site has a lot of information and links to products and projects.
273 |
274 |
275 | # References
276 |
277 | ### Hardware references
278 | * [Semtech SX1276/77/78/79 - 137 MHz to 1020 MHz Low Power Long Range Transceiver](https://semtech.my.salesforce.com/sfc/p/#E0000000JelG/a/2R0000001Rbr/6EfVZUorrpoKFfvaF_Fkpgp5kzjiNyiAbqcpqh9qSjE)
279 | * [Modtronix inAir9](http://modtronix.com/inair9.html)
280 | * [Spidev Documentation](https://github.com/doceme/py-spidev)
281 | * [Make: Tutorial: Raspberry Pi GPIO Pins and Python](http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/)
282 |
283 | ### LoRa performance tests
284 | * [Extreme Range Links: LoRa 868 / 900MHz SX1272 LoRa module for Arduino, Raspberry Pi and Intel Galileo](https://www.cooking-hacks.com/documentation/tutorials/extreme-range-lora-sx1272-module-shield-arduino-raspberry-pi-intel-galileo/)
285 | * [UK LoRa versus FSK - 40km LoS (Line of Sight) test!](http://www.instructables.com/id/Introducing-LoRa-/step17/Other-region-tests/)
286 | * [Andreas Spiess LoRaWAN World Record Attempt](https://www.youtube.com/watch?v=adhWIo-7gr4)
287 |
288 | ### Spread spectrum modulation theory
289 | * [An Introduction to Spread Spectrum Techniques](http://www.ausairpower.net/OSR-0597.html)
290 | * [Theory of Spread-Spectrum Communications-A Tutorial](http://www.fer.unizg.hr/_download/repository/Theory%20of%20Spread-Spectrum%20Communications-A%20Tutorial.pdf)
291 | (technical paper)
292 |
293 |
294 | # Copyright and License
295 |
296 | © 2015 Mayer Analytics Ltd., All Rights Reserved.
297 |
298 | ### Short version
299 | The license is [GNU AGPL](http://www.gnu.org/licenses/agpl-3.0.en.html).
300 |
301 | ### Long version
302 | pySX127x is free software: you can redistribute it and/or modify it under the terms of the
303 | GNU Affero General Public License as published by the Free Software Foundation,
304 | either version 3 of the License, or (at your option) any later version.
305 |
306 | pySX127x is distributed in the hope that it will be useful,
307 | but WITHOUT ANY WARRANTY; without even the implied warranty of
308 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
309 | See the GNU Affero General Public License for more details.
310 |
311 | You can be released from the requirements of the license by obtaining a commercial license.
312 | Such a license is mandatory as soon as you develop commercial activities involving
313 | pySX127x without disclosing the source code of your own applications, or shipping pySX127x with a closed source product.
314 |
315 | You should have received a copy of the GNU General Public License
316 | along with pySX127. If not, see .
317 |
318 | # Other legal boredom
319 | LoRa, LoRaWAN, LoRa Alliance are all trademarks by ... someone.
320 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published by
637 | the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
--------------------------------------------------------------------------------
/SX127x/LoRa.py:
--------------------------------------------------------------------------------
1 | """ Defines the SX127x class and a few utility functions. """
2 | # -*- coding: utf-8 -*-
3 |
4 | # Copyright 2015-2018 Mayer Analytics Ltd.
5 | #
6 | # This file is part of pySX127x.
7 | #
8 | # pySX127x is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
9 | # License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
10 | # version.
11 | #
12 | # pySX127x is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
14 | # details.
15 | #
16 | # You can be released from the requirements of the license by obtaining a commercial license. Such a license is
17 | # mandatory as soon as you develop commercial activities involving pySX127x without disclosing the source code of your
18 | # own applications, or shipping pySX127x with a closed source product.
19 | #
20 | # You should have received a copy of the GNU General Public License along with pySX127. If not, see
21 | # .
22 |
23 |
24 | import sys
25 | from .constants import *
26 | from .board_config import BOARD
27 |
28 |
29 | ################################################## Some utility functions ##############################################
30 |
31 | def set_bit(value, index, new_bit):
32 | """ Set the index'th bit of value to new_bit, and return the new value.
33 | :param value: The integer to set the new_bit in
34 | :type value: int
35 | :param index: 0-based index
36 | :param new_bit: New value the bit shall have (0 or 1)
37 | :return: Changed value
38 | :rtype: int
39 | """
40 | mask = 1 << index
41 | value &= ~mask
42 | if new_bit:
43 | value |= mask
44 | return value
45 |
46 |
47 | def getter(register_address):
48 | """ The getter decorator reads the register content and calls the decorated function to do
49 | post-processing.
50 | :param register_address: Register address
51 | :return: Register value
52 | :rtype: int
53 | """
54 | def decorator(func):
55 | def wrapper(self):
56 | return func(self, self.spi.xfer([register_address, 0])[1])
57 | return wrapper
58 | return decorator
59 |
60 |
61 | def setter(register_address):
62 | """ The setter decorator calls the decorated function for pre-processing and
63 | then writes the result to the register
64 | :param register_address: Register address
65 | :return: New register value
66 | :rtype: int
67 | """
68 | def decorator(func):
69 | def wrapper(self, val):
70 | return self.spi.xfer([register_address | 0x80, func(self, val)])[1]
71 | return wrapper
72 | return decorator
73 |
74 |
75 | ############################################### Definition of the LoRa class ###########################################
76 |
77 | class LoRa(object):
78 |
79 | spi = BOARD.SpiDev() # init and get the board's SPI
80 | mode = None # the mode is backed up here
81 | backup_registers = []
82 | verbose = True
83 | dio_mapping = [None] * 6 # store the dio mapping here
84 |
85 | def __init__(self, verbose=True, do_calibration=True, calibration_freq=868):
86 | """ Init the object
87 |
88 | Send the device to sleep, read all registers, and do the calibration (if do_calibration=True)
89 | :param verbose: Set the verbosity True/False
90 | :param calibration_freq: call rx_chain_calibration with this parameter. Default is 868
91 | :param do_calibration: Call rx_chain_calibration, default is True.
92 | """
93 | self.verbose = verbose
94 | # set the callbacks for DIO0..5 IRQs.
95 | BOARD.add_events(self._dio0, self._dio1, self._dio2, self._dio3, self._dio4, self._dio5)
96 | # set mode to sleep and read all registers
97 | self.set_mode(MODE.SLEEP)
98 | self.backup_registers = self.get_all_registers()
99 | # more setup work:
100 | if do_calibration:
101 | self.rx_chain_calibration(calibration_freq)
102 | # the FSK registers are set up exactly as modtronix do it:
103 | lookup_fsk = [
104 | #[REG.FSK.LNA , 0x23],
105 | #[REG.FSK.RX_CONFIG , 0x1E],
106 | #[REG.FSK.RSSI_CONFIG , 0xD2],
107 | #[REG.FSK.PREAMBLE_DETECT, 0xAA],
108 | #[REG.FSK.OSC , 0x07],
109 | #[REG.FSK.SYNC_CONFIG , 0x12],
110 | #[REG.FSK.SYNC_VALUE_1 , 0xC1],
111 | #[REG.FSK.SYNC_VALUE_2 , 0x94],
112 | #[REG.FSK.SYNC_VALUE_3 , 0xC1],
113 | #[REG.FSK.PACKET_CONFIG_1, 0xD8],
114 | #[REG.FSK.FIFO_THRESH , 0x8F],
115 | #[REG.FSK.IMAGE_CAL , 0x02],
116 | #[REG.FSK.DIO_MAPPING_1 , 0x00],
117 | #[REG.FSK.DIO_MAPPING_2 , 0x30]
118 | ]
119 | self.set_mode(MODE.FSK_STDBY)
120 | for register_address, value in lookup_fsk:
121 | self.set_register(register_address, value)
122 | self.set_mode(MODE.SLEEP)
123 | # set the dio_ mapping by calling the two get_dio_mapping_* functions
124 | self.get_dio_mapping_1()
125 | self.get_dio_mapping_2()
126 |
127 |
128 | # Overridable functions:
129 |
130 | def on_rx_done(self):
131 | pass
132 |
133 | def on_tx_done(self):
134 | pass
135 |
136 | def on_cad_done(self):
137 | pass
138 |
139 | def on_rx_timeout(self):
140 | pass
141 |
142 | def on_valid_header(self):
143 | pass
144 |
145 | def on_payload_crc_error(self):
146 | pass
147 |
148 | def on_fhss_change_channel(self):
149 | pass
150 |
151 | # Internal callbacks for add_events()
152 |
153 | def _dio0(self, channel):
154 | # DIO0 00: RxDone
155 | # DIO0 01: TxDone
156 | # DIO0 10: CadDone
157 | if self.dio_mapping[0] == 0:
158 | self.on_rx_done()
159 | elif self.dio_mapping[0] == 1:
160 | self.on_tx_done()
161 | elif self.dio_mapping[0] == 2:
162 | self.on_cad_done()
163 | else:
164 | raise RuntimeError("unknown dio0mapping!")
165 |
166 | def _dio1(self, channel):
167 | # DIO1 00: RxTimeout
168 | # DIO1 01: FhssChangeChannel
169 | # DIO1 10: CadDetected
170 | if self.dio_mapping[1] == 0:
171 | self.on_rx_timeout()
172 | elif self.dio_mapping[1] == 1:
173 | self.on_fhss_change_channel()
174 | elif self.dio_mapping[1] == 2:
175 | self.on_CadDetected()
176 | else:
177 | raise RuntimeError("unknown dio1mapping!")
178 |
179 | def _dio2(self, channel):
180 | # DIO2 00: FhssChangeChannel
181 | # DIO2 01: FhssChangeChannel
182 | # DIO2 10: FhssChangeChannel
183 | self.on_fhss_change_channel()
184 |
185 | def _dio3(self, channel):
186 | # DIO3 00: CadDone
187 | # DIO3 01: ValidHeader
188 | # DIO3 10: PayloadCrcError
189 | if self.dio_mapping[3] == 0:
190 | self.on_cad_done()
191 | elif self.dio_mapping[3] == 1:
192 | self.on_valid_header()
193 | elif self.dio_mapping[3] == 2:
194 | self.on_payload_crc_error()
195 | else:
196 | raise RuntimeError("unknown dio3 mapping!")
197 |
198 | def _dio4(self, channel):
199 | raise RuntimeError("DIO4 is not used")
200 |
201 | def _dio5(self, channel):
202 | raise RuntimeError("DIO5 is not used")
203 |
204 | # All the set/get/read/write functions
205 |
206 | def get_mode(self):
207 | """ Get the mode
208 | :return: New mode
209 | """
210 | self.mode = self.spi.xfer([REG.LORA.OP_MODE, 0])[1]
211 | return self.mode
212 |
213 | def set_mode(self, mode):
214 | """ Set the mode
215 | :param mode: Set the mode. Use constants.MODE class
216 | :return: New mode
217 | """
218 | # the mode is backed up in self.mode
219 | if mode == self.mode:
220 | return mode
221 | if self.verbose:
222 | sys.stderr.write("Mode <- %s\n" % MODE.lookup[mode])
223 | self.mode = mode
224 | return self.spi.xfer([REG.LORA.OP_MODE | 0x80, mode])[1]
225 |
226 | def write_payload(self, payload):
227 | """ Get FIFO ready for TX: Set FifoAddrPtr to FifoTxBaseAddr. The transceiver is put into STDBY mode.
228 | :param payload: Payload to write (list)
229 | :return: Written payload
230 | """
231 | payload_size = len(payload)
232 | self.set_payload_length(payload_size)
233 |
234 | self.set_mode(MODE.STDBY)
235 | base_addr = self.get_fifo_tx_base_addr()
236 | self.set_fifo_addr_ptr(base_addr)
237 | return self.spi.xfer([REG.LORA.FIFO | 0x80] + payload)[1:]
238 |
239 | def reset_ptr_rx(self):
240 | """ Get FIFO ready for RX: Set FifoAddrPtr to FifoRxBaseAddr. The transceiver is put into STDBY mode. """
241 | self.set_mode(MODE.STDBY)
242 | base_addr = self.get_fifo_rx_base_addr()
243 | self.set_fifo_addr_ptr(base_addr)
244 |
245 | def rx_is_good(self):
246 | """ Check the IRQ flags for RX errors
247 | :return: True if no errors
248 | :rtype: bool
249 | """
250 | flags = self.get_irq_flags()
251 | return not any([flags[s] for s in ['valid_header', 'crc_error', 'rx_done', 'rx_timeout']])
252 |
253 | def read_payload(self , nocheck = False):
254 | """ Read the payload from FIFO
255 | :param nocheck: If True then check rx_is_good()
256 | :return: Payload
257 | :rtype: list[int]
258 | """
259 | if not nocheck and not self.rx_is_good():
260 | return None
261 | rx_nb_bytes = self.get_rx_nb_bytes()
262 | fifo_rx_current_addr = self.get_fifo_rx_current_addr()
263 | self.set_fifo_addr_ptr(fifo_rx_current_addr)
264 | payload = self.spi.xfer([REG.LORA.FIFO] + [0] * rx_nb_bytes)[1:]
265 | return payload
266 |
267 | def get_freq(self):
268 | """ Get the frequency (MHz)
269 | :return: Frequency in MHz
270 | :rtype: float
271 | """
272 | msb, mid, lsb = self.spi.xfer([REG.LORA.FR_MSB, 0, 0, 0])[1:]
273 | f = lsb + 256*(mid + 256*msb)
274 | return f / 16384.
275 |
276 | def set_freq(self, f):
277 | """ Set the frequency (MHz)
278 | :param f: Frequency in MHz
279 | "type f: float
280 | :return: New register settings (3 bytes [msb, mid, lsb])
281 | :rtype: list[int]
282 | """
283 | assert self.mode == MODE.SLEEP or self.mode == MODE.STDBY or self.mode == MODE.FSK_STDBY
284 | i = int(f * 16384.) # choose floor
285 | msb = i // 65536
286 | i -= msb * 65536
287 | mid = i // 256
288 | i -= mid * 256
289 | lsb = i
290 | return self.spi.xfer([REG.LORA.FR_MSB | 0x80, msb, mid, lsb])
291 |
292 | def get_pa_config(self, convert_dBm=False):
293 | v = self.spi.xfer([REG.LORA.PA_CONFIG, 0])[1]
294 | pa_select = v >> 7
295 | max_power = v >> 4 & 0b111
296 | output_power = v & 0b1111
297 | if convert_dBm:
298 | max_power = max_power * .6 + 10.8
299 | if pa_select==0:
300 | output_power = max_power - (15 - output_power)
301 | else:
302 | output_power = 17 - (15 - output_power)
303 | return dict(
304 | pa_select = pa_select,
305 | max_power = max_power,
306 | output_power = output_power
307 | )
308 |
309 | def set_pa_config(self, pa_select=None, max_power=None, output_power=None):
310 | """ Configure the PA
311 | :param pa_select: Selects PA output pin, 0->RFO, 1->PA_BOOST
312 | :param max_power: Select max output power Pmax=10.8+0.6*MaxPower
313 | :param output_power: Output power Pout=Pmax-(15-OutputPower) if PaSelect = 0,
314 | Pout=17-(15-OutputPower) if PaSelect = 1 (PA_BOOST pin)
315 | :return: new register value
316 | """
317 | loc = locals()
318 | current = self.get_pa_config()
319 | loc = {s: current[s] if loc[s] is None else loc[s] for s in loc}
320 | val = (loc['pa_select'] << 7) | (loc['max_power'] << 4) | (loc['output_power'])
321 | return self.spi.xfer([REG.LORA.PA_CONFIG | 0x80, val])[1]
322 |
323 | @getter(REG.LORA.PA_RAMP)
324 | def get_pa_ramp(self, val):
325 | return val & 0b1111
326 |
327 | @setter(REG.LORA.PA_RAMP)
328 | def set_pa_ramp(self, val):
329 | return val & 0b1111
330 |
331 | def get_ocp(self, convert_mA=False):
332 | v = self.spi.xfer([REG.LORA.OCP, 0])[1]
333 | ocp_on = v >> 5 & 0x01
334 | ocp_trim = v & 0b11111
335 | if convert_mA:
336 | if ocp_trim <= 15:
337 | ocp_trim = 45. + 5. * ocp_trim
338 | elif ocp_trim <= 27:
339 | ocp_trim = -30. + 10. * ocp_trim
340 | else:
341 | assert ocp_trim <= 27
342 | return dict(
343 | ocp_on = ocp_on,
344 | ocp_trim = ocp_trim
345 | )
346 |
347 | def set_ocp_trim(self, I_mA):
348 | assert(I_mA >= 45 and I_mA <= 240)
349 | ocp_on = self.spi.xfer([REG.LORA.OCP, 0])[1] >> 5 & 0x01
350 | if I_mA <= 120:
351 | v = int(round((I_mA-45.)/5.))
352 | else:
353 | v = int(round((I_mA+30.)/10.))
354 | v = set_bit(v, 5, ocp_on)
355 | return self.spi.xfer([REG.LORA.OCP | 0x80, v])[1]
356 |
357 | def get_lna(self):
358 | v = self.spi.xfer([REG.LORA.LNA, 0])[1]
359 | return dict(
360 | lna_gain = v >> 5,
361 | lna_boost_lf = v >> 3 & 0b11,
362 | lna_boost_hf = v & 0b11
363 | )
364 |
365 | def set_lna(self, lna_gain=None, lna_boost_lf=None, lna_boost_hf=None):
366 | assert lna_boost_hf is None or lna_boost_hf == 0b00 or lna_boost_hf == 0b11
367 | self.set_mode(MODE.STDBY)
368 | if lna_gain is not None:
369 | # Apparently agc_auto_on must be 0 in order to set lna_gain
370 | self.set_agc_auto_on(lna_gain == GAIN.NOT_USED)
371 | loc = locals()
372 | current = self.get_lna()
373 | loc = {s: current[s] if loc[s] is None else loc[s] for s in loc}
374 | val = (loc['lna_gain'] << 5) | (loc['lna_boost_lf'] << 3) | (loc['lna_boost_hf'])
375 | retval = self.spi.xfer([REG.LORA.LNA | 0x80, val])[1]
376 | if lna_gain is not None:
377 | # agc_auto_on must track lna_gain: GAIN=NOT_USED -> agc_auto=ON, otherwise =OFF
378 | self.set_agc_auto_on(lna_gain == GAIN.NOT_USED)
379 | return retval
380 |
381 | def set_lna_gain(self, lna_gain):
382 | self.set_lna(lna_gain=lna_gain)
383 |
384 | def get_fifo_addr_ptr(self):
385 | return self.spi.xfer([REG.LORA.FIFO_ADDR_PTR, 0])[1]
386 |
387 | def set_fifo_addr_ptr(self, ptr):
388 | return self.spi.xfer([REG.LORA.FIFO_ADDR_PTR | 0x80, ptr])[1]
389 |
390 | def get_fifo_tx_base_addr(self):
391 | return self.spi.xfer([REG.LORA.FIFO_TX_BASE_ADDR, 0])[1]
392 |
393 | def set_fifo_tx_base_addr(self, ptr):
394 | return self.spi.xfer([REG.LORA.FIFO_TX_BASE_ADDR | 0x80, ptr])[1]
395 |
396 | def get_fifo_rx_base_addr(self):
397 | return self.spi.xfer([REG.LORA.FIFO_RX_BASE_ADDR, 0])[1]
398 |
399 | def set_fifo_rx_base_addr(self, ptr):
400 | return self.spi.xfer([REG.LORA.FIFO_RX_BASE_ADDR | 0x80, ptr])[1]
401 |
402 | def get_fifo_rx_current_addr(self):
403 | return self.spi.xfer([REG.LORA.FIFO_RX_CURR_ADDR, 0])[1]
404 |
405 | def get_fifo_rx_byte_addr(self):
406 | return self.spi.xfer([REG.LORA.FIFO_RX_BYTE_ADDR, 0])[1]
407 |
408 | def get_irq_flags_mask(self):
409 | v = self.spi.xfer([REG.LORA.IRQ_FLAGS_MASK, 0])[1]
410 | return dict(
411 | rx_timeout = v >> 7 & 0x01,
412 | rx_done = v >> 6 & 0x01,
413 | crc_error = v >> 5 & 0x01,
414 | valid_header = v >> 4 & 0x01,
415 | tx_done = v >> 3 & 0x01,
416 | cad_done = v >> 2 & 0x01,
417 | fhss_change_ch = v >> 1 & 0x01,
418 | cad_detected = v >> 0 & 0x01,
419 | )
420 |
421 | def set_irq_flags_mask(self,
422 | rx_timeout=None, rx_done=None, crc_error=None, valid_header=None, tx_done=None,
423 | cad_done=None, fhss_change_ch=None, cad_detected=None):
424 | loc = locals()
425 | v = self.spi.xfer([REG.LORA.IRQ_FLAGS_MASK, 0])[1]
426 | for i, s in enumerate(['cad_detected', 'fhss_change_ch', 'cad_done', 'tx_done', 'valid_header',
427 | 'crc_error', 'rx_done', 'rx_timeout']):
428 | this_bit = locals()[s]
429 | if this_bit is not None:
430 | v = set_bit(v, i, this_bit)
431 | return self.spi.xfer([REG.LORA.IRQ_FLAGS_MASK | 0x80, v])[1]
432 |
433 | def get_irq_flags(self):
434 | v = self.spi.xfer([REG.LORA.IRQ_FLAGS, 0])[1]
435 | return dict(
436 | rx_timeout = v >> 7 & 0x01,
437 | rx_done = v >> 6 & 0x01,
438 | crc_error = v >> 5 & 0x01,
439 | valid_header = v >> 4 & 0x01,
440 | tx_done = v >> 3 & 0x01,
441 | cad_done = v >> 2 & 0x01,
442 | fhss_change_ch = v >> 1 & 0x01,
443 | cad_detected = v >> 0 & 0x01,
444 | )
445 |
446 | def set_irq_flags(self,
447 | rx_timeout=None, rx_done=None, crc_error=None, valid_header=None, tx_done=None,
448 | cad_done=None, fhss_change_ch=None, cad_detected=None):
449 | v = self.spi.xfer([REG.LORA.IRQ_FLAGS, 0])[1]
450 | for i, s in enumerate(['cad_detected', 'fhss_change_ch', 'cad_done', 'tx_done', 'valid_header',
451 | 'crc_error', 'rx_done', 'rx_timeout']):
452 | this_bit = locals()[s]
453 | if this_bit is not None:
454 | v = set_bit(v, i, this_bit)
455 | return self.spi.xfer([REG.LORA.IRQ_FLAGS | 0x80, v])[1]
456 |
457 | def clear_irq_flags(self,
458 | RxTimeout=None, RxDone=None, PayloadCrcError=None,
459 | ValidHeader=None, TxDone=None, CadDone=None,
460 | FhssChangeChannel=None, CadDetected=None):
461 | v = 0
462 | for i, s in enumerate(['CadDetected', 'FhssChangeChannel', 'CadDone',
463 | 'TxDone', 'ValidHeader', 'PayloadCrcError',
464 | 'RxDone', 'RxTimeout']):
465 | this_bit = locals()[s]
466 | if this_bit is not None:
467 | v = set_bit(v, eval('MASK.IRQ_FLAGS.' + s), this_bit)
468 | return self.spi.xfer([REG.LORA.IRQ_FLAGS | 0x80, v])[1]
469 |
470 |
471 | def get_rx_nb_bytes(self):
472 | return self.spi.xfer([REG.LORA.RX_NB_BYTES, 0])[1]
473 |
474 | def get_rx_header_cnt(self):
475 | msb, lsb = self.spi.xfer([REG.LORA.RX_HEADER_CNT_MSB, 0, 0])[1:]
476 | return lsb + 256 * msb
477 |
478 | def get_rx_packet_cnt(self):
479 | msb, lsb = self.spi.xfer([REG.LORA.RX_PACKET_CNT_MSB, 0, 0])[1:]
480 | return lsb + 256 * msb
481 |
482 | def get_modem_status(self):
483 | status = self.spi.xfer([REG.LORA.MODEM_STAT, 0])[1]
484 | return dict(
485 | rx_coding_rate = status >> 5 & 0x03,
486 | modem_clear = status >> 4 & 0x01,
487 | header_info_valid = status >> 3 & 0x01,
488 | rx_ongoing = status >> 2 & 0x01,
489 | signal_sync = status >> 1 & 0x01,
490 | signal_detected = status >> 0 & 0x01
491 | )
492 |
493 | def get_pkt_snr_value(self):
494 | v = self.spi.xfer([REG.LORA.PKT_SNR_VALUE, 0])[1]
495 | return (float(v-256) if v > 127 else float(v)) / 4.
496 |
497 | def get_pkt_rssi_value(self):
498 | v = self.spi.xfer([REG.LORA.PKT_RSSI_VALUE, 0])[1]
499 | return v - (164 if BOARD.low_band else 157) # See datasheet 5.5.5. p. 87
500 |
501 | def get_rssi_value(self):
502 | v = self.spi.xfer([REG.LORA.RSSI_VALUE, 0])[1]
503 | return v - (164 if BOARD.low_band else 157) # See datasheet 5.5.5. p. 87
504 |
505 | def get_hop_channel(self):
506 | v = self.spi.xfer([REG.LORA.HOP_CHANNEL, 0])[1]
507 | return dict(
508 | pll_timeout = v >> 7,
509 | crc_on_payload = v >> 6 & 0x01,
510 | fhss_present_channel = v >> 5 & 0b111111
511 | )
512 |
513 | def get_modem_config_1(self):
514 | val = self.spi.xfer([REG.LORA.MODEM_CONFIG_1, 0])[1]
515 | return dict(
516 | bw = val >> 4 & 0x0F,
517 | coding_rate = val >> 1 & 0x07,
518 | implicit_header_mode = val & 0x01
519 | )
520 |
521 | def set_modem_config_1(self, bw=None, coding_rate=None, implicit_header_mode=None):
522 | loc = locals()
523 | current = self.get_modem_config_1()
524 | loc = {s: current[s] if loc[s] is None else loc[s] for s in loc}
525 | val = loc['implicit_header_mode'] | (loc['coding_rate'] << 1) | (loc['bw'] << 4)
526 | return self.spi.xfer([REG.LORA.MODEM_CONFIG_1 | 0x80, val])[1]
527 |
528 | def set_bw(self, bw):
529 | """ Set the bandwidth 0=7.8kHz ... 9=500kHz
530 | :param bw: A number 0,2,3,...,9
531 | :return:
532 | """
533 | self.set_modem_config_1(bw=bw)
534 |
535 | def set_coding_rate(self, coding_rate):
536 | """ Set the coding rate 4/5, 4/6, 4/7, 4/8
537 | :param coding_rate: A number 1,2,3,4
538 | :return: New register value
539 | """
540 | self.set_modem_config_1(coding_rate=coding_rate)
541 |
542 | def set_implicit_header_mode(self, implicit_header_mode):
543 | self.set_modem_config_1(implicit_header_mode=implicit_header_mode)
544 |
545 | def get_modem_config_2(self, include_symb_timout_lsb=False):
546 | val = self.spi.xfer([REG.LORA.MODEM_CONFIG_2, 0])[1]
547 | d = dict(
548 | spreading_factor = val >> 4 & 0x0F,
549 | tx_cont_mode = val >> 3 & 0x01,
550 | rx_crc = val >> 2 & 0x01,
551 | )
552 | if include_symb_timout_lsb:
553 | d['symb_timout_lsb'] = val & 0x03
554 | return d
555 |
556 | def set_modem_config_2(self, spreading_factor=None, tx_cont_mode=None, rx_crc=None):
557 | loc = locals()
558 | # RegModemConfig2 contains the SymbTimout MSB bits. We tack the back on when writing this register.
559 | current = self.get_modem_config_2(include_symb_timout_lsb=True)
560 | loc = {s: current[s] if loc[s] is None else loc[s] for s in loc}
561 | val = (loc['spreading_factor'] << 4) | (loc['tx_cont_mode'] << 3) | (loc['rx_crc'] << 2) | current['symb_timout_lsb']
562 | return self.spi.xfer([REG.LORA.MODEM_CONFIG_2 | 0x80, val])[1]
563 |
564 | def set_spreading_factor(self, spreading_factor):
565 | self.set_modem_config_2(spreading_factor=spreading_factor)
566 |
567 | def set_rx_crc(self, rx_crc):
568 | self.set_modem_config_2(rx_crc=rx_crc)
569 |
570 | def get_modem_config_3(self):
571 | val = self.spi.xfer([REG.LORA.MODEM_CONFIG_3, 0])[1]
572 | return dict(
573 | low_data_rate_optim = val >> 3 & 0x01,
574 | agc_auto_on = val >> 2 & 0x01
575 | )
576 |
577 | def set_modem_config_3(self, low_data_rate_optim=None, agc_auto_on=None):
578 | loc = locals()
579 | current = self.get_modem_config_3()
580 | loc = {s: current[s] if loc[s] is None else loc[s] for s in loc}
581 | val = (loc['low_data_rate_optim'] << 3) | (loc['agc_auto_on'] << 2)
582 | return self.spi.xfer([REG.LORA.MODEM_CONFIG_3 | 0x80, val])[1]
583 |
584 | @setter(REG.LORA.INVERT_IQ)
585 | def set_invert_iq(self, invert):
586 | """ Invert the LoRa I and Q signals
587 | :param invert: 0: normal mode, 1: I and Q inverted
588 | :return: New value of register
589 | """
590 | return 0x27 | (invert & 0x01) << 6
591 |
592 | @getter(REG.LORA.INVERT_IQ)
593 | def get_invert_iq(self, val):
594 | """ Get the invert the I and Q setting
595 | :return: 0: normal mode, 1: I and Q inverted
596 | """
597 | return (val >> 6) & 0x01
598 |
599 | def get_agc_auto_on(self):
600 | return self.get_modem_config_3()['agc_auto_on']
601 |
602 | def set_agc_auto_on(self, agc_auto_on):
603 | self.set_modem_config_3(agc_auto_on=agc_auto_on)
604 |
605 | def get_low_data_rate_optim(self):
606 | return self.set_modem_config_3()['low_data_rate_optim']
607 |
608 | def set_low_data_rate_optim(self, low_data_rate_optim):
609 | self.set_modem_config_3(low_data_rate_optim=low_data_rate_optim)
610 |
611 | def get_symb_timeout(self):
612 | SYMB_TIMEOUT_MSB = REG.LORA.MODEM_CONFIG_2
613 | msb, lsb = self.spi.xfer([SYMB_TIMEOUT_MSB, 0, 0])[1:] # the MSB bits are stored in REG.LORA.MODEM_CONFIG_2
614 | msb = msb & 0b11
615 | return lsb + 256 * msb
616 |
617 | def set_symb_timeout(self, timeout):
618 | bkup_reg_modem_config_2 = self.spi.xfer([REG.LORA.MODEM_CONFIG_2, 0])[1]
619 | msb = timeout >> 8 & 0b11 # bits 8-9
620 | lsb = timeout - 256 * msb # bits 0-7
621 | reg_modem_config_2 = bkup_reg_modem_config_2 & 0xFC | msb # bits 2-7 of bkup_reg_modem_config_2 ORed with the two msb bits
622 | old_msb = self.spi.xfer([REG.LORA.MODEM_CONFIG_2 | 0x80, reg_modem_config_2])[1] & 0x03
623 | old_lsb = self.spi.xfer([REG.LORA.SYMB_TIMEOUT_LSB | 0x80, lsb])[1]
624 | return old_lsb + 256 * old_msb
625 |
626 | def get_preamble(self):
627 | msb, lsb = self.spi.xfer([REG.LORA.PREAMBLE_MSB, 0, 0])[1:]
628 | return lsb + 256 * msb
629 |
630 | def set_preamble(self, preamble):
631 | msb = preamble >> 8
632 | lsb = preamble - msb * 256
633 | old_msb, old_lsb = self.spi.xfer([REG.LORA.PREAMBLE_MSB | 0x80, msb, lsb])[1:]
634 | return old_lsb + 256 * old_msb
635 |
636 | @getter(REG.LORA.PAYLOAD_LENGTH)
637 | def get_payload_length(self, val):
638 | return val
639 |
640 | @setter(REG.LORA.PAYLOAD_LENGTH)
641 | def set_payload_length(self, payload_length):
642 | return payload_length
643 |
644 | @getter(REG.LORA.MAX_PAYLOAD_LENGTH)
645 | def get_max_payload_length(self, val):
646 | return val
647 |
648 | @setter(REG.LORA.MAX_PAYLOAD_LENGTH)
649 | def set_max_payload_length(self, max_payload_length):
650 | return max_payload_length
651 |
652 | @getter(REG.LORA.HOP_PERIOD)
653 | def get_hop_period(self, val):
654 | return val
655 |
656 | @setter(REG.LORA.HOP_PERIOD)
657 | def set_hop_period(self, hop_period):
658 | return hop_period
659 |
660 | def get_fei(self):
661 | msb, mid, lsb = self.spi.xfer([REG.LORA.FEI_MSB, 0, 0, 0])[1:]
662 | msb &= 0x0F
663 | freq_error = lsb + 256 * (mid + 256 * msb)
664 | return freq_error
665 |
666 | @getter(REG.LORA.DETECT_OPTIMIZE)
667 | def get_detect_optimize(self, val):
668 | """ Get LoRa detection optimize setting
669 | :return: detection optimize setting 0x03: SF7-12, 0x05: SF6
670 |
671 | """
672 | return val & 0b111
673 |
674 | @setter(REG.LORA.DETECT_OPTIMIZE)
675 | def set_detect_optimize(self, detect_optimize):
676 | """ Set LoRa detection optimize
677 | :param detect_optimize 0x03: SF7-12, 0x05: SF6
678 | :return: New register value
679 | """
680 | assert detect_optimize == 0x03 or detect_optimize == 0x05
681 | return detect_optimize & 0b111
682 |
683 | @getter(REG.LORA.DETECTION_THRESH)
684 | def get_detection_threshold(self, val):
685 | """ Get LoRa detection threshold setting
686 | :return: detection threshold 0x0A: SF7-12, 0x0C: SF6
687 |
688 | """
689 | return val
690 |
691 | @setter(REG.LORA.DETECTION_THRESH)
692 | def set_detection_threshold(self, detect_threshold):
693 | """ Set LoRa detection optimize
694 | :param detect_threshold 0x0A: SF7-12, 0x0C: SF6
695 | :return: New register value
696 | """
697 | assert detect_threshold == 0x0A or detect_threshold == 0x0C
698 | return detect_threshold
699 |
700 | @getter(REG.LORA.SYNC_WORD)
701 | def get_sync_word(self, sync_word):
702 | return sync_word
703 |
704 | @setter(REG.LORA.SYNC_WORD)
705 | def set_sync_word(self, sync_word):
706 | return sync_word
707 |
708 | @getter(REG.LORA.DIO_MAPPING_1)
709 | def get_dio_mapping_1(self, mapping):
710 | """ Get mapping of pins DIO0 to DIO3. Object variable dio_mapping will be set.
711 | :param mapping: Register value
712 | :type mapping: int
713 | :return: Value of the mapping list
714 | :rtype: list[int]
715 | """
716 | self.dio_mapping = [mapping>>6 & 0x03, mapping>>4 & 0x03, mapping>>2 & 0x03, mapping>>0 & 0x03] \
717 | + self.dio_mapping[4:6]
718 | return self.dio_mapping
719 |
720 | @setter(REG.LORA.DIO_MAPPING_1)
721 | def set_dio_mapping_1(self, mapping):
722 | """ Set mapping of pins DIO0 to DIO3. Object variable dio_mapping will be set.
723 | :param mapping: Register value
724 | :type mapping: int
725 | :return: New value of the register
726 | :rtype: int
727 | """
728 | self.dio_mapping = [mapping>>6 & 0x03, mapping>>4 & 0x03, mapping>>2 & 0x03, mapping>>0 & 0x03] \
729 | + self.dio_mapping[4:6]
730 | return mapping
731 |
732 | @getter(REG.LORA.DIO_MAPPING_2)
733 | def get_dio_mapping_2(self, mapping):
734 | """ Get mapping of pins DIO4 to DIO5. Object variable dio_mapping will be set.
735 | :param mapping: Register value
736 | :type mapping: int
737 | :return: Value of the mapping list
738 | :rtype: list[int]
739 | """
740 | self.dio_mapping = self.dio_mapping[0:4] + [mapping>>6 & 0x03, mapping>>4 & 0x03]
741 | return self.dio_mapping
742 |
743 | @setter(REG.LORA.DIO_MAPPING_2)
744 | def set_dio_mapping_2(self, mapping):
745 | """ Set mapping of pins DIO4 to DIO5. Object variable dio_mapping will be set.
746 | :param mapping: Register value
747 | :type mapping: int
748 | :return: New value of the register
749 | :rtype: int
750 | """
751 | assert mapping & 0b00001110 == 0
752 | self.dio_mapping = self.dio_mapping[0:4] + [mapping>>6 & 0x03, mapping>>4 & 0x03]
753 | return mapping
754 |
755 | def get_dio_mapping(self):
756 | """ Utility function that returns the list of current DIO mappings. Object variable dio_mapping will be set.
757 | :return: List of current DIO mappings
758 | :rtype: list[int]
759 | """
760 | self.get_dio_mapping_1()
761 | return self.get_dio_mapping_2()
762 |
763 | def set_dio_mapping(self, mapping):
764 | """ Utility function that returns the list of current DIO mappings. Object variable dio_mapping will be set.
765 | :param mapping: DIO mapping list
766 | :type mapping: list[int]
767 | :return: New DIO mapping list
768 | :rtype: list[int]
769 | """
770 | mapping_1 = (mapping[0] & 0x03) << 6 | (mapping[1] & 0x03) << 4 | (mapping[2] & 0x3) << 2 | mapping[3] & 0x3
771 | mapping_2 = (mapping[4] & 0x03) << 6 | (mapping[5] & 0x03) << 4
772 | self.set_dio_mapping_1(mapping_1)
773 | return self.set_dio_mapping_2(mapping_2)
774 |
775 | @getter(REG.LORA.VERSION)
776 | def get_version(self, version):
777 | """ Version code of the chip.
778 | Bits 7-4 give the full revision number; bits 3-0 give the metal mask revision number.
779 | :return: Version code
780 | :rtype: int
781 | """
782 | return version
783 |
784 | @getter(REG.LORA.TCXO)
785 | def get_tcxo(self, tcxo):
786 | """ Get TCXO or XTAL input setting
787 | 0 -> "XTAL": Crystal Oscillator with external Crystal
788 | 1 -> "TCXO": External clipped sine TCXO AC-connected to XTA pin
789 | :param tcxo: 1=TCXO or 0=XTAL input setting
790 | :return: TCXO or XTAL input setting
791 | :type: int (0 or 1)
792 | """
793 | return tcxo & 0b00010000
794 |
795 | @setter(REG.LORA.TCXO)
796 | def set_tcxo(self, tcxo):
797 | """ Make TCXO or XTAL input setting.
798 | 0 -> "XTAL": Crystal Oscillator with external Crystal
799 | 1 -> "TCXO": External clipped sine TCXO AC-connected to XTA pin
800 | :param tcxo: 1=TCXO or 0=XTAL input setting
801 | :return: new TCXO or XTAL input setting
802 | """
803 | return (tcxo >= 1) << 4 | 0x09 # bits 0-3 must be 0b1001
804 |
805 | @getter(REG.LORA.PA_DAC)
806 | def get_pa_dac(self, pa_dac):
807 | """ Enables the +20dBm option on PA_BOOST pin
808 | False -> Default value
809 | True -> +20dBm on PA_BOOST when OutputPower=1111
810 | :return: True/False if +20dBm option on PA_BOOST on/off
811 | :rtype: bool
812 | """
813 | pa_dac &= 0x07 # only bits 0-2
814 | if pa_dac == 0x04:
815 | return False
816 | elif pa_dac == 0x07:
817 | return True
818 | else:
819 | raise RuntimeError("Bad PA_DAC value %s" % hex(pa_dac))
820 |
821 | @setter(REG.LORA.PA_DAC)
822 | def set_pa_dac(self, pa_dac):
823 | """ Enables the +20dBm option on PA_BOOST pin
824 | False -> Default value
825 | True -> +20dBm on PA_BOOST when OutputPower=1111
826 | :param pa_dac: 1/0 if +20dBm option on PA_BOOST on/off
827 | :return: New pa_dac register value
828 | :rtype: int
829 | """
830 | return 0x87 if pa_dac else 0x84
831 |
832 | def rx_chain_calibration(self, freq=868.):
833 | """ Run the image calibration (see Semtech documentation section 4.2.3.8)
834 | :param freq: Frequency for the HF calibration
835 | :return: None
836 | """
837 | # backup some registers
838 | op_mode_bkup = self.get_mode()
839 | pa_config_bkup = self.get_register(REG.LORA.PA_CONFIG)
840 | freq_bkup = self.get_freq()
841 | # for image calibration device must be in FSK standby mode
842 | self.set_mode(MODE.FSK_STDBY)
843 | # cut the PA
844 | self.set_register(REG.LORA.PA_CONFIG, 0x00)
845 | # calibration for the LF band
846 | image_cal = (self.get_register(REG.FSK.IMAGE_CAL) & 0xBF) | 0x40
847 | self.set_register(REG.FSK.IMAGE_CAL, image_cal)
848 | while (self.get_register(REG.FSK.IMAGE_CAL) & 0x20) == 0x20:
849 | pass
850 | # Set a Frequency in HF band
851 | self.set_freq(freq)
852 | # calibration for the HF band
853 | image_cal = (self.get_register(REG.FSK.IMAGE_CAL) & 0xBF) | 0x40
854 | self.set_register(REG.FSK.IMAGE_CAL, image_cal)
855 | while (self.get_register(REG.FSK.IMAGE_CAL) & 0x20) == 0x20:
856 | pass
857 | # put back the saved parameters
858 | self.set_mode(op_mode_bkup)
859 | self.set_register(REG.LORA.PA_CONFIG, pa_config_bkup)
860 | self.set_freq(freq_bkup)
861 |
862 | def dump_registers(self):
863 | """ Returns a list of [reg_addr, reg_name, reg_value] tuples. Chip is put into mode SLEEP.
864 | :return: List of [reg_addr, reg_name, reg_value] tuples
865 | :rtype: list[tuple]
866 | """
867 | self.set_mode(MODE.SLEEP)
868 | values = self.get_all_registers()
869 | skip_set = set([REG.LORA.FIFO])
870 | result_list = []
871 | for i, s in REG.LORA.lookup.iteritems():
872 | if i in skip_set:
873 | continue
874 | v = values[i]
875 | result_list.append((i, s, v))
876 | return result_list
877 |
878 | def get_register(self, register_address):
879 | return self.spi.xfer([register_address & 0x7F, 0])[1]
880 |
881 | def set_register(self, register_address, val):
882 | return self.spi.xfer([register_address | 0x80, val])[1]
883 |
884 | def get_all_registers(self):
885 | # read all registers
886 | reg = [0] + self.spi.xfer([1]+[0]*0x3E)[1:]
887 | self.mode = reg[1]
888 | return reg
889 |
890 | def __del__(self):
891 | self.set_mode(MODE.SLEEP)
892 | if self.verbose:
893 | sys.stderr.write("MODE=SLEEP\n")
894 |
895 | def __str__(self):
896 | # don't use __str__ while in any mode other that SLEEP or STDBY
897 | assert(self.mode == MODE.SLEEP or self.mode == MODE.STDBY)
898 |
899 | onoff = lambda i: 'ON' if i else 'OFF'
900 | f = self.get_freq()
901 | cfg1 = self.get_modem_config_1()
902 | cfg2 = self.get_modem_config_2()
903 | cfg3 = self.get_modem_config_3()
904 | pa_config = self.get_pa_config(convert_dBm=True)
905 | ocp = self.get_ocp(convert_mA=True)
906 | lna = self.get_lna()
907 | s = "SX127x LoRa registers:\n"
908 | s += " mode %s\n" % MODE.lookup[self.get_mode()]
909 | s += " freq %f MHz\n" % f
910 | s += " coding_rate %s\n" % CODING_RATE.lookup[cfg1['coding_rate']]
911 | s += " bw %s\n" % BW.lookup[cfg1['bw']]
912 | s += " spreading_factor %s chips/symb\n" % (1 << cfg2['spreading_factor'])
913 | s += " implicit_hdr_mode %s\n" % onoff(cfg1['implicit_header_mode'])
914 | s += " rx_payload_crc %s\n" % onoff(cfg2['rx_crc'])
915 | s += " tx_cont_mode %s\n" % onoff(cfg2['tx_cont_mode'])
916 | s += " preamble %d\n" % self.get_preamble()
917 | s += " low_data_rate_opti %s\n" % onoff(cfg3['low_data_rate_optim'])
918 | s += " agc_auto_on %s\n" % onoff(cfg3['agc_auto_on'])
919 | s += " symb_timeout %s\n" % self.get_symb_timeout()
920 | s += " freq_hop_period %s\n" % self.get_hop_period()
921 | s += " hop_channel %s\n" % self.get_hop_channel()
922 | s += " payload_length %s\n" % self.get_payload_length()
923 | s += " max_payload_length %s\n" % self.get_max_payload_length()
924 | s += " irq_flags_mask %s\n" % self.get_irq_flags_mask()
925 | s += " irq_flags %s\n" % self.get_irq_flags()
926 | s += " rx_nb_byte %d\n" % self.get_rx_nb_bytes()
927 | s += " rx_header_cnt %d\n" % self.get_rx_header_cnt()
928 | s += " rx_packet_cnt %d\n" % self.get_rx_packet_cnt()
929 | s += " pkt_snr_value %f\n" % self.get_pkt_snr_value()
930 | s += " pkt_rssi_value %d\n" % self.get_pkt_rssi_value()
931 | s += " rssi_value %d\n" % self.get_rssi_value()
932 | s += " fei %d\n" % self.get_fei()
933 | s += " pa_select %s\n" % PA_SELECT.lookup[pa_config['pa_select']]
934 | s += " max_power %f dBm\n" % pa_config['max_power']
935 | s += " output_power %f dBm\n" % pa_config['output_power']
936 | s += " ocp %s\n" % onoff(ocp['ocp_on'])
937 | s += " ocp_trim %f mA\n" % ocp['ocp_trim']
938 | s += " lna_gain %s\n" % GAIN.lookup[lna['lna_gain']]
939 | s += " lna_boost_lf %s\n" % bin(lna['lna_boost_lf'])
940 | s += " lna_boost_hf %s\n" % bin(lna['lna_boost_hf'])
941 | s += " detect_optimize %#02x\n" % self.get_detect_optimize()
942 | s += " detection_thresh %#02x\n" % self.get_detection_threshold()
943 | s += " sync_word %#02x\n" % self.get_sync_word()
944 | s += " dio_mapping 0..5 %s\n" % self.get_dio_mapping()
945 | s += " tcxo %s\n" % ['XTAL', 'TCXO'][self.get_tcxo()]
946 | s += " pa_dac %s\n" % ['default', 'PA_BOOST'][self.get_pa_dac()]
947 | s += " fifo_addr_ptr %#02x\n" % self.get_fifo_addr_ptr()
948 | s += " fifo_tx_base_addr %#02x\n" % self.get_fifo_tx_base_addr()
949 | s += " fifo_rx_base_addr %#02x\n" % self.get_fifo_rx_base_addr()
950 | s += " fifo_rx_curr_addr %#02x\n" % self.get_fifo_rx_current_addr()
951 | s += " fifo_rx_byte_addr %#02x\n" % self.get_fifo_rx_byte_addr()
952 | s += " status %s\n" % self.get_modem_status()
953 | s += " version %#02x\n" % self.get_version()
954 | return s
955 |
--------------------------------------------------------------------------------