• Stars
    star
    147
  • Rank 251,347 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Embedded Ethernet driver in Rust

Rust Ethernet Driver for STM32F* microcontrollers

Build Status

Supported microcontrollers

  • STM32F107
  • STM32F4xx
  • STM32F7xx

Pull requests are welcome :)

Usage

Add one of the following to the [dependencies] section in your Cargo.toml (with the correct MCU specified):

stm32-eth = { version = "0.4.1", features = ["stm32f429"] } # For stm32f4xx-like MCUs
stm32-eth = { version = "0.4.1", features = ["stm32f767"] } # For stm32f7xx-like MCUs
stm32-eth = { version = "0.4.1", features = ["stm32f107"] } # For stm32f107

stm32_eth re-exports the underlying HAL as stm32_eth::hal.

In src/main.rs add:

use stm32_eth::{
    hal::gpio::GpioExt,
    hal::rcc::RccExt,
    stm32::Peripherals,
    dma::{RxRingEntry, TxRingEntry},
    EthPins,
};
use fugit::RateExtU32;

fn main() {
    let p = Peripherals::take().unwrap();

    let rcc = p.RCC.constrain();
    // HCLK must be at least 25MHz to use the ethernet peripheral
    let clocks = rcc.cfgr.sysclk(32.MHz()).hclk(32.MHz()).freeze();

    let gpioa = p.GPIOA.split();
    let gpiob = p.GPIOB.split();
    let gpioc = p.GPIOC.split();
    let gpiog = p.GPIOG.split();

    let eth_pins = EthPins {
        ref_clk: gpioa.pa1,
        crs: gpioa.pa7,
        tx_en: gpiog.pg11,
        tx_d0: gpiog.pg13,
        tx_d1: gpiob.pb13,
        rx_d0: gpioc.pc4,
        rx_d1: gpioc.pc5,
    };

    let mut rx_ring: [RxRingEntry; 16] = Default::default();
    let mut tx_ring: [TxRingEntry; 8] = Default::default();

    let parts = stm32_eth::PartsIn {
        mac: p.ETHERNET_MAC,
        mmc: p.ETHERNET_MMC,
        dma: p.ETHERNET_DMA,
        ptp: p.ETHERNET_PTP,
    };

    let stm32_eth::Parts { dma: mut eth_dma, mac: _, ptp: _ } = stm32_eth::new(
        parts,
        &mut rx_ring[..],
        &mut tx_ring[..],
        clocks,
        eth_pins,
    )
    .unwrap();
    eth_dma.enable_interrupt();

    loop {
        if let Ok(pkt) = eth_dma.recv_next(None) {
            // handle received pkt
        }

        let size = 42;
        eth_dma.send(size, None, |buf| {
            // write up to `size` bytes into buf before it is being sent
        }).expect("send");
    }
}

use stm32_eth::stm32::interrupt;
#[interrupt]
fn ETH() {
    stm32_eth::eth_interrupt_handler();
}

smoltcp support

Use feature-flag smoltcp-phy.

To make proper use of smoltcp, you will also have to activate additional smoltcp features. You can do this by adding a dependency on the same version of smoltcp as stm32-eth to your own Cargo.toml with the features you require activated.

Examples

The examples should run and compile on any MCU that has an 802.3 compatible PHY capable of generating the required 50 MHz clock signal connected to the default RMII pins.

The examples use defmt and defmt_rtt for logging, and panic_probe over defmt_rtt for printing panic backtraces.

Alternative pin configuration, HSE & PPS

If the board you're developing for has a High Speed External oscillator connected to the correct pins, the HSE configuration can be activated by setting the STM32_ETH_EXAMPLE_HSE environment variable to one of oscillator or bypass when compiling.

If the board you're developing for uses the nucleo pinout (PG11 and PG13 instead of PB11 and PB12), the pin configuration can be changed by setting the STM32_ETH_EXAMPLE_PINS environment variable to nucleo when compiling.

If you wish to use the alternative PPS output pin (PG8 instead of PB5) for the rtic-timestamp example, the pin configuration can be changed by setting the STM32_ETH_EXAMPLE_PPS_PIN environment variable to alternate when compiling.

Building examples

To build an example, run the following command:

cargo build --release --example <example> \
    --features <MCU feature>,<additional required features> \
    --target <MCU compilation target>

For example, if we wish to build the ip example for an stm32f429, we should run the following command:

cargo build --release --example ip \
        --features stm32f429,smoltcp-phy \
        --target thumbv7em-none-eabihf

If we wish to build the arp example for a Nucleo-F767ZI with a HSE oscillator:

STM32_ETH_EXAMPLE_HSE=bypass STM32_ETH_EXAMPLE_PINS=nucleo \
cargo build --release --example arp \
    --features stm32f767

Running examples

Install probe-run with cargo install probe-run --version '~0.3'

Find the correct value for PROBE_RUN_CHIP for your MCU from the list provided by probe-run --list-chips.

Ensure that probe-run can attach to your MCU

Then, run the following command:

DEFMT_LOG=info PROBE_RUN_CHIP=<probe-run chip> \
cargo run --release --example <example> \
    --features <MCU feature>,<additional required features> \
    --target <MCU compilation target>

For example, if we wish to run the rtic-echo example on an STM32F107RCT6, we should run the following command:

DEFMT_LOG=info PROBE_RUN_CHIP=STM32F107RC \
cargo run --release --example rtic-echo \
    --features stm32f107,smoltcp-phy \
    --target thumbv7m-none-eabi

Or, if we want to run the arp example on a Nucleo-F767ZI with a HSE oscillator:

DEFMT_LOG=info PROBE_RUN_CHIP=STM32F767ZGTx \
STM32_ETH_EXAMPLE_PINS=nucleo STM32_ETH_EXAMPLE_HSE=oscillator \
cargo run --release --example arp \
    --features stm32f767 \
    --target thumbv7em-none-eabihf

More Repositories

1

stm32-rs

Embedded Rust device crates for STM32 microcontrollers
Python
1,281
star
2

stm32f1xx-hal

A Rust embedded-hal HAL impl for the STM32F1 family based on japarics stm32f103xx-hal
Rust
564
star
3

stm32f4xx-hal

A Rust embedded-hal HAL for all MCUs in the STM32 F4 family
Rust
550
star
4

stm32h7xx-hal

Peripheral access API for STM32H7 series microcontrollers
Rust
215
star
5

stm32f3xx-hal

A Rust embedded-hal HAL for all MCUs in the STM32 F3 family
Rust
164
star
6

stm32l4xx-hal

A Hardware abstraction layer for the stm32l432xx series chips written in rust.
Rust
156
star
7

stm32f0xx-hal

A Rust `embedded-hal` implementation for all MCUs in the STM32 F0 family
Rust
125
star
8

stm32f7xx-hal

A Rust embedded-hal HAL for all MCUs in the STM32 F7 family
Rust
115
star
9

stm32-usbd

usb-device implementation for STM32 microcontrollers
Rust
99
star
10

stm32l0xx-hal

A hardware abstraction layer (HAL) for the STM32L0 series microcontrollers written in Rust
Rust
96
star
11

stm32g0xx-hal

Peripheral access API for STM32G0 series microcontrollers
Rust
72
star
12

stm32g4xx-hal

Peripheral access API for STM32G4 series microcontrollers
Rust
58
star
13

stm32-usbd-examples

stm32-usbd examples for different microcontrollers
Rust
48
star
14

stm32wlxx-hal

A Hardware abstraction layer for the stm32wl series chips written in rust.
Rust
45
star
15

stm32f407g-disc

Rust BSP crate for the STM32F4DISCOVERY (STM32F407G-DISC) development board
Rust
43
star
16

synopsys-usb-otg

usb-device implementation for Synopsys USB OTG IP cores
Rust
43
star
17

bxcan

bxCAN peripheral driver for STM32 chips
Rust
31
star
18

stm32-fmc

Hardware Abstraction Layer for STM32 Memory Controllers (FMC/FSMC)
Rust
17
star
19

stm32f429i-disc

Rust BSP crate for the STM32F429I-DISC development board
Rust
15
star
20

stm32-rs-nightlies

Up-to-date builds of current stm32-rs master branch, for use as Cargo git dependencies
Rust
14
star
21

fdcan

FDCAN peripheral driver for STM32 chips
Rust
12
star
22

stm32-rs-mmaps

Textual memory maps of stm32-rs devices, used to help review PRs to stm32-rs
9
star
23

stm32l1xx-hal

[WIP] Peripheral access API for STM32L1 series microcontrollers
Rust
9
star
24

stm32h5xx-hal

Rust
8
star
25

nucleo-f042k6

Rust BSP crate for the STM Nucleo-F042K6 development board
Shell
8
star
26

stm32-device-signature

Device electronic signature 'driver' for STM32 microcontrollers
Rust
8
star
27

stm32f072b-disco

A BSP/example crate for the STM32F072 Discovery kit
Rust
7
star
28

cube-parse

Rust
7
star
29

cube-MX-db

MCU database files from CubeMX
5
star
30

meta

Meta discussions and files applicable to the entire stm32-rs project
3
star
31

stm32c0xx-hal

Peripheral access API for STM32C0 series microcontrollers
Rust
2
star