rp2350 0.1.0

Hardware Abstraction Layer for RP2350 microcontrollers
Documentation
//! BUSCTRL (Bus Control) HAL for RP2350
//!
//! This module provides access to the bus fabric control signals
//! and performance counters for monitoring bus activity.
//!
//! ## Features
//!
//! - Bus arbitration priority control
//! - 4 performance counters for bus activity monitoring
//! - Configurable event selection for each counter
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::busctrl::{Busctrl, PerfEvent};
//!
//! let busctrl = Busctrl::init(dp.BUSCTRL);
//!
//! // Enable performance counters
//! busctrl.enable_perf_counters(true);
//!
//! // Configure counter 0 to count PROC0 access events
//! busctrl.set_perfsel0(PerfEvent::SiobProc0Access);
//!
//! // Read counter value
//! let count = busctrl.perfctr0();
//! ```

use crate::Steal;
use pastey::paste;
use rp2350_pac::BUSCTRL;

/// Performance counter event selection.
///
/// Each downstream port of the main crossbar exposes four events:
/// `StallUpstream`, `StallDownstream`, `AccessContested` and `Access`. All 17
/// ports (SIOB PROC0/1, APB, fast peripherals, SRAM0–9, XIP main0/1 and ROM)
/// are represented.
macro_rules! impl_perf_events {
    ($(($port:ident, $base:literal)),+ $(,)?) => {
        paste! {
            #[doc = "Performance counter event selection."]
            #[derive(Clone, Copy, PartialEq, Eq, Debug)]
            #[cfg_attr(feature = "defmt", derive(defmt::Format))]
            #[repr(u32)]
            pub enum PerfEvent {
                $(
                    #[doc = concat!(stringify!($port), " stall upstream")]
                    [<$port StallUpstream>] = $base,
                    #[doc = concat!(stringify!($port), " stall downstream")]
                    [<$port StallDownstream>] = $base + 1,
                    #[doc = concat!(stringify!($port), " access contested")]
                    [<$port AccessContested>] = $base + 2,
                    #[doc = concat!(stringify!($port), " access")]
                    [<$port Access>] = $base + 3,
                )+
            }
        }
    };
}

impl_perf_events!(
    (SiobProc1, 0),
    (SiobProc0, 4),
    (Apb, 8),
    (Fastperi, 12),
    (Sram9, 16),
    (Sram8, 20),
    (Sram7, 24),
    (Sram6, 28),
    (Sram5, 32),
    (Sram4, 36),
    (Sram3, 40),
    (Sram2, 44),
    (Sram1, 48),
    (Sram0, 52),
    (XipMain1, 56),
    (XipMain0, 60),
    (Rom, 64),
);

/// BUSCTRL peripheral wrapper
///
/// Provides access to bus fabric control and performance counters.
pub struct Busctrl {
    regs: rp2350_pac::BUSCTRL,
}

impl Busctrl {
    /// Initialize the BUSCTRL driver.
    ///
    /// Takes ownership of the BUSCTRL peripheral.
    #[inline]
    pub fn init(regs: BUSCTRL) -> Self {
        Self { regs }
    }

    /// Obtain a handle to the BUSCTRL hardware peripheral.
    ///
    /// # Safety
    ///
    /// The caller must ensure the peripheral has been initialised
    /// and that no data races occur.
    #[inline]
    pub unsafe fn steal() -> Steal<Self> {
        Steal(Self {
            regs: unsafe { BUSCTRL::steal() },
        })
    }

    /// Get reference to the register block
    #[inline]
    pub fn regs(&self) -> &rp2350_pac::busctrl::RegisterBlock {
        &self.regs
    }

    // ========================================================================
    // Bus Priority
    // ========================================================================

    // Per-master priority accessors are generated by `impl_bus_priority!` below.

    /// Check if bus priority has been acknowledged by all arbiters
    #[inline]
    pub fn is_priority_ack(&self) -> bool {
        self.regs.bus_priority_ack.bus_priority_ack()
    }

    // ========================================================================
    // Performance Counters
    // ========================================================================

    /// Enable or disable all performance counters
    #[inline]
    pub fn enable_perf_counters(&self, enable: bool) {
        if enable {
            self.regs.perfctr_en.set_perfctr_en();
        } else {
            self.regs.perfctr_en.clr_perfctr_en();
        }
    }

    /// Check if performance counters are enabled
    #[inline]
    pub fn is_perf_counters_enabled(&self) -> bool {
        self.regs.perfctr_en.perfctr_en()
    }

    // Per-counter accessors are generated by `impl_perf_counters!` below.

    // ======== Utility Functions ========

    /// Clear all performance counters
    #[inline]
    pub fn clear_all_perfctrs(&self) {
        self.regs.perfctr0.write_perfctr0(0);
        self.regs.perfctr1.write_perfctr1(0);
        self.regs.perfctr2.write_perfctr2(0);
        self.regs.perfctr3.write_perfctr3(0);
    }

    /// Get all performance counter values as an array
    #[inline]
    pub fn all_perfctrs(&self) -> [u32; 4] {
        [
            self.perfctr0(),
            self.perfctr1(),
            self.perfctr2(),
            self.perfctr3(),
        ]
    }
}

// ============================================================================
// Generated accessors
// ============================================================================

macro_rules! impl_bus_priority {
    ($(($name:ident, $set:ident, $clr:ident, $get:ident)),+ $(,)?) => {
        paste! {
            $(
                impl Busctrl {
                    #[doc = concat!("Set ", stringify!($name), " to high priority.")]
                    #[inline]
                    pub fn [<set_ $name _high_priority>](&self, high: bool) {
                        if high {
                            self.regs.bus_priority.$set();
                        } else {
                            self.regs.bus_priority.$clr();
                        }
                    }

                    #[doc = concat!("Check if ", stringify!($name), " has high priority.")]
                    #[inline]
                    pub fn [<is_ $name _high_priority>](&self) -> bool {
                        self.regs.bus_priority.$get()
                    }
                }
            )+
        }
    };
}

impl_bus_priority!(
    (proc0, set_proc0, clr_proc0, proc0),
    (proc1, set_proc1, clr_proc1, proc1),
    (dma_r, set_dma_r, clr_dma_r, dma_r),
    (dma_w, set_dma_w, clr_dma_w, dma_w),
);

macro_rules! impl_perf_counters {
    ($($n:literal),+ $(,)?) => {
        paste! {
            $(
                impl Busctrl {
                    #[doc = concat!("Get performance counter ", $n, " value.")]
                    #[inline]
                    pub fn [<perfctr $n>](&self) -> u32 {
                        self.regs.[<perfctr $n>].[<perfctr $n>]()
                    }

                    #[doc = concat!("Clear performance counter ", $n, ".")]
                    #[inline]
                    pub fn [<clear_perfctr $n>](&self) {
                        self.regs.[<perfctr $n>].[<write_perfctr $n>](0);
                    }

                    #[doc = concat!("Set performance counter ", $n, " event selection.")]
                    #[inline]
                    pub fn [<set_perfsel $n>](&self, event: PerfEvent) {
                        self.regs.[<perfsel $n>].[<write_perfsel $n>](event as u32);
                    }

                    #[doc = concat!("Get performance counter ", $n, " event selection.")]
                    #[inline]
                    pub fn [<perfsel $n>](&self) -> u32 {
                        self.regs.[<perfsel $n>].[<perfsel $n>]()
                    }
                }
            )+
        }
    };
}

impl_perf_counters!(0, 1, 2, 3);