mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
Functions | Typedefs
i2c.h File Reference

API Description

An i2c context represents a master on an i2c bus and that context can communicate to multiple i2c slaves by configuring the address. It is considered best practice to make sure the address is correct before doing any calls on i2c, in case another application or even thread changed the addres on that bus. Multiple instances of the same bus can exist.

i2c = mraa_i2c_init(I2C_BUS);
if (i2c == NULL) {
fprintf(stderr, "Failed to initialize I2C\n");
return EXIT_FAILURE;
}
/* set slave address */
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* set device gain */
rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG;
status = mraa_i2c_write(i2c, rx_tx_buf, 2);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* set continuous mode */
rx_tx_buf[0] = HMC5883L_MODE_REG;
rx_tx_buf[1] = HMC5883L_CONT_MODE;
status = mraa_i2c_write(i2c, rx_tx_buf, 2);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
while (flag) {
#if 0
int i = 0;
/*
* alternative, equivalent method which helps to understand exactly what
* the below does
*/
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
for (i = 0; i < DATA_REG_SIZE; i++) {
mraa_i2c_read_byte_data(i2c, HMC5883L_DATA_REG+i);
}
#endif
/* select data register to read */
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
status = mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* do a incremental read from the chosen address */
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
/* this call behaves very similarly to the Wire receive() call */
mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG];
/* scale and calculate direction */
direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
/* check if the signs are reversed */
if (direction < 0) {
direction += 2 * M_PI;
}
fprintf(stdout, "Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG,
y * SCALE_0_92_MG, z * SCALE_0_92_MG);
fprintf(stdout, "Heading : %f\n", direction * 180 / M_PI);
sleep(2);
}
/* stop i2c */

Go to the source code of this file.

Functions

mraa_i2c_context mraa_i2c_init (int bus)
 
mraa_i2c_context mraa_i2c_init_raw (unsigned int bus)
 
mraa_result_t mraa_i2c_frequency (mraa_i2c_context dev, mraa_i2c_mode_t mode)
 
int mraa_i2c_read (mraa_i2c_context dev, uint8_t *data, int length)
 
int mraa_i2c_read_byte (mraa_i2c_context dev)
 
int mraa_i2c_read_byte_data (mraa_i2c_context dev, const uint8_t command)
 
int mraa_i2c_read_word_data (mraa_i2c_context dev, const uint8_t command)
 
int mraa_i2c_read_bytes_data (mraa_i2c_context dev, uint8_t command, uint8_t *data, int length)
 
mraa_result_t mraa_i2c_write (mraa_i2c_context dev, const uint8_t *data, int length)
 
mraa_result_t mraa_i2c_write_byte (mraa_i2c_context dev, const uint8_t data)
 
mraa_result_t mraa_i2c_write_byte_data (mraa_i2c_context dev, const uint8_t data, const uint8_t command)
 
mraa_result_t mraa_i2c_write_word_data (mraa_i2c_context dev, const uint16_t data, const uint8_t command)
 
mraa_result_t mraa_i2c_address (mraa_i2c_context dev, uint8_t address)
 
mraa_result_t mraa_i2c_stop (mraa_i2c_context dev)
 

Typedefs

typedef struct _i2c * mraa_i2c_context
 

Function Documentation

mraa_i2c_context mraa_i2c_init ( int  bus)

Initialise i2c context, using board defintions

Parameters
busi2c bus to use
Returns
i2c context or NULL

Here is the caller graph for this function:

mraa_i2c_context mraa_i2c_init_raw ( unsigned int  bus)

Initialise i2c context, passing in the i2c bus to use.

Parameters
busThe i2c bus to use i.e. /dev/i2c-2 would be "2"
Returns
i2c context or NULL

Here is the caller graph for this function:

mraa_result_t mraa_i2c_frequency ( mraa_i2c_context  dev,
mraa_i2c_mode_t  mode 
)

Sets the frequency of the i2c context. Most platforms do not support this.

Parameters
devThe i2c context
modeThe bus mode
Returns
Result of operation

Here is the caller graph for this function:

int mraa_i2c_read ( mraa_i2c_context  dev,
uint8_t *  data,
int  length 
)

Simple bulk read from an i2c context

Parameters
devThe i2c context
datapointer to the byte array to read data in to
lengthmax number of bytes to read
Returns
length of the read in bytes or -1

Here is the caller graph for this function:

int mraa_i2c_read_byte ( mraa_i2c_context  dev)

Simple read for a single byte from the i2c context

Parameters
devThe i2c context
Returns
The result of the read or -1 if failed

Here is the caller graph for this function:

int mraa_i2c_read_byte_data ( mraa_i2c_context  dev,
const uint8_t  command 
)

Read a single byte from i2c context, from designated register

Parameters
devThe i2c context
commandThe register
Returns
The result of the read or -1 if failed

Here is the caller graph for this function:

int mraa_i2c_read_word_data ( mraa_i2c_context  dev,
const uint8_t  command 
)

Read a single word from i2c context, from designated register

Parameters
devThe i2c context
commandThe register
Returns
The result of the read or -1 if failed

Here is the caller graph for this function:

int mraa_i2c_read_bytes_data ( mraa_i2c_context  dev,
uint8_t  command,
uint8_t *  data,
int  length 
)

Bulk read from i2c context, starting from designated register

Parameters
devThe i2c context
commandThe register
datapointer to the byte array to read data in to
lengthmax number of bytes to read
Returns
The length in bytes passed to the function or -1

Here is the caller graph for this function:

mraa_result_t mraa_i2c_write ( mraa_i2c_context  dev,
const uint8_t *  data,
int  length 
)

Write length bytes to the bus, the first byte in the array is the command/register to write

Parameters
devThe i2c context
datapointer to the byte array to be written
lengththe number of bytes to transmit
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_i2c_write_byte ( mraa_i2c_context  dev,
const uint8_t  data 
)

Write a single byte to an i2c context

Parameters
devThe i2c context
dataThe byte to write
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_i2c_write_byte_data ( mraa_i2c_context  dev,
const uint8_t  data,
const uint8_t  command 
)

Write a single byte to an i2c context

Parameters
devThe i2c context
dataThe byte to write
commandThe register
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_i2c_write_word_data ( mraa_i2c_context  dev,
const uint16_t  data,
const uint8_t  command 
)

Write a single word to an i2c context

Parameters
devThe i2c context
dataThe word to write
commandThe register
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_i2c_address ( mraa_i2c_context  dev,
uint8_t  address 
)

Sets the i2c slave address.

Parameters
devThe i2c context
addressThe address to set for the slave (7-bit address)
Returns
Result of operation

Here is the caller graph for this function:

mraa_result_t mraa_i2c_stop ( mraa_i2c_context  dev)

De-inits an mraa_i2c_context device

Parameters
devThe i2c context
Returns
Result of operation

Here is the caller graph for this function:

Typedef Documentation

typedef struct _i2c* mraa_i2c_context

Opaque pointer definition to the internal struct _i2c

Include dependency graph for i2c.h: