MAX7219 segmented display driver in Rust

Introducing the MAX7219 segmented display driver. The driver was originally written by Maikel Wever and later picked up by me and improved.

Devices

This driver supports the MAX7219 and MAX7221 chips used in segment and led matrix displays.

I’ve tested the driver with my hifive1-revB board with two sergmented displays connected in series.

Connecting displays in series

When connecting displays in series the setup uses a non-standard SPI data send trick to avoid needing additional CS cables.

You use the DOUT pin from first display and connect it directly to the DIN (MOSI) pin on the next display.

Using the driver

To use the driver you need to instantiate embedded-hal for your specific board. Usage is fairly simple after that, here’s a short example using GPIO pins.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![no_std]
#![no_main]

extern crate panic_halt;

use riscv_rt::entry;
use max7219::*;

// replace these with your dev-board embedded-hal
use hifive1::hal::prelude::*;
use hifive1::hal::DeviceResources;
use hifive1::pin;

#[entry]
fn main() -> ! {
let dr = DeviceResources::take().unwrap();
let p = dr.peripherals;
let gpio = dr.pins;

// Configure clocks
hifive1::clock::configure(p.PRCI, p.AONCLK, 320.mhz().into());

// set appropriate pins to output mode
let data = pin!(gpio, spi0_mosi).into_output();
let sck = pin!(gpio, spi0_sck).into_output();
let cs = pin!(gpio, spi0_ss0).into_output();

// init the display driver with prepared pins
let mut display = MAX7219::from_pins(1, data, cs, sck).unwrap();

// make sure to wake the display up
display.power_on().unwrap();
// write given octet of ASCII characters with dots specified by 3rd param bits
display.write_str(0, b"pls help", 0b00100000).unwrap();
// set display intensity lower
display.set_intensity(0, 0x1).unwrap();

loop {}
}

I’ve created a project with examples using the driver via SPI as well as individual GPIO pins utilizing the hifive1-revB development board.
You can also check out the documentation for the driver.