Skip to content

Commit

Permalink
Add CORDIC Support (#132)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AdinAck authored Sep 22, 2024
1 parent 5405840 commit 906b83a
Show file tree
Hide file tree
Showing 4 changed files with 1,027 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bitflags = "1.2"
vcell = "0.1"
static_assertions = "1.1"
fugit = "0.3.5"
fixed = { version = "1.28.0", optional = true }

[dependencies.cortex-m]
version = "0.7.7"
Expand Down Expand Up @@ -89,6 +90,7 @@ log-itm = ["cortex-m-log/itm"]
log-rtt = []
log-semihost = ["cortex-m-log/semihosting"]
defmt-logging = ["defmt"]
cordic = ["dep:fixed"]

[profile.dev]
codegen-units = 1
Expand All @@ -105,3 +107,7 @@ lto = true
[[example]]
name = "flash_with_rtic"
required-features = ["stm32g474"]

[[example]]
name = "cordic"
required-features = ["cordic"]
57 changes: 57 additions & 0 deletions examples/cordic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![deny(warnings)]
#![deny(unsafe_code)]
#![no_main]
#![no_std]

extern crate cortex_m;
extern crate cortex_m_rt as rt;
extern crate stm32g4xx_hal as hal;

use fixed::types::I1F15;
use hal::cordic::{
func::{dynamic::Mode as _, scale::N0, Magnitude, SinCos, Sqrt},
prec::P60,
types::{Q15, Q31},
Ext as _,
};
use hal::prelude::*;
use hal::pwr::PwrExt;
use hal::rcc::Config;
use hal::stm32;
use rt::entry;

#[macro_use]
mod utils;

use utils::logger::println;

#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let pwr = dp.PWR.constrain().freeze();
let mut rcc = dp.RCC.freeze(Config::hsi(), pwr);

let mut cordic = dp
.CORDIC
.constrain(&mut rcc)
.freeze::<Q15, Q31, SinCos, P60>(); // 16 bit arguments, 32 bit results, compute sine and cosine, 60 iterations

// static operation (zero overhead)

cordic.start(I1F15::from_num(-0.25 /* -45 degreees */));

let (sin, cos) = cordic.result();

println!("sin: {}, cos: {}", sin.to_num::<f32>(), cos.to_num::<f32>());

// dynamic operation

let mut cordic = cordic.into_dynamic();

let sqrt = cordic.run::<Sqrt<N0>>(I1F15::from_num(0.25));
println!("sqrt: {}", sqrt.to_num::<f32>());
let magnitude = cordic.run::<Magnitude>((I1F15::from_num(0.25), I1F15::from_num(0.5)));
println!("magnitude: {}", magnitude.to_num::<f32>());

loop {}
}
Loading

0 comments on commit 906b83a

Please sign in to comment.