10To assign an I2C address to a Motoron, type "a" followed by the address
11(in decimal) while the JMP1 pin of the Motoron you wish to change is
12shorted to GND. For example, "a7" sets the address of the Motoron to
15Alternatively, you can type "a" by itself in order to automatically
16to have this program automatically pick an address for you.
18To make the Motoron start using its new address, you can reset it by
19power cycling it, driving its RST line low, or by typing "r".
21The "s" command does a simple scan of the bus to see which addresses
22have devices responding. This can help you make sure you have set up
23your I2C bus correctly.
25The "i" command goes further, sending extra commands to identify the
26Motoron devices on the bus and print information about them.
28Warning: The "i" command sends Motoron commands to every I2C device on
29the bus. If you have devices on your bus that are not Motorons, this
30could cause undesired behavior.
32Warning: The "a" and "r" commands use the I2C general call address (0), so
33they might cause undesired behavior on other devices that use that address.
34Also, they will not work if you disabled the general call address on a
38start_message =
"""Motoron Set I2C Addresses Utility
40Type "h" for help, "a" followed by a number to assign an address, "r" to reset
41Motorons, "s" to scan, "i" to identify devices, or Ctrl+C to quit.
43Warning: If you have devices that are not Motorons, these commands could cause
44undesired behavior. Type "h" for more info.
53from smbus2
import SMBus
71def allow_address_communication(address):
73 if address >= 128:
return False
84def allow_address_assignment(address):
85 return allow_address_communication(address)
and address != 0
90def scan_address(address):
91 if not allow_address_communication(address):
return False
94 if type_is_smbus
is None:
95 type_is_smbus = (getattr(bus,
'i2c_rdwr',
None) !=
None)
98 bus.i2c_rdwr(mc._msg.write(address, []))
100 if e.args[0] == 6
or e.args[0] == 121:
return False
104 bus.writeto(address, b
'')
106 except RuntimeError
as e:
107 if e.__class__.__name__ ==
'AdapterError' and e.error_code == 8:
112def assign_address(line):
114 desired_address_specified =
False
115 desired_address = next_address
118 desired_address = int(line[1:]) & 127
119 desired_address_specified =
True
124 if allow_address_assignment(desired_address):
127 if desired_address_specified
or not scan_address(desired_address):
129 print(
"Found a device at address %d." % desired_address)
131 elif desired_address_specified:
132 print(
"Assignment to address %d not allowed." % desired_address)
136 desired_address = (desired_address + 1) & 127
137 if allow_address_assignment(desired_address):
break
146 mc.write_eeprom_device_number(desired_address)
148 print(
"Assigned address", desired_address)
149 next_address = desired_address + 1
151def scan_for_devices():
152 print(
"Scanning for I2C devices...")
153 for i
in range(0, 128):
155 print(
"Found device at address", i)
161def identify_devices():
162 print(
"Identifying Motoron controllers...")
163 for i
in range(1, 128):
164 if not allow_address_communication(i):
continue
169 test.disable_crc_for_responses()
170 v = test.get_firmware_version()
171 jumper_state = test.get_jumper_state() & 3
174 except RuntimeError
as e:
175 if e.__class__.__name__ ==
'AdapterError' and e.error_code == 8:
179 jumper_string = [
'both',
'on',
'off',
'err'][jumper_state]
180 print(
"%3d: product=0x%04X version=%x.%02x JMP1=%s" % (i, v[
'product_id'],
181 v[
'firmware_version'][
'major'], v[
'firmware_version'][
'minor'], jumper_string))
183def process_input_line(line):
185 elif line[0] ==
'a': assign_address(line)
189 elif line[0] ==
's': scan_for_devices()
190 elif line[0] ==
'i': identify_devices()
191 elif line[0] ==
'h' or line[0] ==
'H' or line[0] ==
'?': print(help_message)
192 elif line ==
'q' or line ==
'quit': sys.exit(0)
193 else: print(
"Error: Unrecognized command. Type h for help.")
198 process_input_line(input(
'Enter command: '))
199except (KeyboardInterrupt, EOFError):
Represents an I2C connection to a Pololu Motoron Motor Controller.