libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
I2C peripheral API
Collaboration diagram for I2C peripheral API:

Functions

void i2c_reset (uint32_t i2c)
 I2C Reset. More...
 
void i2c_peripheral_enable (uint32_t i2c)
 I2C Peripheral Enable. More...
 
void i2c_peripheral_disable (uint32_t i2c)
 I2C Peripheral Disable. More...
 
void i2c_send_start (uint32_t i2c)
 I2C Send Start Condition. More...
 
void i2c_send_stop (uint32_t i2c)
 I2C Send Stop Condition. More...
 
void i2c_clear_stop (uint32_t i2c)
 I2C Clear Stop Flag. More...
 
void i2c_set_own_7bit_slave_address (uint32_t i2c, uint8_t slave)
 I2C Set the 7 bit Slave Address for the Peripheral. More...
 
void i2c_set_own_10bit_slave_address (uint32_t i2c, uint16_t slave)
 I2C Set the 10 bit Slave Address for the Peripheral. More...
 
void i2c_set_own_7bit_slave_address_two (uint32_t i2c, uint8_t slave)
 I2C Set the secondary 7 bit Slave Address for the Peripheral. More...
 
void i2c_enable_dual_addressing_mode (uint32_t i2c)
 I2C Enable dual addressing mode for the Peripheral. More...
 
void i2c_disable_dual_addressing_mode (uint32_t i2c)
 I2C Disable dual addressing mode for the Peripheral. More...
 
void i2c_set_clock_frequency (uint32_t i2c, uint8_t freq)
 I2C Set Peripheral Clock Frequency. More...
 
void i2c_send_data (uint32_t i2c, uint8_t data)
 I2C Send Data. More...
 
void i2c_set_fast_mode (uint32_t i2c)
 I2C Set Fast Mode. More...
 
void i2c_set_standard_mode (uint32_t i2c)
 I2C Set Standard Mode. More...
 
void i2c_set_ccr (uint32_t i2c, uint16_t freq)
 I2C Set Bus Clock Frequency. More...
 
void i2c_set_trise (uint32_t i2c, uint16_t trise)
 I2C Set the Rise Time. More...
 
void i2c_send_7bit_address (uint32_t i2c, uint8_t slave, uint8_t readwrite)
 I2C Send the 7-bit Slave Address. More...
 
uint8_t i2c_get_data (uint32_t i2c)
 I2C Get Data. More...
 
void i2c_enable_interrupt (uint32_t i2c, uint32_t interrupt)
 I2C Enable Interrupt. More...
 
void i2c_disable_interrupt (uint32_t i2c, uint32_t interrupt)
 I2C Disable Interrupt. More...
 
void i2c_enable_ack (uint32_t i2c)
 I2C Enable ACK. More...
 
void i2c_disable_ack (uint32_t i2c)
 I2C Disable ACK. More...
 
void i2c_nack_next (uint32_t i2c)
 I2C NACK Next Byte. More...
 
void i2c_nack_current (uint32_t i2c)
 I2C NACK Next Byte. More...
 
void i2c_set_dutycycle (uint32_t i2c, uint32_t dutycycle)
 I2C Set clock duty cycle. More...
 
void i2c_enable_dma (uint32_t i2c)
 I2C Enable DMA. More...
 
void i2c_disable_dma (uint32_t i2c)
 I2C Disable DMA. More...
 
void i2c_set_dma_last_transfer (uint32_t i2c)
 I2C Set DMA last transfer. More...
 
void i2c_clear_dma_last_transfer (uint32_t i2c)
 I2C Clear DMA last transfer. More...
 
static void i2c_write7_v1 (uint32_t i2c, int addr, const uint8_t *data, size_t n)
 
static void i2c_read7_v1 (uint32_t i2c, int addr, uint8_t *res, size_t n)
 
void i2c_transfer7 (uint32_t i2c, uint8_t addr, const uint8_t *w, size_t wn, uint8_t *r, size_t rn)
 Run a write/read transaction to a given 7bit i2c address If both write & read are provided, the read will use repeated start. More...
 
void i2c_set_speed (uint32_t i2c, enum i2c_speeds speed, uint32_t clock_megahz)
 Set the i2c communication speed. More...
 

Detailed Description

Author
© 2010 Thomas Otto tommi.nosp@m.@via.nosp@m.dmin..nosp@m.org
© 2012 Ken Sarkies ksark.nosp@m.ies@.nosp@m.inter.nosp@m.node.nosp@m..on.n.nosp@m.et

Devices can have up to three I2C peripherals. The peripherals support SMBus and PMBus variants.

A peripheral begins after reset in Slave mode. To become a Master a start condition must be generated. The peripheral will remain in Master mode unless a multimaster contention is lost or a stop condition is generated.

Function Documentation

◆ i2c_clear_dma_last_transfer()

void i2c_clear_dma_last_transfer ( uint32_t  i2c)

I2C Clear DMA last transfer.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 463 of file i2c_common_v1.c.

References I2C_CR2, and I2C_CR2_LAST.

◆ i2c_clear_stop()

void i2c_clear_stop ( uint32_t  i2c)

I2C Clear Stop Flag.

Clear the "Send Stop" flag in the I2C config register

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 140 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_STOP.

◆ i2c_disable_ack()

void i2c_disable_ack ( uint32_t  i2c)

I2C Disable ACK.

Disables acking of own 7/10 bit address

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 385 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_ACK.

Referenced by i2c_read7_v1().

Here is the caller graph for this function:

◆ i2c_disable_dma()

void i2c_disable_dma ( uint32_t  i2c)

I2C Disable DMA.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 443 of file i2c_common_v1.c.

References I2C_CR2, and I2C_CR2_DMAEN.

◆ i2c_disable_dual_addressing_mode()

void i2c_disable_dual_addressing_mode ( uint32_t  i2c)

I2C Disable dual addressing mode for the Peripheral.

Only OAR1 is recognised in 7-bit addressing mode.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 215 of file i2c_common_v1.c.

References I2C_OAR2, and I2C_OAR2_ENDUAL.

◆ i2c_disable_interrupt()

void i2c_disable_interrupt ( uint32_t  i2c,
uint32_t  interrupt 
)

I2C Disable Interrupt.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]interruptUnsigned int32. Interrupt to disable.

Definition at line 363 of file i2c_common_v1.c.

References I2C_CR2.

◆ i2c_enable_ack()

void i2c_enable_ack ( uint32_t  i2c)

I2C Enable ACK.

Enables acking of own 7/10 bit address

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 374 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_ACK.

Referenced by i2c_read7_v1().

Here is the caller graph for this function:

◆ i2c_enable_dma()

void i2c_enable_dma ( uint32_t  i2c)

I2C Enable DMA.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 433 of file i2c_common_v1.c.

References I2C_CR2, and I2C_CR2_DMAEN.

◆ i2c_enable_dual_addressing_mode()

void i2c_enable_dual_addressing_mode ( uint32_t  i2c)

I2C Enable dual addressing mode for the Peripheral.

Both OAR1 and OAR2 are recognised in 7-bit addressing mode.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 202 of file i2c_common_v1.c.

References I2C_OAR2, and I2C_OAR2_ENDUAL.

◆ i2c_enable_interrupt()

void i2c_enable_interrupt ( uint32_t  i2c,
uint32_t  interrupt 
)

I2C Enable Interrupt.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]interruptUnsigned int32. Interrupt to enable.

Definition at line 352 of file i2c_common_v1.c.

References I2C_CR2.

◆ i2c_get_data()

uint8_t i2c_get_data ( uint32_t  i2c)

I2C Get Data.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 341 of file i2c_common_v1.c.

References I2C_DR.

Referenced by i2c_read7_v1().

Here is the caller graph for this function:

◆ i2c_nack_current()

void i2c_nack_current ( uint32_t  i2c)

I2C NACK Next Byte.

Causes the I2C controller to NACK the reception of the current byte

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 408 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_POS.

◆ i2c_nack_next()

void i2c_nack_next ( uint32_t  i2c)

I2C NACK Next Byte.

Causes the I2C controller to NACK the reception of the next byte

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 396 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_POS.

◆ i2c_peripheral_disable()

void i2c_peripheral_disable ( uint32_t  i2c)

I2C Peripheral Disable.

This must not be reset while in Master mode until a communication has finished. In Slave mode, the peripheral is disabled only after communication has ended.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 99 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_PE.

◆ i2c_peripheral_enable()

void i2c_peripheral_enable ( uint32_t  i2c)

I2C Peripheral Enable.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 85 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_PE.

◆ i2c_read7_v1()

static void i2c_read7_v1 ( uint32_t  i2c,
int  addr,
uint8_t *  res,
size_t  n 
)
static

Definition at line 494 of file i2c_common_v1.c.

References i2c_disable_ack(), i2c_enable_ack(), i2c_get_data(), I2C_READ, i2c_send_7bit_address(), i2c_send_start(), i2c_send_stop(), I2C_SR1, I2C_SR1_ADDR, I2C_SR1_RxNE, I2C_SR1_SB, I2C_SR2, I2C_SR2_BUSY, and I2C_SR2_MSL.

Referenced by i2c_transfer7().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ i2c_reset()

void i2c_reset ( uint32_t  i2c)

I2C Reset.

The I2C peripheral and all its associated configuration registers are placed in the reset condition. The reset is effected via the RCC peripheral reset system.

Parameters
[in]i2cUnsigned int32. I2C peripheral identifier I2C register base address.

Definition at line 53 of file i2c_common_v1.c.

References I2C1, I2C2, rcc_periph_reset_pulse(), RST_I2C1, RST_I2C2, and RST_I2C3.

Here is the call graph for this function:

◆ i2c_send_7bit_address()

void i2c_send_7bit_address ( uint32_t  i2c,
uint8_t  slave,
uint8_t  readwrite 
)

I2C Send the 7-bit Slave Address.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]slaveUnsigned int16. Slave address 0...1023.
[in]readwriteUnsigned int8. Single bit to instruct slave to receive or send I2C Read/Write bit.

Definition at line 331 of file i2c_common_v1.c.

References I2C_DR.

Referenced by i2c_read7_v1(), and i2c_write7_v1().

Here is the caller graph for this function:

◆ i2c_send_data()

void i2c_send_data ( uint32_t  i2c,
uint8_t  data 
)

I2C Send Data.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]dataUnsigned int8. Byte to send.

Definition at line 248 of file i2c_common_v1.c.

References I2C_DR.

Referenced by i2c_write7_v1().

Here is the caller graph for this function:

◆ i2c_send_start()

void i2c_send_start ( uint32_t  i2c)

I2C Send Start Condition.

If in Master mode this will cause a restart condition to occur at the end of the current transmission. If in Slave mode, this will initiate a start condition when the current bus activity is completed.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 114 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_START.

Referenced by i2c_read7_v1(), and i2c_write7_v1().

Here is the caller graph for this function:

◆ i2c_send_stop()

void i2c_send_stop ( uint32_t  i2c)

I2C Send Stop Condition.

After the current byte transfer this will initiate a stop condition if in Master mode, or simply release the bus if in Slave mode.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 128 of file i2c_common_v1.c.

References I2C_CR1, and I2C_CR1_STOP.

Referenced by i2c_read7_v1(), and i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_set_ccr()

void i2c_set_ccr ( uint32_t  i2c,
uint16_t  freq 
)

I2C Set Bus Clock Frequency.

Set the bus clock frequency. This is a 12 bit number (0...4095) calculated from the formulae given in the STM32F1 reference manual in the description of the CCR field. It is a divisor of the peripheral clock frequency i2c_set_clock_frequency modified by the fast mode setting i2c_set_fast_mode

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]freqUnsigned int16. Bus Clock Frequency Setting 0...4095.

Definition at line 296 of file i2c_common_v1.c.

References I2C_CCR.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_clock_frequency()

void i2c_set_clock_frequency ( uint32_t  i2c,
uint8_t  freq 
)

I2C Set Peripheral Clock Frequency.

Set the peripheral clock frequency: 2MHz to 36MHz (the APB frequency). Note that this is not the I2C bus clock. This is set in conjunction with the Clock Control register to generate the Master bus clock, see i2c_set_ccr

Parameters
[in]i2cI2C register base address I2C register base address
[in]freqClock Frequency Setting in MHz, valid range depends on part,+ normally 2Mhz->Max APB speed.

Definition at line 233 of file i2c_common_v1.c.

References I2C_CR2.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_dma_last_transfer()

void i2c_set_dma_last_transfer ( uint32_t  i2c)

I2C Set DMA last transfer.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 453 of file i2c_common_v1.c.

References I2C_CR2, and I2C_CR2_LAST.

◆ i2c_set_dutycycle()

void i2c_set_dutycycle ( uint32_t  i2c,
uint32_t  dutycycle 
)

I2C Set clock duty cycle.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]dutycycleUnsigned int32. I2C duty cycle I2C peripheral clock duty cycles.

Definition at line 419 of file i2c_common_v1.c.

References I2C_CCR, I2C_CCR_DUTY, and I2C_CCR_DUTY_DIV2.

◆ i2c_set_fast_mode()

void i2c_set_fast_mode ( uint32_t  i2c)

I2C Set Fast Mode.

Set the clock frequency to the high clock rate mode (up to 400kHz). The actual clock frequency must be set with i2c_set_clock_frequency

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 262 of file i2c_common_v1.c.

References I2C_CCR, and I2C_CCR_FS.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_own_10bit_slave_address()

void i2c_set_own_10bit_slave_address ( uint32_t  i2c,
uint16_t  slave 
)

I2C Set the 10 bit Slave Address for the Peripheral.

This sets an address for Slave mode operation, in 10 bit form.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]slaveUnsigned int16. Slave address 0...1023.

Definition at line 173 of file i2c_common_v1.c.

References I2C_OAR1, and I2C_OAR1_ADDMODE.

◆ i2c_set_own_7bit_slave_address()

void i2c_set_own_7bit_slave_address ( uint32_t  i2c,
uint8_t  slave 
)

I2C Set the 7 bit Slave Address for the Peripheral.

This sets an address for Slave mode operation, in 7 bit form.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]slaveUnsigned int8. Slave address 0...127.

Definition at line 154 of file i2c_common_v1.c.

References I2C_OAR1.

◆ i2c_set_own_7bit_slave_address_two()

void i2c_set_own_7bit_slave_address_two ( uint32_t  i2c,
uint8_t  slave 
)

I2C Set the secondary 7 bit Slave Address for the Peripheral.

This sets a secondary address for Slave mode operation, in 7 bit form.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]slaveUnsigned int8. Slave address 0...127.

Definition at line 188 of file i2c_common_v1.c.

References I2C_OAR2.

◆ i2c_set_speed()

void i2c_set_speed ( uint32_t  i2c,
enum i2c_speeds  speed,
uint32_t  clock_megahz 
)

Set the i2c communication speed.

Parameters
i2cperipheral, eg I2C1
speedone of the listed speed modes i2c_speeds
clock_megahzi2c peripheral clock speed in MHz. Usually, rcc_apb1_frequency / 1e6

Definition at line 552 of file i2c_common_v1.c.

References i2c_set_ccr(), i2c_set_clock_frequency(), i2c_set_fast_mode(), i2c_set_standard_mode(), i2c_set_trise(), i2c_speed_fm_400k, and i2c_speed_sm_100k.

Here is the call graph for this function:

◆ i2c_set_standard_mode()

void i2c_set_standard_mode ( uint32_t  i2c)

I2C Set Standard Mode.

Set the clock frequency to the standard clock rate mode (up to 100kHz). The actual clock frequency must be set with i2c_set_clock_frequency

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.

Definition at line 276 of file i2c_common_v1.c.

References I2C_CCR, and I2C_CCR_FS.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_trise()

void i2c_set_trise ( uint32_t  i2c,
uint16_t  trise 
)

I2C Set the Rise Time.

Set the maximum rise time on the bus according to the I2C specification, as 1 more than the specified rise time in peripheral clock cycles. This is a 6 bit number.

Parameters
[in]i2cUnsigned int32. I2C register base address I2C register base address.
[in]triseUnsigned int16. Rise Time Setting 0...63.

Definition at line 317 of file i2c_common_v1.c.

References I2C_TRISE.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_transfer7()

void i2c_transfer7 ( uint32_t  i2c,
uint8_t  addr,
const uint8_t *  w,
size_t  wn,
uint8_t *  r,
size_t  rn 
)

Run a write/read transaction to a given 7bit i2c address If both write & read are provided, the read will use repeated start.

Both write and read are optional There are likely still issues with repeated start/stop condtions!

Parameters
i2cperipheral of choice, eg I2C1
addr7 bit i2c device address
wbuffer of data to write
wnlength of w
rdestination buffer to read into
rnnumber of bytes to read (r should be at least this long)

Definition at line 535 of file i2c_common_v1.c.

References i2c_read7_v1(), i2c_send_stop(), and i2c_write7_v1().

Here is the call graph for this function:

◆ i2c_write7_v1()

static void i2c_write7_v1 ( uint32_t  i2c,
int  addr,
const uint8_t *  data,
size_t  n 
)
static

Definition at line 468 of file i2c_common_v1.c.

References i2c_send_7bit_address(), i2c_send_data(), i2c_send_start(), I2C_SR1, I2C_SR1_ADDR, I2C_SR1_BTF, I2C_SR1_SB, I2C_SR2, I2C_SR2_BUSY, I2C_SR2_MSL, and I2C_WRITE.

Referenced by i2c_transfer7().

Here is the call graph for this function:
Here is the caller graph for this function: