I have an STM32 Discovery board and I want to test the I2C protocol but I don't have any other devices to test with. I want to know if I can use two I2C interfaces on the same board just to act like two different devices ?
-
1\$\begingroup\$ I don't see why not. They're completely independent peripherals. As long as you can process everything in time, which is not hard, if done correctly \$\endgroup\$– IlyaCommented Feb 18, 2022 at 8:19
-
\$\begingroup\$ The search term you're looking for is 'loopback' \$\endgroup\$– Neil_UKCommented Feb 18, 2022 at 9:00
2 Answers
Yes, of course you can, they are two completely independent interfaces.
Just initialize both and start communicating, but first you need to think what you want to achieve with it, do you want one master interface and one slave interface for testing, or do you want to build a multimaster setup where both can be masters and slaves.
-
\$\begingroup\$ do I need to use some resistors or is it safe to connect it directly ? \$\endgroup\$ Commented Feb 21, 2022 at 10:19
You just set the other I2C interface to slave mode and configure addresses etc accordingly and it will work.
-
\$\begingroup\$ I am new to I2C communications but I know that I2C is multi-master protocol so any device on the bus can be master and slave, isn't ? why should I set one to slave mode ? \$\endgroup\$ Commented Feb 18, 2022 at 8:37
-
1\$\begingroup\$ You are correct about multi-master. It really depends what you are trying to achieve. One option is to have a I2C monitor, that doesn't react to messages, simply logs them. Slave device would be something that responds to a message sent to a certain address. Master would be sending those messages. \$\endgroup\$– RalphCommented Feb 18, 2022 at 8:39
-
3\$\begingroup\$ you can have multiple masters, but only slave device can actually receive something. Masters have no address to be addressed. Masters always initiate communication. So you can have multiple masters, but if you want to send something from one I2C and receive it with another, that another one at that moment in time needs to be a slave and have an address. After it received everything, it can turn into master and initiate communication itself too, no problem \$\endgroup\$– IlyaCommented Feb 18, 2022 at 8:42
-
2\$\begingroup\$ If you are new to I2C I would first find means to see the content of the messages. Maybe a custom monitor, bus pirate or an oscilloscope that can read the messages. Problems with I2C are usually something tiny, like forgetting to put a stop bit where it should be. \$\endgroup\$– RalphCommented Feb 18, 2022 at 8:46
-
\$\begingroup\$ The STM32 has no separate initialization to master or slave modes. There is only one init and you must configure it including the slave addresses but you can choose to select a dummy address. While I2C protocol defines multimaster, not all masters are multimaster capable and slaves by definition are not masters. STM32 does support multimaster. \$\endgroup\$– JustmeCommented Feb 18, 2022 at 9:10