From 2fe73505c88e4a6fcf3da7538a0f6c93c7db4bb4 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 6 Sep 2020 00:38:28 +0800 Subject: [PATCH] improve i2c error reporting --- src/runtime/src/i2c.rs | 49 +++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/src/runtime/src/i2c.rs b/src/runtime/src/i2c.rs index fe585905..c6ba83d6 100644 --- a/src/runtime/src/i2c.rs +++ b/src/runtime/src/i2c.rs @@ -1,34 +1,63 @@ use libboard_zynq; +use crate::artiq_raise; + static mut I2C_BUS: Option = None; -pub extern fn start(_busno: i32) { +pub extern fn start(busno: i32) { + if busno > 0 { + artiq_raise!("I2CError", "I2C bus could not be accessed"); + } unsafe { - (&mut I2C_BUS).as_mut().unwrap().start().expect("I2C start failed") + if (&mut I2C_BUS).as_mut().unwrap().start().is_err() { + artiq_raise!("I2CError", "I2C start failed"); + } } } -pub extern fn restart(_busno: i32) { +pub extern fn restart(busno: i32) { + if busno > 0 { + artiq_raise!("I2CError", "I2C bus could not be accessed"); + } unsafe { - (&mut I2C_BUS).as_mut().unwrap().restart().expect("I2C restart failed") + if (&mut I2C_BUS).as_mut().unwrap().restart().is_err() { + artiq_raise!("I2CError", "I2C restart failed"); + } } } -pub extern fn stop(_busno: i32) { +pub extern fn stop(busno: i32) { + if busno > 0 { + artiq_raise!("I2CError", "I2C bus could not be accessed"); + } unsafe { - (&mut I2C_BUS).as_mut().unwrap().stop().expect("I2C stop failed") + if (&mut I2C_BUS).as_mut().unwrap().stop().is_err() { + artiq_raise!("I2CError", "I2C stop failed"); + } } } -pub extern fn write(_busno: i32, data: i32) -> bool { +pub extern fn write(busno: i32, data: i32) -> bool { + if busno > 0 { + artiq_raise!("I2CError", "I2C bus could not be accessed"); + } unsafe { - (&mut I2C_BUS).as_mut().unwrap().write(data as u8).expect("I2C write failed") + match (&mut I2C_BUS).as_mut().unwrap().write(data as u8) { + Ok(r) => r, + Err(_) => artiq_raise!("I2CError", "I2C write failed"), + } } } -pub extern fn read(_busno: i32, ack: bool) -> i32 { +pub extern fn read(busno: i32, ack: bool) -> i32 { + if busno > 0 { + artiq_raise!("I2CError", "I2C bus could not be accessed"); + } unsafe { - (&mut I2C_BUS).as_mut().unwrap().read(ack).expect("I2C read failed") as i32 + match (&mut I2C_BUS).as_mut().unwrap().read(ack) { + Ok(r) => r as i32, + Err(_) => artiq_raise!("I2CError", "I2C read failed"), + } } }