Questions about I2C... Mainly, how do I use it?

So freedom metal sdk has support for uart and spi, but I don’t see I2C anywhere, including in the documentation. In the documentation, it just links to opencores.org. I’m a little confused about what this means. I’m assuming revb does include i2c support because it says so, but why is it supported differently than UART or SPI? And do I just use the opencore documentation to use it? I’m waiting on my account to be verified there so I’m posting here hoping someone has a guide or something because the revb doc has almost nothing on I2C.

1 Like

+1 for this, we’re trying to add I2C support to the Rust side of things as well and I got kinda stuck on this myself. There’s just the base address for the peripheral but no other info.

1 Like

Is there a git repo or something so I can follow the progress of the rust support you’re working on?

Yup, the official rust-riscv repos are at: https://github.com/riscv-rust

Of main interest are the riscv, e310, 310x-hal, risc-rt and hifive1 repos. That said the rev B has been only added recently and is being worked on. You can find some of that WIP at https://github.com/Disasm/hifive1-revb-workspace

NOTE: I am not associated with the riscv rust group, just trying to help along as I go along. Right now we’re trying to figure out how to best handle memory/linking maps for the hifive1-revb.

The answer to this is not obvious, because you need to know how “cores” work. Chip consists of different parts like CPU, memory, SPI0, SPI1, UART0, … Imagine that you are trying to build huge Lego city from multiple Lego sets. Some of the sets can be identical (UART0 and UART1 have the same registers) but located in different places on a floor ([UART]0 != [UART]1). Some of this peripherals are open-source and I2C is a good example. So you grab I2C from your local Lego shop and place on a floor. Now it’s I2C0. How to use it? Well, you could find everything from I2C documentation. In i2c_latest.tar.gz you can find i2c/trunk/doc/i2c_specs.pdf. Chapter 3 contains register definitions. Now you have a base address (from HiFive datasheet), register offsets (from i2c core datasheet) and a note “All I2C control register addresses are 4-byte aligned.” (from HiFive datasheet). So register 0 will be address+00, register 1 will be address+04 and so on.

1 Like

Thanks!

This actually leaves me kinda where I started though, which is how do I get the documentation? I signed up to opencores, it said wait 2 days to be verified, then I never got an e-mail. Do I just try again with a different e-mail? It is kinda dumb and counter intuitive for documentation on an open source effort for i2c on a mcu to be locked behind 1) a registration wall and 2) an account verification wall. I’ve never had to do that with any arm mcus. You just google the datasheet and … you get all the relevant information.

Oh, that’s strange. Sorry about that: I didn’t expect opencores to be so difficult to use.

Here is the file: i2c_specs.pdf.

The actual HDL for the I2C block is here:

It’s a reimplementation, in Chisel, of the OpenCores I2C.

Thanks @Disasm and @paulw ! Going to be working on this all day. Much appreciated.

Current status: now I have i2c peripheral access bindings for Rust, but current driver implementation on top of them works in a strange way. I see communication with logic analyzer, but i2c peripheral sometimes leaves bus in invalid state.

Now you can use i2c in Rust!

There is an example in riscv-rust-quickstart repo.

Driver code for those who interested: https://github.com/riscv-rust/e310x-hal/blob/6c1033009c304d1877f441f097e8bef703a29055/src/i2c.rs

Awesome. Going to have to try this when I get back home. Thanks!

When will I2C be supported in the Metal library for the HiFive Rel B?

1 Like

Although this topic is kind of stale I just want to add that a HAL driver for the I2C module of the FE310-G002 is now available within the Snowfox Embedded Framework (and further details can be found here).

Actually I wanted to post to this thread first because its thematically more relevant but then the other one post came up which was much more recent so I published the information there (sorry @moderators ).

Just found out that I2C support has been added. You need to clone the latest SDK and point FreedomStudio to it. You can then select the example-i2c and start hacking :slight_smile:

1 Like