Ni-fpga-rs 1.0.0 released!

:tada: ni-fpga-rs is a set of Rust libraries I have been working on-and-off on for the past year for interfacing with NI FPGAs - like the one on the roboRIO!

Examples

The main (ni-fpga) library provides a simple interface for reading and writing to FPGA registers.

use ni_fpga::Session;

fn main() -> Result<(), ni_fpga::Error> {
    let session = Session::open(
        "/boot/user.lvbitx",
        "264D0BA312FF00B741D4742415E1D470",
        "RIO0",
    )?;

    // Register locations vary by FPGA image
    println!("Input voltage: {:?}", session.read::<u16>(99174)?);
    Ok(())
}

A separate (ni-fpga-macros) library provides derive macros for Clusters (FPGA structs) and Enums.

use ni_fpga::Session;
use ni_fpga_macros::Cluster;

#[derive(Cluster)]
struct LEDs {
    comm: u8,
    mode: u8,
    rsl: bool,
}

fn main() -> Result<(), ni_fpga::Error> {
    let session = Session::open(
        "/boot/user.lvbitx",
        "264D0BA312FF00B741D4742415E1D470",
        "RIO0",
    )?;

    // Blink the mode LED on and off repeatedly
    loop {
        let mut leds: LEDs = session.read(98320)?;
        leds.mode = 1 - leds.mode;
        session.write(98320, &leds)?;
        std::thread::sleep(std::time::Duration::from_secs(1));
    }
    Ok(())
}

Why build this?

I want to program an FRC robot in Rust! I intend to implement the embedded-hal traits for the roboRIO, and these libraries are the first step in that effort.

Contributing

Releasing 1.0.0 means that I’m ready to show this project to the world and that I’m fairly comfortable with its APIs. The project is still a long ways from being feature-complete.

If you also want to program a robot in Rust, please check out ni-fpga-rs and first-rust-competition on GitHub. In particular, I need help with:

  • Creating an automated testing strategy
  • Handling fixed-point numeric types
  • Improving documentation
  • Adding support for IRQs and FIFOs (see NI API reference)

Contributions can also be as simple as playing with the library on your own roboRIO and reporting bugs. DM me if you need guidance in setting up the examples.

Thanks for reading!

7 Likes

Regarding testing, please please please DM me if you know how to create and/or simulate FPGA VIs

You’ll need the NI FPGA module, which unfortunately depends on a bunch of expensive Xilinx Vivado stuff. I think there might be a trial version. IIRC, the RoboRIO is basically a myRIO under the hood, so I bet you could follow the FPGA tutorial and program it like a myRIO (I haven’t tried it, though). The processor is a Xilinx Zynq 7020, so if you really wanted, you could probably create an image from Vivado without any of the NI software.

1 Like

You could use this to hook into the FRC fpga image without issues. It works just fine with NIs normal fpga access tools.

As for manually programming the fpga, you do need the fpga toolkit like you were saying. It does show up as its own target however, but that target is installed with the robotics toolkit from NI. Still need fpga module to actually build it though, which is not included with the FRC license or community.

1 Like

I figured I’d post some updates since I’ve had more time to work on the project lately.

  • I’ve added support for I64 / U64 / SGL / DBL registers.
  • Thanks to the advice of those who responded. I was able to generate a bitfile full of fixture registers. It’s now used for running integration tests on a roboRIO.
  • I’ve implemented signed and unsigned fixed point numeric types and added support for FXP registers :tada:

Some big blockers are now out of the way and I can now read from or write to pretty much any register described by the FRC bitfile. Some embedded_hal traits like PWM output are ready to be implemented and I’ve been able to generate some signals, albeit without correct scaling.

The next step of the project is to build an “lvbitfile2rust” tool akin to svd2rust to help keep the trait implementations sane. As always if you’re interested in the project feel free to DM me.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.