@@ -12,9 +12,9 @@ when created, or initialised later on.
1212.. only :: port_pyboard
1313
1414 Example::
15-
15+
1616 from pyb import I2C
17-
17+
1818 i2c = I2C(1) # create on bus 1
1919 i2c = I2C(1, I2C.MASTER) # create and init as a master
2020 i2c.init(I2C.MASTER, baudrate=20000) # init as a master
@@ -24,75 +24,90 @@ when created, or initialised later on.
2424.. only :: port_wipy
2525
2626 Example::
27-
27+
2828 from pyb import I2C
29-
30- i2c = I2C(1 ) # create on bus 1
31- i2c = I2C(1 , I2C.MASTER) # create and init as a master
29+
30+ i2c = I2C(0 ) # create on bus 0
31+ i2c = I2C(0 , I2C.MASTER) # create and init as a master
3232 i2c.init(I2C.MASTER, baudrate=20000) # init as a master
3333 i2c.deinit() # turn off the peripheral
3434
3535Printing the i2c object gives you information about its configuration.
3636
37- The basic methods are send and recv::
37+ .. only :: port_pyboard
3838
39- i2c.send('abc') # send 3 bytes
40- i2c.send(0x42) # send a single byte, given by the number
41- data = i2c.recv(3) # receive 3 bytes
39+ The basic methods are send and recv::
4240
43- To receive inplace, first create a bytearray::
41+ i2c.send('abc') # send 3 bytes
42+ i2c.send(0x42) # send a single byte, given by the number
43+ data = i2c.recv(3) # receive 3 bytes
44+
45+ To receive inplace, first create a bytearray::
4446
45- data = bytearray(3) # create a buffer
46- i2c.recv(data) # receive 3 bytes, writing them into data
47+ data = bytearray(3) # create a buffer
48+ i2c.recv(data) # receive 3 bytes, writing them into data
4749
48- You can specify a timeout (in ms)::
50+ You can specify a timeout (in ms)::
4951
50- i2c.send(b'123', timeout=2000) # timout after 2 seconds
52+ i2c.send(b'123', timeout=2000) # timout after 2 seconds
5153
52- A master must specify the recipient's address::
54+ A master must specify the recipient's address::
5355
54- i2c.init(I2C.MASTER)
55- i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
56- i2c.send(b'456', addr=0x42) # keyword for address
56+ i2c.init(I2C.MASTER)
57+ i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
58+ i2c.send(b'456', addr=0x42) # keyword for address
5759
58- Master also has other methods::
60+ Master also has other methods::
5961
60- i2c.is_ready(0x42) # check if slave 0x42 is ready
61- i2c.scan() # scan for slaves on the bus, returning
62- # a list of valid addresses
63- i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
64- # starting at address 2 in the slave
65- i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
66- # starting at address 2 in the slave, timeout after 1 second
62+ i2c.is_ready(0x42) # check if slave 0x42 is ready
63+ i2c.scan() # scan for slaves on the bus, returning
64+ # a list of valid addresses
65+ i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
66+ # starting at address 2 in the slave
67+ i2c.mem_write('abc', 0x42, 2, timeout=1000) # write 'abc' (3 bytes) to memory of slave 0x42
68+ # starting at address 2 in the slave, timeout after 1 second
69+
70+ .. only :: port_wipy
71+
72+ A master must specify the recipient's address::
73+
74+ i2c.init(I2C.MASTER)
75+ i2c.writeto(0x42, '123') # send 3 bytes to slave with address 0x42
76+ i2c.writeto(addr=0x42, b'456') # keyword for address
77+
78+ Master also has other methods::
79+
80+ i2c.scan() # scan for slaves on the bus, returning
81+ # a list of valid addresses
82+ i2c.readfrom_mem(0x42, 2, 3) # read 3 bytes from memory of slave 0x42,
83+ # starting at address 2 in the slave
84+ i2c.writeto_mem(0x42, 2, 'abc') # write 'abc' (3 bytes) to memory of slave 0x42
85+ # starting at address 2 in the slave, timeout after 1 second
6786
6887Constructors
6988------------
7089
7190.. only :: port_pyboard
7291
7392 .. class :: pyb.I2C(bus, ...)
74-
93+
7594 Construct an I2C object on the given bus. ``bus `` can be 1 or 2.
7695 With no additional parameters, the I2C object is created but not
7796 initialised (it has the settings from the last initialisation of
7897 the bus, if any). If extra arguments are given, the bus is initialised.
7998 See ``init `` for parameters of initialisation.
80-
99+
81100 The physical pins of the I2C busses are:
82-
101+
83102 - ``I2C(1) `` is on the X position: ``(SCL, SDA) = (X9, X10) = (PB6, PB7) ``
84103 - ``I2C(2) `` is on the Y position: ``(SCL, SDA) = (Y9, Y10) = (PB10, PB11) ``
85104
86105.. only :: port_wipy
87106
88107 .. class :: pyb.I2C(bus, ...)
89108
90- Construct an I2C object on the given bus. `bus ` can only be 1.
91- With no additional parameters, the I2C object is created but not
92- initialised (it has the settings from the last initialisation of
93- the bus, if any). If extra arguments are given, the bus is initialised.
94- See `init ` for parameters of initialisation.
95-
109+ Construct an I2C object on the given bus. `bus ` can only be 0.
110+ If the bus is not given, the default one will be selected (0).
96111
97112Methods
98113-------
@@ -103,7 +118,7 @@ Methods
103118
104119.. only :: port_pyboard
105120
106- .. method :: i2c.init(mode, \*, addr=0x12, baudrate=400000, gencall=False)
121+ .. method :: i2c.init(mode, \*, addr=0x12, baudrate=400000, gencall=False)
107122
108123 Initialise the I2C bus with the given parameters:
109124
@@ -112,72 +127,110 @@ Methods
112127 - ``baudrate `` is the SCL clock rate (only sensible for a master)
113128 - ``gencall `` is whether to support general call mode
114129
130+ .. method :: i2c.is_ready(addr)
131+
132+ Check if an I2C device responds to the given address. Only valid when in master mode.
133+
134+ .. method :: i2c.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8)
135+
136+ Read from the memory of an I2C device:
137+
138+ - ``data `` can be an integer (number of bytes to read) or a buffer to read into
139+ - ``addr `` is the I2C device address
140+ - ``memaddr `` is the memory location within the I2C device
141+ - ``timeout `` is the timeout in milliseconds to wait for the read
142+ - ``addr_size `` selects width of memaddr: 8 or 16 bits
143+
144+ Returns the read data.
145+ This is only valid in master mode.
146+
147+ .. method :: i2c.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size=8)
148+
149+ Write to the memory of an I2C device:
150+
151+ - ``data `` can be an integer or a buffer to write from
152+ - ``addr `` is the I2C device address
153+ - ``memaddr `` is the memory location within the I2C device
154+ - ``timeout `` is the timeout in milliseconds to wait for the write
155+ - ``addr_size `` selects width of memaddr: 8 or 16 bits
156+
157+ Returns ``None ``.
158+ This is only valid in master mode.
159+
160+ .. method :: i2c.recv(recv, addr=0x00, \*, timeout=5000)
161+
162+ Receive data on the bus:
163+
164+ - ``recv `` can be an integer, which is the number of bytes to receive,
165+ or a mutable buffer, which will be filled with received bytes
166+ - ``addr `` is the address to receive from (only required in master mode)
167+ - ``timeout `` is the timeout in milliseconds to wait for the receive
168+
169+ Return value: if ``recv `` is an integer then a new buffer of the bytes received,
170+ otherwise the same buffer that was passed in to ``recv ``.
171+
172+ .. method :: i2c.send(send, addr=0x00, \*, timeout=5000)
173+
174+ Send data on the bus:
175+
176+ - ``send `` is the data to send (an integer to send, or a buffer object)
177+ - ``addr `` is the address to send to (only required in master mode)
178+ - ``timeout `` is the timeout in milliseconds to wait for the send
179+
180+ Return value: ``None ``.
181+
115182.. only :: port_wipy
116183
117- .. method :: i2c.init(mode, \*, baudrate=100000)
184+ .. method :: i2c.init(mode, \*, baudrate=100000)
118185
119186 Initialise the I2C bus with the given parameters:
120187
121188 - ``mode `` must be ``I2C.MASTER ``
122189 - ``baudrate `` is the SCL clock rate
123190
124- .. method :: i2c.is_ready (addr)
191+ .. method :: i2c.readfrom (addr, nbytes )
125192
126- Check if an I2C device responds to the given address. Only valid when in master mode.
193+ Read ``nbytes `` from the slave specified by ``addr ``.
194+ Returns a ``bytes `` object with the data read.
127195
128- .. method :: i2c.mem_read(data, addr, memaddr, \*, timeout=5000, addr_size=8 )
196+ .. method :: i2c.readfrom_into( addr, buf )
129197
130- Read from the memory of an I2C device:
198+ Read into ``buf `` from the slave specified by ``addr ``.
199+ Returns the number of bytes read.
131200
132- - ``data `` can be an integer (number of bytes to read) or a buffer to read into
133- - ``addr `` is the I2C device address
134- - ``memaddr `` is the memory location within the I2C device
135- - ``timeout `` is the timeout in milliseconds to wait for the read
136- - ``addr_size `` selects width of memaddr: 8 or 16 bits
201+ .. method :: i2c.writeto(addr, buf, \*, stop=True)
137202
138- Returns the read data.
139- This is only valid in master mode.
203+ Write ``buf `` to the slave specified by ``addr ``. Set ``stop `` to ``False ``
204+ if the transfer should be continued.
205+ Returns the number of bytes written.
140206
141- .. method :: i2c.mem_write(data, addr, memaddr, \*, timeout=5000, addr_size =8)
207+ .. method :: i2c.readfrom_mem( addr, memaddr, nbytes, \*, addrsize =8)
142208
143- Write to the memory of an I2C device:
209+ Read ``nbytes `` from the slave specified by ``addr `` starting from the memory
210+ address specified by ``memaddr ``.
211+ Param ``addrsize `` specifies the address size in bits.
212+ Returns a ``bytes `` object with the data read.
144213
145- - ``data `` can be an integer or a buffer to write from
146- - ``addr `` is the I2C device address
147- - ``memaddr `` is the memory location within the I2C device
148- - ``timeout `` is the timeout in milliseconds to wait for the write
149- - ``addr_size `` selects width of memaddr: 8 or 16 bits
214+ .. method :: i2c.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8)
150215
151- Returns ``None ``.
152- This is only valid in master mode.
216+ Read into ``buf `` from the slave specified by ``addr `` starting from the memory
217+ address specified by ``memaddr ``.
218+ Param ``addrsize `` specifies the address size in bits.
219+ Returns the number of bytes read.
153220
154- .. method :: i2c.recv(recv, addr=0x00, \*, timeout=5000 )
221+ .. method :: i2c.writeto_mem(addr, memaddr, buf, \*, addrsize=8 )
155222
156- Receive data on the bus:
157-
158- - ``recv `` can be an integer, which is the number of bytes to receive,
159- or a mutable buffer, which will be filled with received bytes
160- - ``addr `` is the address to receive from (only required in master mode)
161- - ``timeout `` is the timeout in milliseconds to wait for the receive
162-
163- Return value: if ``recv `` is an integer then a new buffer of the bytes received,
164- otherwise the same buffer that was passed in to ``recv ``.
223+ Write ``buf `` to the slave specified by ``addr `` starting from the
224+ memory address specified by ``memaddr ``. Param ``addrsize `` specifies the
225+ address size in bits.
226+ Set ``stop `` to ``False `` if the transfer should be continued.
227+ Returns the number of bytes written.
165228
166229.. method :: i2c.scan()
167230
168231 Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
169232 Only valid when in master mode.
170233
171- .. method :: i2c.send(send, addr=0x00, \*, timeout=5000)
172-
173- Send data on the bus:
174-
175- - ``send `` is the data to send (an integer to send, or a buffer object)
176- - ``addr `` is the address to send to (only required in master mode)
177- - ``timeout `` is the timeout in milliseconds to wait for the send
178-
179- Return value: ``None ``.
180-
181234Constants
182235---------
183236
0 commit comments