1 个不稳定版本
0.1.0 | 2022年7月16日 |
---|
在 #hal 中排名 143
37KB
512 行
MAX17320 的嵌入式 HAL 驱动程序(2S-4S 模型电压表 m5 电池电量计,具有保护器、内部自放电检测和 SHA-256 认证)
示例
更多示例请参阅 max17320_stm32f401_examples
#![no_std]
#![no_main]
use cortex_m_rt::ExceptionFrame;
use cortex_m_rt::{entry, exception};
use cortex_m_semihosting::hprintln;
use hal::{pac, prelude::*};
use panic_semihosting as _;
use stm32f4xx_hal as hal;
use vl6180x;
#[entry]
fn main() -> ! {
if let (Some(dp), Some(_cp)) = (
pac::Peripherals::take(),
cortex_m::peripheral::Peripherals::take(),
) {
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(48.MHz()).freeze();
let gpiob = dp.GPIOB.split();
let scl = gpiob
.pb8
.into_alternate()
.internal_pull_up(true)
.set_open_drain();
let sda = gpiob
.pb9
.into_alternate()
.internal_pull_up(true)
.set_open_drain();
let i2c = dp.I2C1.i2c((scl, sda), 400.kHz(), &clocks);
let mut bat = max17320::MAX17320::new(i2c, 5.0).expect("mx");
hprintln!("status: {}", bat.read_status().unwrap()).unwrap();
hprintln!("capacity: {}mAh", bat.read_capacity().unwrap()).unwrap();
hprintln!("device name: {}", bat.read_device_name().unwrap()).unwrap();
hprintln!("state of charge: {}%", bat.read_state_of_charge().unwrap()).unwrap();
hprintln!("vcell: {}v", bat.read_vcell().unwrap()).unwrap();
hprintln!("cell1: {}v", bat.read_cell1().unwrap()).unwrap();
hprintln!("temp: {}°C", bat.read_temperature().unwrap()).unwrap();
hprintln!("die temp: {}°C", bat.read_die_temperature().unwrap()).unwrap();
hprintln!("current: {}mA", bat.read_current().unwrap()).unwrap();
hprintln!("tte: {}", bat.read_time_to_empty().unwrap()).unwrap();
hprintln!("ttf: {}", bat.read_time_to_full().unwrap()).unwrap();
hprintln!("prot_status: {}", bat.read_protection_status().unwrap()).unwrap();
hprintln!("prot_alert: {}", bat.read_protection_alert().unwrap()).unwrap();
}
loop {}
}
#[exception]
unsafe fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}
参考
附加说明
- 仅在 STM32F401 微控制器上进行了测试
- 尚未实现 1-Wire 通信协议。请给我发电子邮件或提交一个拉取请求以添加支持。
依赖项
~71KB