I2C¶
Macros¶
-
I2C_OFFSET_0
¶ 0x205000
-
I2C_OFFSET_1
¶ 0x804000
The BCM2835/6/7 has three BSC (I2C) controllers, from which only one is connected to the I2C pins. The older Pis (where the last four digits of the revision number are less than
0004
) have BSC0 connected to the pins, all the other BSC1. The configure script tries to read the/proc/cpuinfo
file, which includes the revision number, and than definesUSE_I2C_BUS_0
accordingly. See Installation
-
I2C_SIZE
¶ 0x18
This macro holds the size of the I2C registers which needs to be mapped.
I2C_FIFO_SIZE
I2C_C_I2CEN
I2C_C_ST
I2C_C_CLEAR
I2C_C_READ
I2C_S_RXS
I2C_S_TXD
I2C_S_DONE
Registers¶
-
volatile uint32_t *
i2c_base_ptr
¶ This pointer points, when mapped, to the base of the I2C registers.
-
struct
i2c_register_map
¶ This struct maps the registers of the BSC controller. The names of the struct members correspond to the registers from the Datasheet:
struct i2c_register_map { uint32_t C; uint32_t S; uint32_t DLEN; uint32_t A; uint32_t FIFO; uint32_t DIV; uint32_t DEL; uint32_t CLKT; };
-
I2C
¶ #define I2C ((volatile struct i2c_register_map *)i2c_base_ptr)
By using this macro, the registers of the I2C can be accessed like this
I2C->FIFO
.
Functions¶
-
uint32_t *
i2c_map
(void)¶ This function maps the I2C registers. It calls
peripheral_map()
with the valuesI2C_OFFSET
andI2C_SIZE
.I2C_OFFSET
is defined ini2c.c
.
-
void
i2c_unmap
(void)¶ This function unmaps the I2C registers.
-
void
i2c_set_address
(uint8_t addr)¶ This function sets the address of the I2C device to communicate with. The address is a seven bit value.
-
void
i2c_set_clkdiv
(uint16_t divisor)¶ This function sets the clock divisor of the BSC controller.
-
void
i2c_set_clkstr
(uint16_t clkstr)¶ This function sets the clock stretch timeout (or delay). This means that the master will wait
clkstr
cycles after the rising clock edge for the slave to respond. After this the timeout flag is set. This can often be left at reset value0x40
.
-
void
i2c_start
(void)¶ Starts the BSC controller and clears the flag register.
-
void
i2c_stop
(void)¶ Disables the BSC controller.
-
void
i2c_write_byte
(uint8_t byte)¶ Write a
byte
of data.
-
uint8_t
i2c_read_byte
(void)¶ This function receives a
byte
of data and returns it.
-
void
i2c_write_data
(const uint8_t *data, uint16_t length)¶ This function writes
length
bytes of data pointed to bydata
.
-
void
i2c_read_data
(uint8_t *data, uint16_t length)¶ This function receives
length
bytes of data and writes them to the arraydata
.
-
void
i2c_write_register
(uint8_t reg, uint8_t data)¶ This function writes to bytes of data. First
reg
and thendata
.
Note
You cannot use two calls to i2c_write_byte()
instead of this function because this is only one transmission, while two times i2c_write_byte()
would be two different transmissons.
-
uint8_t
i2c_read_register
(uint8_t reg)¶ In contrast to
i2c_write_register()
you can use a call toi2c_write_byte()
and toi2c_read_byte()
. This is because I2C needs to make two transmissions anyway to change the read / write bit.