#![macro_use]
use core::convert::Infallible;
use core::future::Future;
use core::pin::Pin as FuturePin;
use core::task::{Context, Poll};
use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
use hpsys::HpsysPin;
use crate::interrupt::InterruptExt;
use crate::{interrupt, peripherals, Peripheral};
use crate::utils::BitIter64;
#[cfg(any(feature = "sf32lb52x"))]
pub(crate) const PA_PIN_COUNT: usize = 44;
pub mod hpsys;
static PA_WAKERS: [AtomicWaker; PA_PIN_COUNT] = [const { AtomicWaker::new() }; PA_PIN_COUNT];
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Level {
Low,
High,
}
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl From<Level> for bool {
fn from(level: Level) -> bool {
match level {
Level::Low => false,
Level::High => true,
}
}
}
impl core::ops::Not for Level {
type Output = Self;
fn not(self) -> Self::Output {
(!bool::from(self)).into()
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Pull {
None,
Up,
Down,
}
#[derive(Debug, Eq, PartialEq)]
pub enum Drive {
Drive0,
Drive1,
Drive2,
Drive3,
}
#[derive(Debug, Eq, PartialEq)]
pub enum SlewRate {
Fast,
Slow,
}
pub struct Input<'d> {
pin: Flex<'d>,
}
impl<'d> Input<'d> {
#[inline]
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, pull: Pull) -> Self {
let mut pin = Flex::new(pin);
pin.set_pull(pull);
pin.set_as_input();
Self { pin }
}
#[inline]
pub fn set_schmitt(&mut self, enable: bool) {
self.pin.set_schmitt(enable)
}
#[inline]
pub fn is_high(&self) -> bool {
self.pin.is_high()
}
#[inline]
pub fn is_low(&self) -> bool {
self.pin.is_low()
}
#[inline]
pub fn get_level(&self) -> Level {
self.pin.get_level()
}
#[inline]
pub async fn wait_for_high(&mut self) {
self.pin.wait_for_high().await;
}
#[inline]
pub async fn wait_for_low(&mut self) {
self.pin.wait_for_low().await;
}
#[inline]
pub async fn wait_for_rising_edge(&mut self) {
self.pin.wait_for_rising_edge().await;
}
#[inline]
pub async fn wait_for_falling_edge(&mut self) {
self.pin.wait_for_falling_edge().await;
}
#[inline]
pub async fn wait_for_any_edge(&mut self) {
self.pin.wait_for_any_edge().await;
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InterruptTrigger {
LevelLow,
LevelHigh,
EdgeLow,
EdgeHigh,
AnyEdge,
}
pub(crate) unsafe fn init(gpio1_it_priority: interrupt::Priority) {
unsafe {
interrupt::GPIO1.disable();
interrupt::GPIO1.set_priority(gpio1_it_priority);
interrupt::GPIO1.enable();
}
crate::rcc::enable::<peripherals::HPSYS_GPIO>();
}
#[cfg(feature = "rt")]
fn irq_handler<const N: usize>(wakers: &[AtomicWaker; N]) {
let gpio = crate::pac::HPSYS_GPIO;
let isr0 = gpio.isr0().read().0;
let isr1 = gpio.isr1().read().0;
let status = (isr1 as u64) << 32 | isr0 as u64;
if status == 0 {
return;
}
for pin_idx in BitIter64(status) {
let mut pin = HpsysPin::new(pin_idx);
pin.disable_interrupt();
pin.clear_interrupt();
wakers[pin_idx as usize].wake();
}
}
#[cfg(feature = "rt")]
#[interrupt]
fn GPIO1() {
irq_handler(&PA_WAKERS);
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
struct InputFuture<'d> {
pin: PeripheralRef<'d, AnyPin>,
}
impl<'d> InputFuture<'d> {
fn new(pin: PeripheralRef<'d, AnyPin>, trigger: InterruptTrigger) -> Self {
let mut hpsys_pin = HpsysPin::new(pin.pin_bank());
hpsys_pin.set_interrupt_trigger(trigger);
hpsys_pin.clear_interrupt();
hpsys_pin.enable_interrupt();
hpsys_pin.set_high();
Self { pin }
}
}
impl<'d> Drop for InputFuture<'d> {
fn drop(&mut self) {
}
}
impl<'d> Future for InputFuture<'d> {
type Output = ();
fn poll(self: FuturePin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let pin_idx = self.pin.pin() as usize;
let hpsys_pin = HpsysPin::new(self.pin.pin_bank());
PA_WAKERS[pin_idx].register(cx.waker());
if hpsys_pin.is_interrupt_disabled() {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
pub struct Output<'d> {
pin: Flex<'d>,
}
impl<'d> Output<'d> {
#[inline]
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
let mut pin = Flex::new(pin);
pin.set_level(initial_output);
pin.set_as_output();
Self { pin }
}
#[inline]
pub fn set_drive_strength(&mut self, strength: Drive) {
self.pin.set_drive_strength(strength)
}
#[inline]
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
self.pin.set_slew_rate(slew_rate)
}
#[inline]
pub fn set_high(&mut self) {
self.pin.set_high()
}
#[inline]
pub fn set_low(&mut self) {
self.pin.set_low()
}
#[inline]
pub fn set_level(&mut self, level: Level) {
self.pin.set_level(level)
}
#[inline]
pub fn is_set_high(&self) -> bool {
self.pin.is_set_high()
}
#[inline]
pub fn is_set_low(&self) -> bool {
self.pin.is_set_low()
}
#[inline]
pub fn get_output_level(&self) -> Level {
self.pin.get_output_level()
}
#[inline]
pub fn toggle(&mut self) {
self.pin.toggle()
}
}
pub struct OutputOpenDrain<'d> {
pin: Flex<'d>,
}
impl<'d> OutputOpenDrain<'d> {
#[inline]
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
let mut pin = Flex::new(pin);
pin.set_low();
pin.set_as_output_od();
pin.set_level(initial_output);
Self { pin }
}
#[inline]
pub fn set_pullup(&mut self, enable: bool) {
if enable {
self.pin.set_pull(Pull::Up);
} else {
self.pin.set_pull(Pull::None);
}
}
#[inline]
pub fn set_drive_strength(&mut self, strength: Drive) {
self.pin.set_drive_strength(strength)
}
#[inline]
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
self.pin.set_slew_rate(slew_rate)
}
#[inline]
pub fn set_high(&mut self) {
self.pin.set_as_input()
}
#[inline]
pub fn set_low(&mut self) {
self.pin.set_as_output()
}
#[inline]
pub fn set_level(&mut self, level: Level) {
match level {
Level::Low => self.set_low(),
Level::High => self.set_high(),
}
}
#[inline]
pub fn is_set_high(&self) -> bool {
!self.is_set_low()
}
#[inline]
pub fn is_set_low(&self) -> bool {
self.pin.is_set_as_output()
}
#[inline]
pub fn get_output_level(&self) -> Level {
self.is_set_high().into()
}
#[inline]
pub fn toggle(&mut self) {
self.pin.toggle()
}
#[inline]
pub fn is_high(&self) -> bool {
self.pin.is_high()
}
#[inline]
pub fn is_low(&self) -> bool {
self.pin.is_low()
}
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
#[inline]
pub async fn wait_for_high(&mut self) {
self.pin.wait_for_high().await;
}
#[inline]
pub async fn wait_for_low(&mut self) {
self.pin.wait_for_low().await;
}
#[inline]
pub async fn wait_for_rising_edge(&mut self) {
self.pin.wait_for_rising_edge().await;
}
#[inline]
pub async fn wait_for_falling_edge(&mut self) {
self.pin.wait_for_falling_edge().await;
}
#[inline]
pub async fn wait_for_any_edge(&mut self) {
self.pin.wait_for_any_edge().await;
}
}
pub struct Analog<'d> {
_pin: Flex<'d>,
}
impl<'d> Analog<'d> {
#[inline]
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
let mut pin = Flex::new(pin);
pin.set_as_analog();
Self { _pin: pin}
}
}
pub struct Flex<'d> {
pub(crate) pin: PeripheralRef<'d, AnyPin>,
inner: HpsysPin,
}
impl<'d> Flex<'d> {
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
into_ref!(pin);
let mut flex = Self {
inner: HpsysPin::new(pin.pin_bank()),
pin: pin.map_into(),
};
flex.disable_interrupt();
unsafe { flex.inner.set_fsel_unchecked(0) };
flex
}
pub fn new_without_init(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
into_ref!(pin);
Self {
inner: HpsysPin::new(pin.pin_bank()),
pin: pin.map_into(),
}
}
pub fn disable_interrupt(&mut self) {
self.inner.disable_interrupt();
self.inner.clear_interrupt();
}
pub fn set_as_output(&mut self) {
self.inner.set_as_output();
}
pub fn set_as_input(&mut self) {
self.inner.set_as_input();
}
pub fn set_as_output_od(&mut self) {
self.inner.set_as_output_od();
}
pub fn set_as_analog(&mut self) {
self.inner.set_as_analog();
}
pub fn is_set_as_output(&self) -> bool {
self.inner.is_set_as_output()
}
pub fn set_high(&mut self) {
self.inner.set_high();
}
pub fn set_low(&mut self) {
self.inner.set_low();
}
pub fn toggle(&mut self) {
self.inner.toggle();
}
pub fn get_output_level(&self) -> Level {
self.inner.get_output_level()
}
pub fn get_level(&self) -> Level {
self.inner.get_level()
}
pub fn set_level(&mut self, level: Level) {
self.inner.set_level(level)
}
pub fn is_high(&self) -> bool {
self.inner.is_high()
}
pub fn is_low(&self) -> bool {
self.inner.is_low()
}
pub fn is_set_high(&self) -> bool {
self.inner.is_set_high()
}
pub fn is_set_low(&self) -> bool {
self.inner.is_set_low()
}
pub fn set_pull(&mut self, pull: Pull) {
self.inner.set_pull(pull);
}
pub fn set_drive_strength(&mut self, drive: Drive) {
self.inner.set_drive_strength(drive);
}
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
self.inner.set_slew_rate(slew_rate);
}
pub fn set_schmitt(&mut self, enable: bool) {
self.inner.set_schmitt(enable);
}
pub fn set_function(&mut self, fsel: u8, af_type: crate::gpio::AfType) {
self.inner.set_function(fsel, af_type);
}
#[inline]
pub async fn wait_for_high(&mut self) {
if self.is_high() {
return;
}
InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelHigh).await;
}
#[inline]
pub async fn wait_for_low(&mut self) {
if self.is_low() {
return;
}
InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelLow).await;
}
#[inline]
pub async fn wait_for_rising_edge(&mut self) {
InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeHigh).await;
}
#[inline]
pub async fn wait_for_falling_edge(&mut self) {
InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeLow).await;
}
#[inline]
pub async fn wait_for_any_edge(&mut self) {
InputFuture::new(self.pin.reborrow(), InterruptTrigger::AnyEdge).await;
}
}
impl<'d> Drop for Flex<'d> {
#[inline]
fn drop(&mut self) {
self.pin.set_as_disconnected();
}
}
pub struct AfType {
pub(crate) pull: Pull,
}
impl AfType {
pub(crate) fn new(pull: Pull) -> Self {
Self { pull }
}
}
pub(crate) trait SealedPin: Sized {
fn pin_bank(&self) -> u8;
#[inline]
fn _pin(&self) -> u8 {
self.pin_bank() & 0x7f
}
#[inline]
fn _bank(&self) -> u8 {
self.pin_bank() >> 7
}
#[inline]
fn set_as_disconnected(&self) {
let mut pin = HpsysPin::new(self.pin_bank());
pin.disable_interrupt();
pin.clear_interrupt();
pin.set_ipr(false, false);
unsafe { pin.set_fsel_unchecked(0) };
pin.set_drive_strength(Drive::Drive0);
pin.set_pull(Pull::None);
}
#[inline]
fn set_function(&self, fsel: u8, af_type: AfType) {
let mut pin = HpsysPin::new(self.pin_bank());
pin.set_function(fsel, af_type);
}
}
#[allow(private_bounds)]
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + SealedPin + Sized + 'static {
fn degrade(self) -> AnyPin {
AnyPin {
pin_bank: self.pin_bank(),
}
}
#[inline]
fn pin(&self) -> u8 {
self._pin()
}
#[inline]
fn bank(&self) -> u8 {
self._bank()
}
}
pub struct AnyPin {
pin_bank: u8,
}
impl AnyPin {
pub unsafe fn steal(pin_bank: u8) -> Self {
Self { pin_bank }
}
}
impl_peripheral!(AnyPin);
impl Pin for AnyPin {}
impl SealedPin for AnyPin {
fn pin_bank(&self) -> u8 {
self.pin_bank
}
}
macro_rules! impl_pin {
($name:ident, $bank:expr, $pin_num:expr) => {
impl Pin for peripherals::$name {}
impl SealedPin for peripherals::$name {
#[inline]
fn pin_bank(&self) -> u8 {
($bank as u8) * 128 + $pin_num
}
}
impl From<peripherals::$name> for crate::gpio::AnyPin {
fn from(val: peripherals::$name) -> Self {
crate::gpio::Pin::degrade(val)
}
}
};
}
mod eh02 {
use super::*;
impl<'d> embedded_hal_02::digital::v2::InputPin for Input<'d> {
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
}
}
impl<'d> embedded_hal_02::digital::v2::OutputPin for Output<'d> {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Output<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
impl<'d> embedded_hal_02::digital::v2::InputPin for OutputOpenDrain<'d> {
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
}
}
impl<'d> embedded_hal_02::digital::v2::OutputPin for OutputOpenDrain<'d> {
type Error = Infallible;
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for OutputOpenDrain<'d> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for OutputOpenDrain<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
impl<'d> embedded_hal_02::digital::v2::InputPin for Flex<'d> {
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
}
}
impl<'d> embedded_hal_02::digital::v2::OutputPin for Flex<'d> {
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Input<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::InputPin for Input<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Output<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::OutputPin for Output<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Output<'d> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}
impl<'d> embedded_hal_1::digital::ErrorType for OutputOpenDrain<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::OutputPin for OutputOpenDrain<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_1::digital::StatefulOutputPin for OutputOpenDrain<'d> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}
impl<'d> embedded_hal_1::digital::InputPin for OutputOpenDrain<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}
impl<'d> embedded_hal_1::digital::ErrorType for Flex<'d> {
type Error = Infallible;
}
impl<'d> embedded_hal_1::digital::InputPin for Flex<'d> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
}
}
impl<'d> embedded_hal_1::digital::OutputPin for Flex<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Flex<'d> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}
impl<'d> embedded_hal_async::digital::Wait for Flex<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}
impl<'d> embedded_hal_async::digital::Wait for Input<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}
impl<'d> embedded_hal_async::digital::Wait for OutputOpenDrain<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}