#pic32 #mips #run-time #startup

macro no-std mips-rt-macros

Attributes re-exported in mips-rt

3 unstable releases

0.3.4 Jul 31, 2023
0.3.0 Jan 16, 2022
0.2.1 Sep 25, 2021

#8 in #pic32

Download history 1212/week @ 2024-07-23 878/week @ 2024-07-30 750/week @ 2024-08-06 541/week @ 2024-08-13 544/week @ 2024-08-20 705/week @ 2024-08-27 695/week @ 2024-09-03 837/week @ 2024-09-10 961/week @ 2024-09-17 811/week @ 2024-09-24 859/week @ 2024-10-01 1096/week @ 2024-10-08 964/week @ 2024-10-15 1237/week @ 2024-10-22 672/week @ 2024-10-29 433/week @ 2024-11-05

3,455 downloads per month
Used in 8 crates (via mips-rt)

MIT/Apache

15KB
374 lines

pic32-rs

Rust crates for PIC32 programming including PIC32 HAL modules

This repository contains code to program PIC32MX microcontrollers with Rust. It uses the mipsel-unknown-none target, which is intended for use with MIPS MCU ("bare metal") targets and is used to generate code for the classical MIPS32r2 ISA having 32 bit wide instructions.

The repository contains the following

  • mips-rt: Basic Rust runtime and startup files for MIPS based microcontrollers
  • pic32-hal: HAL crate for PIC32 microcontrollers. There are currently HAL modules for the MIPS core timer, GPIO, interrupt controller, SPI, UART, I2C and USB.
  • example applications

Moreover, there are peripheral access crates (PACs) under the repository pic32-pac. There is also a repository alloc-pic32 to support dynamic memory allocation.

Compiling

To set up the toolchain, the following commands may be used.

rustup default nightly
rustup component add rust-src
cargo install cargo-binutils
rustup component add llvm-tools-preview

cargo-binutils includes cargo-objcopy that can be used to generate Intel HEX files. When other tools are used to generate HEX files (see below) or if your Flash memory programmer can deal with ELF files then cargo-binutils is not needed.

This code can be compiled with the nightly toolchain using cargo.

See also the blinky example on how to compile a PIC32 application.

A .cargo/config file is needed to specify the linker script (e.g. 32MX150F128B_procdefs.ld), to specify the target and to build standard library crates. Below see an example .cargo/config file.

[target.mipsel-unknown-none]
rustflags = ["-C", "link-arg=-T32MX150F128B_procdefs.ld"]

[build]
target = "mipsel-unknown-none"

[unstable]
build-std = ["core", "compiler_builtins", "alloc"]

Using the above files, the build can be done with cargo. To save code space, a release build may make sense.

cargo build --release

Creating hex files

The MMU-less PIC32 variants have a simple Fixed Mapping Translation (FMT) mechanism integrated in their cores that converts the virtual addresses used by the processor and known to the compiler to physical addresses. However, the Flash memory controller of the PIC32 MCUs operates on physical addresses.

Hex files with virtual addresses

If your programmer (such as pic32prog) accepts virtual addresses, cargo-objcopy can be used.

cargo objcopy --release -- -O ihex somefilename.hex

Hex files with physical addresses

If your programmer (such as the MPLAB IPE tools) accepts physical addresses only you need to create hex files that include physical addressed. One way to create such hex file is to use the tool xc32-bin2hex, which is part of the XC32 compiler toolchain. This tools converts the virtual addresses used in an ELF file to physical addresses before writing the hex file.

xc32-bin2hex target/mipsel-unknown-none/<your_elf_file>

An alternative to xc32-bin2hex is pic32-bin2hex, which is part of the chipKIT compiler.

Details on Linking

To link the final application image, three linker script files are used

  • a file memory.x containing the memory map of the used device (needs to be adapted to the Flash memory size and the SRAM size).
  • a file device.x contained in the peripheral access crate that provides symbolic names for the interrupt vectors
  • a main linker script link.x, which is part of the mips-rt crate. This main linker scripts sources the contents of memory.x and device.x.

lib.rs:

Internal implementation details of mips-rt.

Do not use this crate directly. Almost identical to cortex_m_rt_macros

Dependencies

~205–640KB
~15K SLoC