Accessing multiple slave devices

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Accessing multiple slave devices

ソリューションへジャンプ
2,430件の閲覧回数
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,423件の閲覧回数
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,424件の閲覧回数
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,390件の閲覧回数
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,387件の閲覧回数
pb632146
Contributor V
Found its Lpi2c_Ip_MasterSetSlaveAddr i dont need to toggle it on and off just change the slave address
0 件の賞賛
返信
2,326件の閲覧回数
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,337件の閲覧回数
AshutoshNama
Contributor III

 Got it!

0 件の賞賛
返信