1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
.. _machine.I2C:
class I2C -- a two-wire serial protocol
=======================================
I2C is a two-wire protocol for communicating between devices. At the physical
level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
I2C objects are created attached to a specific bus. They can be initialised
when created, or initialised later on.
.. only:: port_wipy
Example::
from machine import I2C
i2c = I2C(0) # create on bus 0
i2c = I2C(0, I2C.MASTER) # create and init as a master
i2c.init(I2C.MASTER, baudrate=20000) # init as a master
i2c.deinit() # turn off the peripheral
Printing the i2c object gives you information about its configuration.
.. only:: port_wipy
A master must specify the recipient's address::
i2c.init(I2C.MASTER)
i2c.writeto(0x42, '123') # send 3 bytes to slave with address 0x42
i2c.writeto(addr=0x42, b'456') # keyword for address
Master also has other methods::
i2c.scan() # scan for slaves on the bus, returning
# a list of valid addresses
i2c.readfrom_mem(0x42, 2, 3) # read 3 bytes from memory of slave 0x42,
# starting at address 2 in the slave
i2c.writeto_mem(0x42, 2, 'abc') # write 'abc' (3 bytes) to memory of slave 0x42
# starting at address 2 in the slave, timeout after 1 second
Constructors
------------
.. only:: port_wipy
.. class:: machine.I2C(bus, ...)
Construct an I2C object on the given bus. `bus` can only be 0.
If the bus is not given, the default one will be selected (0).
.. only:: port_esp8266
.. class:: machine.I2C(scl, sda, \*, freq=400000)
Construct and return a new I2C object.
See the init method below for a description of the arguments.
General Methods
---------------
.. only:: port_wipy
.. method:: i2c.init(mode, \*, baudrate=100000, pins=(SDA, SCL))
Initialise the I2C bus with the given parameters:
- ``mode`` must be ``I2C.MASTER``
- ``baudrate`` is the SCL clock rate
- ``pins`` is an optional tuple with the pins to assign to the I2C bus.
.. only:: port_esp8266
.. method:: i2c.init(scl, sda, \*, freq=400000)
Initialise the I2C bus with the given arguments:
- `scl` is a pin object for the SCL line
- `sda` is a pin object for the SDA line
- `freq` is the SCL clock rate
.. method:: i2c.deinit()
Turn off the I2C bus.
Availability: WiPy.
.. method:: i2c.scan()
Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
those that respond. A device responds if it pulls the SDA line low after
its address (including a read bit) is sent on the bus.
Note: on WiPy the I2C object must be in master mode for this method to be valid.
Primitive I2C operations
------------------------
The following methods implement the primitive I2C master bus operations and can
be combined to make any I2C transaction. They are provided if you need more
control over the bus, otherwise the standard methods (see below) can be used.
.. method:: i2c.start()
Send a start bit on the bus (SDA transitions to low while SCL is high).
Availability: ESP8266.
.. method:: i2c.stop()
Send a stop bit on the bus (SDA transitions to high while SCL is high).
Availability: ESP8266.
.. method:: i2c.readinto(buf)
Reads bytes from the bus and stores them into `buf`. The number of bytes
read is the length of `buf`. An ACK will be sent on the bus after
receiving all but the last byte, and a NACK will be sent following the last
byte.
Availability: ESP8266.
.. method:: i2c.write(buf)
Write all the bytes from `buf` to the bus. Checks that an ACK is received
after each byte and raises an OSError if not.
Availability: ESP8266.
Standard bus operations
-----------------------
The following methods implement the standard I2C master read and write
operations that target a given slave device.
.. method:: i2c.readfrom(addr, nbytes)
Read `nbytes` from the slave specified by `addr`.
Returns a `bytes` object with the data read.
.. method:: i2c.readfrom_into(addr, buf)
Read into `buf` from the slave specified by `addr`.
The number of bytes read will be the length of `buf`.
On WiPy the return value is the number of bytes read. Otherwise the
return value is `None`.
.. method:: i2c.writeto(addr, buf, \*, stop=True)
Write the bytes from `buf` to the slave specified by `addr`.
The `stop` argument (only available on WiPy) tells if a stop bit should be
sent at the end of the transfer. If `False` the transfer should be
continued later on.
On WiPy the return value is the number of bytes written. Otherwise the
return value is `None`.
Memory operations
-----------------
Some I2C devices act as a memory device (or set of registers) that can be read
from and written to. In this case there are two addresses associated with an
I2C transaction: the slave address and the memory address. The following
methods are convenience functions to communicate with such devices.
.. method:: i2c.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8)
Read `nbytes` from the slave specified by `addr` starting from the memory
address specified by `memaddr`.
The argument `addrsize` specifies the address size in bits (on ESP8266
this argument is not recognised and the address size is always 8 bits).
Returns a `bytes` object with the data read.
.. method:: i2c.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8)
Read into `buf` from the slave specified by `addr` starting from the
memory address specified by `memaddr`. The number of bytes read is the
length of `buf`.
The argument `addrsize` specifies the address size in bits (on ESP8266
this argument is not recognised and the address size is always 8 bits).
On WiPy the return value is the number of bytes read. Otherwise the
return value is `None`.
.. method:: i2c.writeto_mem(addr, memaddr, buf, \*, addrsize=8)
Write `buf` to the slave specified by `addr` starting from the
memory address specified by `memaddr`.
The argument `addrsize` specifies the address size in bits (on ESP8266
this argument is not recognised and the address size is always 8 bits).
On WiPy the return value is the number of bytes written. Otherwise the
return value is `None`.
Constants
---------
.. data:: I2C.MASTER
for initialising the bus to master mode
Availability: WiPy.
|