I am confused on how to add multiple slaves devices to one master. I thought it was to add another channel but that doesn't seem to be it as you can't have duplicate hardware channels.
How do you add another slave address? I currently have one device at 0x39 and wish to add one at 0x38, accessed through the same scl/sda line.
Solved! Go to Solution.
Dear, @pb632146
You don't need to make extra channels in order to connect several slave devices to a single I2C master on the same SDA/SCL line. Rather, you use the I2C driver to programmatically control the slave addresses. This is a solution breakdown that is developer-friendly:
Single I2C Channel: Keep using the same hardware channel (LPI2C_1) in your configuration.
Manage Slave Addresses in Code:
The configuration tool is for initializing one channel with one default slave address (e.g., 0x39).
In your application code, switch between slave addresses dynamically using API calls provided by your I2C driver.
Dynamic Address Switching:
Use your I2C library or HAL to set the target slave address before sending or receiving data. For example, in an NXP SDK environment:
c
I2C_MasterStart(LPI2C_1, 0x38, kI2C_Write);
// Perform operations with slave at 0x38
I2C_MasterStop(LPI2C_1);
I2C_MasterStart(LPI2C_1, 0x39, kI2C_Write);
// Perform operations with slave at 0x39
I2C_MasterStop(LPI2C_1);
Shared Bus Handling:
Ensure that only one device communicates at a time, as all slaves are connected to the same bus. This is managed automatically if the protocol is followed correctly.
In short, you don't need multiple channels—just handle the slave address selection in your application code!
Dear, @pb632146
You don't need to make extra channels in order to connect several slave devices to a single I2C master on the same SDA/SCL line. Rather, you use the I2C driver to programmatically control the slave addresses. This is a solution breakdown that is developer-friendly:
Single I2C Channel: Keep using the same hardware channel (LPI2C_1) in your configuration.
Manage Slave Addresses in Code:
The configuration tool is for initializing one channel with one default slave address (e.g., 0x39).
In your application code, switch between slave addresses dynamically using API calls provided by your I2C driver.
Dynamic Address Switching:
Use your I2C library or HAL to set the target slave address before sending or receiving data. For example, in an NXP SDK environment:
c
I2C_MasterStart(LPI2C_1, 0x38, kI2C_Write);
// Perform operations with slave at 0x38
I2C_MasterStop(LPI2C_1);
I2C_MasterStart(LPI2C_1, 0x39, kI2C_Write);
// Perform operations with slave at 0x39
I2C_MasterStop(LPI2C_1);
Shared Bus Handling:
Ensure that only one device communicates at a time, as all slaves are connected to the same bus. This is managed automatically if the protocol is followed correctly.
In short, you don't need multiple channels—just handle the slave address selection in your application code!
Thank you for the advice, I tried to implement this but what is ki2c_Write and masterstart/stop supposed to be for s32k344? Do i need to write those things myself? Are those functions in documentation?
Hi,
yes you're right.
Slave address is provided at initialization time through the master configuration structure, but it can be changed at runtime by using LPI2C_Ip_MasterSetSlaveAddr(), which sets the slave address which will be used for any future transfers initiated by the LPI2C master..
Got it!