kernel/hil/gpio_async.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Interface for GPIO pins that require split-phase operation to control.
6
7use crate::hil;
8use crate::ErrorCode;
9
10/// Interface for banks of asynchronous GPIO pins.
11///
12/// GPIO pins are asynchronous when there is an asynchronous interface
13/// used to control them. The most common example is when using a GPIO
14/// extender on an I2C or SPI bus. With asynchronous GPIO functions,
15/// every config action results in an eventual callback function that
16/// indicates that the configuration has finished (unless the initial
17/// function call returns an error code, then no callback will be
18/// generated).
19///
20/// Asynchronous GPIO pins are grouped into ports because it is assumed that
21/// the remote entity that is controlling the pins can control multiple pins.
22/// Typically, a port will be provided by a particular driver.
23///
24/// The API for the Port mirrors the synchronous GPIO interface.
25pub trait Port {
26 /// Try to disable a GPIO pin. This cannot be supported for all devices.
27 fn disable(&self, pin: usize) -> Result<(), ErrorCode>;
28
29 /// Configure a pin as an ouput GPIO.
30 fn make_output(&self, pin: usize) -> Result<(), ErrorCode>;
31
32 /// Configure a pin as an input GPIO. Not all FloatingMode settings may
33 /// be supported by a given device.
34 fn make_input(&self, pin: usize, mode: hil::gpio::FloatingState) -> Result<(), ErrorCode>;
35
36 /// Get the state (0 or 1) of an input pin. The value will be returned
37 /// via a callback.
38 fn read(&self, pin: usize) -> Result<(), ErrorCode>;
39
40 /// Toggle an output GPIO pin.
41 fn toggle(&self, pin: usize) -> Result<(), ErrorCode>;
42
43 /// Assert a GPIO pin high.
44 fn set(&self, pin: usize) -> Result<(), ErrorCode>;
45
46 /// Clear a GPIO pin low.
47 fn clear(&self, pin: usize) -> Result<(), ErrorCode>;
48
49 /// Setup an interrupt on a GPIO input pin. The identifier should be
50 /// the port number and will be returned when the interrupt callback
51 /// fires.
52 fn enable_interrupt(&self, pin: usize, mode: hil::gpio::InterruptEdge)
53 -> Result<(), ErrorCode>;
54
55 /// Disable an interrupt on a GPIO input pin.
56 fn disable_interrupt(&self, pin: usize) -> Result<(), ErrorCode>;
57
58 fn is_pending(&self, pin: usize) -> bool;
59}
60
61/// The gpio_async Client interface is used to both receive callbacks
62/// when a configuration command finishes and to handle interrupt events
63/// from pins with interrupts enabled.
64pub trait Client {
65 /// Called when an interrupt occurs. The pin that interrupted is included,
66 /// and the identifier that was passed with the call to `enable_interrupt`
67 /// is also returned.
68 fn fired(&self, pin: usize, identifier: usize);
69
70 /// Done is called when a configuration command finishes.
71 fn done(&self, value: usize);
72}