probe_rs_target/
chip_detection.rs

1//! Chip detection information.
2
3use indexmap::IndexMap;
4use serde::{Deserialize, Serialize};
5use serde_with::rust::maps_duplicate_key_is_error;
6
7use crate::serialize::{hex_keys_indexmap, hex_u_int};
8
9/// Vendor-specific chip detection information.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(deny_unknown_fields)]
12pub enum ChipDetectionMethod {
13    /// Microchip ATSAM chip detection information.
14    AtsamDsu(AtsamDsuDetection),
15
16    /// Espressif chip detection information.
17    Espressif(EspressifDetection),
18
19    /// Nordic Semiconductor FICR CONFIGID-based chip detection information.
20    NordicConfigId(NordicConfigIdDetection),
21
22    /// Nordic Semiconductor FICR INFO-based chip detection information.
23    NordicFicrInfo(NordicFicrDetection),
24
25    /// Infineon XMC4000 SCU chip detection information.
26    InfineonXmcScu(InfineonXmcScuDetection),
27
28    /// Infineon PSOC silicon ID chip detection information.
29    InfineonPsocSiid(InfineonPsocSiidDetection),
30}
31
32impl ChipDetectionMethod {
33    /// Returns the ATSAM detection information if available.
34    pub fn as_atsam_dsu(&self) -> Option<&AtsamDsuDetection> {
35        if let Self::AtsamDsu(v) = self {
36            Some(v)
37        } else {
38            None
39        }
40    }
41
42    /// Returns the Espressif detection information if available.
43    pub fn as_espressif(&self) -> Option<&EspressifDetection> {
44        if let Self::Espressif(v) = self {
45            Some(v)
46        } else {
47            None
48        }
49    }
50
51    /// Returns the Nordic CONFIGID detection information if available.
52    pub fn as_nordic_configid(&self) -> Option<&NordicConfigIdDetection> {
53        if let Self::NordicConfigId(v) = self {
54            Some(v)
55        } else {
56            None
57        }
58    }
59
60    /// Returns the Nordic FICR detection information if available.
61    pub fn as_nordic_ficr(&self) -> Option<&NordicFicrDetection> {
62        if let Self::NordicFicrInfo(v) = self {
63            Some(v)
64        } else {
65            None
66        }
67    }
68
69    /// Returns the Infineon XMC SCU detection information if available.
70    pub fn as_infineon_xmc_scu(&self) -> Option<&InfineonXmcScuDetection> {
71        if let Self::InfineonXmcScu(v) = self {
72            Some(v)
73        } else {
74            None
75        }
76    }
77
78    /// Returns the Infineon PSOC silicon ID detection information if available.
79    pub fn as_infineon_psoc_siid(&self) -> Option<&InfineonPsocSiidDetection> {
80        if let Self::InfineonPsocSiid(v) = self {
81            Some(v)
82        } else {
83            None
84        }
85    }
86}
87
88/// Microchip ATSAM chip detection information when the device contains a DSU.
89#[derive(Debug, Clone, Serialize, Deserialize)]
90#[serde(deny_unknown_fields)]
91pub struct AtsamDsuDetection {
92    /// DSU DID register, Processor field
93    pub processor: u8,
94
95    /// DSU DID register, Family field
96    pub family: u8,
97
98    /// DSU DID register, Series field
99    pub series: u8,
100
101    /// Devsel => Target field value
102    #[serde(serialize_with = "hex_keys_indexmap")]
103    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
104    pub variants: IndexMap<u8, String>,
105}
106
107/// Espressif chip detection information.
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(deny_unknown_fields)]
110pub struct EspressifDetection {
111    /// Debug module IDCODE
112    #[serde(serialize_with = "hex_u_int")]
113    pub idcode: u32,
114
115    /// Magic chip value => Target name.
116    #[serde(serialize_with = "hex_keys_indexmap")]
117    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
118    pub variants: IndexMap<u32, String>,
119}
120
121/// Nordic FICR CONFIGID-based chip detection information.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[serde(deny_unknown_fields)]
124pub struct NordicConfigIdDetection {
125    /// FICR CONFIGID address
126    #[serde(serialize_with = "hex_u_int")]
127    pub configid_address: u32,
128
129    /// CONFIGID.HWID => Target name.
130    #[serde(serialize_with = "hex_keys_indexmap")]
131    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
132    pub hwid: IndexMap<u32, String>,
133}
134
135/// Nordic FICR INFO-based chip detection information.
136#[derive(Debug, Clone, Serialize, Deserialize)]
137#[serde(deny_unknown_fields)]
138pub struct NordicFicrDetection {
139    /// FICR INFO.PART address
140    #[serde(serialize_with = "hex_u_int")]
141    pub part_address: u32,
142
143    /// FICR INFO.VARIANT address
144    #[serde(serialize_with = "hex_u_int")]
145    pub variant_address: u32,
146
147    /// The value of INFO.PART
148    #[serde(serialize_with = "hex_u_int")]
149    pub part: u32,
150
151    /// INFO.VARIANT => Target name.
152    #[serde(serialize_with = "hex_keys_indexmap")]
153    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
154    pub variants: IndexMap<u32, String>,
155}
156
157/// Infineon XMC4000 SCU chip detection information.
158#[derive(Debug, Clone, Serialize, Deserialize)]
159#[serde(deny_unknown_fields)]
160pub struct InfineonXmcScuDetection {
161    /// Chip partid
162    #[serde(serialize_with = "hex_u_int")]
163    pub part: u16,
164
165    /// SCU_IDCHIP register value, bits \[19:4\]
166    #[serde(serialize_with = "hex_u_int")]
167    pub scu_id: u32,
168
169    /// Flash size in kB => Target name.
170    // Intentionally not hex-keyed, sizes look better in decimal.
171    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
172    pub variants: IndexMap<u32, String>,
173}
174
175/// Infineon PSOC SIID chip detection information.
176#[derive(Debug, Clone, Serialize, Deserialize)]
177#[serde(deny_unknown_fields)]
178pub struct InfineonPsocSiidDetection {
179    /// Chip family ID
180    #[serde(serialize_with = "hex_u_int")]
181    pub family_id: u16,
182
183    /// Silicon ID => Target name.
184    #[serde(serialize_with = "hex_keys_indexmap")]
185    #[serde(deserialize_with = "maps_duplicate_key_is_error::deserialize")]
186    pub silicon_ids: IndexMap<u16, String>,
187}