1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use std::time::Duration;

use crate::io::uart::Uart;
use crate::test_utils::e2e_command::TestCommand;
use crate::test_utils::rpc::{ConsoleRecv, ConsoleSend};
use crate::test_utils::status::Status;

// Bring in the auto-generated sources.
include!(env!("spi_passthru"));

impl ConfigJedecId {
    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiConfigureJedecId.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl StatusRegister {
    pub fn read(uart: &dyn Uart) -> Result<Self> {
        TestCommand::SpiReadStatus.send_with_crc(uart)?;
        Self::recv(uart, Duration::from_secs(300), false)
    }

    pub fn write(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiWriteStatus.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl SfdpData {
    pub fn write(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiWriteSfdp.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl UploadInfo {
    pub fn execute<F>(uart: &dyn Uart, f: F) -> Result<Self>
    where
        F: FnOnce() -> Result<()>,
    {
        TestCommand::SpiWaitForUpload.send_with_crc(uart)?;
        f()?;
        Self::recv(uart, Duration::from_secs(300), false)
    }
}

impl SpiFlashReadId {
    pub fn execute(uart: &dyn Uart) -> Result<Self> {
        TestCommand::SpiFlashReadId.send_with_crc(uart)?;
        let data = SpiFlashReadId::recv(uart, Duration::from_secs(300), false)?;
        Ok(data)
    }
}

impl SpiFlashReadSfdp {
    pub fn execute(&self, uart: &dyn Uart) -> Result<SfdpData> {
        TestCommand::SpiFlashReadSfdp.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        let sfdp = SfdpData::recv(uart, Duration::from_secs(300), false)?;
        Ok(sfdp)
    }
}

impl SpiFlashEraseSector {
    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiFlashEraseSector.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl SpiFlashWrite {
    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiFlashWrite.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl SpiPassthruSwapMap {
    pub fn apply_address_swap(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiPassthruSetAddressMap.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl SpiMailboxMap {
    pub fn apply(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiMailboxMap.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }

    pub fn disable(uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiMailboxUnmap.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}

impl SpiMailboxWrite {
    pub fn execute(&self, uart: &dyn Uart) -> Result<()> {
        TestCommand::SpiMailboxWrite.send_with_crc(uart)?;
        self.send_with_crc(uart)?;
        Status::recv(uart, Duration::from_secs(300), false)?;
        Ok(())
    }
}