cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G0 MCU as I2C Master and Slave on the same peripheral

stm_newbie
Associate II

Is it possible to use the same I2C peripheral to be used as both a master and a slave?

I made use of an STM32G0 MCU and using examples from CubeMX, I got the peripheral I2C2 to work as a slave and a master separately where I am using interrupts for the slave mode and polling for the master.

I am trying to combine it and make it work in both modes. Here is how I am trying to do it:

HAL_I2C_DisableListen_IT(&hi2c2);
result = HAL_I2C_Master_Receive(&hi2c2, (uint16_t) (0x69 << 1), rcvd_data, 3, HAL_MAX_DELAY);
adc_result = rcvd_data[0];
adc_result = (adc_result << 4) + rcvd_data[1];
		
HAL_I2C_EnableListen_IT(&hi2c2);

My code works some of the time but hangs up pretty quick and needs a reset. I don't have a logic analyzer to study the behavior.

Am I doing this right? Is there an example I could follow?

Any help is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

> Are you saying that it could have only one role at a time? is it not possible to switch roles during runtime?

It is possible to switch as desired during runtime. But if you're in master mode, the slave will not ACK when its address is called. And you'll also have two masters on the same bus, which is not desirable.

> The MCU reads from the new sensor (different peripheral address) and presents it to the legacy device at the old address.

Okay, that sort of makes sense as to why you'd want this functionality. But you would be talking over the other master. The STM32 can support multi-master functionality, but not all other devices or firmware can.

> Am I better off choosing an STM32 MCU that comes with two I2C peripherals and setting them as master and slave separately?

Yes, that would certainly make things simpler and more likely to succeed.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4
TDK
Guru

It can't be both a master and a slave at the same time, but you could switch between modes as desired.

Most I2C buses have a single master and a device is either a master or a slave. The master initiates all communication. It's not typical to switch between modes. What are you doing that you need that functionality?

If you feel a post has answered your question, please click "Accept as Solution".

Are you saying that it could have only one role at a time? is it not possible to switch roles during runtime?

I have some legacy devices in the field that reads from a sensor with an I2C output. The devices in question cannot be updated.

The sensor in question has gone obsolete and I am designing an adapter board where I am using the STM32 MCU to mimic the sensor's address. The MCU reads from the new sensor (different peripheral address) and presents it to the legacy device at the old address.

Does that make sense?

Am I better off choosing an STM32 MCU that comes with two I2C peripherals and setting them as master and slave separately?

> Are you saying that it could have only one role at a time? is it not possible to switch roles during runtime?

It is possible to switch as desired during runtime. But if you're in master mode, the slave will not ACK when its address is called. And you'll also have two masters on the same bus, which is not desirable.

> The MCU reads from the new sensor (different peripheral address) and presents it to the legacy device at the old address.

Okay, that sort of makes sense as to why you'd want this functionality. But you would be talking over the other master. The STM32 can support multi-master functionality, but not all other devices or firmware can.

> Am I better off choosing an STM32 MCU that comes with two I2C peripherals and setting them as master and slave separately?

Yes, that would certainly make things simpler and more likely to succeed.

If you feel a post has answered your question, please click "Accept as Solution".

>>It is possible to switch as desired during runtime. But if you're in master mode, the slave will not ACK when its address is called. And you'll also have two masters on the same bus, which is not desirable.

I think something similar is happening with my code logic. I will try an MCU with two I2C peripherals