Accessing multiple slave devices

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Accessing multiple slave devices

跳至解决方案
2,365 次查看
pb632146
Contributor V

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 项奖励
回复
1 解答
2,358 次查看
AshutoshNama
Contributor III

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 项奖励
回复
5 回复数
2,359 次查看
AshutoshNama
Contributor III

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 项奖励
回复
2,325 次查看
pb632146
Contributor V

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 项奖励
回复
2,322 次查看
pb632146
Contributor V
Found its Lpi2c_Ip_MasterSetSlaveAddr i dont need to toggle it on and off just change the slave address
0 项奖励
回复
2,261 次查看
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 项奖励
回复
2,272 次查看
AshutoshNama
Contributor III

 Got it!

0 项奖励
回复