stm32_rust_template/driver/gpio/mod.rs
1//! # GPIO Driver
2//!
3//! Provides a hardware abstraction layer for General Purpose Input/Output (GPIO)
4//! pins on STM32 microcontrollers.
5//!
6//! This module defines the GPIO trait and supporting types for pin configuration,
7//! interrupt handling, and digital I/O operations across different STM32 families.
8#![allow(dead_code)]
9
10use bitflags::bitflags;
11use core::ops::FnMut;
12
13/// Represents a GPIO pin identifier.
14/// The interpretation of this identifier is implementation-specific.
15/// For example, it could be a simple number (0-15) for a single port,
16/// or a value like `(port << 4) | pin` for multiple ports.
17pub type Pin = u32;
18
19/// Defines the direction of a GPIO pin.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Direction {
22 /// Input (default)
23 Input,
24 /// Output
25 Output,
26}
27
28/// Defines the output mode of a GPIO pin.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum OutputMode {
31 /// Push-pull (default)
32 PushPull,
33 /// Open-drain
34 OpenDrain,
35}
36
37/// Defines the internal pull-up or pull-down resistor configuration.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub enum PullResistor {
40 /// None (default)
41 None,
42 /// Pull-up
43 PullUp,
44 /// Pull-down
45 PullDown,
46}
47
48/// Defines the trigger condition for an external interrupt.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum EventTrigger {
51 /// None (default)
52 None,
53 /// Rising-edge
54 RisingEdge,
55 /// Falling-edge
56 FallingEdge,
57 /// Either edge (rising and falling)
58 EitherEdge,
59}
60
61bitflags! {
62 /// Represents GPIO interrupt events.
63 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
64 pub struct EventType: u32 {
65 /// Rising-edge detected
66 const RISING_EDGE = (1 << 0);
67 /// Falling-edge detected
68 const FALLING_EDGE = (1 << 1);
69 /// Either edge detected (if supported)
70 const EITHER_EDGE = (1 << 2);
71 }
72}
73
74/// A generic error type for the GPIO driver, using i32 for error codes.
75pub type Error = i32;
76
77/// A specialized Result type for GPIO operations.
78pub type Result<T> = core::result::Result<T, Error>;
79
80/// A trait that defines a standard interface for a GPIO driver.
81/// This trait manages a collection of GPIO pins.
82pub trait Gpio<'a> {
83 /// Initializes a GPIO pin and registers a callback for events.
84 fn setup(&mut self, pin: Pin, callback: impl FnMut(Pin, EventType) + 'a) -> Result<()>;
85
86 /// Sets the direction of a GPIO pin.
87 fn set_direction(&mut self, pin: Pin, direction: Direction) -> Result<()>;
88
89 /// Sets the output mode of a GPIO pin.
90 fn set_output_mode(&mut self, pin: Pin, mode: OutputMode) -> Result<()>;
91
92 /// Sets the internal pull-up or pull-down resistor for a GPIO pin.
93 fn set_pull_resistor(&mut self, pin: Pin, resistor: PullResistor) -> Result<()>;
94
95 /// Sets the event trigger for a GPIO pin.
96 fn set_event_trigger(&mut self, pin: Pin, trigger: EventTrigger) -> Result<()>;
97
98 /// Sets the output level of a GPIO pin.
99 /// `value` is true for high, false for low.
100 fn set_output(&mut self, pin: Pin, value: bool);
101
102 /// Gets the input level of a GPIO pin.
103 /// Returns true for high, false for low.
104 fn get_input(&self, pin: Pin) -> bool;
105}
106
107#[cfg(feature = "stm32f407")]
108pub mod stm32f407;
109
110#[cfg(feature = "stm32f401")]
111pub mod stm32f401;
112
113#[cfg(feature = "stm32f411")]
114pub mod stm32f411;
115
116#[cfg(feature = "stm32f103")]
117pub mod stm32f103;