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

225 lines
5.4 KiB
Rust
Raw Normal View History

2021-11-24 03:18:30 +01:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
2023-05-30 00:10:36 +02:00
#[path = "../common.rs"]
mod common;
2021-11-24 03:18:30 +01:00
2023-05-30 00:10:36 +02:00
use common::*;
2021-11-24 03:18:30 +01:00
use defmt::assert;
use embassy_executor::Spawner;
2022-07-10 21:08:12 +02:00
use embassy_stm32::gpio::{Flex, Input, Level, Output, OutputOpenDrain, Pull, Speed};
2021-11-24 03:18:30 +01:00
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(config());
2021-11-24 03:18:30 +01:00
info!("Hello World!");
2021-12-07 00:28:32 +01:00
// Arduino pins D0 and D1
// They're connected together with a 1K resistor.
let mut a = peri!(p, UART_RX);
let mut b = peri!(p, UART_TX);
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());
assert!(!b.is_high());
assert!(a.is_set_low());
assert!(!a.is_set_high());
2021-11-24 03:18:30 +01:00
}
{
let mut a = Output::new(&mut a, Level::High, Speed::Low);
2021-12-07 05:00:35 +01:00
delay();
assert!(!b.is_low());
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test is_set_low / is_set_high
a.set_low();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.set_high();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_high());
// Test toggle
a.toggle();
delay();
assert!(b.is_low());
assert!(a.is_set_low());
assert!(!a.is_set_high());
a.toggle();
delay();
assert!(b.is_high());
assert!(!a.is_set_low());
assert!(a.is_set_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
{
2022-07-10 21:08:12 +02:00
//Flex pin configured as input
let mut b = Flex::new(&mut b);
2022-07-08 21:59:09 +02:00
b.set_as_input(Pull::None);
{
2022-07-10 21:08:12 +02:00
//Flex pin configured as output
let mut a = Flex::new(&mut a); //Flex pin configured as output
2022-07-08 22:24:29 +02:00
a.set_low(); // Pin state must be set before configuring the pin, thus we avoid unknown state
2022-07-08 21:59:09 +02:00
a.set_as_output(Speed::Low);
delay();
assert!(b.is_low());
}
{
2022-07-10 21:08:12 +02:00
//Flex pin configured as output
let mut a = Flex::new(&mut a);
2022-07-08 21:59:09 +02:00
a.set_high();
2022-07-08 22:24:29 +02:00
a.set_as_output(Speed::Low);
2022-07-08 21:59:09 +02:00
delay();
assert!(b.is_high());
}
}
2022-07-08 22:02:49 +02:00
2022-07-08 21:59:09 +02:00
// Test input no pull
{
2022-07-10 21:08:12 +02:00
let mut b = Flex::new(&mut b);
b.set_as_input(Pull::None); // no pull, the status is undefined
2022-07-08 21:59:09 +02:00
2022-07-08 22:02:49 +02:00
let mut a = Flex::new(&mut a);
a.set_low();
2022-07-10 21:08:12 +02:00
a.set_as_output(Speed::Low);
2022-07-08 22:34:17 +02:00
2022-07-08 21:59:09 +02:00
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
// Test input pulldown
{
2022-07-08 22:08:14 +02:00
let mut b = Flex::new(&mut b);
2022-07-10 21:08:12 +02:00
b.set_as_input(Pull::Down);
2022-07-08 21:59:09 +02:00
delay();
assert!(b.is_low());
2022-07-08 22:10:52 +02:00
2022-07-08 22:08:14 +02:00
let mut a = Flex::new(&mut a);
a.set_low();
a.set_as_output(Speed::Low);
2022-07-08 21:59:09 +02:00
delay();
assert!(b.is_low());
a.set_high();
delay();
assert!(b.is_high());
}
2022-07-08 22:10:52 +02:00
2022-07-08 21:59:09 +02:00
// Test input pullup
{
2022-07-08 22:10:52 +02:00
let mut b = Flex::new(&mut b);
2022-07-10 21:08:12 +02:00
b.set_as_input(Pull::Up);
2022-07-08 21:59:09 +02:00
delay();
2022-07-08 22:10:52 +02:00
assert!(b.is_high());
2022-07-08 21:59:09 +02:00
2022-07-08 22:10:52 +02:00
let mut a = Flex::new(&mut a);
2022-07-08 21:59:09 +02:00
a.set_high();
2022-07-08 22:10:52 +02:00
a.set_as_output(Speed::Low);
2022-07-08 21:59:09 +02:00
delay();
assert!(b.is_high());
2022-07-08 22:10:52 +02:00
a.set_low();
delay();
assert!(b.is_low());
2022-07-08 21:59:09 +02:00
}
2022-07-08 22:16:01 +02:00
2022-07-08 21:59:09 +02:00
// Test output open drain
{
2022-07-08 22:16:01 +02:00
let mut b = Flex::new(&mut b);
b.set_as_input(Pull::Down);
2022-07-08 21:59:09 +02:00
2022-07-08 22:16:01 +02:00
let mut a = Flex::new(&mut a);
a.set_low();
2022-07-10 21:08:12 +02:00
a.set_as_input_output(Speed::Low, Pull::None);
2022-07-08 21:59:09 +02:00
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);
}