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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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!("gpio"));

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

    pub fn write(uart: &dyn Uart, pin: u32, state: bool) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::Write,
            pin_mask: pin,
            state: state.into(),
        };
        payload.execute(uart)
    }
    pub fn set_enabled(uart: &dyn Uart, pin: u32, state: bool) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::SetEnabled,
            pin_mask: pin,
            state: state.into(),
        };
        payload.execute(uart)
    }

    pub fn write_all(uart: &dyn Uart, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::WriteAll,
            pin_mask: 0,
            state,
        };
        payload.execute(uart)
    }
    pub fn set_enabled_all(uart: &dyn Uart, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::SetEnabledAll,
            pin_mask: 0,
            state,
        };
        payload.execute(uart)
    }

    pub fn write_masked(uart: &dyn Uart, mask: u32, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::WriteMasked,
            pin_mask: mask,
            state,
        };
        payload.execute(uart)
    }
    pub fn set_enabled_masked(uart: &dyn Uart, mask: u32, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::SetEnabledMasked,
            pin_mask: mask,
            state,
        };
        payload.execute(uart)
    }
    pub fn set_input_noise_filter(uart: &dyn Uart, mask: u32, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::SetInputNoiseFilter,
            pin_mask: mask,
            state,
        };
        payload.execute(uart)
    }
    pub fn irq_restore_all(uart: &dyn Uart, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::IrqRestoreAll,
            pin_mask: 0,
            state,
        };
        payload.execute(uart)
    }
    pub fn irq_disable_all(uart: &dyn Uart, state: u32) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::IrqDisableAll,
            pin_mask: 0,
            state,
        };
        payload.execute(uart)
    }
    pub fn irq_acknowledge_all(uart: &dyn Uart) -> Result<()> {
        let payload = GpioSet {
            action: GpioAction::IrqAcknowledgeAll,
            pin_mask: 0,
            state: 0,
        };
        payload.execute(uart)
    }
    pub fn irq_set_trigger(uart: &dyn Uart, state: u32, trigger: String) -> Result<()> {
        match trigger.as_str() {
            "rising" => {
                let payload = GpioSet {
                    action: GpioAction::IrqSetTriggerRisingEdge,
                    pin_mask: 0,
                    state,
                };
                payload.execute(uart)
            }
            "falling" => {
                let payload = GpioSet {
                    action: GpioAction::IrqSetTriggerFallingEdge,
                    pin_mask: 0,
                    state,
                };
                payload.execute(uart)
            }
            "high" => {
                let payload = GpioSet {
                    action: GpioAction::IrqSetTriggerHigh,
                    pin_mask: 0,
                    state,
                };
                payload.execute(uart)
            }
            "low" => {
                let payload = GpioSet {
                    action: GpioAction::IrqSetTriggerLow,
                    pin_mask: 0,
                    state,
                };
                payload.execute(uart)
            }
            _ => {
                log::error!("unsupport trigger: {:?}", trigger);
                Ok(())
            }
        }
    }
}

impl GpioGet {
    pub fn read_all(uart: &dyn Uart) -> Result<u32> {
        TestCommand::GpioGet.send(uart)?;
        let data = GpioGet::recv(uart, Duration::from_secs(300), false)?;
        Ok(data.state)
    }
}