embassy/tests/stm32/src/bin/gpio.rs

191 lines
4.6 KiB
Rust
Raw Normal View History

2021-11-24 03:18:30 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use defmt::assert;
use embassy::executor::Spawner;
2022-07-08 21:59:09 +02:00
use embassy_stm32::gpio::{Input, Level, Output, OutputOpenDrain, Flex, Pull, Speed};
2021-11-24 03:18:30 +01:00
use embassy_stm32::Peripherals;
use example_common::*;
2021-12-07 05:00:35 +01:00
#[embassy::main(config = "config()")]
2021-11-24 03:18:30 +01:00
async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World!");
2021-12-07 00:28:32 +01:00
// Arduino pins D0 and D1
// They're connected together with a 1K resistor.
#[cfg(feature = "stm32f103c8")]
let (mut a, mut b) = (p.PA9, p.PA10);
2021-12-07 00:28:32 +01:00
#[cfg(feature = "stm32g491re")]
let (mut a, mut b) = (p.PC4, p.PC5);
#[cfg(feature = "stm32g071rb")]
let (mut a, mut b) = (p.PC4, p.PC5);
#[cfg(feature = "stm32f429zi")]
2021-11-24 03:18:30 +01:00
let (mut a, mut b) = (p.PG14, p.PG9);
#[cfg(feature = "stm32wb55rg")]
let (mut a, mut b) = (p.PA3, p.PA2);
#[cfg(feature = "stm32h755zi")]
let (mut a, mut b) = (p.PB6, p.PB7);
#[cfg(feature = "stm32u585ai")]
let (mut a, mut b) = (p.PD9, p.PD8);
2021-11-24 03:18:30 +01:00
// Test initial output
{
let b = Input::new(&mut b, Pull::None);
{
let _a = Output::new(&mut a, Level::Low, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_low());
2021-11-24 03:18:30 +01:00
}
{
let _a = Output::new(&mut a, Level::High, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_high());
2021-11-24 03:18:30 +01:00
}
}
// Test input no pull
{
let b = Input::new(&mut b, Pull::None);
// no pull, the status is undefined
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_low());
a.set_high();
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_high());
2021-11-24 03:18:30 +01:00
}
// Test input pulldown
{
let b = Input::new(&mut b, Pull::Down);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_low());
2021-11-24 03:18:30 +01:00
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_low());
a.set_high();
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_high());
2021-11-24 03:18:30 +01:00
}
// Test input pullup
{
let b = Input::new(&mut b, Pull::Up);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_high());
2021-11-24 03:18:30 +01:00
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_low());
a.set_high();
2021-12-07 05:00:35 +01:00
delay();
assert!(b.is_high());
2021-11-24 03:18:30 +01:00
}
2022-07-08 21:59:09 +02:00
// Test output open drain
{
let b = Input::new(&mut b, Pull::Down);
// no pull, the status is undefined
let mut a = OutputOpenDrain::new(&mut a, Level::Low, Speed::Low, Pull::None);
delay();
assert!(b.is_low());
a.set_high(); // High-Z output
delay();
assert!(b.is_low());
}
// FLEX
// Test initial output
{
let mut b = Flex::new(&mut b);
b.set_as_input(Pull::None);
{
let mut a = Flex::new(&mut a);
a.set_low();
a.set_as_output(Speed::Low);
delay();
assert!(b.is_low());
}
{
let mut a = Flex::new(&mut a);
a.set_as_output(Speed::Low);
a.set_high();
delay();
assert!(b.is_high());
}
}
/*
// Test input no pull
{
let b = Input::new(&mut b, Pull::None);
// no pull, the status is undefined
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pulldown
{
let b = Input::new(&mut b, Pull::Down);
delay();
assert!(b.is_low());
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pullup
{
let b = Input::new(&mut b, Pull::Up);
delay();
assert!(b.is_high());
let mut a = Output::new(&mut a, Level::Low, Speed::Low);
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test output open drain
{
let b = Input::new(&mut b, Pull::Down);
// no pull, the status is undefined
let mut a = OutputOpenDrain::new(&mut a, Level::Low, Speed::Low, Pull::None);
delay();
assert!(b.is_low());
a.set_high(); // High-Z output
delay();
assert!(b.is_low());
}
*/
2021-11-24 03:18:30 +01:00
info!("Test OK");
cortex_m::asm::bkpt();
}
2021-12-07 05:00:35 +01:00
fn delay() {
#[cfg(feature = "stm32h755zi")]
cortex_m::asm::delay(10000);
#[cfg(not(feature = "stm32h755zi"))]
cortex_m::asm::delay(1000);
}