LPC55S28-EVK Working both as I2C master and slave at same time.

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

LPC55S28-EVK Working both as I2C master and slave at same time.

1,443 Views
ArielF
Contributor I

I'm working with a LPC55S28 microcontroller that communicates with another device via I2C. 

The flow is as following: The LPC working as master sends a message to the device (acting as slave).

When the device receives the LPC's packet, answers to the LPC's slave address, with another message. 

So I need the LPC to work both as I2c master and slave. 

I tried to configure it by merging lpcxpresso55s28_i2c_polling_b2b_master and lpcxpresso55s28_i2c_interrupt_b2b_transfer_slave samples. But it doesn't work. 

Is there a way to do that? Is there any sample code?

Thanks

Ariel

0 Kudos
Reply
9 Replies

1,439 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Ariel,

I think it is okay that you use the IIC module as master to access the device. The master means that it generates the SCL signal to access slave IIC, the master can write the device, it also can READ message from the device.

This is the procedure to read the device from master side, the master send the 7 bits address and one bit WRITE/READ command to the slave , the slave detects whether the address match with predefined slave address, if it matches, the slave polls if WRITE or READ bit command, if it is READ command, it will drive SDA line for each SCL clock, so the master can get message from slave.

 

Hope it can help you

BR

XiangJun Rong

0 Kudos
Reply

1,433 Views
ArielF
Contributor I

Hi XiangJun Rong,

Thanks for your answer. The issue is that the device that acted as slave, when it answer to the LPC, answers as master so the LPC must act as slave. 

Is this possible?

Thanks

Ariel

0 Kudos
Reply

1,427 Views
ArielF
Contributor I

Hi, 

I want to explain it more the flow is the following

1) LPC send message to device

       LPC (Master)   --------------------->  Device (Slave)

2)    Device answers to the LPC

       Device (Master) -------------------> LPC (Slave)

Because that I can't use master read from the slave to receive the answer.

I need the LPC will be able to work both as master and slave.

Thanks

Ariel

0 Kudos
Reply

1,410 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Ariel,

I think it is okay to exchange the master and slave for the LPC55S28.

If you set the MSTEN bit and clear SLVEN bit, the LPC is master, if you clear MSTEN and set the SLVEN bit, the LPC is slave.

Hope it can help you

BR

XiangJun Rong

xiangjun_rong_0-1620618562158.png

 

0 Kudos
Reply

1,405 Views
ArielF
Contributor I

Hi, 

Thanks again for your answer.

Do you mean I need to set the SLVEN and to unset the MSTEN immediately after the LPC sends the message to the slave device? And once the LPC receives the message (acting as slave) to set again the MSTEN and to unset the SLVEN?

Thanks

Ariel

0 Kudos
Reply

1,398 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Ariel,

The answer is Yes, you can configure the IIC as either master or slave by configuring the MSTEN and  the SLVEN bits. There is IIC status register, you can check the IIC status register to know the status of IIC, then decide when you configure it as master or slave.

Hope it can help you

BR

XiangJun Rong

0 Kudos
Reply

1,390 Views
ArielF
Contributor I

Hi,

As we spoke I wrote the code below, where I send the message to the slave and immediately after it I set the SLVEN bit and unset the MSTEN bit. Is it ok?

Because since I added the line: I2C_CFG->CFG = I2C_CFG_MSTEN(0) | I2C_CFG_SLVEN(1); the slave stopped receiving the I2C message?

res = I2C_MasterStart(I2C_CFG, address, kI2C_Write);
if (res != kStatus_Success) return res;

res = I2C_MasterWriteBlocking(I2C_CFG, buffer, length, 4);
if (res != kStatus_Success) return res;

res = I2C_MasterStop(I2C_CFG);
if (res != kStatus_Success) return res;

I2C_CFG->CFG = I2C_CFG_MSTEN(0) | I2C_CFG_SLVEN(1);

Thanks

Ariel

0 Kudos
Reply

1,382 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

I think your code is correct, the I2C_MasterWriteBlocking(I2C_CFG, buffer, length, 4) will transfer the buffer data with the length size before it returns.

Hope it can help you

BR

XiangJun Rong

0 Kudos
Reply

1,377 Views
ArielF
Contributor I

Hi, 

Thanks for your help!

Ariel

 

0 Kudos
Reply