rust-embedded / rust-embedded/aarch32

Generate read, write and modify automatically

Open
#139 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
66
Forks
25
Avg merge
4d 6h
Merged PRs (30d)
4

Description

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));

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the SysReg, SysRegRead, and related register trait definitions and the existing read, write, and modify implementations. Compare the associated-type and macro approaches, then confirm that generated methods remain discoverable and documentation remains readable; the work is done when the chosen approach removes the boilerplate consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.