/onewire

OneWire bus implementation in Rust using embedded-hal

Primary LanguageRustApache License 2.0Apache-2.0

OneWire

This crate is an OneWire-Bus implementation ontop of generic Input- and OutputPins from the embedded-hal.

Build Status License Crates.io Documentation PRs Welcome

How to use

Below is an example how to create a new OneWire instance, search for devices and read the temperature from a DS18B20. The example currently requires the stm32f103xx-hal to be patched with this PR.

fn main() -> ! {
    let mut cp: cortex_m::Peripherals = cortex_m::Peripherals::take().unwrap();
    let mut peripherals = stm32f103xx::Peripherals::take().unwrap();
    let mut flash = peripherals.FLASH.constrain();
    let clocks = rcc.cfgr.freeze(&mut flash.acr);
    let mut rcc = peripherals.RCC.constrain();
    let mut gpioc = peripherals.GPIOC.split(&mut rcc.apb2);
    
    let mut delay = stm32f103xx_hal::delay::Delay::new(cp.SYST, clocks);
    
    let mut one = gpioc
        .pc15
        .into_open_drain_output(&mut gpioc.crh)
        .downgrade();
        
    let mut wire = OneWire::new(&mut one, false);
    
    if wire.reset(&mut delay).is_err() {
        // missing pullup or error on line
        loop {}
    }
    
    // search for devices
    let mut search = DeviceSearch::new();
    while let Some(device) = wire.search_next(&mut search, &mut delay).unwrap() {
        match device.address[0] {
            ds18b20::FAMILY_CODE => {
                let mut ds18b20 = DS18b20::new(device).unwrap();
                
                // request sensor to measure temperature
                let resolution = ds18b20.measure_temperature(&mut wire, &mut delay).unwrap();
                
                // wait for compeltion, depends on resolution 
                delay.delay_ms(resolution.time_ms());
                
                // read temperature
                let temperature = ds18b20.read_temperature(&mut wire, &mut delay).unwrap();
            },
            _ => {
                // unknown device type            
            }
        }
    }
    
    loop {}
}

The code from the example is copy&pasted from a working project, but not tested in this specific combination.