Skip to content

Commit 906b83a

Browse files
authored
Add CORDIC Support (#132)
* add CORDIC support * should fix CI failure * add example * require defmt for cordic example * put cordic stuff behind feature gate this will fail CI pending fix * significant refactor and dynamic mode * remove some erroneous whitespace, O -> OFFSET, more inline const exprs * simplify type signature, distinguish state vs feature
1 parent 5405840 commit 906b83a

File tree

4 files changed

+1027
-0
lines changed

4 files changed

+1027
-0
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bitflags = "1.2"
1919
vcell = "0.1"
2020
static_assertions = "1.1"
2121
fugit = "0.3.5"
22+
fixed = { version = "1.28.0", optional = true }
2223

2324
[dependencies.cortex-m]
2425
version = "0.7.7"
@@ -89,6 +90,7 @@ log-itm = ["cortex-m-log/itm"]
8990
log-rtt = []
9091
log-semihost = ["cortex-m-log/semihosting"]
9192
defmt-logging = ["defmt"]
93+
cordic = ["dep:fixed"]
9294

9395
[profile.dev]
9496
codegen-units = 1
@@ -105,3 +107,7 @@ lto = true
105107
[[example]]
106108
name = "flash_with_rtic"
107109
required-features = ["stm32g474"]
110+
111+
[[example]]
112+
name = "cordic"
113+
required-features = ["cordic"]

examples/cordic.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate cortex_m;
7+
extern crate cortex_m_rt as rt;
8+
extern crate stm32g4xx_hal as hal;
9+
10+
use fixed::types::I1F15;
11+
use hal::cordic::{
12+
func::{dynamic::Mode as _, scale::N0, Magnitude, SinCos, Sqrt},
13+
prec::P60,
14+
types::{Q15, Q31},
15+
Ext as _,
16+
};
17+
use hal::prelude::*;
18+
use hal::pwr::PwrExt;
19+
use hal::rcc::Config;
20+
use hal::stm32;
21+
use rt::entry;
22+
23+
#[macro_use]
24+
mod utils;
25+
26+
use utils::logger::println;
27+
28+
#[entry]
29+
fn main() -> ! {
30+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
31+
let pwr = dp.PWR.constrain().freeze();
32+
let mut rcc = dp.RCC.freeze(Config::hsi(), pwr);
33+
34+
let mut cordic = dp
35+
.CORDIC
36+
.constrain(&mut rcc)
37+
.freeze::<Q15, Q31, SinCos, P60>(); // 16 bit arguments, 32 bit results, compute sine and cosine, 60 iterations
38+
39+
// static operation (zero overhead)
40+
41+
cordic.start(I1F15::from_num(-0.25 /* -45 degreees */));
42+
43+
let (sin, cos) = cordic.result();
44+
45+
println!("sin: {}, cos: {}", sin.to_num::<f32>(), cos.to_num::<f32>());
46+
47+
// dynamic operation
48+
49+
let mut cordic = cordic.into_dynamic();
50+
51+
let sqrt = cordic.run::<Sqrt<N0>>(I1F15::from_num(0.25));
52+
println!("sqrt: {}", sqrt.to_num::<f32>());
53+
let magnitude = cordic.run::<Magnitude>((I1F15::from_num(0.25), I1F15::from_num(0.5)));
54+
println!("magnitude: {}", magnitude.to_num::<f32>());
55+
56+
loop {}
57+
}

0 commit comments

Comments
 (0)