3 Replies Latest reply on Aug 10, 2015 8:47 PM by LeCa_2156671

    GPIO simulate I2C

    LeCa_2156671

      Hi BCM,

           Per userc_4697 jota_1939431kwang

       

           My 20736 use external SPI flash for memory, so the two Multi-pin of BSC(i2c) is configurate as SPI MOSI/SCK.

           Now one of my new system need I2C port for data transmition. And I plan to choose 2 GPIOs to simulate I2C output.

       

           Here is my question:

           1. I plan to simulate P0 / P1 output as SCK / SDA

           2. I plan to program a function for I2C output by this two GPIOs such as

                I2C write()

                {

                     // for 400Kbps I2C output 1 bit

                     P0 low

                     P1 high or low   for data bit 1 / 0

                     P0 high

                     delay(2.5 uS)

                     P0 low

                     delay(2.5 uS)

                }

       

           Here is my worry:

           If I put this I2C function in one of my function running in APPs, but as we know that APPs priority is very low, it might be interrupt by higer thread running in stack. If so, the I2C write function might be interrupt during delay(), which might cause I2C SCK frequency unstable.

       

           Is my worry unnecessary ?? or it might be like this.

       

           Is there any good suggestion when the BSC port has already configurated as SPI port, but I still need one I2C port ?

       

           Many thanks...

        • 2. Re: GPIO simulate I2C
          JacobT_81

          What you plan to do is called bit banging an i2c port. I can't personally recommend doing this because there are a lot of variables involved in getting it right and there is a much easier workaround if you are willing to add hardware.

           

          What will be much easier is muxing your i2c port with your SPI1. To do this you'll need a switch (even a DIP switch will work) or i2c expander to connect/disconnect the i2c clock and data lines entirely via GPIO. On the other side of the switch attach your i2c devices.

           

           

          In the makefile add:

               APP_PATCHES_AND_LIBS += enable_spiffy1_i2c_interop.a

               APP_PATCHES_AND_LIBS += a4wp_hal_api.a

           

          And in your .c file import:

              #include "a4wp_hal_api.h"

           

          And in your create function call:

               a4wp_hal_i2c_mux_configure();

           

          Then in your create function (following boot from SFLASH) toggle the Chip select GPIO of the SFLASH to turn it off, then the GPIO of the i2c switch to turn it on. You can now carry out i2c communication as if nothing was ever changed.

           

          This i2c switch will be a feature of the new development board to be released in the future.

           

          Jacob

          • 3. Re: GPIO simulate I2C
            LeCa_2156671

            It's great, and we will try to test with your suggestion.