Skip to content

Generate read, write and modify automatically #139

@jonathanpallant

Description

@jonathanpallant

If we expand the SysReg trait definitions to include an associated type, we can reduce a lot of the boiler-plate and add read, write and modify functions effectively for free.

/// MPIDR (*Multiprocessor Affinity Register*)
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mpidr(pub u32);

impl SysReg for Mpidr {
    const CP: u32 = 15;
    const CRN: u32 = 0;
    const OP1: u32 = 0;
    const CRM: u32 = 0;
    const OP2: u32 = 5;
}

impl crate::register::SysRegRead for Mpidr {}

impl Mpidr {
    #[inline]
    /// Reads MPIDR (*Multiprocessor Affinity Register*)
    pub fn read() -> Mpidr {
        unsafe { Self(<Self as SysRegRead>::read_raw()) }
    }
}

could become

/// MPIDR (*Multiprocessor Affinity Register*)
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mpidr(pub u32);

impl SysReg for Mpidr {
    const CP: u32 = 15;
    const CRN: u32 = 0;
    const OP1: u32 = 0;
    const CRM: u32 = 0;
    const OP2: u32 = 5;
}

impl SysRegRead for Mpidr {
    type R = Mpidr;

    fn convert(value: u32) -> Self { Self(value) }
}

However, I'd want to check that the readability of the documentation doesn't suffer if we do this, and the fn read() method is still easily discoverable. Perhaps examples of how to read the register would help. Or, we could do it with a macro instead:

/// MPIDR (*Multiprocessor Affinity Register*)
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Mpidr(pub u32);

sysreg_read!(cp = p15, crn = 0, op1 = 0, crm = 0, op2 = 5, Mpidr, Self(value));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions