Accessing multiple slave devices

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Accessing multiple slave devices

Jump to solution
354 Views
pb632146
Contributor IV

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.

 

pb632146_0-1734097549706.png

 

0 Kudos
Reply
1 Solution
347 Views
AshutoshNama
Contributor II

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!

View solution in original post

0 Kudos
Reply
5 Replies
348 Views
AshutoshNama
Contributor II

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!

0 Kudos
Reply
314 Views
pb632146
Contributor IV

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?

0 Kudos
Reply
311 Views
pb632146
Contributor IV
Found its Lpi2c_Ip_MasterSetSlaveAddr i dont need to toggle it on and off just change the slave address
0 Kudos
Reply
250 Views
PetrS
NXP TechSupport
NXP TechSupport

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..

So to access other slave, call LPI2C_Ip_MasterSetSlaveAddr() with slave address as parameter before new Lpi2c_Ip_MasterSendData and Lpi2c_Ip_MasterReceiveData functions call.
 
BR, Petr
0 Kudos
Reply
261 Views
AshutoshNama
Contributor II

 Got it!

0 Kudos
Reply