bcm2835  1.52
Functions
I2C access

Functions

int bcm2835_i2c_begin (void)
 
void bcm2835_i2c_end (void)
 
void bcm2835_i2c_setSlaveAddress (uint8_t addr)
 
void bcm2835_i2c_setClockDivider (uint16_t divider)
 
void bcm2835_i2c_set_baudrate (uint32_t baudrate)
 
uint8_t bcm2835_i2c_write (const char *buf, uint32_t len)
 
uint8_t bcm2835_i2c_read (char *buf, uint32_t len)
 
uint8_t bcm2835_i2c_read_register_rs (char *regaddr, char *buf, uint32_t len)
 
uint8_t bcm2835_i2c_write_read_rs (char *cmds, uint32_t cmds_len, char *buf, uint32_t buf_len)
 

Detailed Description

These functions let you use I2C (The Broadcom Serial Control bus with the Philips I2C bus/interface version 2.1 January 2000.) to interface with an external I2C device.

Function Documentation

int bcm2835_i2c_begin ( void  )

Start I2C operations. Forces RPi I2C pins P1-03 (SDA) and P1-05 (SCL) to alternate function ALT0, which enables those pins for I2C interface. You should call bcm2835_i2c_end() when all I2C functions are complete to return the pins to their default functions

Returns
1 if successful, 0 otherwise (perhaps because you are not running as root)
See also
bcm2835_i2c_end()
void bcm2835_i2c_end ( void  )

End I2C operations. I2C pins P1-03 (SDA) and P1-05 (SCL) are returned to their default INPUT behaviour.

uint8_t bcm2835_i2c_read ( char *  buf,
uint32_t  len 
)

Transfers any number of bytes from the currently selected I2C slave. (as previously set by

See also
bcm2835_i2c_setSlaveAddress)
Parameters
[in]bufBuffer of bytes to receive.
[in]lenNumber of bytes in the buf buffer, and the number of bytes to received.
Returns
reason see bcm2835I2CReasonCodes
uint8_t bcm2835_i2c_read_register_rs ( char *  regaddr,
char *  buf,
uint32_t  len 
)

Allows reading from I2C slaves that require a repeated start (without any prior stop) to read after the required slave register has been set. For example, the popular MPL3115A2 pressure and temperature sensor. Note that your device must support or require this mode. If your device does not require this mode then the standard combined:

See also
bcm2835_i2c_write
bcm2835_i2c_read are a better choice. Will read from the slave previously set by
bcm2835_i2c_setSlaveAddress
Parameters
[in]regaddrBuffer containing the slave register you wish to read from.
[in]bufBuffer of bytes to receive.
[in]lenNumber of bytes in the buf buffer, and the number of bytes to received.
Returns
reason see bcm2835I2CReasonCodes
void bcm2835_i2c_set_baudrate ( uint32_t  baudrate)

Sets the I2C clock divider by converting the baudrate parameter to the equivalent I2C clock divider. ( see

See also
bcm2835_i2c_setClockDivider) For the I2C standard 100khz you would set baudrate to 100000 The use of baudrate corresponds to its use in the I2C kernel device driver. (Of course, bcm2835 has nothing to do with the kernel driver)
void bcm2835_i2c_setClockDivider ( uint16_t  divider)

Sets the I2C clock divider and therefore the I2C clock speed.

Parameters
[in]dividerThe desired I2C clock divider, one of BCM2835_I2C_CLOCK_DIVIDER_*, see bcm2835I2CClockDivider
void bcm2835_i2c_setSlaveAddress ( uint8_t  addr)

Sets the I2C slave address.

Parameters
[in]addrThe I2C slave address.
uint8_t bcm2835_i2c_write ( const char *  buf,
uint32_t  len 
)

Transfers any number of bytes to the currently selected I2C slave. (as previously set by

See also
bcm2835_i2c_setSlaveAddress)
Parameters
[in]bufBuffer of bytes to send.
[in]lenNumber of bytes in the buf buffer, and the number of bytes to send.
Returns
reason see bcm2835I2CReasonCodes
uint8_t bcm2835_i2c_write_read_rs ( char *  cmds,
uint32_t  cmds_len,
char *  buf,
uint32_t  buf_len 
)

Allows sending an arbitrary number of bytes to I2C slaves before issuing a repeated start (with no prior stop) and reading a response. Necessary for devices that require such behavior, such as the MLX90620. Will write to and read from the slave previously set by

See also
bcm2835_i2c_setSlaveAddress
Parameters
[in]cmdsBuffer containing the bytes to send before the repeated start condition.
[in]cmds_lenNumber of bytes to send from cmds buffer
[in]bufBuffer of bytes to receive.
[in]buf_lenNumber of bytes to receive in the buf buffer.
Returns
reason see bcm2835I2CReasonCodes