1use crate::{
4 api::{Convertible, PeripheralOnBus, PeripheralsOnBus, Validatable},
5 device::DeviceIn,
6 gpio_bank::{GpioBankIn, GpioBankOut},
7 peripheral_bus::PeripheralBusOut,
8 types::DeviceId,
9};
10use serde_derive::{Deserialize, Serialize};
11
12#[derive(Debug, Deserialize)]
14pub struct GpioIn {
15 pub version: u8,
17 pub banks: Vec<GpioBankIn>,
19}
20
21#[derive(Debug, PartialEq, Serialize)]
23pub struct GpioOut {
24 pub version: u8,
26 pub banks: Vec<GpioBankOut>,
28}
29
30impl Convertible for GpioIn {
31 type Output = GpioOut;
32
33 fn to_output(&self, id: &DeviceId, device: &DeviceIn) -> GpioOut {
35 GpioOut {
36 version: self.version.clone(),
37 banks: self
38 .banks
39 .iter()
40 .filter(|bank| bank.is_valid_for(&id, &device))
41 .map(|bank| bank.to_output(&id, &device))
42 .collect(),
43 }
44 }
45}
46
47impl PeripheralsOnBus for GpioIn {
48 fn peripheral_buses(&self, id: &DeviceId, device: &DeviceIn) -> Vec<PeripheralBusOut> {
49 self.banks
50 .iter()
51 .filter(|bank| bank.is_valid_for(&id, &device))
52 .map(|bank| bank.peripheral_bus())
53 .collect()
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use crate::tests::*;
61 use crate::{
62 peripheral_bus::PeripheralBusIn,
63 types::{Bus, Valid},
64 };
65
66 fn gpio_under_test() -> GpioOut {
67 GpioIn {
68 version: 2,
69 banks: vec![
70 GpioBankIn {
71 name: "GPIOA".to_owned(),
72 pins: valid_gpio_pins(),
73 valid: Valid::default(),
74 bus: PeripheralBusIn {
75 name: Bus::AHB,
76 field: "IOPA".to_owned(),
77 resetable: true,
78 },
79 },
80 GpioBankIn {
81 name: "GPIOB".to_owned(),
82 pins: valid_gpio_pins(),
83 valid: Valid::default(),
84 bus: PeripheralBusIn {
85 name: Bus::AHB,
86 field: "IOPB".to_owned(),
87 resetable: true,
88 },
89 },
90 ],
91 }
92 .to_output(&valid_device_id(), &valid_device_in())
93 }
94
95 #[test]
96 fn has_a_version_number() {
97 assert_eq!(gpio_under_test().version, 2);
98 }
99 #[test]
100 fn has_some_banks() {
101 let banks = gpio_under_test().banks;
102 let mut banks = banks.iter();
103
104 assert_eq!(banks.next().unwrap().GPIO, "GPIOA");
105 assert_eq!(banks.next().unwrap().GPIO, "GPIOB");
106 assert!(banks.next().is_none());
107 }
108}