aboutsummaryrefslogtreecommitdiff
path: root/tests/stm32
diff options
context:
space:
mode:
authoreZio Pan <[email protected]>2024-03-21 16:06:34 +0800
committereZio Pan <[email protected]>2024-03-23 09:15:25 +0800
commit0d065ab2d658ebfad0c6e4bba562e474d6ca1012 (patch)
treeb72d8798c8bbd0c5327a41b2cdb55d39082a58ac /tests/stm32
parentc42d9f9eaae546faae46c4d1121f1fbc393c2073 (diff)
stm32 CORDIC: add HIL test
Diffstat (limited to 'tests/stm32')
-rw-r--r--tests/stm32/Cargo.toml13
-rw-r--r--tests/stm32/gen_test.py2
-rw-r--r--tests/stm32/src/bin/cordic.rs152
3 files changed, 163 insertions, 4 deletions
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml
index e42470004..345c72a03 100644
--- a/tests/stm32/Cargo.toml
+++ b/tests/stm32/Cargo.toml
@@ -15,7 +15,7 @@ stm32f446re = ["embassy-stm32/stm32f446re", "chrono", "stop", "can", "not-gpdma"
15stm32f767zi = ["embassy-stm32/stm32f767zi", "chrono", "not-gpdma", "eth", "rng"] 15stm32f767zi = ["embassy-stm32/stm32f767zi", "chrono", "not-gpdma", "eth", "rng"]
16stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac", "ucpd"] 16stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac", "ucpd"]
17stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"] 17stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan"]
18stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash"] 18stm32h563zi = ["embassy-stm32/stm32h563zi", "chrono", "eth", "rng", "hash", "cordic"]
19stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash", "cryp"] 19stm32h753zi = ["embassy-stm32/stm32h753zi", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash", "cryp"]
20stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash", "cryp"] 20stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash", "cryp"]
21stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"] 21stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "not-gpdma", "rng", "fdcan"]
@@ -25,8 +25,8 @@ stm32l496zg = ["embassy-stm32/stm32l496zg", "not-gpdma", "rng"]
25stm32l4a6zg = ["embassy-stm32/stm32l4a6zg", "chrono", "not-gpdma", "rng", "hash"] 25stm32l4a6zg = ["embassy-stm32/stm32l4a6zg", "chrono", "not-gpdma", "rng", "hash"]
26stm32l4r5zi = ["embassy-stm32/stm32l4r5zi", "chrono", "not-gpdma", "rng"] 26stm32l4r5zi = ["embassy-stm32/stm32l4r5zi", "chrono", "not-gpdma", "rng"]
27stm32l552ze = ["embassy-stm32/stm32l552ze", "not-gpdma", "rng", "hash"] 27stm32l552ze = ["embassy-stm32/stm32l552ze", "not-gpdma", "rng", "hash"]
28stm32u585ai = ["embassy-stm32/stm32u585ai", "chrono", "rng", "hash"] 28stm32u585ai = ["embassy-stm32/stm32u585ai", "chrono", "rng", "hash", "cordic"]
29stm32u5a5zj = ["embassy-stm32/stm32u5a5zj", "chrono", "rng", "hash"] 29stm32u5a5zj = ["embassy-stm32/stm32u5a5zj", "chrono", "rng", "hash", "cordic"]
30stm32wb55rg = ["embassy-stm32/stm32wb55rg", "chrono", "not-gpdma", "ble", "mac" , "rng"] 30stm32wb55rg = ["embassy-stm32/stm32wb55rg", "chrono", "not-gpdma", "ble", "mac" , "rng"]
31stm32wba52cg = ["embassy-stm32/stm32wba52cg", "chrono", "rng", "hash"] 31stm32wba52cg = ["embassy-stm32/stm32wba52cg", "chrono", "rng", "hash"]
32stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"] 32stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"]
@@ -48,6 +48,7 @@ embassy-stm32-wpan = []
48not-gpdma = [] 48not-gpdma = []
49dac = [] 49dac = []
50ucpd = [] 50ucpd = []
51cordic = ["dep:num-traits"]
51 52
52cm0 = ["portable-atomic/unsafe-assume-single-core"] 53cm0 = ["portable-atomic/unsafe-assume-single-core"]
53 54
@@ -83,6 +84,7 @@ chrono = { version = "^0.4", default-features = false, optional = true}
83sha2 = { version = "0.10.8", default-features = false } 84sha2 = { version = "0.10.8", default-features = false }
84hmac = "0.12.1" 85hmac = "0.12.1"
85aes-gcm = {version = "0.10.3", default-features = false, features = ["aes", "heapless"] } 86aes-gcm = {version = "0.10.3", default-features = false, features = ["aes", "heapless"] }
87num-traits = {version="0.2", default-features = false,features = ["libm"], optional = true}
86 88
87# BEGIN TESTS 89# BEGIN TESTS
88# Generated by gen_test.py. DO NOT EDIT. 90# Generated by gen_test.py. DO NOT EDIT.
@@ -92,6 +94,11 @@ path = "src/bin/can.rs"
92required-features = [ "can",] 94required-features = [ "can",]
93 95
94[[bin]] 96[[bin]]
97name = "cordic"
98path = "src/bin/cordic.rs"
99required-features = [ "rng", "cordic",]
100
101[[bin]]
95name = "cryp" 102name = "cryp"
96path = "src/bin/cryp.rs" 103path = "src/bin/cryp.rs"
97required-features = [ "cryp",] 104required-features = [ "cryp",]
diff --git a/tests/stm32/gen_test.py b/tests/stm32/gen_test.py
index 8ff156c0e..daf714376 100644
--- a/tests/stm32/gen_test.py
+++ b/tests/stm32/gen_test.py
@@ -14,7 +14,7 @@ for f in sorted(glob('./src/bin/*.rs')):
14 with open(f, 'r') as f: 14 with open(f, 'r') as f:
15 for line in f: 15 for line in f:
16 if line.startswith('// required-features:'): 16 if line.startswith('// required-features:'):
17 features = line.split(':', 2)[1].strip().split(',') 17 features = [feature.strip() for feature in line.split(':', 2)[1].strip().split(',')]
18 18
19 tests[name] = features 19 tests[name] = features
20 20
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
13use defmt::*;
14use embassy_executor::Spawner;
15use embassy_stm32::{bind_interrupts, cordic, peripherals, rng};
16use num_traits::Float;
17use {defmt_rtt as _, panic_probe as _};
18
19bind_interrupts!(struct Irqs {
20 RNG => rng::InterruptHandler<peripherals::RNG>;
21});
22
23/* input value control, can be changed */
24
25const ARG1_LENGTH: usize = 9;
26const ARG2_LENGTH: usize = 4; // this might not be the exact length of ARG2, since ARG2 need to be inside [0, 1]
27
28const INPUT_Q1_31_LENGHT: usize = ARG1_LENGTH + ARG2_LENGTH;
29const INPUT_U8_LENGTH: usize = 4 * INPUT_Q1_31_LENGHT;
30
31#[embassy_executor::main]
32async 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}