opentitanlib/test_utils/
spi_passthru.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 std::time::Duration;
7
8use crate::io::uart::Uart;
9use crate::test_utils::e2e_command::TestCommand;
10use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
11use crate::test_utils::status::Status;
12
13// Bring in the auto-generated sources.
14include!(env!("spi_passthru"));
15
16impl ConfigJedecId {
17    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
18        TestCommand::SpiConfigureJedecId.send_with_crc(uart)?;
19        self.send_with_crc(uart)?;
20        Status::recv(uart, Duration::from_secs(300), false)?;
21        Ok(())
22    }
23}
24
25impl StatusRegister {
26    pub fn read(uart: &dyn Uart) -> Result<Self> {
27        TestCommand::SpiReadStatus.send_with_crc(uart)?;
28        Self::recv(uart, Duration::from_secs(300), false)
29    }
30
31    pub fn write(&self, uart: &dyn Uart) -> Result<()> {
32        TestCommand::SpiWriteStatus.send_with_crc(uart)?;
33        self.send_with_crc(uart)?;
34        Status::recv(uart, Duration::from_secs(300), false)?;
35        Ok(())
36    }
37}
38
39impl SfdpData {
40    pub fn write(&self, uart: &dyn Uart) -> Result<()> {
41        TestCommand::SpiWriteSfdp.send_with_crc(uart)?;
42        self.send_with_crc(uart)?;
43        Status::recv(uart, Duration::from_secs(300), false)?;
44        Ok(())
45    }
46}
47
48impl UploadInfo {
49    pub fn execute<F>(uart: &dyn Uart, f: F) -> Result<Self>
50    where
51        F: FnOnce() -> Result<()>,
52    {
53        TestCommand::SpiWaitForUpload.send_with_crc(uart)?;
54        f()?;
55        Self::recv(uart, Duration::from_secs(300), false)
56    }
57}
58
59impl SpiFlashReadId {
60    pub fn execute(uart: &dyn Uart) -> Result<Self> {
61        TestCommand::SpiFlashReadId.send_with_crc(uart)?;
62        let data = SpiFlashReadId::recv(uart, Duration::from_secs(300), false)?;
63        Ok(data)
64    }
65}
66
67impl SpiFlashReadSfdp {
68    pub fn execute(&self, uart: &dyn Uart) -> Result<SfdpData> {
69        TestCommand::SpiFlashReadSfdp.send_with_crc(uart)?;
70        self.send_with_crc(uart)?;
71        let sfdp = SfdpData::recv(uart, Duration::from_secs(300), false)?;
72        Ok(sfdp)
73    }
74}
75
76impl SpiFlashEraseSector {
77    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
78        TestCommand::SpiFlashEraseSector.send_with_crc(uart)?;
79        self.send_with_crc(uart)?;
80        Status::recv(uart, Duration::from_secs(300), false)?;
81        Ok(())
82    }
83}
84
85impl SpiFlashWrite {
86    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
87        TestCommand::SpiFlashWrite.send_with_crc(uart)?;
88        self.send_with_crc(uart)?;
89        Status::recv(uart, Duration::from_secs(300), false)?;
90        Ok(())
91    }
92}
93
94impl SpiPassthruSwapMap {
95    pub fn apply_address_swap(&self, uart: &dyn Uart) -> Result<()> {
96        TestCommand::SpiPassthruSetAddressMap.send_with_crc(uart)?;
97        self.send_with_crc(uart)?;
98        Status::recv(uart, Duration::from_secs(300), false)?;
99        Ok(())
100    }
101}
102
103impl SpiMailboxMap {
104    pub fn apply(&self, uart: &dyn Uart) -> Result<()> {
105        TestCommand::SpiMailboxMap.send_with_crc(uart)?;
106        self.send_with_crc(uart)?;
107        Status::recv(uart, Duration::from_secs(300), false)?;
108        Ok(())
109    }
110
111    pub fn disable(uart: &dyn Uart) -> Result<()> {
112        TestCommand::SpiMailboxUnmap.send_with_crc(uart)?;
113        Status::recv(uart, Duration::from_secs(300), false)?;
114        Ok(())
115    }
116}
117
118impl SpiMailboxWrite {
119    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
120        TestCommand::SpiMailboxWrite.send_with_crc(uart)?;
121        self.send_with_crc(uart)?;
122        Status::recv(uart, Duration::from_secs(300), false)?;
123        Ok(())
124    }
125}