GPIO simulate I2C

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
LeCa_2156671
Level 4
Level 4
First like received First like given

Hi BCM,

     Per maxsong j.tkwang

     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...

0 Likes
3 Replies
Anonymous
Not applicable
0 Likes
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

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

0 Likes

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

0 Likes