components/i2c/master_i2c_interface.c
/**
 * @brief test code to read esp-i2c-slave
 *        We need to fill the buffer of esp slave device, then master can read them out.
 *
 * _______________________________________________________________________________________
 * | start | slave_addr + rd_bit +ack | read n-1 bytes + ack | read 1 byte + nack | stop |
 * --------|--------------------------|----------------------|--------------------|------|
 *
 */
@brief test code to read esp-i2c-slave
We need to fill the buffer of esp slave device, then master can read them out.


@brief test code to read esp - i2c - slave We need to fill the buffer of esp slave device then master can read them out .
esp_err_t i2c_master_read_slave(i2c_port_t i2c_num,
                                               int slave_address, 
                                               uint8_t* data_rd, 
                                               size_t size)
{
    if (size == 0) {
        return ESP_OK;
    }
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, ( slave_address << 1 ) | READ_BIT, ACK_CHECK_EN);
    if (size > 1) {
        i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
    }
    i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
    i2c_master_stop(cmd);
    esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    return ret;
}
esp_err_t i2c_master_read_slave ( i2c_port_t i2c_num , int slave_address , uint8_t * data_rd , size_t size ) { if ( size == 0 ) { return ESP_OK ; } i2c_cmd_handle_t cmd = i2c_cmd_link_create ( ) ; i2c_master_start ( cmd ) ; i2c_master_write_byte ( cmd , ( slave_address << 1 ) | READ_BIT , ACK_CHECK_EN ) ; if ( size > 1 ) { i2c_master_read ( cmd , data_rd , size - 1 , ACK_VAL ) ; } i2c_master_read_byte ( cmd , data_rd + size - 1 , NACK_VAL ) ; i2c_master_stop ( cmd ) ; esp_err_t ret = i2c_master_cmd_begin ( i2c_num , cmd , 1000 / portTICK_RATE_MS ) ; i2c_cmd_link_delete ( cmd ) ; return ret ; }
@brief test code to read esp-i2c-slave
        We need to fill the buffer of esp slave device, then master can read them out.
@brief test code to read esp - i2c - slave We need to fill the buffer of esp slave device then master can read them out .