Skip to content

Commit

Permalink
Merge branch 'dev/can' into maint-v0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dstric-aqueduct committed Dec 12, 2022
2 parents 2ab80a7 + 6da6c97 commit 081d027
Show file tree
Hide file tree
Showing 21 changed files with 2,360 additions and 921 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
.idea
.vscode
Cargo.lock
41 changes: 9 additions & 32 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Thanks for helping us build embedded Rust support for NXP's i.MX RT processors! Please open an issue if

- you find a bug in the HAL, RAL, or IOMUXC crates
- you find a bug in any chip HAL
- you have an idea for a feature
- something isn't clear in our documentation

Expand All @@ -20,45 +20,22 @@ You'll need
```bash
rustup target add thumbv7em-none-eabihf
```
### Chip-specific HAL(s)

### RAL
We support one HAL crate per i.MX RT processor family. A "processor family" is described by an NXP datasheet and reference manual. For example, the `imxrt1060-hal` supports the [i.MX RT1060 Crossover Processors](https://www.nxp.com/docs/en/nxp/data-sheets/IMXRT1060CEC.pdf), which includes the following processors:

The `imxrt-ral` crate is auto-generated from the checked-in SVD files, available in `imxrt-ral/svd`. It's checked into git, and you should always have whatever represents the latest auto-generated RAL. Generally, you should **not** manually change RAL source files; rather, you should describe changes in `imxrtral.py`, the Python script that auto-generates the RAL.
- i.MX RT 1061
- i.MX RT 1062

To generate the RAL,
When developing the HAL(s) a quick way to check everything compiles, in the project root

- Install Python 3. You'll need at least Python 3.6.
- Install the Python dependencies needed to generate the RAL: `pip3 install --user svdtools`. Alternatively, use the rules in the RAL's `Makefile` to create a virtual environment with the necessary dependencies: `make venv update-venv && source venv/bin/activate`.
- Run `make` in the `imxrt-ral` directory: `make -C imxrt-ral`. The auto-generation script might generate warnings; that's OK.

If everything went well, you should find that the `imxrt-ral/src` directory is populated with Rust files. If you made changes in `imxrtral.py`, you should see those changes reflected in the Rust files. The RAL can build by itself: `cd imxrt-ral && cargo check --features imxrt1062 --target thumbv7em-none-eabihf`.

If you add a SVD patch, or if you change something in `imxrtral.py`, you'll need to re-generate the RAL to realize the change.

### HAL

Make sure you've generated the RAL (see above). When developing the HAL, specify a feature flag from the command line. To check the HAL for `imxrt1062` processors, `cd imxrt-hal`, then

```
cargo check --features imxrt1062 --target thumbv7em-none-eabihf
```

### IOMUXC

The `imxrt-iomuxc` crate family does not require any feature flags, and it will build for your host. Consider using `--package` flags to build and test the crate family in one command:

cargo check --target thumbv7em-none-eabihf
```
cargo build --package=imxrt-iomuxc --package=imxrt-iomuxc-build
cargo test -p imxrt-iomuxc -p imxrt-iomuxc-build
```

### SVD Patches

To modify the RAL, you'll need to describe your change as an SVD patch. If you'd like to add patches to the i.MX RT SVD files, learn about [svdtools](https://github.com/stm32-rs/svdtools). Use some of the existing SVD patches as a guide.

### Testing

Our CI system ensures that the RAL and HAL builds for all processor variants. But, we can't automatically test against hardware! To test your changes on hardware, you'll need to test the RAL and the HAL using another project, like a Rust BSP crate. Some BSP crates that use the `imxrt-hal` include
Our CI system ensures that the RAL and HAL(s) build for all processor variants. But, we can't automatically test against hardware! To test your changes on hardware, you'll need to test the RAL and the HAL(s) using another project, like a Rust BSP crate. Some BSP crates that use the `imxrt1060-hal` include

- [the `imxrt1060evk-bsp` crate](https://github.com/imxrt-rs/imxrt1060evk-bsp)
- [the `teensy4-bsp` crate](https://github.com/mciantyre/teensy4-rs)
Expand All @@ -76,4 +53,4 @@ Follow the instructions in those projects to prepare an environment for testing

## Release Steps

To create a release of the RAL and HAL crates, see [RELEASE.md](docs/RELEASE.md).
To create a release of the RAL and HAL crates, see [RELEASE.md](docs/RELEASE.md).
28 changes: 12 additions & 16 deletions imxrt-hal/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# imxrt-hal
# imxrt1060-hal

This project provides a Rust HAL (hardware abstraction layer) for all NXP i.MX RT
microcontrollers based on the imxrt-ral crate.
`imxrt1060-hal` is a Rust hardware abstraction layer that's specific to i.MX
RT 1060 processors. This includes some of the following processor parts:

A feature flag needs to be set for any of the supported i.MX RT SoC.
- i.MX RT 1061
- i.MX RT 1062

## What is it?
It is the successor to `imxrt-hal`, version 0.4, with `feature = "imxrt1062"`.

imxrt-hal is an experiment into a lightweight hardware abstraction layer. It
provides access to some of the peripherals of the i.MX RT series processors
from NXP using embedded-hal and other community driven hardware APIs.
## Features

The main aims are fast compilation, compactness, and simplicity.
The table below describes the optional features supported by `imxrt1060-hal`.

Please consider trying it out and contributing or leaving feedback!

## Goals

* Simple to use and hard to use incorrectly
* All peripherals and busses supported
* Support the entire i.MX RT Series with a single crate to maximize code reuse
| Feature | Description |
| -------- | ---------------------------------- |
| `"rt"` | Runtime support with `cortex-m-rt` |
| `"rtic"` | Support for RTIC |
104 changes: 104 additions & 0 deletions imxrt-hal/src/can/embedded_hal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! `embedded_hal` trait impls.
use super::{Data, ExtendedId, Frame, Id, OverrunError, StandardId, CAN};

use crate::iomuxc::consts::Unsigned;
use embedded_hal::can;

impl<M> can::Can for CAN<M>
where
M: Unsigned,
{
type Frame = Frame;

type Error = OverrunError;

fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
match self.transmit(frame) {
Ok(_status) => Ok(Some(frame.clone())),
Err(nb::Error::WouldBlock) => Err(nb::Error::WouldBlock),
Err(nb::Error::Other(e)) => match e {},
}
}

fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
let data: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];
let id = StandardId::new(0).unwrap();

Ok(Frame::new_data(id, Data::new(&data).unwrap()))
}
}

impl can::Error for OverrunError {
fn kind(&self) -> can::ErrorKind {
can::ErrorKind::Overrun
}
}

impl can::Frame for Frame {
fn new(id: impl Into<can::Id>, data: &[u8]) -> Option<Self> {
let id = match id.into() {
can::Id::Standard(id) => unsafe {
Id::Standard(StandardId::new_unchecked(id.as_raw()))
},
can::Id::Extended(id) => unsafe {
Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
},
};

let data = Data::new(data)?;
Some(Frame::new_data(id, data))
}

fn new_remote(id: impl Into<can::Id>, dlc: usize) -> Option<Self> {
let id = match id.into() {
can::Id::Standard(id) => unsafe {
Id::Standard(StandardId::new_unchecked(id.as_raw()))
},
can::Id::Extended(id) => unsafe {
Id::Extended(ExtendedId::new_unchecked(id.as_raw()))
},
};

if dlc <= 8 {
Some(Frame::new_remote(id, dlc as u8))
} else {
None
}
}

#[inline]
fn is_extended(&self) -> bool {
self.is_extended()
}

#[inline]
fn is_remote_frame(&self) -> bool {
self.is_remote_frame()
}

#[inline]
fn id(&self) -> can::Id {
match self.id() {
Id::Standard(id) => unsafe {
can::Id::Standard(can::StandardId::new_unchecked(id.as_raw()))
},
Id::Extended(id) => unsafe {
can::Id::Extended(can::ExtendedId::new_unchecked(id.as_raw()))
},
}
}

#[inline]
fn dlc(&self) -> usize {
self.dlc().into()
}

fn data(&self) -> &[u8] {
if let Some(data) = self.data() {
data
} else {
&[]
}
}
}
27 changes: 27 additions & 0 deletions imxrt-hal/src/can/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Filter bank API.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum FlexCanIde {
#[default]
None = 0,
Ext = 1,
Rtr = 2,
Std = 3,
Inactive
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub enum FlexCanFlten {
AcceptAll = 0,
#[default]
RejectAll = 1,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct FlexCanFilter {
pub filter_id: u8,
pub id: u32,
pub ide: FlexCanIde,
pub remote: FlexCanIde
}

Loading

0 comments on commit 081d027

Please sign in to comment.