Skip to content

i2c_request

gicking edited this page Feb 10, 2018 · 5 revisions

back to Command Reference / I2C

Description

Request data from an I2C slave via pins SCLand SDA. Before the request, a start condition must be generated by i2c_start(). The transmission is terminated by a stop condition, which is generated via i2c_stop().

Inclusion

  • defined in i2c.h
  • not loaded by main_general.h
  • for custom I2C interrupts set set #define USE_I2C_ISR in config.h. The corresponding interrupt function name is I2C_ISR

Warning: test with Arduino example "Wire/slave sender" failed. Likely this routine is buggy!

Syntax

i2c_request(addr,num,buf)

Parameters

  • input:

    • addr: I2C address of slave to get data from (uint8_t)
    • num: number of bytes to receive (uint8_t)
  • output:

    • buf: buffer for received data (uint8_t array)

Returns

  • 0: acknowledge received, 1: timeout occurred

Example Code

The below function periodically reads data from an Arduino configured as I2C slave using example "Wire/slave sender".

#include "i2c.h"

#define ADDR_I2C 8
#define NUM_I2C  6

setup() {
  ...
  i2c_init();
  ...
}

loop() {
  uint8_t  buf[NUM_I2C]
  ...
  i2c_start();
  i2c_request(ADDR_I2C,NUM_I2C,buf);
  i2c_stop();
  ...
}

Relevant Tutorial

Notes and Warnings

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally