diff options
| author | eZio Pan <[email protected]> | 2024-03-21 16:06:34 +0800 |
|---|---|---|
| committer | eZio Pan <[email protected]> | 2024-03-23 09:15:25 +0800 |
| commit | 0d065ab2d658ebfad0c6e4bba562e474d6ca1012 (patch) | |
| tree | b72d8798c8bbd0c5327a41b2cdb55d39082a58ac /tests/stm32/src | |
| parent | c42d9f9eaae546faae46c4d1121f1fbc393c2073 (diff) | |
stm32 CORDIC: add HIL test
Diffstat (limited to 'tests/stm32/src')
| -rw-r--r-- | tests/stm32/src/bin/cordic.rs | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/tests/stm32/src/bin/cordic.rs b/tests/stm32/src/bin/cordic.rs new file mode 100644 index 000000000..b580cc79b --- /dev/null +++ b/tests/stm32/src/bin/cordic.rs | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | // required-features: rng, cordic | ||
| 2 | |||
| 3 | // Test Cordic driver, with Q1.31 format, Sin function, at 24 iterations (aka PRECISION = 6), using DMA transfer | ||
| 4 | |||
| 5 | // Only test on STM32H563ZI, STM32U585AI and STM32U5a5JI. | ||
| 6 | // STM32G491RE is not tested, since it memory.x has less memory size than it actually has, | ||
| 7 | // and the test seems use much memory than memory.x suggest. | ||
| 8 | // see https://github.com/embassy-rs/stm32-data/issues/301#issuecomment-1925412561 | ||
| 9 | |||
| 10 | #![no_std] | ||
| 11 | #![no_main] | ||
| 12 | |||
| 13 | use defmt::*; | ||
| 14 | use embassy_executor::Spawner; | ||
| 15 | use embassy_stm32::{bind_interrupts, cordic, peripherals, rng}; | ||
| 16 | use num_traits::Float; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | RNG => rng::InterruptHandler<peripherals::RNG>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | /* input value control, can be changed */ | ||
| 24 | |||
| 25 | const ARG1_LENGTH: usize = 9; | ||
| 26 | const ARG2_LENGTH: usize = 4; // this might not be the exact length of ARG2, since ARG2 need to be inside [0, 1] | ||
| 27 | |||
| 28 | const INPUT_Q1_31_LENGHT: usize = ARG1_LENGTH + ARG2_LENGTH; | ||
| 29 | const INPUT_U8_LENGTH: usize = 4 * INPUT_Q1_31_LENGHT; | ||
| 30 | |||
| 31 | #[embassy_executor::main] | ||
| 32 | async fn main(_spawner: Spawner) { | ||
| 33 | let dp = embassy_stm32::init(Default::default()); | ||
| 34 | |||
| 35 | // | ||
| 36 | // use RNG generate random Q1.31 value | ||
| 37 | // | ||
| 38 | // we don't generate floating-point value, since not all binary value are valid floating-point value, | ||
| 39 | // and Q1.31 only accept a fixed range of value. | ||
| 40 | |||
| 41 | let mut rng = rng::Rng::new(dp.RNG, Irqs); | ||
| 42 | |||
| 43 | let mut input_buf_u8 = [0u8; INPUT_U8_LENGTH]; | ||
| 44 | unwrap!(rng.async_fill_bytes(&mut input_buf_u8).await); | ||
| 45 | |||
| 46 | // convert every [u8; 4] to a u32, for a Q1.31 value | ||
| 47 | let input_q1_31 = unsafe { core::mem::transmute::<[u8; INPUT_U8_LENGTH], [u32; INPUT_Q1_31_LENGHT]>(input_buf_u8) }; | ||
| 48 | |||
| 49 | let mut input_f64_buf = [0f64; INPUT_Q1_31_LENGHT]; | ||
| 50 | |||
| 51 | let mut cordic_output_f64_buf = [0f64; ARG1_LENGTH * 2]; | ||
| 52 | |||
| 53 | // convert Q1.31 value back to f64, for software calculation verify | ||
| 54 | for (val_u32, val_f64) in input_q1_31.iter().zip(input_f64_buf.iter_mut()) { | ||
| 55 | *val_f64 = cordic::utils::q1_31_to_f64(*val_u32); | ||
| 56 | } | ||
| 57 | |||
| 58 | let mut arg2_f64_buf = [0f64; ARG2_LENGTH]; | ||
| 59 | let mut arg2_f64_len = 0; | ||
| 60 | |||
| 61 | // check if ARG2 is in range [0, 1] (limited by CORDIC peripheral with Sin mode) | ||
| 62 | for &arg2 in &input_f64_buf[ARG1_LENGTH..] { | ||
| 63 | if arg2 >= 0.0 { | ||
| 64 | arg2_f64_buf[arg2_f64_len] = arg2; | ||
| 65 | arg2_f64_len += 1; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | // the actal value feed to CORDIC | ||
| 70 | let arg1_f64_ls = &input_f64_buf[..ARG1_LENGTH]; | ||
| 71 | let arg2_f64_ls = &arg2_f64_buf[..arg2_f64_len]; | ||
| 72 | |||
| 73 | let mut cordic = cordic::Cordic::new( | ||
| 74 | dp.CORDIC, | ||
| 75 | unwrap!(cordic::Config::new( | ||
| 76 | cordic::Function::Sin, | ||
| 77 | Default::default(), | ||
| 78 | Default::default(), | ||
| 79 | false, | ||
| 80 | )), | ||
| 81 | ); | ||
| 82 | |||
| 83 | //#[cfg(feature = "stm32g491re")] | ||
| 84 | //let (mut write_dma, mut read_dma) = (dp.DMA1_CH4, dp.DMA1_CH5); | ||
| 85 | |||
| 86 | #[cfg(any(feature = "stm32h563zi", feature = "stm32u585ai", feature = "stm32u5a5zj"))] | ||
| 87 | let (mut write_dma, mut read_dma) = (dp.GPDMA1_CH4, dp.GPDMA1_CH5); | ||
| 88 | |||
| 89 | let cordic_start_point = embassy_time::Instant::now(); | ||
| 90 | |||
| 91 | let cnt = unwrap!( | ||
| 92 | cordic | ||
| 93 | .async_calc_32bit( | ||
| 94 | &mut write_dma, | ||
| 95 | &mut read_dma, | ||
| 96 | arg1_f64_ls, | ||
| 97 | Some(arg2_f64_ls), | ||
| 98 | &mut cordic_output_f64_buf, | ||
| 99 | ) | ||
| 100 | .await | ||
| 101 | ); | ||
| 102 | |||
| 103 | let cordic_end_point = embassy_time::Instant::now(); | ||
| 104 | |||
| 105 | // since we get 2 output for 1 calculation, the output length should be ARG1_LENGTH * 2 | ||
| 106 | defmt::assert!(cnt == ARG1_LENGTH * 2); | ||
| 107 | |||
| 108 | let mut software_output_f64_buf = [0f64; ARG1_LENGTH * 2]; | ||
| 109 | |||
| 110 | // for software calc, if there is no ARG2 value, insert a 1.0 as value (the reset value for ARG2 in CORDIC) | ||
| 111 | let arg2_f64_ls = if arg2_f64_len == 0 { &[1.0] } else { arg2_f64_ls }; | ||
| 112 | |||
| 113 | let software_inputs = arg1_f64_ls | ||
| 114 | .iter() | ||
| 115 | .zip( | ||
| 116 | arg2_f64_ls | ||
| 117 | .iter() | ||
| 118 | .chain(core::iter::repeat(&arg2_f64_ls[arg2_f64_ls.len() - 1])), | ||
| 119 | ) | ||
| 120 | .zip(software_output_f64_buf.chunks_mut(2)); | ||
| 121 | |||
| 122 | let software_start_point = embassy_time::Instant::now(); | ||
| 123 | |||
| 124 | for ((arg1, arg2), res) in software_inputs { | ||
| 125 | let (raw_res1, raw_res2) = (arg1 * core::f64::consts::PI).sin_cos(); | ||
| 126 | |||
| 127 | (res[0], res[1]) = (raw_res1 * arg2, raw_res2 * arg2); | ||
| 128 | } | ||
| 129 | |||
| 130 | let software_end_point = embassy_time::Instant::now(); | ||
| 131 | |||
| 132 | for (cordic_res, software_res) in cordic_output_f64_buf[..cnt] | ||
| 133 | .chunks(2) | ||
| 134 | .zip(software_output_f64_buf.chunks(2)) | ||
| 135 | { | ||
| 136 | for (cord_res, soft_res) in cordic_res.iter().zip(software_res.iter()) { | ||
| 137 | defmt::assert!((cord_res - soft_res).abs() <= 2.0.powi(-19)); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | // This comparsion is just for fun. Since it not a equal compare: | ||
| 142 | // software use 64-bit floating point, but CORDIC use 32-bit fixed point. | ||
| 143 | trace!( | ||
| 144 | "calculate count: {}, Cordic time: {} us, software time: {} us", | ||
| 145 | ARG1_LENGTH, | ||
| 146 | (cordic_end_point - cordic_start_point).as_micros(), | ||
| 147 | (software_end_point - software_start_point).as_micros() | ||
| 148 | ); | ||
| 149 | |||
| 150 | info!("Test OK"); | ||
| 151 | cortex_m::asm::bkpt(); | ||
| 152 | } | ||
