Home

Awesome

Brzo I2C

Brzo i2c implements an i2c master and is written in assembly language for the esp8266. The core features are:

For further information you may refer to the wiki

HW and Tool Chain Support

Brzo i2c was tested with several i2c devices. If you find one which is not working, please open an issue.

NOTE for AM2320 Sensor: The wake-up sequence is not compatible with brzo_i2c, see analysis here

How to install the Brzo I2C Library

And then just include brzo_i2c in your sketch as any other library with #include "brzo_i2c.h".

Disabling or enabling Interrupts during i2c reads or writes

In brzo_i2c.h you can set the behaviour how interrupts during i2c reads or writes are handled: Setting BRZO_I2C_DISABLE_INTERRUPTS to 1 will disable all interrupts during i2c read or writes. The default is all interrupts are disabled. For further information about disabling or enabling interrupts refer to the wiki

I2C Setup

To setup the i2c bus you need to call at least once brzo_i2c_setup.

brzo_i2c_setup(uint8_t sda, uint8_t scl, uint32_t clock_stretch_time_out_usec).
Input

Returns

Example: brzo_i2c_setup(5, 12, 200);

I2C Transactions

The motivation for i2c transactions is found on the wiki. An i2c transactions always begins with brzo_i2c_start_transaction and ends with brzo_i2c_end_transaction. Within a transaction you can combine any number i2c commands with or without a repeated start, i.e. brzo_i2c_write or brzo_i2c_read. A transaction is bound to one i2c slave and all commands are executed at the same SCL speed. Thus, brzo_i2c_start_transaction takes the slave address and SCL speed in KHz as input parameters.

Begin a transaction

brzo_i2c_start_transaction(uint8_t slave_address, uint16_t SCL_frequency_KHz)

Input

Returns

Example: Start a transaction to an i2c slave at 0x20 with 1 MHz SCL speed. brzo_i2c_start_transaction(0x20, 1000);

End a Transaction

uint8_t brzo_i2c_end_transaction()

Note:

Input

Returns

I2C Commands

The following i2c commands shall only be used within a transaction!

I2C write

brzo_i2c_write(uint8_t *data, uint8_t no_of_bytes, boolean repeated_start)

Note:

Input

Returns

Example

I2C Read

brzo_i2c_read(uint8_t *data, uint8_t no_of_bytes, boolean repeated_start)

Note:

Input

Returns

Example

I2C Acknowledge Polling

Please see the wiki for further information about ACK Polling.

brzo_i2c_ACK_polling(uint16_t ACK_polling_time_out_usec)

Note:

Input

Returns

Example

Example of I2C Transactions

Examples of sketches are located in /examples folder.

#include "brzo_i2c.h"

uint8_t SDA_PIN = 5;
uint8_t SCL_PIN = 12;
uint8_t ADT7420_ADR = 0x49;
uint8_t buffer[3];
uint8_t return_code = 0;
uint32_t ADC_code = 0;
long temp;

void setup() {
  // Setup i2c with clock stretching timeout of 2000 usec
  brzo_i2c_setup(SDA_PIN, SCL_PIN, 2000);
...
}

...

// Start a transaction to the ADT7420 at 400 KHz

brzo_i2c_start_transaction(ADT7420_ADR, 400);
  buffer[0] = 0x03; // select configuration register
  buffer[1] = 0xA0; // select 16 bit resolution
  brzo_i2c_write(buffer, 2, false); // Write two bytes with no repeated start
  buffer[0] = 0x00; // select temperature register
  brzo_i2c_write(buffer, 1, true); // Write one byte WITH repeated start
  brzo_i2c_read(buffer, 2, false); // Read temperature, two bytes
return_code = brzo_i2c_end_transaction();

if (return_code == 0) {
  ADC_code = ((buffer[0] << 8) | buffer[1]);
  temp = ADC_code / 128.0;
 }
else {
  // Error Handling here...
}