aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/pio_programs/clock_divider.rs
blob: 02e353f53b0cd6b4ddd106d600fa168edb9875c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//! Helper functions for calculating PIO clock dividers

use fixed::traits::ToFixed;
use fixed::types::extra::U8;

use crate::clocks::clk_sys_freq;

/// Calculate a PIO clock divider value based on the desired target frequency.
///
/// # Arguments
///
/// * `target_hz` - The desired PIO clock frequency in Hz
///
/// # Returns
///
/// A fixed-point divider value suitable for use in a PIO state machine configuration
#[inline]
pub fn calculate_pio_clock_divider(target_hz: u32) -> fixed::FixedU32<U8> {
    // Requires a non-zero frequency
    assert!(target_hz > 0, "PIO clock frequency cannot be zero");

    // Calculate the divider
    let divider = (clk_sys_freq() + target_hz / 2) / target_hz;
    divider.to_fixed()
}