Git Product home page Git Product logo

bxcan's Introduction

STM32 Peripheral Access Crates

CI crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io Matrix

This repository provides Rust device support crates for all STM32 microcontrollers, providing a safe API to that device's peripherals using svd2rust and a community-built collection of patches to the basic SVD files. There is one crate per device family, and each supported device is a feature-gated module in that crate. These crates are commonly known as peripheral access crates or "PACs".

To view the generated code that makes up each crate, visit the stm32-rs-nightlies repository, which is automatically rebuilt on every commit to stm32-rs master. The stm32-rs repository contains the patches to the underlying SVD files and the tooling to generate the crates.

While these crates are widely used, not every register of every device will have been tested on hardware, and so errors or omissions may remain. We can't make any guarantee of correctness. Please report any bugs you find!

You can see current coverage status for each chip here. Coverage means that individual fields are documented with possible values, but even devices with low coverage should have every register and field available in the API. That page also allows you to drill down into each field on each register on each peripheral.

Using Device Crates In Your Own Project

In your own project's Cargo.toml:

[dependencies.stm32f4]
version = "0.15.1"
features = ["stm32f405", "rt"]

The rt feature is optional but helpful. See svd2rust for details.

Then, in your code:

use stm32f4::stm32f405;

let mut peripherals = stm32f405::Peripherals::take().unwrap();

Refer to svd2rust documentation for further usage.

Replace stm32f4 and stm32f405 with your own device; see the individual crate READMEs for the complete list of supported devices. All current STM32 devices should be supported to some level.

Using Latest "Nightly" Builds

Whenever the master branch of this repository is updated, all device crates are built and deployed to the stm32-rs-nightlies repository. You can use this in your Cargo.toml:

[dependencies.stm32f4]
git = "https://github.com/stm32-rs/stm32-rs-nightlies"
features = ["stm32f405", "rt"]

The nightlies should always build and be as stable as the latest release, but contain the latest patches and updates.

Generating Device Crates / Building Locally

  • Install svd2rust, svdtools, and form:
    • On x86-64 Linux, run make install to download pre-built binaries at the current version used by stm32-rs
    • Otherwise, build using cargo (double check versions against scripts/tool_install.sh):
      • cargo install form --version 0.12.1
      • cargo install svdtools --version 0.3.17
      • cargo install svd2rust --version 0.33.4
  • Install rustfmt: rustup component add rustfmt
  • Generate patched SVD files: make patch (you probably want -j for all make invocations)
    • Alternatively you could install cargo-make runner and then use it instead of make. Works on MS Windows natively:
      • cargo install cargo-make
      • cargo make patch
  • Generate svd2rust device crates: make svd2rust
  • Optional: Format device crates: make form

Motivation and Objectives

This project serves two purposes:

  • Create a source of high-quality STM32 SVD files, with manufacturer errors and inconsistencies fixed. These files could be used with svd2rust or other tools, or in other projects. They should hopefully be useful in their own right.
  • Create and publish svd2rust-generated crates covering all STM32s, using the SVD files.

When this project began, many individual crates existed for specific STM32 devices, typically maintained separately with hand-edited updates to the SVD files. This project hopes to reduce that duplication of effort and centralise the community's STM32 device support in one place.

Helping

This project is still young and there's a lot to do!

  • More peripheral patches need to be written, most of all. See what we've got in peripherals/ and grab a reference manual!
  • Also everything needs testing, and you can't so easily automate finding bugs in the SVD files...

Supported Device Families

crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io crates.io

Please see the individual crate READMEs for the full list of devices each crate supports. All SVDs released by ST for STM32 devices are covered, so probably your device is supported to some extent!

Devices that are nearly identical, like the STM32F405/F415, are supported by ST under a single SVD file STM32F405, so if you can't find your exact device check if its sibling is supported instead. The crate READMEs make this clear.

Many peripherals are not yet patched to provide the type-safe friendly-name interface (enumerated values); please consider helping out with this!

Check out the full list of supported devices here.

Adding New Devices

  • Update SVD zips in svd/vendor to include new SVDs.
  • Run make extract to extract the new zip files.
  • Add new YAML file in devices/ with the new SVD path and include any required SVD patches for this device, such as renaming or merging fields.
  • Add the new devices to stm32_part_table.yaml.
  • Add the new devices to scripts/makecrates.py.
  • You can run scripts/matchperipherals.py script to find out what existing peripherals could be cleanly applied to this new SVD. If they look sensible, you can include them in your device YAML. This requires a Python environment with the pyyaml and svdtools dependencies. Example command: python scripts/matchperipherals.py peripherals/rcc devices/stm32h562.yaml
  • Re-run scripts/makecrates.py devices/ to update the crates with the new devices.
  • Run make to rebuild, which will make a patched SVD and then run svd2rust on it to generate the final library.

If adding a new STM32 family (not just a new device to an existing family), complete these steps as well:

  • Add the new devices to the CRATES field in Makefile.
  • Update this Readme to include the new devices.
  • Add the devices to workflows/ci.yaml and workflows/nightlies.yaml.

Updating Existing Devices/Peripherals

  • Using Linux, run make extract at least once to pull the SVDs out.
  • Edit the device or peripheral YAML (see below for format).
  • Using Linux, run make to rebuild all the crates using svd patch and svd2rust.
  • Test your new stuff compiles: cd stm32f4; cargo build --features stm32f405

If you've added a new peripheral, consider using the matchperipherals.py script to see which devices it would cleanly apply to.

To generate a new peripheral file from scratch, consider using periphtemplate.py, which creates an empty peripheral file based on a single SVD file, with registers and fields ready to be populated. For single bit wide fields with names ending in 'E' or 'D' it additionally generates sample "Enabled"/"Disabled" entries to save time.

Device and Peripheral YAML Format

Please see the svdtools documentation for full details of the patch file format.

Style Guide

  • Enumerated values should be named in the past tense ("enabled", "masked", etc).
  • Descriptions should start with capital letters but do not end with a period

Releasing

Notes for maintainers:

  1. Create PR preparing for new release:
    • Update CHANGELOG.md with changes since last release and new contributors
    • Update README.md to bump version number in example snippet
    • Update scripts/makecrates.py to update version number for generated PACs
  2. Merge PR once CI passes, pull master locally.
  3. make clean
  4. make -j16 form
  5. for f in stm32f0 stm32f1 stm32f2 stm32f3 stm32f4 stm32f7 stm32h7 stm32l0 stm32l1 stm32l4 stm32l5 stm32g0 stm32g4 stm32mp1 stm32wl stm32wb; cd $f; pwd; cargo publish --allow-dirty --no-default-features; cd ..; end
  6. git tag -a vX.X.X -m vX.X.X
  7. git push vX.X.X

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.

bxcan's People

Contributors

bors[bot] avatar jonas-schievink avatar newam avatar samcrow avatar sh3rm4n avatar systec-ms avatar timokroeger avatar xoviat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bxcan's Issues

Move `Can::enable` into `CanConfig`

It's easy to forget to call it, so making it consume the CanConfig and marking that with #[must_use] makes that harder. This is a breaking change.

embedded-can support

Hi,

I'm trying to understand the state of affairs with embedded-can but I'm having a hard time figuring it out. Can anyone shed some light?

Note that I've also created an issue in the embedded rust WG

Thanks,

version conflicts

I am building some things that work with multiple device crates and have just started getting cargo update errors because one crate has update bxcan while another has not:

error: failed to select a version for `defmt`.
    ... required by package `bxcan v0.5.0`
    ... which is depended on by `stm32f7xx-hal v0.2.0 (https://github.com/stm32-rs/stm32f7xx-hal#c0bb1f2b)`
    ... which is depended on by `eg_stm_hal v0.1.0 (/home/paul/githubClones/eg_stm_hal/boards/none-stm32f722)`
versions that meet the requirements `^0.2.0` are: 0.2.1, 0.2.0

the package `defmt` links to the native library `defmt`, but it conflicts with a previous package which links to `defmt` as well:
package `defmt v0.1.3`
    ... which is depended on by `bxcan v0.4.0`
    ... which is depended on by `stm32f0xx-hal v0.17.1 (https://github.com/stm32-rs/stm32f0xx-hal#e9c99650)`
    ... which is depended on by `eg_stm_hal v0.1.0 (/home/paul/githubClones/eg_stm_hal/boards/none-stm32f722)`

failed to select a version for `defmt` which could resolve this conflict

It seems that bxcan demands a specific version of defmt and also in the README.md usage suggests demanding a specific version of bxcan (now bxcan = "0.5.0" but I see it is updated automatically).

Any chance you can be more flexible by depending on defmt = "^..." or ">=..." and suggest similar flexibility regarding bxcan in your README?

Logic for checking for empty or preemptable tx mailbox condition seems incorrect

The condition here:

if tir.txrq().bit_is_set() && id <= IdReg::from_register(tir.bits()) {

should return an Ok if the mailbox is empty or if it is not transmitted and has a lower priority, however the condition seems to return WouldBlock in the case that the message is still pending, but the Id is greater than or equal (same or lower priority).

Should this be reversed to be && id > IdReg::from_register(tir.bits()) so it only returns WouldBlock in the case that the requested id is higher (lower priority) than the existing pending frame?

Support for two FIFOs

Is there a way to configure the FIFO assignment of a filter? From the datasheet, it's configured using the CAN_FFAxR register and I don't see any references to that register in the source for this.

Have you considered for this to become part of the stm32-rs organization?

Basically the title. I believe that if this is available only on STM32s and the HALs will become dependent on it, it might be a good idea to be maintained as part of the organization.

What do you think about it? This is just an idea I had when reviewing a PR that was adding the dependency, I certainly don't want to force you to move the repository there.

Version and linkage foo between crates.io and GitHub

Hi,

thanks for the crate, great work. However I noticed some things:

  • on crates.io, this is listed as v0.5.0 and it links to another fork of this GitHub repo
  • said fork is tagged only up to v0.4.0 and depends on defmt v0.1.3 and therefore breaks builds with the current recommended version of defmt, i.e. v0.2.0
  • this creates dependency hell because docs of defmt, examples, etc all are geared to be compatible with defmt v0.2.0
  • this GitHub repo here depends on defmt v0.2.0 already, but that is not of importance since crates.io does not link here

Is it fair to assume that this here is the active development crate? If that is the case, may you point crates.io in the right direction, please? While at it, a release for 0.5.0 could be created here as well.

I'm sorry to have to bother you with this, but I don't see how I could fix this myself.

Cheers

Implement `free()` method

In the case I understood the API correctly, it is currently not possible to retrieve the peripheral consumed with Can::new associated with the Instance trait

pub unsafe trait Instance {

To retrieve the peripheral, maybe a free() method should be implemented like that:

diff --git a/src/lib.rs b/src/lib.rs
index 2584107..f589e00 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -470,6 +470,10 @@ where
     pub fn split(self) -> (Tx<I>, Rx<I>) {
         unsafe { (Tx::conjure(), Rx::conjure()) }
     }
+
+    pub fn free(self) ->  I {
+        self.instance
+    }
 }
 
 impl<I: FilterOwner> Can<I> {

This is probably not enough, I guess. I have only glanced over the implementation, but I imagine, that the peripheral has to be deconfigured to safely free it.

can initialization process doesn't work on stm32f4

This does seem to work (though I haven't yet sent a message):

    dp.RCC.apb2enr.write(|w| w.syscfgen().enabled());

    let gpioa = dp.GPIOA.split();
    let gpiob = dp.GPIOB.split();
    let gpioc = dp.GPIOC.split();
    let mut rcc = dp.RCC.constrain();

    let clocks = rcc
        .cfgr
        .use_hse(16.mhz())
        .sysclk(48.mhz())
        .pclk1(24.mhz())
        .freeze();

    let mut delay = Delay::new(cp.SYST, clocks);

    CAN1::enable(&mut rcc.apb1);

    dp.CAN1.mcr.modify(|_, w| w.sleep().clear_bit());
    let mut waiting = true;
    while waiting {
        let msr = dp.CAN1.msr.read();
        if msr.slak().bit_is_set() {
            waiting = false;
        }
    }

    dp.CAN1.mcr.modify(|_, w| w.dbf().clear_bit());

    let r_mcr = dp.CAN1.mcr.read().bits();
    let r_msr = dp.CAN1.msr.read().bits();
    let r_tsr = dp.CAN1.tsr.read().bits();

    /* request initializiation */
    dp.CAN1.mcr.modify(|_, w| w.inrq().set_bit());
    let mut waiting = true;
    while waiting {
        let msr = dp.CAN1.msr.read();
        if msr.inak().bit_is_set() {
            waiting = false;
        }
    }

    dp.CAN1.btr.modify(|_, w| unsafe { w.bits(0x001c_0003) });

compare to the initialization process in this pacakge

        can.mcr
            .modify(|_, w| w.sleep().clear_bit().inrq().set_bit());
        while can.msr.read().inak().bit_is_clear() {}

        can.mcr
            .modify(|_, w| w.sleep().set_bit().inrq().clear_bit());
        while can.msr.read().slak().bit_is_clear() {}

        let msr = can.msr.read();
        while !msr.slak().bit_is_set() {
            can.mcr
                .modify(|_, w| w.abom().set_bit().sleep().clear_bit());
        }

I'm not sure why it's currently done this way, but the slak() bit is not set.

API ideas: Filterbank builder pattern

I’m in the process of porting this to stm32f1xx devices with two CAN instances.

With the current API its easy to forget to enable filters especially when only using CAN2.
I propose following API to ensure filters are configured:

// Single intsance
let filters = can
    .filters()
    .bank()
    .bank()
    .enable(); // Clears the FINIT bit

// Multiple intsances
let (can1_filters, can2_filters) = can1
    .filters()
    .bank()
    .bank()
    .slave_filters()
    .bank()
    .bank()
    .enable(); // Clears the FINIT bit

// Require the filters so the user does not forget to set them up and
// starts debugging because no messages are received at all.
can1.enable(can1_filters);
can2.enable(can2_filters);

What do you think? Any pitfalls I overlooked?

`Can::builder`

Currently (after #37), users have to perform 2 steps to use bxCAN: Call Can::new, then call Can::modify_config. The rest is very hard to forget because of #[must_use] on CanConfig.

We could reduce this to 1 step by replacing Can::new by Can::builder, and have that return a CanBuilder that functions similar to CanConfig, but gives you a Can instance when finished.

align configure api with filter API?

They both enter an 'initialization mode', though I think the modes are different. I could add a new method called modify_config that would allow method chaining, like the filter configuration. This would be a non-breaking change for almost all projects.

Issue with creating new instance of can for STM32F429 Discovery board using bxcan

Problem while executing the example file can-send.rs from the stm32f4xx_hal crate [https://github.com/stm32-rs/stm32f4xx-hal/blob/master/examples/can-send.rs] for stm32f429 Discovery board, cannot find any way to instantiate can using new(), the compiler shows error saying there is no such function like new().
I have been facing this issue since a while. It would be great if you could help.

image

update to `embedded-can` traits

hi, i was wondering what is the current status and if there are any plans to update to embedded-can traits? i am happy to be of any help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.