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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Device-specific types for [LTC6813](<https://www.analog.com/en/products/ltc6813-1.html>)
use crate::commands::*;
use crate::monitor::{
    ADCMode, ChannelIndex, ChannelType, CommandTime, DeviceTypes, GroupedRegisterIndex, NoPolling, NoWriteCommandError,
    RegisterAddress, RegisterLocator, ToCommandBitmap, ToCommandTiming, ToFullCommand, LTC681X,
};
use core::slice::Iter;
use embedded_hal::blocking::spi::Transfer;
use embedded_hal::digital::v2::OutputPin;

/// Cell selection for ADC conversion
///
/// See page 62 of [datasheet](<https://www.analog.com/media/en/technical-documentation/data-sheets/ltc6813-1.pdf>)
/// for conversion times
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum CellSelection {
    /// All cells
    All = 0x0,
    /// Cells 1, 7, 13
    Group1 = 0x1,
    /// Cells 2, 8, 14
    Group2 = 0x2,
    /// Cells 3, 9, 15
    Group3 = 0x3,
    /// Cells 4, 10, 16
    Group4 = 0x4,
    /// Cells 5, 11, 17
    Group5 = 0x5,
    /// cells 6, 12, 18
    Group6 = 0x6,
}

/// GPIO selection for ADC conversion,
///
/// See page 62 of [datasheet](<https://www.analog.com/media/en/technical-documentation/data-sheets/ltc6813-1.pdf>)
/// for conversion times
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum GPIOSelection {
    /// GPIO 1-5, 2nd Reference, GPIO 6-9
    All = 0x0,
    /// GPIO 1 and GPIO 6
    Group1 = 0x1,
    /// GPIO 2 and GPIO 7
    Group2 = 0x2,
    /// GPIO 3 and GPIO 8
    Group3 = 0x3,
    /// GPIO 4 and GPIO 9
    Group4 = 0x4,
    /// GPIO 5
    Group5 = 0x5,
    /// 2nd Reference
    Group6 = 0x6,
}

/// Available registers
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Register {
    CellVoltageA,
    CellVoltageB,
    CellVoltageC,
    CellVoltageD,
    CellVoltageE,
    CellVoltageF,
    AuxiliaryA,
    AuxiliaryB,
    AuxiliaryC,
    AuxiliaryD,
    StatusA,
    StatusB,
    ConfigurationA,
    ConfigurationB,
}

/// All conversion channels
#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub enum Channel {
    Cell1,
    Cell2,
    Cell3,
    Cell4,
    Cell5,
    Cell6,
    Cell7,
    Cell8,
    Cell9,
    Cell10,
    Cell11,
    Cell12,
    Cell13,
    Cell14,
    Cell15,
    Cell16,
    Cell17,
    Cell18,
    GPIO1,
    GPIO2,
    GPIO3,
    GPIO4,
    GPIO5,
    GPIO6,
    GPIO7,
    GPIO8,
    GPIO9,
    SecondReference,
}

/// Device type of LTC6813
#[cfg_attr(test, derive(Debug))]
pub struct LTC6813 {}

impl DeviceTypes for LTC6813 {
    type CellSelection = CellSelection;
    type GPIOSelection = GPIOSelection;
    type Register = Register;
    type Channel = Channel;

    const CELL_COUNT: usize = 18;
    const GPIO_COUNT: usize = 9;

    const OVERLAP_TEST_REG_1: Option<Self::Register> = Some(Register::CellVoltageC);
    const OVERLAP_TEST_REG_2: Option<Self::Register> = Some(Register::CellVoltageE);

    const REG_STATUS_A: Self::Register = Register::StatusA;
    const REG_STATUS_B: Self::Register = Register::StatusB;

    const REG_CONF_A: Self::Register = Register::ConfigurationA;
    const REG_CONF_B: Option<Self::Register> = Some(Register::ConfigurationB);
}

impl<B, CS, const L: usize> LTC681X<B, CS, NoPolling, LTC6813, L>
where
    B: Transfer<u8>,
    CS: OutputPin,
{
    /// Creates a client instant for LTC6813 variant
    pub fn ltc6813(bus: B, cs: CS) -> Self {
        LTC681X::new(bus, cs)
    }
}

impl ToCommandBitmap for CellSelection {
    fn to_bitmap(&self) -> u16 {
        *self as u16
    }
}

impl ToCommandBitmap for GPIOSelection {
    fn to_bitmap(&self) -> u16 {
        *self as u16
    }
}

impl ToFullCommand for Register {
    /// Returns the precalculated full command
    fn to_read_command(&self) -> [u8; 4] {
        match self {
            Register::CellVoltageA => CMD_R_CELL_V_REG_A,
            Register::CellVoltageB => CMD_R_CELL_V_REG_B,
            Register::CellVoltageC => CMD_R_CELL_V_REG_C,
            Register::CellVoltageD => CMD_R_CELL_V_REG_D,
            Register::CellVoltageE => CMD_R_CELL_V_REG_E,
            Register::CellVoltageF => CMD_R_CELL_V_REG_F,
            Register::AuxiliaryA => CMD_R_AUX_V_REG_A,
            Register::AuxiliaryB => CMD_R_AUX_V_REG_B,
            Register::AuxiliaryC => CMD_R_AUX_V_REG_C,
            Register::AuxiliaryD => CMD_R_AUX_V_REG_D,
            Register::StatusA => CMD_R_STATUS_A,
            Register::StatusB => CMD_R_STATUS_B,
            Register::ConfigurationA => CMD_R_CONF_A,
            Register::ConfigurationB => CMD_R_CONF_B,
        }
    }

    fn to_write_command(&self) -> Result<[u8; 4], NoWriteCommandError> {
        match self {
            Register::ConfigurationA => Ok(CMD_W_CONF_A),
            Register::ConfigurationB => Ok(CMD_W_CONF_B),
            _ => Err(NoWriteCommandError {}),
        }
    }
}

impl ToCommandTiming for CellSelection {
    fn to_conv_command_timing(&self, mode: ADCMode) -> CommandTime {
        match self {
            CellSelection::All => match mode {
                ADCMode::Fast => CommandTime::new(1121, 1296),
                ADCMode::Normal => CommandTime::new(2343, 3041),
                ADCMode::Filtered => CommandTime::new(201_325, 4437),
                ADCMode::Other => CommandTime::new(12_816, 7230),
            },
            CellSelection::Group1
            | CellSelection::Group2
            | CellSelection::Group3
            | CellSelection::Group4
            | CellSelection::Group5
            | CellSelection::Group6 => match mode {
                ADCMode::Fast => CommandTime::new(203, 232),
                ADCMode::Normal => CommandTime::new(407, 523),
                ADCMode::Filtered => CommandTime::new(33_570, 756),
                ADCMode::Other => CommandTime::new(2152, 1221),
            },
        }
    }
}

impl ToCommandTiming for GPIOSelection {
    fn to_conv_command_timing(&self, mode: ADCMode) -> CommandTime {
        match self {
            GPIOSelection::All => match mode {
                ADCMode::Fast => CommandTime::new(1825, 2116),
                ADCMode::Normal => CommandTime::new(3862, 5025),
                ADCMode::Filtered => CommandTime::new(335_498, 7353),
                ADCMode::Other => CommandTime::new(21316, 12007),
            },
            GPIOSelection::Group1 | GPIOSelection::Group2 | GPIOSelection::Group3 | GPIOSelection::Group4 => match mode
            {
                ADCMode::Fast => CommandTime::new(380, 439),
                ADCMode::Normal => CommandTime::new(788, 1000),
                ADCMode::Filtered => CommandTime::new(67_100, 1_500),
                ADCMode::Other => CommandTime::new(4_300, 2_4000),
            },
            GPIOSelection::Group5 | GPIOSelection::Group6 => match mode {
                ADCMode::Fast => CommandTime::new(200, 229),
                ADCMode::Normal => CommandTime::new(403, 520),
                ADCMode::Filtered => CommandTime::new(34_000, 753),
                ADCMode::Other => CommandTime::new(2_100, 1_200),
            },
        }
    }
}

impl GroupedRegisterIndex for Register {
    fn to_index(&self) -> usize {
        match self {
            Register::CellVoltageA => 0,
            Register::CellVoltageB => 1,
            Register::CellVoltageC => 2,
            Register::CellVoltageD => 3,
            Register::CellVoltageE => 4,
            Register::CellVoltageF => 5,
            Register::AuxiliaryA => 0,
            Register::AuxiliaryB => 1,
            Register::AuxiliaryC => 2,
            Register::AuxiliaryD => 3,
            Register::StatusA => 0,
            Register::StatusB => 1,
            Register::ConfigurationA => 0,
            Register::ConfigurationB => 1,
        }
    }
}

impl ChannelIndex for Channel {
    fn to_cell_index(&self) -> Option<usize> {
        match self {
            Channel::Cell1 => Some(0),
            Channel::Cell2 => Some(1),
            Channel::Cell3 => Some(2),
            Channel::Cell4 => Some(3),
            Channel::Cell5 => Some(4),
            Channel::Cell6 => Some(5),
            Channel::Cell7 => Some(6),
            Channel::Cell8 => Some(7),
            Channel::Cell9 => Some(8),
            Channel::Cell10 => Some(9),
            Channel::Cell11 => Some(10),
            Channel::Cell12 => Some(11),
            Channel::Cell13 => Some(12),
            Channel::Cell14 => Some(13),
            Channel::Cell15 => Some(14),
            Channel::Cell16 => Some(15),
            Channel::Cell17 => Some(16),
            Channel::Cell18 => Some(17),
            _ => None,
        }
    }

    fn to_gpio_index(&self) -> Option<usize> {
        match self {
            Channel::GPIO1 => Some(0),
            Channel::GPIO2 => Some(1),
            Channel::GPIO3 => Some(2),
            Channel::GPIO4 => Some(3),
            Channel::GPIO5 => Some(4),
            Channel::GPIO6 => Some(5),
            Channel::GPIO7 => Some(6),
            Channel::GPIO8 => Some(7),
            Channel::GPIO9 => Some(8),
            _ => None,
        }
    }
}

impl From<Channel> for ChannelType {
    fn from(channel: Channel) -> Self {
        match channel {
            Channel::GPIO1 => ChannelType::GPIO,
            Channel::GPIO2 => ChannelType::GPIO,
            Channel::GPIO3 => ChannelType::GPIO,
            Channel::GPIO4 => ChannelType::GPIO,
            Channel::GPIO5 => ChannelType::GPIO,
            Channel::GPIO6 => ChannelType::GPIO,
            Channel::GPIO7 => ChannelType::GPIO,
            Channel::GPIO8 => ChannelType::GPIO,
            Channel::GPIO9 => ChannelType::GPIO,
            Channel::SecondReference => ChannelType::Reference,
            _ => ChannelType::Cell,
        }
    }
}

impl RegisterAddress<LTC6813> {
    pub const fn ltc6813(channel: Channel, register: Register, slot: usize) -> Self {
        RegisterAddress {
            channel,
            register,
            slot,
        }
    }
}

const CELL_REGISTER_LOCATIONS: [RegisterAddress<LTC6813>; 18] = [
    RegisterAddress::ltc6813(Channel::Cell1, Register::CellVoltageA, 0),
    RegisterAddress::ltc6813(Channel::Cell7, Register::CellVoltageC, 0),
    RegisterAddress::ltc6813(Channel::Cell13, Register::CellVoltageE, 0),
    RegisterAddress::ltc6813(Channel::Cell2, Register::CellVoltageA, 1),
    RegisterAddress::ltc6813(Channel::Cell8, Register::CellVoltageC, 1),
    RegisterAddress::ltc6813(Channel::Cell14, Register::CellVoltageE, 1),
    RegisterAddress::ltc6813(Channel::Cell3, Register::CellVoltageA, 2),
    RegisterAddress::ltc6813(Channel::Cell9, Register::CellVoltageC, 2),
    RegisterAddress::ltc6813(Channel::Cell15, Register::CellVoltageE, 2),
    RegisterAddress::ltc6813(Channel::Cell4, Register::CellVoltageB, 0),
    RegisterAddress::ltc6813(Channel::Cell10, Register::CellVoltageD, 0),
    RegisterAddress::ltc6813(Channel::Cell16, Register::CellVoltageF, 0),
    RegisterAddress::ltc6813(Channel::Cell5, Register::CellVoltageB, 1),
    RegisterAddress::ltc6813(Channel::Cell11, Register::CellVoltageD, 1),
    RegisterAddress::ltc6813(Channel::Cell17, Register::CellVoltageF, 1),
    RegisterAddress::ltc6813(Channel::Cell6, Register::CellVoltageB, 2),
    RegisterAddress::ltc6813(Channel::Cell12, Register::CellVoltageD, 2),
    RegisterAddress::ltc6813(Channel::Cell18, Register::CellVoltageF, 2),
];

impl RegisterLocator<LTC6813> for CellSelection {
    fn get_locations(&self) -> Iter<'static, RegisterAddress<LTC6813>> {
        match self {
            CellSelection::All => CELL_REGISTER_LOCATIONS.iter(),
            CellSelection::Group1 => CELL_REGISTER_LOCATIONS[0..3].iter(),
            CellSelection::Group2 => CELL_REGISTER_LOCATIONS[3..6].iter(),
            CellSelection::Group3 => CELL_REGISTER_LOCATIONS[6..9].iter(),
            CellSelection::Group4 => CELL_REGISTER_LOCATIONS[9..12].iter(),
            CellSelection::Group5 => CELL_REGISTER_LOCATIONS[12..15].iter(),
            CellSelection::Group6 => CELL_REGISTER_LOCATIONS[15..18].iter(),
        }
    }
}

const GPIO_REGISTER_LOCATIONS: [RegisterAddress<LTC6813>; 10] = [
    RegisterAddress::ltc6813(Channel::GPIO1, Register::AuxiliaryA, 0),
    RegisterAddress::ltc6813(Channel::GPIO6, Register::AuxiliaryC, 0),
    RegisterAddress::ltc6813(Channel::GPIO2, Register::AuxiliaryA, 1),
    RegisterAddress::ltc6813(Channel::GPIO7, Register::AuxiliaryC, 1),
    RegisterAddress::ltc6813(Channel::GPIO3, Register::AuxiliaryA, 2),
    RegisterAddress::ltc6813(Channel::GPIO8, Register::AuxiliaryC, 2),
    RegisterAddress::ltc6813(Channel::GPIO4, Register::AuxiliaryB, 0),
    RegisterAddress::ltc6813(Channel::GPIO9, Register::AuxiliaryD, 0),
    RegisterAddress::ltc6813(Channel::GPIO5, Register::AuxiliaryB, 1),
    RegisterAddress::ltc6813(Channel::SecondReference, Register::AuxiliaryB, 2),
];

impl RegisterLocator<LTC6813> for GPIOSelection {
    fn get_locations(&self) -> Iter<'static, RegisterAddress<LTC6813>> {
        match self {
            GPIOSelection::All => GPIO_REGISTER_LOCATIONS.iter(),
            GPIOSelection::Group1 => GPIO_REGISTER_LOCATIONS[0..2].iter(),
            GPIOSelection::Group2 => GPIO_REGISTER_LOCATIONS[2..4].iter(),
            GPIOSelection::Group3 => GPIO_REGISTER_LOCATIONS[4..6].iter(),
            GPIOSelection::Group4 => GPIO_REGISTER_LOCATIONS[6..8].iter(),
            GPIOSelection::Group5 => GPIO_REGISTER_LOCATIONS[8..9].iter(),
            GPIOSelection::Group6 => GPIO_REGISTER_LOCATIONS[9..10].iter(),
        }
    }
}