Skip to content

Commit 8136476

Browse files
committed
Added I2C driver docs
docs: Changes on the images and added more details about the I2C slave
1 parent ed53b6c commit 8136476

File tree

4 files changed

+367
-1
lines changed

4 files changed

+367
-1
lines changed

Diff for: docs/source/_static/arduino_i2c_master.png

78.1 KB
Loading

Diff for: docs/source/_static/arduino_i2c_slave.png

114 KB
Loading

Diff for: docs/source/api/i2c.rst

+365
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
###
2+
I2C
3+
###
4+
5+
About
6+
-----
7+
8+
I2C (Inter-Integrated Circuit) / TWI (Two-wire Interface) is a widely used serial communication to connect devices in a short distance. This is one of the most common peripherals used to connect sensors, EEPROMs, RTC, ADC, DAC, displays, OLED, and many other devices and microcontrollers.
9+
10+
This serial communication is considered as a low-speed bus, and multiple devices can be connected on the same two-wires bus, each with a unique 7-bits address (up to 128 devices). These two wires are called SDA (serial data line) and SCL (serial clock line).
11+
12+
.. note:: The SDA and SCL lines require pull-up resistors. See the device datasheet for more details about the resistors' values and the operating voltage.
13+
14+
I2C Modes
15+
*********
16+
17+
The I2C can be used in two different modes:
18+
19+
* **I2C Master Mode**
20+
* In this mode, the ESP32 generates the clock signal and initiates the communication with the slave device.
21+
22+
.. figure:: ../_static/arduino_i2c_master.png
23+
:align: center
24+
:width: 720
25+
:figclass: align-center
26+
27+
* **I2C Slave Mode**
28+
* The slave mode, the clock is generated by the master device and responds to the master if the destination address is the same as the destination.
29+
30+
.. figure:: ../_static/arduino_i2c_slave.png
31+
:align: center
32+
:width: 520
33+
:figclass: align-center
34+
35+
Arduino-ESP32 I2C API
36+
---------------------
37+
38+
The ESP32 I2C library is based on the `Arduino Wire Library`_ and implements a few more APIs, described in this documentation.
39+
40+
I2C Common API
41+
**************
42+
43+
Here are the common functions used for master and slave modes.
44+
45+
begin
46+
^^^^^
47+
48+
This function is used to start the peripheral using the default configuration.
49+
50+
.. code-block:: arduino
51+
52+
bool begin();
53+
54+
setPins
55+
^^^^^^^
56+
57+
This function is used to define the ``SDA`` and ``SCL`` pins.
58+
59+
.. note:: Call this function before ``begin`` to change the pins from the default ones.
60+
61+
.. code-block:: arduino
62+
63+
bool setPins(int sdaPin, int sclPin);
64+
65+
* ``sdaPin`` sets the GPIO to be used as the I2C peripheral data line.
66+
67+
* ``sclPin`` sets the GPIO to be used as the I2C peripheral clock line.
68+
69+
The default pins may vary from board to board. On the *Generic ESP32* the default I2C pins are:
70+
71+
* ``sdaPin`` **GPIO21**
72+
73+
* ``sclPin`` **GPIO22**
74+
75+
setClock
76+
^^^^^^^^
77+
78+
Use this function to set the bus clock. The default value will be used if this function is not used.
79+
80+
.. code-block:: arduino
81+
82+
bool setClock(uint32_t);
83+
84+
getClock
85+
^^^^^^^^
86+
87+
Use this function to get the bus clock.
88+
89+
.. code-block:: arduino
90+
91+
uint32_t getClock();
92+
93+
setTimeOut
94+
^^^^^^^^^^
95+
96+
Set the bus timeout given in milliseconds. The default value is 50ms.
97+
98+
.. code-block:: arduino
99+
100+
void setTimeOut(uint16_t timeOutMillis);
101+
102+
* ``timeOutMillis`` sets the timeout in ms.
103+
104+
getTimeOut
105+
^^^^^^^^^^
106+
107+
Get the bus timeout in milliseconds.
108+
109+
.. code-block:: arduino
110+
111+
uint16_t getTimeOut();
112+
113+
.. _i2c write:
114+
115+
write
116+
^^^^^
117+
118+
This function writes data to the buffer.
119+
120+
.. code-block:: arduino
121+
122+
size_t write(uint8_t);
123+
124+
or
125+
126+
.. code-block:: arduino
127+
128+
size_t write(const uint8_t *, size_t);
129+
130+
The return will be the size of the data added to the buffer.
131+
132+
.. _i2c end:
133+
134+
end
135+
^^^
136+
137+
This function will finish the communication and release all the allocated resources. After calling ``end`` you need to use ``begin`` again in order to initialize the I2C driver again.
138+
139+
.. code-block:: arduino
140+
141+
bool end();
142+
143+
144+
I2C Master Mode
145+
***************
146+
147+
This mode is used to initiate communication to the slave.
148+
149+
Basic Usage
150+
^^^^^^^^^^^
151+
152+
To start using I2C master mode on the Arduino, the first step is to include the ``Wire.h`` header to the sketch.
153+
154+
.. code-block:: arduino
155+
156+
#include "Wire.h"
157+
158+
Now, we can start the peripheral configuration by calling ``begin`` function.
159+
160+
.. code-block:: arduino
161+
162+
Wire.begin();
163+
164+
By using ``begin`` without any arguments, all the settings will be done by using the default values. To set the values by your own, see the function description. This function is described here: `i2c begin`_
165+
166+
After calling ``begin``, we can start the transmission by calling ``beginTransmission`` and passing the I2C slave address:
167+
168+
.. code-block:: arduino
169+
170+
Wire.beginTransmission(I2C_DEV_ADDR);
171+
172+
To write some bytes to the slave, use the ``write`` function.
173+
174+
.. code-block:: arduino
175+
176+
Wire.write(x);
177+
178+
You can pass different data types using ``write`` function. This function is described here: `i2c write`_
179+
180+
.. note:: The ``write`` function does not write directly to the slave device but adds to the I2C buffer. To do so, you need to use the ``endTransmission`` function to send the buffered bytes to the slave device.
181+
182+
.. code-block:: arduino
183+
184+
Wire.endTransmission(true);
185+
186+
After calling ``endTransmission``, the data stored in the I2C buffer will be transmitted to the slave device.
187+
188+
Now you can request a reading from the slave device. The ``requestFrom`` will ask for a readout to the selected device by giving the address and the size.
189+
190+
.. code-block:: arduino
191+
192+
Wire.requestFrom(I2C_DEV_ADDR, SIZE);
193+
194+
and the ``readBytes`` will read it.
195+
196+
.. code-block:: arduino
197+
198+
Wire.readBytes(temp, error);
199+
200+
.. _i2c begin:
201+
202+
I2C Master APIs
203+
***************
204+
205+
Here are the I2C master APIs. These function are intended to be used only for master mode.
206+
207+
begin
208+
^^^^^
209+
210+
In master mode, the ``begin`` function can be used by passing the **pins** and **bus frequency**. Use this function only for the master mode.
211+
212+
.. code-block:: arduino
213+
214+
bool begin(int sdaPin, int sclPin, uint32_t frequency)
215+
216+
Alternatively, you can use the ``begin`` function without any argument to use all default values.
217+
218+
This function will return ``true`` if the peripheral was initialized correctly.
219+
220+
beginTransmission
221+
^^^^^^^^^^^^^^^^^
222+
223+
This function is used to star a communication process with the slave device. Call this function by passing the slave ``address`` before writing the message to the buffer.
224+
225+
.. code-block:: arduino
226+
227+
void beginTransmission(uint16_t address)
228+
229+
endTransmission
230+
^^^^^^^^^^^^^^^
231+
232+
After writing to the buffer using `i2c write`_, use the function ``endTransmission`` to send the message to the slave device address defined on the ``beginTransmission`` function.
233+
234+
.. code-block:: arduino
235+
236+
uint8_t endTransmission(bool sendStop);
237+
238+
* ``sendStop`` enables **(true)** or disables **(false)** the stop signal *(only used in master mode)*.
239+
240+
Calling the this function without ``sendStop`` is equivalent to ``sendStop = true``.
241+
242+
.. code-block:: arduino
243+
244+
uint8_t endTransmission(void);
245+
246+
This function will return the error code.
247+
248+
requestFrom
249+
^^^^^^^^^^^
250+
251+
To read from the slave device, use the ``requestFrom`` function.
252+
253+
.. code-block:: arduino
254+
255+
uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop)
256+
257+
* ``address`` set the device address.
258+
259+
* ``size`` define the size to be requested.
260+
261+
* ``sendStop`` enables (true) or disables (false) the stop signal.
262+
263+
This function will return the number of bytes read from the device.
264+
265+
Example Application - WireMaster.ino
266+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267+
268+
Here is an example of how to use the I2C in Master Mode.
269+
270+
.. literalinclude:: ../../../libraries/Wire/examples/WireMaster/WireMaster.ino
271+
:language: arduino
272+
273+
274+
I2C Slave Mode
275+
**************
276+
277+
This mode is used to accept communication from the master.
278+
279+
Basic Usage
280+
^^^^^^^^^^^
281+
282+
To start using I2C as slave mode on the Arduino, the first step is to include the ``Wire.h`` header to the scketch.
283+
284+
.. code-block:: arduino
285+
286+
#include "Wire.h"
287+
288+
Before calling ``begin`` we must create two callback functions to handle the communication with the master device.
289+
290+
.. code-block:: arduino
291+
292+
Wire.onReceive(onReceive);
293+
294+
and
295+
296+
.. code-block:: arduino
297+
298+
Wire.onRequest(onRequest);
299+
300+
The ``onReceive`` will handle the request from the master device uppon a slave read request and the ``onRequest`` will handle the answer to the master.
301+
302+
Now, we can start the peripheral configuration by calling ``begin`` function with the device address.
303+
304+
.. code-block:: arduino
305+
306+
Wire.begin((uint8_t)I2C_DEV_ADDR);
307+
308+
By using ``begin`` without any arguments, all the settings will be done by using the default values. To set the values by your own, see the function description. This function is described here: `i2c begin`_
309+
310+
.. code-block:: arduino
311+
312+
Wire.slaveWrite((uint8_t *)message, strlen(message));
313+
314+
I2C Slave APIs
315+
**************
316+
317+
Here are the I2C slave APIs. These function are intended to be used only for slave mode.
318+
319+
begin
320+
^^^^^
321+
322+
In slave mode, the ``begin`` function must be used by passing the **slave address**. You can also define the **pins** and the **bus frequency**.
323+
324+
.. code-block:: arduino
325+
326+
bool Wire.begin(uint8_t addr, int sdaPin, int sclPin, uint32_t frequency)
327+
328+
This function will return ``true`` if the peripheral was initialized correctly.
329+
330+
onReceive
331+
^^^^^^^^^
332+
333+
The ``onReceive`` function is used to define the callback for the data received from the master.
334+
335+
.. code-block:: arduino
336+
337+
void onReceive( void (*)(int) );
338+
339+
onRequest
340+
^^^^^^^^^
341+
342+
The ``onRequest`` function is used to define the callback for the data to be send to the master.
343+
344+
.. code-block:: arduino
345+
346+
void onRequest( void (*)(void) );
347+
348+
slaveWrite
349+
^^^^^^^^^^
350+
351+
To write back to the master device, use the ``slaveWrite`` function.
352+
353+
.. code-block:: arduino
354+
355+
size_t slaveWrite(const uint8_t *, size_t);
356+
357+
Example Application - WireSlave.ino
358+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
359+
360+
Here is an example of how to use the I2C in Slave Mode.
361+
362+
.. literalinclude:: ../../../libraries/Wire/examples/WireSlave/WireSlave.ino
363+
:language: arduino
364+
365+
.. _Arduino Wire Library: https://www.arduino.cc/en/reference/wire

Diff for: docs/source/libraries.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Libraries
33
#########
44

5-
Here is where the Libraries API's descriptions are located.
5+
Here is where the Libraries API's descriptions are located:
66

77
.. toctree::
88
:maxdepth: 1
@@ -12,6 +12,7 @@ Here is where the Libraries API's descriptions are located.
1212
Deep Sleep <api/deepsleep>
1313
ESPNOW <api/espnow>
1414
GPIO <api/gpio>
15+
I2C <api/i2c>
1516
RainMaker <api/rainmaker>
1617
Reset Reason <api/reset_reason>
1718
Wi-Fi <api/wifi>

0 commit comments

Comments
 (0)