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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Debug probe interface.

pub mod bmp;
pub mod openocd;

use crate::{
    cli::{ProbeCmd, ProbeSubCmd},
    templates::Registry,
    utils::{block_with_signals, register_signals, run_command},
};
use anyhow::{anyhow, bail, Result};
use drone_config as config;
use serde::{Deserialize, Serialize};
use signal_hook::iterator::Signals;
use std::process::Command;
use termcolor::StandardStream;

/// An `enum` of all supported debug probes.
#[allow(missing_docs)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Probe {
    Bmp,
    Openocd,
}

/// ITM handling mode.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProbeItm {
    /// Use default mode for the debug probe.
    Auto,
    /// SWO pin is connected to the debug probe.
    Internal,
    /// SWO pin is connected to an external USB-UART converter.
    External,
}

impl Probe {
    /// Returns default mode for the given debug probe.
    pub fn itm_default(&self) -> &ProbeItm {
        match self {
            Self::Bmp => &ProbeItm::External,
            Self::Openocd => &ProbeItm::Internal,
        }
    }

    /// Returns default UART endpoint value for the given debug probe.
    pub fn itm_external_endpoint(&self) -> &str {
        match self {
            Self::Bmp => "/dev/ttyBmpTarg",
            Self::Openocd => "/dev/ttyUSB0",
        }
    }
}

impl ProbeCmd {
    /// Runs the `drone probe` command.
    #[allow(clippy::too_many_lines)]
    pub fn run(&self, shell: &mut StandardStream) -> Result<()> {
        let Self { probe_sub_cmd } = self;
        let signals = register_signals()?;
        let registry = Registry::new()?;
        let config = &config::Config::read_from_current_dir()?;
        let config_probe = config
            .probe
            .as_ref()
            .ok_or_else(|| anyhow!("Missing `probe` section in `{}`", config::CONFIG_NAME))?;
        match probe_sub_cmd {
            ProbeSubCmd::Reset(cmd) => {
                if config_probe.bmp.is_some() {
                    return bmp::ResetCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe,
                    }
                    .run();
                } else if let Some(config_probe_openocd) = &config_probe.openocd {
                    return openocd::ResetCmd {
                        cmd,
                        signals,
                        registry,
                        config_probe_openocd,
                    }
                    .run();
                }
            }
            ProbeSubCmd::Flash(cmd) => {
                if config_probe.bmp.is_some() {
                    return bmp::FlashCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe,
                    }
                    .run();
                } else if let Some(config_probe_openocd) = &config_probe.openocd {
                    return openocd::FlashCmd {
                        cmd,
                        signals,
                        registry,
                        config_probe_openocd,
                    }
                    .run();
                }
            }
            ProbeSubCmd::Gdb(cmd) => {
                if config_probe.bmp.is_some() {
                    return bmp::GdbCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe,
                    }
                    .run();
                } else if let Some(config_probe_openocd) = &config_probe.openocd {
                    return openocd::GdbCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe,
                        config_probe_openocd,
                    }
                    .run();
                }
            }
            ProbeSubCmd::Itm(cmd) => {
                let config_probe_itm = config_probe.itm.as_ref().ok_or_else(|| {
                    anyhow!("Missing `probe.itm` section in `{}`", config::CONFIG_NAME)
                })?;
                if config_probe.bmp.is_some() {
                    return bmp::ItmCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe,
                        config_probe_itm,
                        shell,
                    }
                    .run();
                } else if let Some(config_probe_openocd) = &config_probe.openocd {
                    return openocd::ItmCmd {
                        cmd,
                        signals,
                        registry,
                        config,
                        config_probe_itm,
                        config_probe_openocd,
                    }
                    .run();
                }
            }
        }
        bail!(
            "Suitable debug probe configuration is not found in `{}`",
            config::CONFIG_NAME
        );
    }
}

/// Configures the endpoint with `stty` command.
pub fn setup_uart_endpoint(signals: &Signals, endpoint: &str, baud_rate: u32) -> Result<()> {
    let mut stty = Command::new("stty");
    stty.arg(format!("--file={}", endpoint));
    stty.arg("speed");
    stty.arg(format!("{}", baud_rate));
    stty.arg("raw");
    block_with_signals(signals, true, || run_command(stty))
}