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.
51from smbus2 import SMBus, i2c_msg
53# Make a MotoronI2C object configured to use the general call address (0).
54mc = motoron.MotoronI2C(address=0)
56# The next address this program will try to use when you send an "a" command
57# to automatically assign an address.
60# This function defines which I2C addresses this program is
61# allowed to communicate with.
62def allow_address_communication(address):
64 if address >= 128:
return False
75def allow_address_assignment(address):
76 return allow_address_communication(address)
and address != 0
79def scan_address(address):
80 if not allow_address_communication(address):
return False
82 mc.bus.i2c_rdwr(i2c_msg.write(address, []))
85 if e.args[0] == 6
or e.args[0] == 121:
return False
88def assign_address(line):
90 desired_address_specified =
False
91 desired_address = next_address
94 desired_address = int(line[1:]) & 127
95 desired_address_specified =
True
100 if allow_address_assignment(desired_address):
103 if desired_address_specified
or not scan_address(desired_address):
105 print(
"Found a device at address %d." % desired_address)
107 elif desired_address_specified:
108 print(
"Assignment to address %d not allowed." % desired_address)
112 desired_address = (desired_address + 1) & 127
113 if allow_address_assignment(desired_address):
break
122 mc.write_eeprom_device_number(desired_address)
124 print(
"Assigned address", desired_address)
125 next_address = desired_address + 1
127def scan_for_devices():
128 print(
"Scanning for I2C devices...")
129 for i
in range(0, 128):
131 print(
"Found device at address", i)
137def identify_devices():
138 print(
"Identifying Motoron controllers...")
139 for i
in range(1, 128):
140 if not allow_address_communication(i):
continue
145 test.disable_crc_for_responses()
146 v = test.get_firmware_version()
147 jumper_state = test.get_jumper_state() & 3
150 jumper_string = [
'both',
'on',
'off',
'err'][jumper_state]
151 print(
"%3d: product=0x%04X version=%x.%02x JMP1=%s" % (i, v[
'product_id'],
152 v[
'firmware_version'][
'major'], v[
'firmware_version'][
'minor'], jumper_string))
154def process_input_line(line):
156 elif line[0] ==
'a': assign_address(line)
160 elif line[0] ==
's': scan_for_devices()
161 elif line[0] ==
'i': identify_devices()
162 elif line[0] ==
'h' or line[0] ==
'H' or line[0] ==
'?': print(help_message)
163 elif line ==
'q' or line ==
'quit': sys.exit(0)
164 else: print(
"Error: Unreocgnized command. Type h for help.")
169 process_input_line(input(
'Enter command: '))
170except (KeyboardInterrupt, EOFError):
Represents an I2C connection to a Pololu Motoron Motor Controller.