Author Topic: FPGA beginner: I2C slave  (Read 3427 times)

0 Members and 1 Guest are viewing this topic.

Offline greenstrikeTopic starter

  • Contributor
  • Posts: 17
  • Country: it
FPGA beginner: I2C slave
« on: December 04, 2024, 01:59:35 pm »
Hi guys, I would like to start creating an I2C slave.
Due I would make my design indipendent from FPGA brand/model, I would avoid to use FPGA embedded I2C hardware.
As starting point can I use this design for my FPGA?
Thanks.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9972
  • Country: us
Re: FPGA beginner: I2C slave
« Reply #1 on: December 04, 2024, 02:58:16 pm »
Ordinarily, I would recommend starting at OpenCores.org but I can't get the web site working today.  So, off to Google searching for 'open source fpga i2c core' and there are a lot of hits with many sharing code through github, etc.
 
The following users thanked this post: Murphy86

Offline Mahagam

  • Regular Contributor
  • *
  • Posts: 60
  • Country: pl
Re: FPGA beginner: I2C slave
« Reply #2 on: December 04, 2024, 04:15:04 pm »
 

Offline Murphy86

  • Newbie
  • Posts: 5
  • Country: ee
Re: FPGA beginner: I2C slave
« Reply #3 on: December 05, 2024, 06:20:23 pm »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).
 

Offline sw_guy

  • Regular Contributor
  • *
  • Posts: 50
  • Country: fi
Re: FPGA beginner: I2C slave
« Reply #4 on: December 05, 2024, 07:25:35 pm »
Schmitt trigger is as important in the CLK pin as it is in the SLA pin. And do yourself a favor and use a FPGA that supports 3-state pin as already mentioned by Murphy86.
I have implemented i2c slave using this code:
https://github.com/jiacaiyuan/i2c_slave/blob/master/FPGA_i2c_slave/i2c_slave/i2c.v

With minor changes and digital filter (=an external clock to oversample SCL and SLA lines to make it glitch-tolerant).

The example provided by Mahagam also works.

Neither of the codes above need external clock which is easier to start with, but they are vulnerable for glitches. Also with schmitt trigger. For learning purposes this is fine and work well enough on the workbench. In case the application is closer to a production application, I recommend digital filtering.

sw guy
« Last Edit: December 05, 2024, 09:52:13 pm by sw_guy »
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15899
  • Country: fr
Re: FPGA beginner: I2C slave
« Reply #5 on: December 05, 2024, 10:40:02 pm »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).

Yes, all FPGAs have bidir IOs.
 

Online pcprogrammer

  • Super Contributor
  • ***
  • Posts: 4727
  • Country: nl
Re: FPGA beginner: I2C slave
« Reply #6 on: December 06, 2024, 07:44:12 am »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).

Yes, all FPGAs have bidir IOs.

The only issue might be that they are not open collector or drain, like real I2C devices use. Can cause problems in multi master/slave setups and bus contention.

Offline greenstrikeTopic starter

  • Contributor
  • Posts: 17
  • Country: it
Re: FPGA beginner: I2C slave
« Reply #7 on: December 06, 2024, 10:06:21 am »
Actually I am using MachXO2, if I correctly understand almost all inputs are Schmitt trigger.
I'll read more carefully datasheet to check if there are open collector outputs.

I focused on external mos due I am newbie about FPGAs and this is my first FPGA application where I am using low cost device, with this solution (I mean 2 pins for SDA and external Schmitt trigger for SCL) I am planning to make chip type indipendent solution.
For the while I have no restrictions in terms of space, so external components for me are "free".

 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4881
  • Country: dk
Re: FPGA beginner: I2C slave
« Reply #8 on: December 06, 2024, 10:18:45 am »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).

Yes, all FPGAs have bidir IOs.

The only issue might be that they are not open collector or drain, like real I2C devices use. Can cause problems in multi master/slave setups and bus contention.

not a problem, you just three-state the output unless you want to drive a low

assign dout = data ? 1'bz : 1'b0;

 

Offline sw_guy

  • Regular Contributor
  • *
  • Posts: 50
  • Country: fi
Re: FPGA beginner: I2C slave
« Reply #9 on: December 06, 2024, 01:12:24 pm »
Actually I am using MachXO2, if I correctly understand almost all inputs are Schmitt trigger.

I have used both of the examples above with MACHXO2.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 15899
  • Country: fr
Re: FPGA beginner: I2C slave
« Reply #10 on: December 06, 2024, 08:38:52 pm »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).

Yes, all FPGAs have bidir IOs.

The only issue might be that they are not open collector or drain, like real I2C devices use. Can cause problems in multi master/slave setups and bus contention.

not a problem, you just three-state the output unless you want to drive a low

assign dout = data ? 1'bz : 1'b0;

Well yes, that's how a bidir IO is handled. For an open drain  behavior, you assign either Z or 0. End of the story. I'll be curious to hear about how a tri-state push-pull output used only either in Hi-Z or 0 level differs concretely from an open-drain.
Of course, there's always the risk of a bug in your RTL that will inadvertently set it to 1 and wreak havoc, but for such a simple thing, you would have to do it on purpose really.
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4881
  • Country: dk
Re: FPGA beginner: I2C slave
« Reply #11 on: December 06, 2024, 11:29:05 pm »
You don't really need 3 separate pins and mosfet. I have yet to see modern FPGA that won't support bidirectional IO (exclude some special pins).

Yes, all FPGAs have bidir IOs.

The only issue might be that they are not open collector or drain, like real I2C devices use. Can cause problems in multi master/slave setups and bus contention.

not a problem, you just three-state the output unless you want to drive a low

assign dout = data ? 1'bz : 1'b0;

Well yes, that's how a bidir IO is handled. For an open drain  behavior, you assign either Z or 0. End of the story. I'll be curious to hear about how a tri-state push-pull output used only either in Hi-Z or 0 level differs concretely from an open-drain.
Of course, there's always the risk of a bug in your RTL that will inadvertently set it to 1 and wreak havoc, but for such a simple thing, you would have to do it on purpose really.

in many cases there is no difference, but a tri-stated push-pull output is often limited by the supply voltage, a real open drain usually isn't
 

Offline laugensalm

  • Regular Contributor
  • *
  • Posts: 135
  • Country: ch
Re: FPGA beginner: I2C slave
« Reply #12 on: December 07, 2024, 01:29:26 pm »
The point is indeed that a real open drain would allow a 3.3V Vcc chip to speak to a 5V slave without internal diodes or drivers being fried, for instance.

This is *not* the case with an emulated open-drain behaviour of a classic push/pull tristate I/O without the push. Current will then flow, possibly through internal clamping diodes and do funny things, depending on the I/O architecture.

WRT schmitt triggers: You can safely forget about these with an up to date FPGA, as their standard I/O hysteresis and slew rate configuration options are way more than sufficient, and you typically do the debouncing yourself by sampling the SCL input with an internal clock of a multiple of the max. standard SCL frequency. Most FPGA types allow you to configure a slow slew rate/glitch filtering option in their I/O constraints files and tools mostly detect if you are emulating an open drain output.

For standard applications, I'm clocking in 4 SCL bits and check for "1100" for a falling edge condition - good enough to detect glitches as well. Clock streching support may require another setup, but you don't seem to require that.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf