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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use core::convert::TryFrom;
#[derive(Copy, Clone, Debug)]
pub struct Error {
repr: Repr,
}
#[derive(Copy, Clone, Debug)]
enum Repr {
Device(Status),
Simple(ErrorKind),
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error {
repr: Repr::Simple(kind),
}
}
}
impl From<Status> for Error {
fn from(status: Status) -> Error {
Error {
repr: Repr::Device(status),
}
}
}
impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match &self.repr {
Repr::Device(status) => write!(fmt, "{}", status),
Repr::Simple(kind) => write!(fmt, "{}", kind),
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum Status {
CheckmacVerifyFailed = 0x01,
Execution = 0x0F,
Parse = 0x03,
Crc = 0xFF,
Unknown = 0xD5,
Ecc = 0x05,
SelfTest = 0x07,
HealthTest = 0x08,
}
impl TryFrom<u8> for Status {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x00 => Err(()),
0x01 => Ok(Self::CheckmacVerifyFailed),
0x0F => Ok(Self::Execution),
0x03 => Ok(Self::Parse),
0xFF => Ok(Self::Crc),
0x05 => Ok(Self::Ecc),
0x07 => Ok(Self::SelfTest),
0x08 => Ok(Self::HealthTest),
_ => Ok(Self::Unknown),
}
}
}
impl core::fmt::Display for Status {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::CheckmacVerifyFailed => write!(fmt, "checkmac or verify failed"),
Self::Crc => write!(
fmt,
"bad crc found (command not properly received by device) or other comm error"
),
Self::Ecc => write!(
fmt,
"computation error during ECC processing causing invalid results"
),
Self::Execution => write!(fmt, "chip can't execute the command"),
Self::HealthTest => write!(fmt, "random number generator health test error"),
Self::Parse => write!(
fmt,
"command received byte length, opcode or parameter was illegal"
),
Self::SelfTest => write!(fmt, "chip is in self test failure mode"),
Self::Unknown => write!(fmt, "response contains unknown non-zero status byte"),
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum ErrorKind {
AssertFailure = 0xF6,
BadOpcode = 0xF2,
BadParam = 0xE2,
CommFail = 0xF0,
ConfigZoneLocked = 0x01,
DataZoneLocked = 0x04,
FuncFail = 0xE0,
InvalidId = 0xE3,
InvalidSize = 0xE4,
NotLocked = 0xF8,
ResyncWithWakeup = 0xE8,
RxCrcError = 0xE5,
RxFail = 0xE6,
SmallBuffer = 0xED,
Timeout = 0xF1,
TooManyCommRetries = 0xEC,
TxFail = 0xF7,
Unimplemented = 0xF5,
UseFlagsConsumed = 0xFC,
WakeFailed = 0xD0,
}
impl core::fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::AssertFailure => write!(fmt, "failed run-time consistency check"),
Self::BadOpcode => write!(fmt, "opcode is not supported by the device"),
Self::BadParam => write!(fmt, "bad argument (out of range, null pointer, etc.)"),
Self::CommFail => write!(fmt, "communication with device failed"),
Self::ConfigZoneLocked => write!(fmt, "config zone is locked"),
Self::DataZoneLocked => write!(fmt, "data zone is locked"),
Self::FuncFail => write!(
fmt,
"function could not execute due to incorrect condition / state"
),
Self::InvalidId => write!(fmt, "invalid device id, id not set"),
Self::InvalidSize => write!(
fmt,
"count value is out of range or greater than buffer size"
),
Self::NotLocked => write!(fmt, "required zone was not locked"),
Self::ResyncWithWakeup => write!(
fmt,
"re-synchronization succeeded, but only after generating a Wake-up"
),
Self::RxCrcError => write!(fmt, "crc error in data received from device"),
Self::RxFail => write!(
fmt,
"timed out while waiting for response. Number of bytes received is > 0"
),
Self::SmallBuffer => write!(fmt, "supplied buffer is too small for data required"),
Self::Timeout => write!(fmt, "timed out while waiting for response"),
Self::TooManyCommRetries => {
write!(
fmt,
"device did not respond too many times, indicating no device present"
)
}
Self::TxFail => write!(fmt, "failed to write"),
Self::Unimplemented => write!(
fmt,
"function or some element of it hasn't been implemented yet"
),
Self::UseFlagsConsumed => {
write!(fmt, "use flags on the device indicates its consumed fully")
}
Self::WakeFailed => write!(fmt, "device did not respond to wake call as expected"),
}
}
}