|
| 1 | +use embassy_stm32::pac::common::W; |
| 2 | +use embassy_time::Timer; |
| 3 | +use uor_peripherals::spi::peripheral::UORMonoCsSPI; |
| 4 | +use uor_utils::linear_algebra::vectors::{VectorR3, VectorR3F}; |
| 5 | + |
| 6 | +use crate::mmc5983ma::utils::{MMC5983MABandwidth, MMC5983MARegisters}; |
| 7 | + |
| 8 | +pub struct MMC5983MA<'a> { |
| 9 | + pub spi_service: UORMonoCsSPI<'a>, |
| 10 | +} |
| 11 | + |
| 12 | +impl<'a> MMC5983MA<'a> { |
| 13 | + pub fn new(spi_service: UORMonoCsSPI<'a>) -> Self { |
| 14 | + MMC5983MA { spi_service: spi_service } |
| 15 | + } |
| 16 | + |
| 17 | + // Read from chip when rw_n high |
| 18 | + pub async fn transfer( |
| 19 | + &mut self, |
| 20 | + register: MMC5983MARegisters, |
| 21 | + write_data: u8, |
| 22 | + rw_n: bool, |
| 23 | + ) -> u8 { |
| 24 | + let mut read_buf: [u16; 1] = [0]; |
| 25 | + let mut write: u16 = 0; |
| 26 | + // Set address bits |
| 27 | + write = write | ((register as u16) << 8); |
| 28 | + write = write | (write_data as u16); |
| 29 | + |
| 30 | + if rw_n { |
| 31 | + write = write | 0b1000_0000; |
| 32 | + } |
| 33 | + |
| 34 | + let _ = self.spi_service.transfer::<u16>(&mut read_buf, &mut [write]).await; |
| 35 | + read_buf[0] as u8 |
| 36 | + } |
| 37 | + |
| 38 | + pub async fn start_measurement( |
| 39 | + &mut self, |
| 40 | + mag: bool, |
| 41 | + temp: bool, |
| 42 | + ) { |
| 43 | + let mut write_data = 0; |
| 44 | + if mag { |
| 45 | + write_data = write_data | 0b0000_0001; |
| 46 | + } |
| 47 | + if temp { |
| 48 | + write_data = write_data | 0b0000_0010; |
| 49 | + } |
| 50 | + |
| 51 | + self.transfer(MMC5983MARegisters::InternalControl0, write_data, false).await; |
| 52 | + } |
| 53 | + |
| 54 | + pub async fn get_raw_mag_data(&mut self) -> VectorR3 { |
| 55 | + let mut x_val: u32 = 0; |
| 56 | + let mut y_val: u32 = 0; |
| 57 | + let mut z_val: u32 = 0; |
| 58 | + |
| 59 | + x_val = x_val | ((self.transfer(MMC5983MARegisters::XOut0, 0, true).await as u32) << 10); |
| 60 | + x_val = x_val | ((self.transfer(MMC5983MARegisters::XOut1, 0, true).await as u32) << 2); |
| 61 | + |
| 62 | + y_val = y_val | ((self.transfer(MMC5983MARegisters::YOut0, 0, true).await as u32) << 10); |
| 63 | + y_val = y_val | ((self.transfer(MMC5983MARegisters::YOut1, 0, true).await as u32) << 2); |
| 64 | + |
| 65 | + z_val = z_val | ((self.transfer(MMC5983MARegisters::ZOut0, 0, true).await as u32) << 10); |
| 66 | + z_val = z_val | ((self.transfer(MMC5983MARegisters::ZOut1, 0, true).await as u32) << 2); |
| 67 | + |
| 68 | + // The lower two bits of the X,Y, and Z vector magnitudes |
| 69 | + let xyz_lower = self.transfer(MMC5983MARegisters::XYZOut2, 0, true).await as u32; |
| 70 | + |
| 71 | + // TODO: GET RID OF THE EVIL MAGIC NUMBERS GRRRR |
| 72 | + x_val = x_val | ((xyz_lower & 0b1100_0000) >> 6); |
| 73 | + y_val = y_val | ((xyz_lower & 0b0011_0000) >> 6); |
| 74 | + z_val = z_val | ((xyz_lower & 0b0000_1100) >> 6); |
| 75 | + |
| 76 | + VectorR3::new(x_val as i64, y_val as i64, z_val as i64) |
| 77 | + } |
| 78 | + |
| 79 | + pub async fn get_18bit_mag(&mut self) -> VectorR3F { |
| 80 | + let sensor_vector = VectorR3F::from(self.get_raw_mag_data().await); |
| 81 | + let mut output: VectorR3F = VectorR3F::new(-5.0, -5.0, -5.0) |
| 82 | + + (sensor_vector * VectorR3F::new((10 / 2_i32.pow(18) as f64), (10 / 2_i32.pow(18) as f64), (10 / 2_i32.pow(18) as f64))); |
| 83 | + output |
| 84 | + } |
| 85 | + |
| 86 | + pub async fn generate_mag_vector( |
| 87 | + &mut self, |
| 88 | + bandwidth: MMC5983MABandwidth, |
| 89 | + ) -> VectorR3F { |
| 90 | + self.transfer(MMC5983MARegisters::InternalControl1, bandwidth.clone() as u8, false); |
| 91 | + self.start_measurement(true, false).await; |
| 92 | + // TODO: GET RID OF MORE EVIL MAGIC NUMBERS GRRRR |
| 93 | + |
| 94 | + match bandwidth { |
| 95 | + MMC5983MABandwidth::Hz100 => { |
| 96 | + Timer::after_millis(9).await; |
| 97 | + } |
| 98 | + MMC5983MABandwidth::Hz200 => { |
| 99 | + Timer::after_millis(5).await; |
| 100 | + } |
| 101 | + MMC5983MABandwidth::Hz400 => { |
| 102 | + Timer::after_millis(3).await; |
| 103 | + } |
| 104 | + MMC5983MABandwidth::Hz800 => { |
| 105 | + Timer::after_millis(1).await; |
| 106 | + } |
| 107 | + } |
| 108 | + // This all assumes 18 bit mode |
| 109 | + self.get_18bit_mag().await |
| 110 | + } |
| 111 | +} |
0 commit comments