opentitanlib/test_utils/
load_bitstream.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use anyhow::Result;
6use clap::Args;
7use std::path::{Path, PathBuf};
8use std::time::Duration;
9
10use crate::app::{StagedProgressBar, TransportWrapper};
11use crate::transport::common::fpga::FpgaProgram;
12
13/// Load a bitstream into the FPGA.
14#[derive(Debug, Args)]
15pub struct LoadBitstream {
16    /// Whether to clear out any existing bitstream.
17    #[arg(long)]
18    pub clear_bitstream: bool,
19
20    /// The bitstream to load for the test.
21    #[arg(long)]
22    pub bitstream: Option<PathBuf>,
23
24    /// Duration of the reset pulse.
25    #[arg(long, value_parser = humantime::parse_duration, default_value = "50ms")]
26    pub rom_reset_pulse: Duration,
27
28    /// Duration of ROM detection timeout.
29    #[arg(long, value_parser = humantime::parse_duration, default_value = "2s")]
30    pub rom_timeout: Duration,
31}
32
33impl LoadBitstream {
34    pub fn init(&self, transport: &TransportWrapper) -> Result<()> {
35        // Clear out existing bitstream, if requested.
36        if self.clear_bitstream {
37            log::info!("Clearing bitstream.");
38            transport.fpga_ops()?.clear_bitstream()?;
39        }
40        // Load the specified bitstream, if provided.
41        if let Some(bitstream) = &self.bitstream {
42            self.load(transport, bitstream)?;
43        }
44
45        Ok(())
46    }
47
48    pub fn load(&self, transport: &TransportWrapper, file: &Path) -> Result<()> {
49        log::info!("Loading bitstream: {:?}", file);
50        let payload = std::fs::read(file)?;
51        let progress = StagedProgressBar::new();
52        let operation = FpgaProgram {
53            bitstream: payload,
54            rom_reset_pulse: self.rom_reset_pulse,
55            rom_timeout: self.rom_timeout,
56            progress: Box::new(progress),
57        };
58
59        if operation.should_skip(transport)? {
60            return Ok(());
61        }
62
63        transport
64            .fpga_ops()?
65            .load_bitstream(&operation.bitstream, &*operation.progress)
66    }
67}