qtest_stm32f4nucleo/
lib.rs1pub mod gpio;
6
7pub mod timer;
8
9use gpio::Gpio;
10use timer::Timer;
11
12#[derive(Debug, Clone)]
14pub struct Peripherals {
15 gpio_a: Gpio,
16 gpio_b: Gpio,
17 gpio_c: Gpio,
18 gpio_d: Gpio,
19 gpio_e: Gpio,
20 gpio_f: Gpio,
21 gpio_g: Gpio,
22 gpio_h: Gpio,
23 timer2: Timer,
24 timer5: Timer,
25}
26
27macro_rules! create_gpio_accessors {
29 ($($name:ident),*) => {
30 $(
31 pub fn $name(&self) -> &Gpio {
32 &self.$name
33 }
34 )*
35 };
36}
37macro_rules! create_timer_accessors {
38 ($($name:ident),*) => {
39 $(
40 pub fn $name(&self) -> &Timer {
41 &self.$name
42 }
43 )*
44 };
45}
46
47impl Peripherals {
48 pub fn new() -> Self {
50 Peripherals {
51 gpio_a: Gpio::new(0x40020000),
52 gpio_b: Gpio::new(0x40020400),
53 gpio_c: Gpio::new(0x40020800),
54 gpio_d: Gpio::new(0x40020C00),
55 gpio_e: Gpio::new(0x40021000),
56 gpio_f: Gpio::new(0x40021400),
57 gpio_g: Gpio::new(0x40021800),
58 gpio_h: Gpio::new(0x40021C00),
59 timer2: Timer::new(0x40000000), timer5: Timer::new(0x40000C00), }
62 }
63
64 pub fn get_gpio(&self, name: &str) -> Option<&Gpio> {
65 let normalized = name
66 .to_lowercase()
67 .chars()
68 .filter(|c| c.is_alphanumeric()) .collect::<String>();
70
71 match normalized.as_str() {
72 "gpioa" => Some(&self.gpio_a),
73 "gpiob" => Some(&self.gpio_b),
74 "gpioc" => Some(&self.gpio_c),
75 "gpiod" => Some(&self.gpio_d),
76 "gpioe" => Some(&self.gpio_e),
77 "gpiof" => Some(&self.gpio_f),
78 "gpiog" => Some(&self.gpio_g),
79 "gpioh" => Some(&self.gpio_h),
80 _ => None,
81 }
82 }
83
84 pub fn get_timer(&self, name: &str) -> Option<&Timer> {
85 let normalized = name
86 .to_lowercase()
87 .chars()
88 .filter(|c| c.is_alphanumeric())
89 .collect::<String>();
90
91 match normalized.as_str() {
92 "timer2" => Some(&self.timer2),
93 "timer5" => Some(&self.timer5),
94 _ => None,
95 }
96 }
97
98 create_gpio_accessors!(gpio_a, gpio_b, gpio_c);
100 create_timer_accessors!(timer2, timer5);
101}
102
103impl Default for Peripherals {
104 fn default() -> Self {
105 Self::new()
106 }
107}