Article Index

 

Page 274

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/i2c_master.h"

void app_main(void)
{
    i2c_master_bus_config_t i2cBus = {
        .i2c_port = I2C_NUM_0,
        .scl_io_num = 15,
        .sda_io_num = 16,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true};
    i2c_master_bus_handle_t bus_handle;
    i2c_new_master_bus(&i2cBus, &bus_handle);

    i2c_device_config_t i2cdev = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x40,
        .scl_speed_hz = 100000,
        .scl_wait_us = 10};
    i2c_master_dev_handle_t dev_handle;
    i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);

    uint8_t buf[10] = {0xE7};
    i2c_master_transmit(dev_handle, buf, 1, 1000);
    i2c_master_receive(dev_handle, buf, 1, 100);
    printf("User Register = %X \r\n", buf[0]);
}

 

Page 278

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/i2c_master.h"

void app_main(void)
{
    i2c_master_bus_config_t i2cBus = {
        .i2c_port = I2C_NUM_0,
        .scl_io_num = 15,
        .sda_io_num = 16,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true};
    i2c_master_bus_handle_t bus_handle;
    i2c_new_master_bus(&i2cBus, &bus_handle);

    i2c_device_config_t i2cdev = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x40,
        .scl_speed_hz = 100000,
        .scl_wait_us = 10};
    i2c_master_dev_handle_t dev_handle;
    i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);

    uint8_t buf[10] = {0xF3};
    i2c_master_transmit(dev_handle, buf, 1, 100);
    esp_err_t err;
    do
    {
        err = i2c_master_receive(dev_handle, buf, 3, 100);
        if (err != ESP_OK)
        {
            i2c_master_bus_rm_device(dev_handle);
            i2c_del_master_bus(bus_handle);
            i2c_new_master_bus(&i2cBus, &bus_handle);
            i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);
        };
    } while (err != ESP_OK);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];
    printf("msb %d \n lsb %d \n checksum %d \n", msb, lsb, check);
    fflush(stdout);
}

 

Page 278 complete program from fragment

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/i2c_master.h"

void app_main(void)
{
    i2c_master_bus_config_t i2cBus = {
        .i2c_port = I2C_NUM_0,
        .scl_io_num = 15,
        .sda_io_num = 16,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true};
    i2c_master_bus_handle_t bus_handle;
    i2c_new_master_bus(&i2cBus, &bus_handle);

    i2c_device_config_t i2cdev = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x40,
        .scl_speed_hz = 100000,
        .scl_wait_us = 10};
    i2c_master_dev_handle_t dev_handle;
    i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);

    uint8_t buf[10] = {0xF3};
    i2c_master_transmit(dev_handle, buf, 1, 100);
    esp_err_t err;
    do
    {
        err = i2c_master_receive(dev_handle, buf, 3, 100);
        vTaskDelay(10 / portTICK_PERIOD_MS);
    } while (err != ESP_OK);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];
    printf("msb %d \n lsb %d \n checksum %d \n", msb, lsb, check);
    fflush(stdout);
}

 

Page 279 Complete program from fragment

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/i2c_master.h"

void app_main(void)
{
    i2c_master_bus_config_t i2cBus = {
        .i2c_port = I2C_NUM_0,
        .scl_io_num = 15,
        .sda_io_num = 16,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true};
    i2c_master_bus_handle_t bus_handle;
    i2c_new_master_bus(&i2cBus, &bus_handle);

    i2c_device_config_t i2cdev = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x40,
        .scl_speed_hz = 100000,
        .scl_wait_us = 10};
    i2c_master_dev_handle_t dev_handle;
    i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);

    uint8_t buf[10] = {0xF3};
    i2c_master_transmit(dev_handle, buf, 1, 100);
    vTaskDelay(50 / portTICK_PERIOD_MS);
    i2c_master_receive(dev_handle, buf, 3, 100);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];
    printf("msb %d \n lsb %d \n checksum %d \n", msb, lsb, check);
    fflush(stdout);
}

 

Page 282

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/i2c_master.h"

uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check)
{
    uint32_t data32 = ((uint32_t)msb << 16) | ((uint32_t)lsb << 8) |
                      (uint32_t)check;
    uint32_t divisor = 0x988000;
    for (int i = 0; i < 16; i++)
    {
        if (data32 & (uint32_t)1 << (23 - i))
            data32 ^= divisor;
        divisor >>= 1;
    };
    return (uint8_t)data32;
}

void app_main(void)
{
    i2c_master_bus_config_t i2cBus = {
        .i2c_port = I2C_NUM_0,
        .scl_io_num = 15,
        .sda_io_num = 16,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true};
    i2c_master_bus_handle_t bus_handle;
    i2c_new_master_bus(&i2cBus, &bus_handle);

    i2c_device_config_t i2cdev = {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = 0x40,
        .scl_speed_hz = 100000,
        .scl_wait_us = 10};
    i2c_master_dev_handle_t dev_handle;
    i2c_master_bus_add_device(bus_handle, &i2cdev, &dev_handle);

    uint8_t buf[10] = {0xF3};
    i2c_master_transmit(dev_handle, buf, 1, 100);
    vTaskDelay(50 / portTICK_PERIOD_MS);
    i2c_master_receive(dev_handle, buf, 3, 100);
    uint8_t msb = buf[0];
    uint8_t lsb = buf[1];
    uint8_t check = buf[2];

    unsigned int data16 = ((unsigned int)msb << 8) |
                          (unsigned int)(lsb & 0xFC);
    float temp = (float)(-46.85 + (175.72 * data16 / (float)65536));
    printf("Temperature %f C \n\r", temp);
    printf("crc = %d\n\r", crcCheck(msb, lsb, check));
    buf[0] = 0xF5;
    i2c_master_transmit(dev_handle, buf, 1, 100);
    vTaskDelay(50 / portTICK_PERIOD_MS);
    i2c_master_receive(dev_handle, buf, 3, 100);
    msb = buf[0];
    lsb = buf[1];
    check = buf[2];
    data16 = ((unsigned int)msb << 8) | (unsigned int)(lsb & 0xFC);
    float hum = -6 + (125.0 * (float)data16) / 65536;
    printf("Humidity %f %% \n\r", hum);
    printf("crc = %d\n\r", crcCheck(msb, lsb, check));
}