-
Notifications
You must be signed in to change notification settings - Fork 51
Add FMAC implementation #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boondocklabs
wants to merge
12
commits into
stm32-rs:main
Choose a base branch
from
boondocklabs:fmac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ced789f
Fix SysCfg hardfault
boondocklabs 557637a
Update button with workaround for wfi() and debug probes
boondocklabs 033b58a
Remove unused import
boondocklabs 6abbaef
Merge branch 'stm32-rs:main' into main
boondocklabs 1ea476a
Add FMAC implementation
boondocklabs a607975
Fix comments in example
boondocklabs 8bdda80
Fix comment spelling mistake
boondocklabs 8c2b233
Updates
boondocklabs 4b98fce
Fix request line in writer
boondocklabs bac7b62
Remove DBGMCU register modification from button example which is set …
boondocklabs 72c4223
Merge branch 'main' into fmac
boondocklabs 084d92d
Merge branch 'stm32-rs:main' into fmac
boondocklabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ stm32g4 = { version = "0.16.0", features = ["atomics"] } | |||||||||
paste = "1.0" | ||||||||||
fugit = "0.3.7" | ||||||||||
stm32-usbd = { version = "0.7.0", optional = true } | ||||||||||
fixed = { version = "1.28.0", optional = true } | ||||||||||
fixed = { version = "1.28.0" } | ||||||||||
embedded-io = "0.6" | ||||||||||
stm32-hrtim = { version = "0.1.0", optional = true } | ||||||||||
|
||||||||||
|
@@ -82,9 +82,23 @@ usb = ["dep:stm32-usbd"] | |||||||||
stm32g431 = ["stm32g4/stm32g431", "cat2"] | ||||||||||
stm32g441 = ["stm32g4/stm32g441", "cat2"] | ||||||||||
stm32g473 = ["stm32g4/stm32g473", "cat3", "adc3", "adc4", "adc5"] | ||||||||||
stm32g474 = ["stm32g4/stm32g474", "cat3", "adc3", "adc4", "adc5", "stm32-hrtim/stm32g474"] | ||||||||||
stm32g474 = [ | ||||||||||
"stm32g4/stm32g474", | ||||||||||
"cat3", | ||||||||||
"adc3", | ||||||||||
"adc4", | ||||||||||
"adc5", | ||||||||||
"stm32-hrtim/stm32g474", | ||||||||||
] | ||||||||||
stm32g483 = ["stm32g4/stm32g483", "cat3", "adc3", "adc4", "adc5"] | ||||||||||
stm32g484 = ["stm32g4/stm32g484", "cat3", "adc3", "adc4", "adc5", "stm32-hrtim/stm32g484"] | ||||||||||
stm32g484 = [ | ||||||||||
"stm32g4/stm32g484", | ||||||||||
"cat3", | ||||||||||
"adc3", | ||||||||||
"adc4", | ||||||||||
"adc5", | ||||||||||
"stm32-hrtim/stm32g484", | ||||||||||
] | ||||||||||
stm32g491 = ["stm32g4/stm32g491", "cat4", "adc3"] | ||||||||||
stm32g4a1 = ["stm32g4/stm32g4a1", "cat4", "adc3"] | ||||||||||
|
||||||||||
|
@@ -106,9 +120,10 @@ defmt = [ | |||||||||
"embedded-hal/defmt-03", | ||||||||||
"embedded-io/defmt-03", | ||||||||||
"embedded-test/defmt", | ||||||||||
"stm32-hrtim?/defmt" | ||||||||||
"stm32-hrtim?/defmt", | ||||||||||
] | ||||||||||
cordic = ["dep:fixed"] | ||||||||||
cordic = [] | ||||||||||
fmac = [] | ||||||||||
Comment on lines
+125
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
adc3 = [] | ||||||||||
adc4 = [] | ||||||||||
adc5 = [] | ||||||||||
|
@@ -174,6 +189,10 @@ required-features = ["usb"] | |||||||||
name = "cordic" | ||||||||||
required-features = ["cordic"] | ||||||||||
|
||||||||||
[[example]] | ||||||||||
name = "fmac" | ||||||||||
required-features = ["fmac"] | ||||||||||
|
||||||||||
[[example]] | ||||||||||
name = "hrtim-adc-trigger" | ||||||||||
required-features = ["hrtim"] | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default.rtt] | ||
enabled = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#![deny(warnings)] | ||
#![deny(unsafe_code)] | ||
#![no_main] | ||
#![no_std] | ||
|
||
use hal::prelude::*; | ||
use hal::stm32; | ||
use stm32g4xx_hal as hal; | ||
|
||
use cortex_m_rt::entry; | ||
|
||
#[macro_use] | ||
mod utils; | ||
|
||
use utils::logger::info; | ||
|
||
use stm32g4xx_hal::fmac::{ | ||
buffer::{BufferLayout, Watermark}, | ||
function::{Gain, IIR}, | ||
Buffer, FmacExt, | ||
}; | ||
|
||
use fixed::types::I1F15; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
utils::logger::init(); | ||
|
||
info!("start"); | ||
let dp = stm32::Peripherals::take().expect("cannot take peripherals"); | ||
let mut rcc = dp.RCC.constrain(); | ||
|
||
// The FMAC has an internal memory area of 256x16bit words that must be allocated to the | ||
// three different buffers named X1 (input buffer), X2 (coefficients), and Y (output buffer). | ||
// | ||
// The BufferLayout struct takes three generic consts and will calculate the base offsets at compile time, | ||
// which must be passed as a generic parameter to the constrain method of the FMAC instance. | ||
// | ||
// Create an FMAC instance with a memory layout of 32 inputs, 2 coefficients, and 1 output buffer words | ||
// | ||
// For IIR filters, RM0440 indicates X2 coeffecient buffer should be 2N + 2M where | ||
// N is the number of feedforward coefficients and M is the number of feedback coefficients. | ||
// | ||
// Following constrain(), the buffer layout has been applied to the peripheral and cannot be changed. | ||
let mut fmac = dp | ||
.FMAC | ||
.constrain::<BufferLayout<32, 4, 1>>(&mut rcc) | ||
.reset(); | ||
|
||
// Configure an IIR filter with 1 feedforward, and 1 feedback coefficient, | ||
// with both coefficients set to 1.0. | ||
// | ||
// This is equivalent to a multiply accumulate operation | ||
// Y[n] = ∑ X1[n] * X2[0] + Y[n-1] * X2[1] | ||
// | ||
// The inputs will be set to ~0.01 (to nearest fixed point representation), and the output will increment | ||
// by 0.01 into Y for each input sample, and the final read of Y should be about 0.319 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
// Fill the input buffer with 0.01 | ||
fmac.preload_buffer(Buffer::X1, |_index| I1F15::from_num(0.01)); | ||
|
||
// Fill the coefficients with I1F15::MAX (max value representable by I1F15, ~1.0) | ||
fmac.preload_buffer(Buffer::X2, |_index| I1F15::MAX); | ||
fmac.preload_buffer(Buffer::Y, |_index| I1F15::ZERO); | ||
|
||
// Watermarks can be set on the X1 and Y buffers | ||
// to control when the input empty and output full | ||
// flags are asserted to cause early interrupts or DMA | ||
// to avoid underflow or overflow conditions. | ||
fmac.set_watermark(Buffer::X1, Watermark::Threshold1); | ||
fmac.set_watermark(Buffer::Y, Watermark::Threshold1); | ||
|
||
// Select the IIR function, specifying the number of | ||
// feedforward and feedback coefficients, and the gain | ||
fmac.select_function(IIR { | ||
feedforward_coeffs: 1, | ||
feedback_coeffs: 1, | ||
gain: Gain::ZeroDB, | ||
}); | ||
|
||
let fmac = fmac.start(); | ||
|
||
info!("Input buffer full: {}", fmac.is_input_full()); | ||
|
||
info!("Waiting for result"); | ||
while !fmac.is_result_available() {} | ||
|
||
info!("Reading results"); | ||
|
||
let mut count = 0; | ||
loop { | ||
while let Some(output) = fmac.read() { | ||
count += 1; | ||
info!("Output {}: {}", count, output.to_num::<f32>()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make this no longer optional?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually originated from DualDac PR, which wasn't feature gated and depends on
fixed::types
- not strictly necessary for this PR, but if DualDac is merged as-is, it wouldn't be optional. Could either feature gate dualdac or just leave this as is