|
| 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 | + |
| 15 | +The I2C can be used in two different modes: |
| 16 | + |
| 17 | +* I2C Master Mode |
| 18 | + * In this mode, the ESP32 generates the clock signal and initiates the communication with the slave device. |
| 19 | + |
| 20 | +.. figure:: ../_static/arduino_i2c_master.png |
| 21 | + :align: center |
| 22 | + :width: 720 |
| 23 | + :figclass: align-center |
| 24 | + |
| 25 | +* I2C Slave Mode |
| 26 | + * 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. |
| 27 | + |
| 28 | +.. figure:: ../_static/arduino_i2c_slave.png |
| 29 | + :align: center |
| 30 | + :width: 520 |
| 31 | + :figclass: align-center |
| 32 | + |
| 33 | +Arduino-ESP32 I2C API |
| 34 | +--------------------- |
| 35 | + |
| 36 | +The ESP32 I2C library is based on the `Arduino Wire Library`_ and implements a few more APIs, describes in this documentation. |
| 37 | + |
| 38 | +I2C Common API |
| 39 | +************** |
| 40 | + |
| 41 | +Here are the common functions used for master and slave modes. |
| 42 | + |
| 43 | +setPins |
| 44 | +^^^^^^^ |
| 45 | + |
| 46 | +This function is used to define the ``SDA`` and ``SCL`` pins. |
| 47 | + |
| 48 | +.. code-block:: arduino |
| 49 | +
|
| 50 | + bool setPins(int sdaPin, int sclPin) |
| 51 | +
|
| 52 | +* ``sdaPin`` sets the GPIO to be used as the I2C peripheral data line. |
| 53 | + |
| 54 | +* ``sclPin`` sets the GPIO to be used as the I2C peripheral clock line. |
| 55 | + |
| 56 | +setClock |
| 57 | +^^^^^^^^ |
| 58 | + |
| 59 | +Use this function to set the bus clock. The default value will be used if this function is not used. |
| 60 | + |
| 61 | +.. code-block:: arduino |
| 62 | +
|
| 63 | + bool setClock(uint32_t); |
| 64 | +
|
| 65 | +getClock |
| 66 | +^^^^^^^^ |
| 67 | + |
| 68 | +Use this function to get the bus clock. |
| 69 | + |
| 70 | +.. code-block:: arduino |
| 71 | +
|
| 72 | + uint32_t getClock(); |
| 73 | +
|
| 74 | +setTimeOut |
| 75 | +^^^^^^^^^^ |
| 76 | + |
| 77 | +Set the bus timeout given in milliseconds. The default value is 50ms. |
| 78 | + |
| 79 | +.. code-block:: arduino |
| 80 | +
|
| 81 | + void setTimeOut(uint16_t timeOutMillis); |
| 82 | +
|
| 83 | +* ``timeOutMillis`` sets the timeout in ms. |
| 84 | + |
| 85 | +getTimeOut |
| 86 | +^^^^^^^^^^ |
| 87 | + |
| 88 | +Get the bus timeout in milliseconds. |
| 89 | + |
| 90 | +.. code-block:: arduino |
| 91 | +
|
| 92 | + uint16_t getTimeOut(); |
| 93 | +
|
| 94 | +.. _i2c write: |
| 95 | + |
| 96 | +write |
| 97 | +^^^^^ |
| 98 | + |
| 99 | +This function writes data to the buffer. |
| 100 | + |
| 101 | +.. code-block:: arduino |
| 102 | +
|
| 103 | + size_t write(uint8_t); |
| 104 | + |
| 105 | +or |
| 106 | + |
| 107 | +.. code-block:: arduino |
| 108 | +
|
| 109 | + size_t write(const uint8_t *, size_t); |
| 110 | +
|
| 111 | +The return will be the size of the data added to the buffer. |
| 112 | + |
| 113 | +end |
| 114 | +^^^ |
| 115 | + |
| 116 | +This function will finish the communication and release all the allocated resources. After calling ``end`` you need to user ``begin`` again. |
| 117 | + |
| 118 | +.. code-block:: arduino |
| 119 | +
|
| 120 | + bool end(); |
| 121 | +
|
| 122 | +
|
| 123 | +I2C Master Mode |
| 124 | +*************** |
| 125 | + |
| 126 | +This mode is used to initiate communication to the slave. |
| 127 | + |
| 128 | +Basic Usage |
| 129 | +^^^^^^^^^^^ |
| 130 | + |
| 131 | +To start using I2C on the Arduino, the first step is to include the ``Wire.h`` header to the scketch. |
| 132 | + |
| 133 | +.. code-block:: arduino |
| 134 | +
|
| 135 | + #include "Wire.h" |
| 136 | +
|
| 137 | +Now, we can start the peripheral configuration by calling ``begin`` function. |
| 138 | + |
| 139 | +.. code-block:: arduino |
| 140 | +
|
| 141 | + Wire.begin(); |
| 142 | +
|
| 143 | +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`_ |
| 144 | + |
| 145 | +After calling ``begin``, we can start the transmission by calling ``beginTransmission`` and passing the I2C slave address: |
| 146 | + |
| 147 | +.. code-block:: arduino |
| 148 | +
|
| 149 | + Wire.beginTransmission(I2C_DEV_ADDR); |
| 150 | +
|
| 151 | +To write some bytes to the slave, use the ``write`` function. |
| 152 | + |
| 153 | +.. code-block:: arduino |
| 154 | +
|
| 155 | + Wire.write(x); |
| 156 | +
|
| 157 | +You can pass different data types using ``write`` function. This function is described here: `i2c write`_ |
| 158 | + |
| 159 | +.. note:: The ``write`` function does not writes 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. |
| 160 | + |
| 161 | +.. code-block:: arduino |
| 162 | +
|
| 163 | + Wire.endTransmission(true); |
| 164 | +
|
| 165 | +After calling ``endTransmission``, the data stored in the I2C buffer will be transmitted to the slave device. |
| 166 | + |
| 167 | +Now you can request a reading from the slave device. The ``requestFrom`` will ask a readout to the selected device by giving the address and the size. |
| 168 | + |
| 169 | +.. code-block:: arduino |
| 170 | +
|
| 171 | + Wire.requestFrom(I2C_DEV_ADDR, SIZE); |
| 172 | +
|
| 173 | +and the ``readBytes`` will read it. |
| 174 | + |
| 175 | +.. code-block:: arduino |
| 176 | +
|
| 177 | + Wire.readBytes(temp, error); |
| 178 | +
|
| 179 | +.. _i2c begin: |
| 180 | + |
| 181 | +begin |
| 182 | +^^^^^ |
| 183 | + |
| 184 | +.. code-block:: arduino |
| 185 | +
|
| 186 | + bool begin(int sdaPin, int sclPin, uint32_t frequency) |
| 187 | +
|
| 188 | +beginTransmission |
| 189 | +^^^^^^^^^^^^^^^^^ |
| 190 | + |
| 191 | +.. code-block:: arduino |
| 192 | +
|
| 193 | + void beginTransmission(uint16_t address) |
| 194 | +
|
| 195 | +endTransmission |
| 196 | +^^^^^^^^^^^^^^^ |
| 197 | + |
| 198 | +.. code-block:: arduino |
| 199 | +
|
| 200 | + uint8_t endTransmission(bool sendStop); |
| 201 | +
|
| 202 | +* ``sendStop`` enables (true) or disables (false) the stop. |
| 203 | + |
| 204 | +.. code-block:: arduino |
| 205 | + |
| 206 | + uint8_t endTransmission(void); |
| 207 | +
|
| 208 | +requestFrom |
| 209 | +^^^^^^^^^^^ |
| 210 | + |
| 211 | +.. code-block:: arduino |
| 212 | +
|
| 213 | + uint8_t requestFrom(uint16_t address, uint8_t size, bool sendStop) |
| 214 | +
|
| 215 | +* ``address`` set the device address. |
| 216 | + |
| 217 | +* ``size`` define the size to be requested. |
| 218 | + |
| 219 | +* ``sendStop`` enables (true) or disables (false) the stop. |
| 220 | + |
| 221 | +This function will return the number of bytes read from the device. |
| 222 | + |
| 223 | +Example Application |
| 224 | +^^^^^^^^^^^^^^^^^^^ |
| 225 | + |
| 226 | +Here is an example on how to use the I2C in Master Mode. |
| 227 | + |
| 228 | +.. code-block:: arduino |
| 229 | +
|
| 230 | + #include "Wire.h" |
| 231 | +
|
| 232 | + #define I2C_DEV_ADDR 0x55 |
| 233 | +
|
| 234 | + uint32_t i = 0; |
| 235 | +
|
| 236 | + void setup() { |
| 237 | + Serial.begin(115200); |
| 238 | + Serial.setDebugOutput(true); |
| 239 | + Wire.begin(); |
| 240 | + } |
| 241 | +
|
| 242 | + void loop() { |
| 243 | + delay(5000); |
| 244 | +
|
| 245 | + //Write message to the slave |
| 246 | + Wire.beginTransmission(I2C_DEV_ADDR); |
| 247 | + Wire.printf("Hello World! %u", i++); |
| 248 | + uint8_t error = Wire.endTransmission(true); |
| 249 | + Serial.printf("endTransmission: %u\n", error); |
| 250 | + |
| 251 | + //Read 16 bytes from the slave |
| 252 | + error = Wire.requestFrom(I2C_DEV_ADDR, 16); |
| 253 | + Serial.printf("requestFrom: %u\n", error); |
| 254 | + if(error){ |
| 255 | + uint8_t temp[error]; |
| 256 | + Wire.readBytes(temp, error); |
| 257 | + log_print_buf(temp, error); |
| 258 | + } |
| 259 | + } |
| 260 | +
|
| 261 | +
|
| 262 | +I2C Slave Mode |
| 263 | +************** |
| 264 | + |
| 265 | +This mode is used to accept communication from the master. |
| 266 | + |
| 267 | +Basic Usage |
| 268 | +^^^^^^^^^^^ |
| 269 | + |
| 270 | +begin |
| 271 | +^^^^^ |
| 272 | + |
| 273 | +.. code-block:: arduino |
| 274 | +
|
| 275 | + bool Wire.begin(uint8_t addr, int sdaPin, int sclPin, uint32_t frequency) |
| 276 | +
|
| 277 | +onReceive |
| 278 | +^^^^^^^^^ |
| 279 | + |
| 280 | +.. code-block:: arduino |
| 281 | +
|
| 282 | + void onReceive( void (*)(int) ); |
| 283 | +
|
| 284 | +onRequest |
| 285 | +^^^^^^^^^ |
| 286 | + |
| 287 | +.. code-block:: arduino |
| 288 | +
|
| 289 | + void onRequest( void (*)(void) ); |
| 290 | +
|
| 291 | +slaveWrite |
| 292 | +^^^^^^^^^^ |
| 293 | + |
| 294 | +.. code-block:: arduino |
| 295 | +
|
| 296 | + size_t slaveWrite(const uint8_t *, size_t); |
| 297 | +
|
| 298 | +Example Application |
| 299 | +^^^^^^^^^^^^^^^^^^^ |
| 300 | + |
| 301 | +Here is an example on how to use the I2C in Slave Mode. |
| 302 | + |
| 303 | +.. code-block:: arduino |
| 304 | +
|
| 305 | + #include "Wire.h" |
| 306 | +
|
| 307 | + #define I2C_DEV_ADDR 0x55 |
| 308 | +
|
| 309 | + uint32_t i = 0; |
| 310 | +
|
| 311 | + void onRequest(){ |
| 312 | + Wire.print(i++); |
| 313 | + Wire.print(" Packets."); |
| 314 | + Serial.println("onRequest"); |
| 315 | + } |
| 316 | +
|
| 317 | + void onReceive(int len){ |
| 318 | + Serial.printf("onReceive[%d]: ", len); |
| 319 | + while(Wire.available()){ |
| 320 | + Serial.write(Wire.read()); |
| 321 | + } |
| 322 | + Serial.println(); |
| 323 | + } |
| 324 | +
|
| 325 | + void setup() { |
| 326 | + Serial.begin(115200); |
| 327 | + Serial.setDebugOutput(true); |
| 328 | + Wire.onReceive(onReceive); |
| 329 | + Wire.onRequest(onRequest); |
| 330 | + Wire.begin((uint8_t)I2C_DEV_ADDR); |
| 331 | +
|
| 332 | + #if CONFIG_IDF_TARGET_ESP32 |
| 333 | + char message[64]; |
| 334 | + snprintf(message, 64, "%u Packets.", i++); |
| 335 | + Wire.slaveWrite((uint8_t *)message, strlen(message)); |
| 336 | + #endif |
| 337 | + } |
| 338 | +
|
| 339 | + void loop() { |
| 340 | +
|
| 341 | + } |
| 342 | +
|
| 343 | +.. _Arduino Wire Library: https://www.arduino.cc/en/reference/wire |
0 commit comments