← Back to Pull requests

feat: add async USB, D21x boot chain and board presets #25

Merged
NanaHigh merged 181 files (24673 lines) on Nov 01, 2026 09:06 UTC
View on GitHub
NanaHigh NanaHigh commented on Nov 01, 2026 06:54 UTC
Author

Summary

This PR turns the existing D13x SPI-NOR boot path into a multi-media boot
chain, adds async USB upgrade support, introduces a typed DDR driver and
makes firmware packaging board-driven.

It is not only a HAL refactor: the main additions are D21x SPI-NAND boot,
async runtime support, QSPI DMA/quad transfers, retry-safe upgrade storage,
board presets and automatic raw application image generation (not just PBP).

What's new

Multi-media boot chain

  • D13x SPI-NOR and D21x SPI-NAND now share one boot chain.

  • The bootloader is no longer tied to a specific SoC.

  • Media is selected by features; chip code only supplies QSPI pins, pads and

  • DMA channel selection.
  • PBP, application and bootloader now have distinct entry points and stacks.
  • Async USB upgrade

  • Async USB device stack based on Embassy.

  • Upgrade protocol, shell and storage are separated from the USB controller.

  • Bootloader can run the async upgrade path.

  • Storage flush failures are reported to the host instead of being silently

  • ignored.

    DDR support

  • New typed DDR controller/PHY driver.

  • DRAM profiles and training sequences.

  • D21x PBP DDR bring-up and a standalone DDR example use the same driver.
  • QSPI and flash storage

  • Blocking and DMA-backed QSPI transfers.

  • Quad read/write support.

  • SPI NOR and SPI NAND drivers.

  • Upgrade storage now keeps buffered data after erase/program failures so the

  • host can retry.

    Board presets and image packaging

  • Firmware is built from board presets instead of per-application TOML files.

  • D13x and D21x have default and quad+dma presets.

  • The packer emits both the normal AIC.FW image and a raw NOR/NAND image for

  • direct flash programming.
  • Component features and PBP features can be selected independently.
  • Runtime and interrupts

  • Embassy executor and machine-timer driver.

  • Independent application and bootloader stacks.

  • Upstream PLIC wrapper integration with source range limited to implemented

  • D21x interrupts.
  • DMA request-ID tables for D13x and D21x.
  • Before / After

    | Area | Before | After |
    |---|---|---|
    | Boot media | D13x SPI-NOR mainly | D13x NOR + D21x NAND |
    | Bootloader | Chip-coupled media setup | Media-selected storage backends |
    | USB | Existing blocking upgrade path | Async USB upgrade stack |
    | Runtime | PBP/app entry only | PBP + app + bootloader entries |
    | DDR | No HAL DDR module | Typed DDR bring-up and profiles |
    | QSPI | Basic transfer support | DMA + quad transfers |
    | Packaging | Per-app TOML/manifest | Board presets + raw images |
    | Storage errors | Flush failures ignored | Failures propagated and retryable |

    Usage

    Build a complete image from a board preset:

    cargo run -p aicfwc -- --board d21x-nand-quad-dma --app app-blinky --build
    

    Build the boot chain only:

    cargo run -p aicfwc -- --board d13x-nor-quad-dma --bootloader --build
    

    The normal image is for AiBurn/USB upgrade. The generated raw image is for
    direct flash programming.

    Hardware validation

  • D13x NOR quad+dma: USB upgrade, reset and boot OK

  • D21x NAND quad+dma: USB upgrade, reset and boot OK
  • PBP Dram demo


    <img width="467" height="88" alt="07b1e22c82acbbb978e693530f611b94" src="https://github.com/user-attachments/assets/0357fb01-9725-4c5a-9090-13a5611c5d8f" />

    D13x NOR quad+dma


    <img width="642" height="294" alt="41c591b83a5ffc095f3ee4591fa920c8" src="https://github.com/user-attachments/assets/ced4185f-0442-46ec-82de-e8d5e0ed47c2" />

    D21x NAND quad+dma


    <img width="634" height="337" alt="b8688cd670e418be33016cd862daabd2" src="https://github.com/user-attachments/assets/604822f6-1fe4-4323-a043-d4bed1e17186" />

    [Rx][timestamp] is added by serial port debugging software.

    Review approach

    The PR is intentionally split by subsystem and capability:

  • HAL foundation and interrupts

  • DDR

  • QSPI and flash storage

  • Async USB and upgrade

  • Runtime and entry points

  • Board presets and packaging

  • Bootloader media decoupling

  • CI coverage
  • Reviewing commit by commit is recommended; the final tree is the validated
    configuration.

    Diff: 24673 lines (+18551 / -6122) across 181 files Reviewer: luojia65 Review duration: 131m
    All Vouch review confidence checks passed
    Residual risk 0.43 = Change Risk (0.72) × (1 − Review Confidence (0.40)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.72 · 24673 lines diff across 181 files
    0.72
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.37 · 3 substantive comment(s)
    0.37
    Model 3 · Review Timing & Attention
    Session adequacy: 21% · Reviewer familiarity: 50%
    39%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.400
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    7 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Nov 01, 2026 06:55 UTC
    Reviewer

    Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

    An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

    luojia65 luojia65 commented on code on Nov 01, 2026 07:56 UTC
    Code Review
    artinchip-hal/src/clint/instance.rs:1
    @@ -22,4 +24,50 @@ impl Clint {
         pub const fn register_block(&self) -> &'static RegisterBlock {
             unsafe { &*self.reg }
         }
    +
    +    /// Machine timer counter, low word.
    +    #[cfg(not(feature = "d21x"))]
    +    const MTIME_LO: usize = 0xBFF8;
    +    /// Machine timer counter, high word.
    +    #[cfg(not(feature = "d21x"))]
    +    const MTIME_HI: usize = 0xBFFC;

    This can be considered as a separate peripheral, like struct ClintV2 or other names according to its core vendor (like struct THeadClint). Consider using separate structs instead of config features, in case that we don't have to switch between features when run cargo tests or cargo checks.

    luojia65 luojia65 commented on code on Nov 01, 2026 07:58 UTC
    Code Review
    artinchip-hal/src/interrupt.rs:1
    @@ -1,3 +1,206 @@
    -//! Public interrupt handling API for ArtInChip SoC.
    +//! Public interrupt handling API for ArtInChip SoCs.
    +//!
    +//! Controller selection is derived automatically from the active SoC feature:
    +//! - **CLIC** (`d12x`, `d13x`, `g73x`, `m6800`): hardware-vectored interrupts via `mtvt`.
    +//! - **PLIC** (`d21x`): machine external interrupt demultiplexed in trap dispatch.
    +//!
    +//! Async drivers program interrupts exclusively through [`typelevel`] and
    +//! [`InterruptSource`], remaining independent of the underlying controller.
     
    +#[cfg(not(feature = "d21x"))]
     pub mod clic;
    +
    +#[cfg(feature = "d21x")]
    +pub mod plic;
    +
    +/// Active chip interrupt controller implementation.
    +#[cfg(not(feature = "d21x"))]
    +pub use clic::ClicController as ActiveController;
    +#[cfg(feature = "d21x")]
    +pub use plic::PlicController as ActiveController;
    +
    +/// Type-level interrupt ZSTs and traits for the selected SoC.
    +#[cfg(not(feature = "d21x"))]
    +pub use clic::typelevel;
    +#[cfg(feature = "d21x")]
    +pub use plic::typelevel;
    +
    +/// Operations provided by the chip's interrupt controller.
    +pub trait InterruptController {

    Is this trait necessary? What will happen if we don't introduce extra interrupt controller traits?

    luojia65 luojia65 approved these changes on Nov 01, 2026 09:06 UTC
    Approved Reviewer

    Approved these changes.

    luojia65 luojia65 approved these changes on Nov 01, 2026 09:12 UTC
    Approved Reviewer

    LGTM with minor modifications :)

    NanaHigh NanaHigh commented on Nov 01, 2026 09:52 UTC
    Author

    LGTM with minor modifications :)

    Splited Clint into ClintV1 and ClintV1 and removed redundant InterruptController trait in latest branch.

    Reviewers
    Assignees
    NanaHigh NanaHigh
    Labels
    risk: medium confidence: 39.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 71.5% (HIGH) · Higher = larger blast radius
    71.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 39.8% · Lower = rushed or shallow review
    39.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 43.1% · Moderate risk
    43.1%