I2C API
The ALP SDK I2C driver provides a CMSIS-Driver compatible interface for I2C master and slave communication across all E1M modules.
Header
#include "alp_i2c.h"
Initialization
#include "alp_i2c.h"
/* Get the I2C driver instance for bus 0 */
ALP_I2C_Driver_t *i2c = ALP_I2C_GetDriver(0);
/* Initialize and power on */
i2c->Initialize(i2c_event_callback);
i2c->PowerControl(ALP_POWER_FULL);
/* Configure as master, standard mode (100 kHz) */
i2c->Control(ALP_I2C_BUS_SPEED, ALP_I2C_BUS_SPEED_STANDARD);
Master Transmit
#define SENSOR_ADDR 0x48
uint8_t tx_data[] = {0x00, 0x01}; /* Register address + value */
i2c->MasterTransmit(SENSOR_ADDR, tx_data, sizeof(tx_data), false);
while (i2c->GetStatus().busy);
The xfer_pending parameter (last argument) controls the STOP condition:
false-- Send STOP after the transfer (normal single transaction)true-- Do not send STOP (for repeated START sequences)
Master Receive
uint8_t reg_addr = 0x00;
uint8_t rx_data[2];
/* Write register address with repeated START */
i2c->MasterTransmit(SENSOR_ADDR, ®_addr, 1, true);
while (i2c->GetStatus().busy);
/* Read 2 bytes from the register */
i2c->MasterReceive(SENSOR_ADDR, rx_data, 2, false);
while (i2c->GetStatus().busy);
Event Callback
void i2c_event_callback(uint32_t event)
{
if (event & ALP_I2C_EVENT_TRANSFER_DONE) {
/* Transfer completed successfully */
}
if (event & ALP_I2C_EVENT_TRANSFER_INCOMPLETE) {
/* Slave sent NACK before all bytes transferred */
}
if (event & ALP_I2C_EVENT_ADDRESS_NACK) {
/* No slave acknowledged the address */
}
if (event & ALP_I2C_EVENT_BUS_ERROR) {
/* Bus error (misplaced START/STOP) */
}
}
Bus Speed Modes
| Mode | Constant | Speed |
|---|---|---|
| Standard | ALP_I2C_BUS_SPEED_STANDARD | 100 kHz |
| Fast | ALP_I2C_BUS_SPEED_FAST | 400 kHz |
| Fast Plus | ALP_I2C_BUS_SPEED_FAST_PLUS | 1 MHz |
API Reference
Functions
| Function | Description |
|---|---|
ALP_I2C_GetDriver() | Get driver instance for an I2C bus |
Initialize() | Initialize the I2C driver with event callback |
Uninitialize() | De-initialize and release resources |
PowerControl() | Set power state |
Control() | Configure bus speed and addressing mode |
MasterTransmit() | Transmit data as bus master |
MasterReceive() | Receive data as bus master |
SlaveTransmit() | Transmit data as bus slave |
SlaveReceive() | Receive data as bus slave |
GetDataCount() | Get number of bytes transferred |
GetStatus() | Get current driver status |
Abort() | Abort an in-progress transfer |
Events
| Event | Description |
|---|---|
ALP_I2C_EVENT_TRANSFER_DONE | Transfer completed successfully |
ALP_I2C_EVENT_TRANSFER_INCOMPLETE | Transfer incomplete (NACK received) |
ALP_I2C_EVENT_ADDRESS_NACK | Slave address not acknowledged |
ALP_I2C_EVENT_ARBITRATION_LOST | Master lost bus arbitration |
ALP_I2C_EVENT_BUS_ERROR | Bus error detected |
Pull-Up Resistors
I2C requires external pull-up resistors on SDA and SCL. Recommended values:
| Speed Mode | Pull-Up Resistance |
|---|---|
| Standard | 4.7 kohm |
| Fast | 2.2 kohm |
| Fast Plus | 1 kohm |
See the Design Guide for carrier board I2C layout recommendations.