kirdy/src/device/hw_rev.rs

63 lines
2.1 KiB
Rust

use stm32f4xx_hal::gpio::{Input, PE10, PE11, PE8, PE9};
use crate::device::sys_timer::sleep;
use stm32f4xx_hal::signature;
use crc::{Crc, CRC_24_BLE};
pub struct HwRevPins{
pub h0: PE8<Input>,
pub h1: PE9<Input>,
pub h2: PE10<Input>,
pub h3: PE11<Input>,
}
pub struct HWRev {
pub major: u8,
pub minor: u8,
}
impl HWRev {
pub fn detect_hw_rev(hwrev_pins: HwRevPins) -> Self {
let (h0, h1, h2, h3) = (
hwrev_pins.h0.is_high(), hwrev_pins.h1.is_high(),
hwrev_pins.h2.is_high(), hwrev_pins.h3.is_high()
);
match (h0, h1, h2, h3) {
(true, true, true, true) => HWRev { major: 0, minor: 3 },
(_, _, _, _) => HWRev { major: 0, minor: 0 }
}
}
/// On Rev0_3, during power up, digital power rails are stabilized way before analog power rails
/// This causes improper initialization on any peripherals requiring calibrations
/// See Issue #32 on Kirdy Hw Repo
pub fn startup_delay_before_gpio_init(&mut self){
if self.major == 0 && self.minor == 3 {
sleep(5000);
}
}
/// On Rev0_3, it lacks pre-allocated Mac Addresses accessible on PCB.
/// This functions generate a random Mac Address with 96bit unique UUID inside STM32
/// See Issue #36 on Kirdy Hw Repo
pub fn get_mac_address(&mut self) -> [u8; 6] {
if self.major == 0 && self.minor == 3 {
let uid = signature::Uid::get();
let mut uid_data: [u8; 12] = [0; 12];
uid_data[0] = uid.x() as u8;
uid_data[1] = (uid.x() >> 8) as u8;
uid_data[2] = uid.y() as u8;
uid_data[3] = (uid.y() >> 8) as u8;
uid_data[4..11].clone_from_slice(uid.lot_num().as_bytes());
let crc: Crc<u32> = Crc::<u32>::new(&CRC_24_BLE);
let mut digest = crc.digest();
digest.update(&uid_data);
let crc24 = digest.finalize();
[ 0x02, 0xE0, 0xD5, (crc24 >> 16) as u8, (crc24 >> 8) as u8, (crc24 as u8)]
} else {
unimplemented!()
}
}
}