Skip to content

Your first hardware design

This design toggles an LED about once a second from a 27 MHz clock.

blink.flux
__fpga void blink(clock clk, bits<1>& led) {
bits<25> divider = 0;
bits<1> ledState = 1;
__onRise(clk) {
if (divider == 13500000) {
divider = 0;
ledState = ledState ^ 1;
} else {
divider = divider + 1;
}
}
led = ledState;
}
  • clock clk is the clock domain.
  • divider and ledState are registers, because they are assigned inside __onRise. The initial values are their power-up values.
  • bits<1>& led is an output. Output ports are passed by reference.
flux --emit=hardware blink.flux
module @blink [sequential] {
clock %clk
output %led : u1
state %divider : u25 [init=u25 0]
state %ledState : u1 [init=u1 1]
...

Print the look-up table mapping and its statistics:

flux --emit=lut --stats blink.flux

The host program calls the hardware function with board handles, then instantiates the result.

blink_board.flux
#include "blink.flux"
#include "src/flux/boards.hpp"
int main() {
flux::boards::TangNano20K board;
auto clk = board.onBoardClock();
auto led = board.leds(0, 1);
auto design = blink(clk, led);
board.instantiate(design);
return 0;
}
flux --emit=fs --board=tang-nano-20k --top=blink blink_board.flux --image-output=blink.fs --manifest-output=blink.json

The compiler reports the resources used and writes the configuration image with a manifest describing its pins. Loading it onto hardware is a separate, experimental step: see programming.