![]() |
SyterKit 0.4.0.x
SyterKit is a bare-metal framework
|
System I2C (Inter-Integrated Circuit) driver for Allwinner (sunxi) platforms. More...
#include <io.h>#include <stdarg.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <types.h>#include <timer.h>#include <common.h>#include <log.h>#include <sys-clk.h>#include "reg-ncat.h"#include "sys-i2c.h"
Macros | |
| #define | I2C_WRITE 0 |
| I2C write transaction type. | |
| #define | I2C_READ 1 |
| I2C read transaction type. | |
| #define | I2C_OK 0 |
| I2C operation successful status. | |
| #define | I2C_NOK 1 |
| I2C operation failed general status. | |
| #define | I2C_NACK 2 |
| I2C operation failed with NACK status. | |
| #define | I2C_NOK_LA 3 /* Lost arbitration */ |
| I2C operation failed with lost arbitration status. | |
| #define | I2C_NOK_TOUT 4 /* time out */ |
| I2C operation failed with timeout status. | |
| #define | I2C_START_TRANSMIT 0x08 |
| I2C status code for successful start condition transmission. | |
| #define | I2C_RESTART_TRANSMIT 0x10 |
| I2C status code for successful repeated start condition transmission. | |
| #define | I2C_ADDRWRITE_ACK 0x18 |
| I2C status code for successful slave address write with ACK. | |
| #define | I2C_ADDRREAD_ACK 0x40 |
| I2C status code for successful slave address read with ACK. | |
| #define | I2C_DATAWRITE_ACK 0x28 |
| I2C status code for successful data write with ACK. | |
| #define | I2C_READY 0xf8 |
| I2C status code for bus ready state. | |
| #define | I2C_DATAREAD_NACK 0x58 |
| I2C status code for successful data read with NACK. | |
| #define | I2C_DATAREAD_ACK 0x50 |
| I2C status code for successful data read with ACK. | |
Functions | |
| static int32_t | sunxi_i2c_send_byteaddr (sunxi_i2c_t *i2c_dev, uint32_t byteaddr) |
| Send a byte address over I2C. | |
| static int32_t | sunxi_i2c_send_start (sunxi_i2c_t *i2c_dev) |
| Generate I2C start condition. | |
| static int32_t | sunxi_i2c_send_slave_addr (sunxi_i2c_t *i2c_dev, uint32_t saddr, uint32_t rw) |
| Send I2C slave address with read/write bit. | |
| static int32_t | sunxi_i2c_send_restart (sunxi_i2c_t *i2c_dev) |
| Generate I2C repeated start condition. | |
| static int32_t | sunxi_i2c_stop (sunxi_i2c_t *i2c_dev) |
| Generate I2C stop condition. | |
| static int32_t | sunxi_i2c_get_data (sunxi_i2c_t *i2c_dev, uint8_t *data_addr, uint32_t data_count) |
| Receive data from I2C device. | |
| static int32_t | sunxi_i2c_send_data (sunxi_i2c_t *i2c_dev, uint8_t *data_addr, uint32_t data_count) |
| Sends data to the I2C device. | |
| static int | _sunxi_i2c_read (sunxi_i2c_t *i2c_dev, uint8_t chip, uint32_t addr, int alen, uint8_t *buffer, int len) |
| sunxi_i2c read function | |
| static int | _sunxi_i2c_write (sunxi_i2c_t *i2c_dev, uint8_t chip, uint32_t addr, int alen, uint8_t *buffer, int len) |
| sunxi_i2c write function | |
| int | sunxi_i2c_write (sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t data) |
| Write a single byte to I2C device register. | |
| int | sunxi_i2c_read (sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t *data) |
| Read a single byte from I2C device register. | |
| static void | sunxi_i2c_bus_reset (sunxi_i2c_t *i2c_dev) |
| Resets the I2C bus for the specified I2C device. | |
| static void | sunxi_i2c_set_clock (sunxi_i2c_t *i2c_dev) |
| Configures the clock settings for the specified I2C device. | |
| static void | sunxi_i2c_bus_clk_open (sunxi_i2c_t *i2c_dev) |
| Opens the clock for the specified I2C device. | |
| static void | sunxi_i2c_bus_en (sunxi_i2c_t *i2c_dev) |
| Enables the I2C bus for the specified I2C device. | |
| void | sunxi_i2c_init (sunxi_i2c_t *i2c_dev) |
| Initialize I2C controller and bus. | |
System I2C (Inter-Integrated Circuit) driver for Allwinner (sunxi) platforms.
This file implements I2C communication functionality for Allwinner SoCs, supporting standard I2C master operations including initialization, read/write transactions, clock configuration, and bus reset. The driver handles both 100kHz (standard) and 400kHz (fast) I2C speeds.
| #define I2C_ADDRREAD_ACK 0x40 |
I2C status code for successful slave address read with ACK.
| #define I2C_ADDRWRITE_ACK 0x18 |
I2C status code for successful slave address write with ACK.
| #define I2C_DATAREAD_ACK 0x50 |
I2C status code for successful data read with ACK.
| #define I2C_DATAREAD_NACK 0x58 |
I2C status code for successful data read with NACK.
| #define I2C_DATAWRITE_ACK 0x28 |
I2C status code for successful data write with ACK.
| #define I2C_NACK 2 |
I2C operation failed with NACK status.
| #define I2C_NOK 1 |
I2C operation failed general status.
| #define I2C_NOK_LA 3 /* Lost arbitration */ |
I2C operation failed with lost arbitration status.
| #define I2C_NOK_TOUT 4 /* time out */ |
I2C operation failed with timeout status.
| #define I2C_OK 0 |
I2C operation successful status.
| #define I2C_READ 1 |
I2C read transaction type.
| #define I2C_READY 0xf8 |
I2C status code for bus ready state.
| #define I2C_RESTART_TRANSMIT 0x10 |
I2C status code for successful repeated start condition transmission.
| #define I2C_START_TRANSMIT 0x08 |
I2C status code for successful start condition transmission.
| #define I2C_WRITE 0 |
I2C write transaction type.
|
static |
sunxi_i2c read function
| i2c_dev | Pointer to the sunxi_i2c controller device structure |
| chip | Device address |
| addr | Register to be read from in the device |
| alen | Length of the register address |
| buffer | Buffer to store the read data |
| len | Length of the data to be read |
Internal I2C read function
Performs a complete I2C read transaction, including start, address, register, and data transfer.
| i2c_dev | Pointer to the I2C device structure |
| chip | Device address |
| addr | Register address to read from |
| alen | Length of the register address (1-3 bytes) |
| buffer | Buffer to store read data |
| len | Number of bytes to read |
|
static |
sunxi_i2c write function
| i2c_dev | Pointer to the sunxi_i2c controller device structure |
| chip | Device address |
| addr | Register to be read/written in the device |
| alen | Length of the register address |
| buffer | Data to be written/read |
| len | Length of the data |
Internal I2C write function
Performs a complete I2C write transaction, including start, address, register, and data transfer.
| i2c_dev | Pointer to the I2C device structure |
| chip | Device address |
| addr | Register address to write to |
| alen | Length of the register address (1-3 bytes) |
| buffer | Buffer containing data to write |
| len | Number of bytes to write |
|
inlinestatic |
Opens the clock for the specified I2C device.
This function de-asserts the reset signal for the I2C clock and enables the clock gating. It first de-asserts the reset, then clears the clock gating bit, waits for a brief period, and finally re-enables the clock.
| i2c_dev | Pointer to the I2C device structure that contains the clock control registers' base address and offsets. |
|
inlinestatic |
Enables the I2C bus for the specified I2C device.
This function sets the control register to enable the I2C bus and clears any error flags by writing to the error flag register.
| i2c_dev | Pointer to the I2C device structure that contains the base address for the I2C hardware registers. |
|
static |
Resets the I2C bus for the specified I2C device.
This function attempts to reset the I2C control by toggling the SCL and SDA lines until the bus is idle. It first checks the control register to determine if a reset is needed, and then it performs the necessary toggling of the lines.
| i2c_dev | Pointer to the I2C device structure that contains the base address for the I2C hardware registers. |
|
static |
Receive data from I2C device.
Receives multiple bytes of data from an I2C device, handling ACK/NACK appropriately.
| i2c_dev | Pointer to the I2C device structure |
| data_addr | Buffer to store received data |
| data_count | Number of bytes to receive |
| void sunxi_i2c_init | ( | sunxi_i2c_t * | i2c_dev | ) |
Initialize I2C controller and bus.
Initializes the I2C controller by configuring GPIO pins, enabling clocks, resetting the bus, setting clock frequency, and enabling the I2C controller.
| i2c_dev | Pointer to the I2C device structure containing configuration parameters |
| int sunxi_i2c_read | ( | sunxi_i2c_t * | i2c_dev, |
| uint8_t | addr, | ||
| uint32_t | reg, | ||
| uint8_t * | data | ||
| ) |
Read a single byte from I2C device register.
sunxi_i2c read function
Reads a single byte from a specified register on an I2C device.
| i2c_dev | Pointer to the I2C device structure |
| addr | I2C device address |
| reg | Register address to read from |
| data | Pointer to store the read byte |
|
static |
Send a byte address over I2C.
Sends a single byte address to the I2C device and waits for acknowledgment.
| i2c_dev | Pointer to the I2C device structure |
| byteaddr | Byte address to send |
|
static |
Sends data to the I2C device.
This function sends a specified number of bytes of data to the I2C device. It writes data to the data register and waits for the acknowledgment from the device for each byte sent. If a timeout occurs while waiting for acknowledgment, the function will return an error code.
| i2c_dev | Pointer to the I2C device structure containing the device's configuration and register base address. |
| data_addr | Pointer to the data buffer that contains the data to be sent. |
| data_count | The number of bytes to send from the data buffer. |
|
static |
Generate I2C repeated start condition.
Generates a repeated start condition on the I2C bus to change operation mode.
| i2c_dev | Pointer to the I2C device structure |
|
static |
Send I2C slave address with read/write bit.
Transmits the slave address along with read/write bit and waits for acknowledgment.
| i2c_dev | Pointer to the I2C device structure |
| saddr | Slave device address |
| rw | Read (1) or Write (0) operation |
|
static |
Generate I2C start condition.
Generates a start condition on the I2C bus to initiate communication.
| i2c_dev | Pointer to the I2C device structure |
|
static |
Configures the clock settings for the specified I2C device.
This function calculates and sets the appropriate clock divider values based on the desired I2C speed and the source clock frequency. It finds suitable values for clk_m and clk_n, and adjusts the clock control register accordingly.
| i2c_dev | Pointer to the I2C device structure that contains the base address for the I2C hardware registers and clock configuration parameters. |
|
static |
Generate I2C stop condition.
Generates a stop condition on the I2C bus to terminate communication.
| i2c_dev | Pointer to the I2C device structure |
| int sunxi_i2c_write | ( | sunxi_i2c_t * | i2c_dev, |
| uint8_t | addr, | ||
| uint32_t | reg, | ||
| uint8_t | data | ||
| ) |
Write a single byte to I2C device register.
sunxi_i2c write function
Writes a single byte to a specified register on an I2C device.
| i2c_dev | Pointer to the I2C device structure |
| addr | I2C device address |
| reg | Register address to write to |
| data | Byte value to write |