0
\$\begingroup\$

I am using stm32f105 to control 5 I2C buses and some other peripheral circuits. The problem is that I used all dedicated I2C pins by other peripheral circuits. Is it possible I change a GPIO (General purpose I/O) pins as I2C.

For that purpose, a pin must be output for a while and than change to input (for SDA). i.e. a pin must be output and load the address, then change to input and receive the ACK from slave, then again read or receive data from slave, then finally change back to output.

I am wondering is it practical? is there any source code for that any where ?

\$\endgroup\$
  • 5
    \$\begingroup\$ XY - do you really, really need 5 separate I2C buses? \$\endgroup\$ – brhans Aug 8 '16 at 21:26
2
\$\begingroup\$

Yes, it's possible. Set both pins to open drain output in GPIOx_CRL or GPIOx_CRH, and you don't have to worry about switching the pin direction. If the port bit is 1, then the wire is pulled up by the external resitor, and you can read back 1 in GPIOx_IDR, or the slave pulls it low, and you'll have 0 in GPIOx_IDR.

There is a straightforward software implementation on Wikipedia. Use TIM6 or TIM7 for the delay, or DWT->CYCCNT if there are no timers left available. Leave out the arbitration_lost parts if you are the only master on the bus.

\$\endgroup\$
2
\$\begingroup\$

I2C is an ADDRESSABLE bus. As long as you're not controlling many devices with the same address, you should be able to put multiple devices on the same bus.

To answer your question -- yes, it is possible to bit-bang I2C, but it's more of a pain than bit-banging SPI. You don't want to do it unless you really have to.

Before you start bit banging, if you can't put multiple devices on one bus, you might consider I2C switches and multiplexers

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.