Skip to content

camchambers/cargo-r4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cargo-r4

A ravedude-like tool for flashing Rust firmware to the Arduino Uno R4 (WiFi & Minima), with an easy-to-use BSP library.

Overview

The Arduino Uno R4 uses an ARM Cortex-M4 (Renesas RA4M1) instead of the AVR architecture, which means the standard ravedude + avrdude toolchain doesn't work. This project provides:

  • cargo-r4: Automated flashing tool (like ravedude for AVR)
  • r4-bsp: Board Support Package library for easy hardware access
  • xtask: Build automation for streamlined development

Note: This tool is currently optimized for Linux. Windows and macOS support is planned but not yet fully verified.

Quick Start

# Clone and build
git clone <repo-url>
cd cargo-r4
cargo build --release

# Flash an example (builds + flashes automatically)
cargo xtask flash blinky

# Or use the convenience script
./flash.sh heartbeat

Features

  • One-command flashing - cargo xtask flash <example> handles everything
  • Automatic cargo run - Just cargo run --release to flash
  • Auto-detect Arduino R4 boards (WiFi & Minima variants)
  • Automatic bootloader trigger using bossac
  • ELF to BIN conversion using objcopy
  • Serial monitor for viewing output
  • r4-bsp library - Professional BSP with embedded-hal support
  • Optimized GPIO - High-performance pin operations
  • Modular architecture - Clean, maintainable codebase
  • Flexible tool detection - Supports environment variable overrides

Installation

Prerequisites

You need two external tools for flashing:

  1. bossac - BOSSA flash programmer for SAM-BA bootloaders
  2. objcopy - Binary utilities (usually arm-none-eabi-objcopy)

Installing Tools

On Linux (Debian/Ubuntu):

sudo apt install bossac gcc-arm-none-eabi

On macOS:

brew install bossa-cli arm-none-eabi-gcc

On Arch Linux:

sudo pacman -S arm-none-eabi-binutils
# Install bossac from AUR or use arduino-cli

Via Arduino CLI (all platforms):

# Install Arduino CLI
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh

# Install Arduino tools (includes bossac)
arduino-cli core install arduino:renesas_uno

The tools will be at:

  • Linux/macOS: ~/.arduino15/packages/arduino/tools/bossac/*/

You can also place bossac and objcopy binaries in a bin/ directory next to the cargo-r4 executable, and they will be found automatically.

A helper script is provided to assist with tool setup:

./setup-tools.sh

Environment Variable Overrides

You can override tool paths using environment variables:

export R4_BOSSAC_PATH=/path/to/custom/bossac
export R4_OBJCOPY_PATH=/path/to/custom/objcopy

The tool search order is:

  1. Environment variables (R4_BOSSAC_PATH, R4_OBJCOPY_PATH)
  2. System PATH
  3. Local bin/ directory
  4. Workspace bin/ directory

Building cargo-r4

cargo build --release

The binary will be at target/release/cargo-r4.

Usage

Easiest: xtask automation

# Build and flash an example in one command
cargo xtask flash blinky

# Or use the convenience script
./flash.sh heartbeat

With cargo run (automatic)

From the examples directory:

cd examples
cargo run --release --example blinky

The .cargo/config.toml runner automatically flashes to your Arduino R4.

Manual flashing

# Build your firmware
cargo build --release

# Flash it
./target/release/cargo-r4 path/to/your/firmware.elf

Options

  • --baud <RATE>: Set serial monitor baud rate (default: 115200)

Creating Firmware for Arduino R4

Easy Way: Use r4-bsp Library

The r4-bsp library provides a professional, embedded-hal compatible interface:

#![no_std]
#![no_main]

use cortex_m_rt::entry;
use panic_halt as _;
use r4_bsp::{Board, Led, delay_ms};

#[entry]
fn main() -> ! {
    // Initialize hardware (must be called first!)
    Board::init();
    
    let mut led = Led::new();
    
    loop {
        led.on();
        delay_ms(500);
        led.off();
        delay_ms(500);
    }
}

embedded-hal Compatibility

The r4-bsp library implements the standard embedded-hal traits, allowing you to use any compatible driver:

use embedded_hal::digital::OutputPin;
use r4_bsp::{Board, Pin};

// Pin and Led implement OutputPin trait
let mut pin = Pin::new(1, 2);
pin.set_high().ok();
pin.set_low().ok();

See the examples/ directory for complete working examples, including setup files (Cargo.toml, memory.x, build.rs).

Development Workflows

Quickest Methods

One-Command Flash (Recommended):

cargo xtask flash blinky
cargo xtask flash heartbeat

Convenience Script:

./flash.sh blinky

Cargo Run (from examples directory):

cd examples
cargo run --release --example blinky

What Happens During Flash

When you run cargo xtask flash blinky:

  1. Build - Compiles blinky.rs for thumbv7em-none-eabihf
  2. Detect - Finds your Arduino R4 (WiFi or Minima)
  3. Bootloader - Triggers bootloader mode via bossac
  4. Convert - Transforms ELF to BIN format
  5. Flash - Writes firmware via BOSSA protocol
  6. Monitor - Opens serial connection at 115200 baud

All automatic! Press Ctrl+C to exit the serial monitor.

Recommended Development Workflow

Quick testing:

cargo xtask flash <example_name>

Development iteration:

cd examples
# Edit your example file...
cargo run --release --example <name>

Custom projects: Add this to your .cargo/config.toml:

[target.thumbv7em-none-eabihf]
runner = "/path/to/cargo-r4"

Then just cargo run --release in your project.

Hardware Details

Arduino R4 WiFi

  • VID: 0x2341
  • PID: 0x1002 (ESP32-S3 USB bridge)
  • MCU: Renesas RA4M1 (ARM Cortex-M4F @ 48MHz)
  • Flash: 256KB (240KB usable, 16KB bootloader)
  • RAM: 32KB
  • Built-in LED: P102 (Port 1, Pin 2)

Arduino R4 Minima

  • VID: 0x2341
  • PID: 0x0069
  • MCU: Renesas RA4M1 (ARM Cortex-M4F @ 48MHz)
  • Flash: 256KB (240KB usable)
  • RAM: 32KB

Troubleshooting

Board not detected

  • Ensure the board is connected via USB
  • Check that you have permissions for serial ports (add user to dialout group on Linux)
  • Try unplugging and replugging the board

Flash fails

  • Make sure no other program (Arduino IDE, serial monitor) is using the port
  • Try pressing the reset button twice quickly to manually enter bootloader mode
  • Check that the binary size doesn't exceed available flash (240KB)

LED doesn't blink

  • Verify the memory.x file has the correct flash origin (0x00004000)
  • Check that the firmware was compiled for thumbv7em-none-eabihf target
  • Ensure the binary includes all necessary sections (.text, .data)

License

MIT License - see LICENSE file for details.

Acknowledgments

  • Arduino team for the R4 board and bootloader
  • Renesas for the RA4M1 MCU and FSP
  • BOSSA project for the flash programming tool
  • es_r4 reference implementation https://github.com/eyyyyyyy3/es_r4

"Arduino® is a trademark of Arduino SA. This project is not affiliated with or endorsed by Arduino SA."

About

A cargo-compatible flashing tool and Board Support Package (BSP) for Arduino Uno R4 (WiFi & Minima). Like ravedude, but for ARM Cortex-M4.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors