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_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_send_data (uint32_t i2c, uint8_t data)
 I2C Send Data. More...
 
uint8_t i2c_get_data (uint32_t i2c)
 I2C Get Data. More...
 
void i2c_enable_analog_filter (uint32_t i2c)
 
void i2c_disable_analog_filter (uint32_t i2c)
 
void i2c_set_digital_filter (uint32_t i2c, uint8_t dnf_setting)
 Set the I2C digital filter. More...
 
void i2c_set_prescaler (uint32_t i2c, uint8_t presc)
 
void i2c_set_data_setup_time (uint32_t i2c, uint8_t s_time)
 
void i2c_set_data_hold_time (uint32_t i2c, uint8_t h_time)
 
void i2c_set_scl_high_period (uint32_t i2c, uint8_t period)
 
void i2c_set_scl_low_period (uint32_t i2c, uint8_t period)
 
void i2c_enable_stretching (uint32_t i2c)
 
void i2c_disable_stretching (uint32_t i2c)
 
void i2c_set_7bit_addr_mode (uint32_t i2c)
 
void i2c_set_10bit_addr_mode (uint32_t i2c)
 
void i2c_set_7bit_address (uint32_t i2c, uint8_t addr)
 
void i2c_set_10bit_address (uint32_t i2c, uint16_t addr)
 
void i2c_set_write_transfer_dir (uint32_t i2c)
 
void i2c_set_read_transfer_dir (uint32_t i2c)
 
void i2c_set_bytes_to_transfer (uint32_t i2c, uint32_t n_bytes)
 
bool i2c_is_start (uint32_t i2c)
 
void i2c_enable_autoend (uint32_t i2c)
 
void i2c_disable_autoend (uint32_t i2c)
 
bool i2c_nack (uint32_t i2c)
 
bool i2c_busy (uint32_t i2c)
 
bool i2c_transmit_int_status (uint32_t i2c)
 
bool i2c_transfer_complete (uint32_t i2c)
 
bool i2c_received_data (uint32_t i2c)
 
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_rxdma (uint32_t i2c)
 I2C Enable reception DMA. More...
 
void i2c_disable_rxdma (uint32_t i2c)
 I2C Disable reception DMA. More...
 
void i2c_enable_txdma (uint32_t i2c)
 I2C Enable transmission DMA. More...
 
void i2c_disable_txdma (uint32_t i2c)
 I2C Disable transmission DMA. More...
 
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

Function Documentation

◆ i2c_busy()

bool i2c_busy ( uint32_t  i2c)

Definition at line 268 of file i2c_common_v2.c.

References I2C_ISR, and I2C_ISR_BUSY.

◆ 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 89 of file i2c_common_v2.c.

References I2C_ICR, and I2C_ICR_STOPCF.

◆ i2c_disable_analog_filter()

void i2c_disable_analog_filter ( uint32_t  i2c)

Definition at line 150 of file i2c_common_v2.c.

References I2C_CR1, and I2C_CR1_ANFOFF.

◆ i2c_disable_autoend()

void i2c_disable_autoend ( uint32_t  i2c)

Definition at line 258 of file i2c_common_v2.c.

References I2C_CR2.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ 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 306 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_disable_rxdma()

void i2c_disable_rxdma ( uint32_t  i2c)

I2C Disable reception DMA.

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

Definition at line 326 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_disable_stretching()

void i2c_disable_stretching ( uint32_t  i2c)

Definition at line 205 of file i2c_common_v2.c.

References I2C_CR1, and I2C_CR1_NOSTRETCH.

◆ i2c_disable_txdma()

void i2c_disable_txdma ( uint32_t  i2c)

I2C Disable transmission DMA.

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

Definition at line 346 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_enable_analog_filter()

void i2c_enable_analog_filter ( uint32_t  i2c)

Definition at line 145 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_enable_autoend()

void i2c_enable_autoend ( uint32_t  i2c)

Definition at line 253 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_AUTOEND.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ 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 295 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_enable_rxdma()

void i2c_enable_rxdma ( uint32_t  i2c)

I2C Enable reception DMA.

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

Definition at line 316 of file i2c_common_v2.c.

References I2C_CR1, and I2C_CR1_RXDMAEN.

◆ i2c_enable_stretching()

void i2c_enable_stretching ( uint32_t  i2c)

Definition at line 200 of file i2c_common_v2.c.

References I2C_CR1.

◆ i2c_enable_txdma()

void i2c_enable_txdma ( uint32_t  i2c)

I2C Enable transmission DMA.

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

Definition at line 336 of file i2c_common_v2.c.

References I2C_CR1, and I2C_CR1_TXDMAEN.

◆ 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 140 of file i2c_common_v2.c.

References I2C_RXDR.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_is_start()

bool i2c_is_start ( uint32_t  i2c)

Definition at line 248 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_START.

◆ i2c_nack()

bool i2c_nack ( uint32_t  i2c)

Definition at line 263 of file i2c_common_v2.c.

References I2C_ISR, and I2C_ISR_NACKF.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ 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 48 of file i2c_common_v2.c.

References I2C_CR1.

◆ 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 33 of file i2c_common_v2.c.

References I2C_CR1, and I2C_CR1_PE.

◆ i2c_received_data()

bool i2c_received_data ( uint32_t  i2c)

Definition at line 283 of file i2c_common_v2.c.

References I2C_ISR, and I2C_ISR_RXNE.

Referenced by i2c_transfer7().

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 130 of file i2c_common_v2.c.

References I2C_TXDR.

Referenced by i2c_transfer7().

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 63 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_START.

Referenced by i2c_transfer7().

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 77 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_STOP.

◆ i2c_set_10bit_addr_mode()

void i2c_set_10bit_addr_mode ( uint32_t  i2c)

Definition at line 215 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_ADD10.

◆ i2c_set_10bit_address()

void i2c_set_10bit_address ( uint32_t  i2c,
uint16_t  addr 
)

Definition at line 226 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_SADD_10BIT_SHIFT.

◆ i2c_set_7bit_addr_mode()

void i2c_set_7bit_addr_mode ( uint32_t  i2c)

Definition at line 210 of file i2c_common_v2.c.

References I2C_CR2.

◆ i2c_set_7bit_address()

void i2c_set_7bit_address ( uint32_t  i2c,
uint8_t  addr 
)

Definition at line 220 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_SADD_7BIT_SHIFT.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_set_bytes_to_transfer()

void i2c_set_bytes_to_transfer ( uint32_t  i2c,
uint32_t  n_bytes 
)

Definition at line 242 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_NBYTES_SHIFT.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_set_data_hold_time()

void i2c_set_data_hold_time ( uint32_t  i2c,
uint8_t  h_time 
)

Definition at line 182 of file i2c_common_v2.c.

References I2C_TIMINGR, and I2C_TIMINGR_SDADEL_SHIFT.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_data_setup_time()

void i2c_set_data_setup_time ( uint32_t  i2c,
uint8_t  s_time 
)

Definition at line 176 of file i2c_common_v2.c.

References I2C_TIMINGR, and I2C_TIMINGR_SCLDEL_SHIFT.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_digital_filter()

void i2c_set_digital_filter ( uint32_t  i2c,
uint8_t  dnf_setting 
)

Set the I2C digital filter.

These bits are used to configure the digital noise filter on SDA and SCL input. The digital filter will filter spikes with a length of up to dnf_setting * I2CCLK clocks

Parameters
i2cperipheral of interest
dnf_setting0 to disable, else 1..15 i2c clocks

Definition at line 163 of file i2c_common_v2.c.

References I2C_CR1, I2C_CR1_DNF_MASK, and I2C_CR1_DNF_SHIFT.

◆ 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 118 of file i2c_common_v2.c.

References I2C_OAR1, and I2C_OAR1_OA1MODE.

◆ 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 103 of file i2c_common_v2.c.

References I2C_OAR1.

◆ i2c_set_prescaler()

void i2c_set_prescaler ( uint32_t  i2c,
uint8_t  presc 
)

Definition at line 170 of file i2c_common_v2.c.

References I2C_TIMINGR, and I2C_TIMINGR_PRESC_SHIFT.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_read_transfer_dir()

void i2c_set_read_transfer_dir ( uint32_t  i2c)

Definition at line 237 of file i2c_common_v2.c.

References I2C_CR2, and I2C_CR2_RD_WRN.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_set_scl_high_period()

void i2c_set_scl_high_period ( uint32_t  i2c,
uint8_t  period 
)

Definition at line 188 of file i2c_common_v2.c.

References I2C_TIMINGR, and I2C_TIMINGR_SCLH_SHIFT.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_scl_low_period()

void i2c_set_scl_low_period ( uint32_t  i2c,
uint8_t  period 
)

Definition at line 194 of file i2c_common_v2.c.

References I2C_TIMINGR, and I2C_TIMINGR_SCLL_SHIFT.

Referenced by i2c_set_speed().

Here is the caller graph for this function:

◆ i2c_set_speed()

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

Set the i2c communication speed.

NOTE: 1MHz mode not yet implemented! Min clock speed: 8MHz for FM, 2Mhz for SM,

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 420 of file i2c_common_v2.c.

References i2c_set_data_hold_time(), i2c_set_data_setup_time(), i2c_set_prescaler(), i2c_set_scl_high_period(), i2c_set_scl_low_period(), i2c_speed_fm_400k, i2c_speed_fmp_1m, and i2c_speed_sm_100k.

Here is the call graph for this function:

◆ i2c_set_write_transfer_dir()

void i2c_set_write_transfer_dir ( uint32_t  i2c)

Definition at line 232 of file i2c_common_v2.c.

References I2C_CR2.

Referenced by i2c_transfer7().

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

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 362 of file i2c_common_v2.c.

References i2c_disable_autoend(), i2c_enable_autoend(), i2c_get_data(), i2c_nack(), i2c_received_data(), i2c_send_data(), i2c_send_start(), i2c_set_7bit_address(), i2c_set_bytes_to_transfer(), i2c_set_read_transfer_dir(), i2c_set_write_transfer_dir(), i2c_transfer_complete(), and i2c_transmit_int_status().

Here is the call graph for this function:

◆ i2c_transfer_complete()

bool i2c_transfer_complete ( uint32_t  i2c)

Definition at line 278 of file i2c_common_v2.c.

References I2C_ISR, and I2C_ISR_TC.

Referenced by i2c_transfer7().

Here is the caller graph for this function:

◆ i2c_transmit_int_status()

bool i2c_transmit_int_status ( uint32_t  i2c)

Definition at line 273 of file i2c_common_v2.c.

References I2C_ISR, and I2C_ISR_TXIS.

Referenced by i2c_transfer7().

Here is the caller graph for this function: