All Projects â†’ rust-embedded â†’ register-rs

rust-embedded / register-rs

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
Unified interface for type-safe MMIO and CPU register access in Rust

Programming Languages

rust
11053 projects
Makefile
30231 projects

Projects that are alternatives of or similar to register-rs

Cortex M Quickstart
Template to develop bare metal applications for Cortex-M microcontrollers
Stars: ✭ 372 (+675%)
Mutual labels:  bare-metal, no-std
Rust Raspberrypi Os Tutorials
📚 Learn to write an embedded OS in Rust 🦀
Stars: ✭ 7,275 (+15056.25%)
Mutual labels:  operating-system, bare-metal
Cortex M Rtic
Real-Time Interrupt-driven Concurrency (RTIC) framework for ARM Cortex-M microcontrollers
Stars: ✭ 623 (+1197.92%)
Mutual labels:  bare-metal, no-std
drone-stm32-map
STM32 peripheral mappings for Drone, an Embedded Operating System.
Stars: ✭ 16 (-66.67%)
Mutual labels:  bare-metal, no-std
TinyMIPS
The Project TinyMIPS is dedicated to enabling undergraduates to build a complete computer system from scratch.
Stars: ✭ 29 (-39.58%)
Mutual labels:  cpu, operating-system
Drone Core
The core crate for Drone, an Embedded Operating System.
Stars: ✭ 263 (+447.92%)
Mutual labels:  bare-metal, no-std
Drone
CLI utility for Drone, an Embedded Operating System.
Stars: ✭ 114 (+137.5%)
Mutual labels:  bare-metal, no-std
drone-cortexm
ARM® Cortex®-M platform crate for Drone, an Embedded Operating System.
Stars: ✭ 31 (-35.42%)
Mutual labels:  bare-metal, no-std
Aros
Main AROS repository for active development. Contains the main Operating System components and Build System.
Stars: ✭ 146 (+204.17%)
Mutual labels:  operating-system, bare-metal
Packer Bare Metal
Building bare metal OS images with Packer, VirtualBox and qemu-img
Stars: ✭ 115 (+139.58%)
Mutual labels:  operating-system, bare-metal
Xargo
The sysroot manager that lets you build and customize `std`
Stars: ✭ 841 (+1652.08%)
Mutual labels:  bare-metal, no-std
The Hack General Purpose Computer
Using HDL, from Boolean algebra and elementary logic gates to building a Central Processing Unit, a memory system, and a hardware platform, leading up to a 16-bit general-purpose computer. Then, implementing the modern software hierarchy designed to enable the translation and execution of object-based, high-level languages on a bare-bone computer hardware platform; Including Virtual machine,Compiler and Operating system.
Stars: ✭ 39 (-18.75%)
Mutual labels:  cpu, operating-system
Alchemy
A toy operating system written in Rust.
Stars: ✭ 16 (-66.67%)
Mutual labels:  operating-system, bare-metal
Ustc Tmips
Stars: ✭ 6 (-87.5%)
Mutual labels:  cpu, operating-system
Advanced-xv6
Modern improvements for MIT's xv6 OS
Stars: ✭ 26 (-45.83%)
Mutual labels:  cpu, operating-system
login register boiler plate code flutter
Login/Singin & Register/SignUp Screen
Stars: ✭ 59 (+22.92%)
Mutual labels:  register
stm32f103xx
DEPRECATED
Stars: ✭ 31 (-35.42%)
Mutual labels:  no-std
managed ml systems and iot
Managed Machine Learning Systems and Internet of Things Live Lesson
Stars: ✭ 35 (-27.08%)
Mutual labels:  cpu
kianRiscV
KianRISC-V! No RISC-V, no fun! RISC-V CPU with strong design rules and unittested! CPU you can trust! kianv rv32im risc-v a hdmi soc with harris computer architecture in verilog: multicycle, singlecycle and 5-stage pipelining Processor. Multicycle Soc with firmware that runs raytracer, mandelbrot, 3d hdmi gfx, dma controller, etc.....
Stars: ✭ 167 (+247.92%)
Mutual labels:  cpu
Api-Doc
A Technology for Rest API Documentation 💻 📜 "Dockerized"
Stars: ✭ 14 (-70.83%)
Mutual labels:  register

crates.io crates.io

Deprecated

DEPRECATED: This crate is no longer supported, and dependents are instead recommended to switch to tock-registers.

register-rs was started with a single goal: To bring the goodness of tock-registers, which in the past was only available for MMIO registers, to CPU registers as well, to provide a unified interface for both. Since release 0.7.0, tock-registers now itself provides the infrastructure to implement the tock register traits for CPU registers, rendering this crate obsolete.

register-rs

Unified interface for type-safe MMIO and CPU register access in Rust.

Outline

Usage

This crate uses the tock-register-interface, please refer to their Readme for the whole API. Make sure to check the register_structs! explanation, especially the fact that you must explicitly annotate gaps.

Defining MMIO registers

use register::{mmio::*, register_bitfields, register_structs};

register_bitfields! {
    u32,

    GPFSEL1 [
        FSEL14 OFFSET(12) NUMBITS(3) [
            Input = 0b000,
            Output = 0b001,
            TXD0 = 0b100
        ],

        FSEL15 OFFSET(15) NUMBITS(3) [
            Input = 0b000,
            Output = 0b001,
            RXD0 = 0b100
        ]
    ]
}

register_structs! {
    #[allow(non_snake_case)]
    pub RegisterBlock {
        (0x000 => GPFSEL1: ReadWrite<u32, GPFSEL1::Register>),
        (0x004 => SYSTMR_HI: ReadOnly<u32>),
        (0x008 => @END),
    }
}

fn main() {
    let regs = 0x1337_0000 as *const RegisterBlock;

    unsafe { (*regs).SYSTMR_HI.get() };
}

The Deref pattern for drivers

The MMIO part of this crate can and will often be used for implementing device drivers. In this case, you might find the Deref pattern useful for referencing your registers. It alleviates you from manually dereferencing each time a register access is due, and also encapsulates the unsafe keyword.

Here is an example (extending the code snippet from above):

register_bitfields! {
    u32,

    // omitted
}

register_structs! {
    #[allow(non_snake_case)]
    pub RegisterBlock {
        // omitted
    }
}

pub struct DeviceDriver {
    base_addr: usize,
}

impl ops::Deref for DeviceDriver {
    type Target = RegisterBlock;

    fn deref(&self) -> &Self::Target {
        unsafe { &*self.ptr() }
    }
}

impl DeviceDriver {
    pub fn new(base_addr: usize) -> Self {
        DeviceDriver { base_addr }
    }

    /// Returns a pointer to the register block
    fn ptr(&self) -> *const RegisterBlock {
        self.base_addr as *const _
    }

    fn do_something(&self) -> u32 {
        self.GPFSEL1.set(0x1337);
        self.SYSTMR_HI.get()
    }
}

Defining CPU registers

For CPU registers, you only need to implement the respective read/write trait. All other methods are provided by default.

#![feature(llvm_asm)]

use register::{cpu::RegisterReadWrite, register_bitfields};

register_bitfields! {u32,
    pub CNTP_CTL_EL0 [
        /// Enables the timer.
        ENABLE        OFFSET(0)  NUMBITS(1) [],

        /// Timer interrupt mask bit.
        IMASK         OFFSET(1)  NUMBITS(1) [],

        /// The status of the timer.
        ISTATUS       OFFSET(2)  NUMBITS(1) []
    ]
}

struct Reg;

impl RegisterReadWrite<u32, CNTP_CTL_EL0::Register> for Reg {
    /// Reads the raw bits of the CPU register.
    #[inline(always)]
    fn get(&self) -> u32 {
        let reg;
        unsafe {
            llvm_asm!("mrs $0, CNTP_CTL_EL0" : "=r"(reg) ::: "volatile");
        }
        reg
    }

    /// Writes raw bits to the CPU register.
    #[inline(always)]
    fn set(&self, value: u32) {
        unsafe {
            llvm_asm!("msr CNTP_CTL_EL0, $0" :: "r"(value) :: "volatile");
        }
    }
}

static CNTP_CTL_EL0: Reg = Reg {};

fn main() {
    CNTP_CTL_EL0.modify(CNTP_CTL_EL0::ENABLE::SET + CNTP_CTL_EL0::IMASK::SET);
}

Testing

In order to use this crate in custom_test_frameworks environments, please set the no_std_unit_tests feature flag in your dependency section.

Otherwise, you might encounter errors like the following:

error[E0463]: can't find crate for `test`
  --> src/bsp/driver/bcm/bcm2xxx_gpio.rs:52:1
   |
52 | / register_structs! {
53 | |     #[allow(non_snake_case)]
54 | |     RegisterBlock {
55 | |         (0x00 => GPFSEL0: ReadWrite<u32>),
...  |
66 | |     }
67 | | }
   | |_^ can't find crate
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].