#expander #i2c #i2c-driver #driver #hardware

no-std sc16is752

A no_std crate for interfacing with the SC16IS752 I2C port expander. This lib provides access to both UARTs and 8x GPIO lines

2 releases

0.1.1 Dec 19, 2023
0.1.0 Nov 25, 2023

#1912 in Embedded development

Download history 3/week @ 2023-12-17 34/week @ 2024-02-25 2/week @ 2024-03-03 89/week @ 2024-03-10 5/week @ 2024-03-17 22/week @ 2024-03-31

116 downloads per month

MIT license

21KB
428 lines

Rust no_std NXP SC16IS752 I2C driver

Dual UART and 8x GPIO port expander

AKA:

  • Waveshare Serial Expansion HAT
  • MCU-752
  • Isolated 485 HAT
  • Raspberry Pi UART Expander

Datasheet


lib.rs:

Example


let mut device = SC16IS752::new(SC16IS750_ADDRESS, i2c)?;
device.initalise(Channel::A, UartConfig::default().baudrate(9600))?;
device.gpio_set_pin_mode(GPIO::GPIO0, PinMode::Output)?;
device.flush(Channel::A)?;
loop {
    device.write_byte(Channel::A, "a".as_bytes()[0])?;
    println!("RX A = {:?}", device.read_byte(Channel::A)?);

    for byte in b"This is channel A" {
        device.write_byte(Channel::A, byte)?;
    }
    let mut buf_a: Vec<u8> = vec![];
    for _ in 0..device.fifo_available_data(Channel::A)? {
        match device.read_byte(Channel::A)? {
            Some(byte) => buf_a.push(byte),
            None => break,
        }
    }
    println!("RX UART A = {}", String::from_utf8_lossy(&buf_a));

    thread::sleep(Duration::from_millis(250));
    println!("Testing GPIO states");
    device.gpio_set_pin_state(GPIO::GPIO0, PinState::High)?;
    println!(
        "GPIO0 = {:?} (Output)",
        device.gpio_get_pin_state(GPIO::GPIO0)?
    );
    println!("Set GPIO0 to low");
    device.gpio_set_pin_state(GPIO::GPIO0, PinState::Low)?;
    thread::sleep(Duration::from_millis(250));
    println!(
        "GPIO0 = {:?} (Output toggled)",
        device.gpio_get_pin_state(GPIO::GPIO0)?
    );

Dependencies

~68KB