Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F4 gpio #1

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ authors = ["Vitaly Domnikov <[email protected]>"]
categories = ["embedded", "hardware-support", "no-std"]
description = "Peripheral access API for STM32C0 series microcontrollers"
documentation = "https://docs.rs/stm32c0xx-hal"
edition = "2018"
edition = "2021"
keywords = ["arm", "cortex-m", "stm32c0xx", "hal"]
license = "MIT/Apache-2.0"
name = "stm32c0xx-hal"
readme = "README.md"
repository = "https://github.com/stm32-rs/stm32c0xx-hal"
version = "0.0.0"
rust-version = "1.65"

[package.metadata.docs.rs]
features = ["stm32c031", "rt"]
Expand Down Expand Up @@ -46,8 +47,20 @@ panic-semihosting = "0.5.6"
default = ["i2c-blocking"]
device-selected = []
rt = ["stm32c0/rt"]
stm32c011 = ["stm32c0/stm32c011", "device-selected"]
stm32c031 = ["stm32c0/stm32c031", "device-selected"]
stm32c011 = ["stm32c0/stm32c011", "device-selected", "gpio-c0xx"]
stm32c031 = ["stm32c0/stm32c031", "device-selected", "gpio-c0xx_453"]

gpio-c0xx = ["gpiof"]
gpio-c0xx_453 = ["gpiod", "gpiof"]
#gpio-c0xx = [
# "debug", "gpioa", "gpiob", "gpioc", "gpiof", "i2c1", "i2s", "i2s1", "ir", "rcc", "spi1", "tim1", "tim14", "tim16", "tim17", "tim3", "usart1", "usart2",
#]
#gpio-c0xx_453 = [
# "debug", "gpioa", "gpiob", "gpioc", "gpiod", "gpiof", "i2c1", "i2s", "i2s1", "ir", "rcc", "spi1", "tim1", "tim14", "tim16", "tim17", "tim3", "usart1", "usart2",
#]

gpiod = []
gpiof = []

i2c-blocking = []
i2c-nonblocking = []
Expand All @@ -60,3 +73,7 @@ debug = false
codegen-units = 1
incremental = false
lto = false

[[example]]
name = "i2c"
required-features = ["gpio-c0xx_453"]
2 changes: 1 addition & 1 deletion examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> ! {
let mut led = port_a.pa5.into_push_pull_output();

loop {
led.toggle().ok();
led.toggle();
for _ in 0..1_000_000 {
cortex_m::asm::nop();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/blinky_delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ fn main() -> ! {
let mut delay = dp.TIM3.delay(&mut rcc);
loop {
delay.delay(500.millis());
led.toggle().ok();
led.toggle();
}
}
2 changes: 1 addition & 1 deletion examples/blinky_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> ! {
timer.start(500.millis());

loop {
led.toggle().unwrap();
led.toggle();
block!(timer.wait()).unwrap();
}
}
6 changes: 3 additions & 3 deletions examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ fn main() -> ! {
let mut led = port_a.pa5.into_push_pull_output();

loop {
if button.is_high().unwrap_or_default() {
led.set_low().ok();
if button.is_high() {
led.set_low();
} else {
led.set_high().ok();
led.set_high();
}
}
}
10 changes: 5 additions & 5 deletions examples/clockout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ fn main() -> ! {
let mut rcc = dp.RCC.freeze(Config::hsi(Prescaler::NotDivided));
let gpioa = dp.GPIOA.split(&mut rcc);

let mut mco =
gpioa
.pa9
.set_speed(Speed::VeryHigh)
.mco(MCOSrc::SysClk, Prescaler::Div2, &mut rcc);
let mut mco = gpioa.pa9.into_alternate().speed(Speed::VeryHigh).mco(
MCOSrc::SysClk,
Prescaler::Div2,
&mut rcc,
);
mco.enable();

let mut lsco = gpioa.pa2.lsco(LSCOSrc::LSE, &mut rcc);
Expand Down
2 changes: 1 addition & 1 deletion examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> ! {
let sda = gpiob.pb9.into_open_drain_output_in_state(PinState::High);
let scl = gpiob.pb8.into_open_drain_output_in_state(PinState::High);

let mut i2c = dp.I2C.i2c(sda, scl, Config::new(400.kHz()), &mut rcc);
let mut i2c = dp.I2C.i2c((scl, sda), Config::new(400.kHz()), &mut rcc);

i2c.write(0x2a, &[0x80, 0xff]).unwrap();
i2c.write(0x2a, &[0x01, 0x04, 0x00, 0x00]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod app {

#[task(binds = TIM17, shared = [timer], local = [led])]
fn timer_tick(mut ctx: timer_tick::Context) {
ctx.local.led.toggle().ok();
ctx.local.led.toggle();
ctx.shared.timer.lock(|tim| tim.clear_irq());
}

Expand Down
2 changes: 1 addition & 1 deletion examples/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn main() -> ! {
let mut watchdog = dp.WWDG.constrain(&mut rcc);
// let mut watchdog = dp.IWDG.constrain();

led.set_high().ok();
led.set_high();
watchdog.start(20.millis());

loop {}
Expand Down
3 changes: 3 additions & 0 deletions src/analog/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ adc_pin! {
Channel12: (PA12<Analog>, 12u8),
Channel13: (PA13<Analog>, 13u8),
Channel14: (PA14<Analog>, 14u8),
}
#[cfg(feature = "gpio-c0xx_453")]
adc_pin! {
Channel17: (PB0<Analog>, 17u8),
Channel18: (PB1<Analog>, 18u8),
Channel19: (PB2<Analog>, 19u8),
Expand Down
30 changes: 29 additions & 1 deletion src/exti.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! External interrupt controller
use crate::gpio::SignalEdge;
use crate::gpio::{Input, Pin, PinExt, PinMode, SignalEdge};
use crate::stm32::EXTI;

/// EXTI trigger event
Expand Down Expand Up @@ -27,6 +27,7 @@ pub enum Event {
LSE_CSS = 31,
}

#[allow(unused)]
impl Event {
pub(crate) fn from_code(n: u8) -> Event {
match n {
Expand Down Expand Up @@ -121,3 +122,30 @@ impl ExtiExt for EXTI {
}
}
}

impl<const P: char, const N: u8, MODE: PinMode> Pin<P, N, MODE> {
/// Configures the pin as external trigger
pub fn listen(self, edge: SignalEdge, exti: &mut EXTI) -> Pin<P, N, Input> {
let pin = self.into_mode();
let offset = (N % 4) * 8;
let mask = (pin.port_id() as u32) << offset;
let reset = !(0xff << offset);
match N as u8 {
0..=3 => exti
.exticr1
.modify(|r, w| unsafe { w.bits(r.bits() & reset | mask) }),
4..=7 => exti
.exticr2
.modify(|r, w| unsafe { w.bits(r.bits() & reset | mask) }),
8..=11 => exti
.exticr3
.modify(|r, w| unsafe { w.bits(r.bits() & reset | mask) }),
12..=16 => exti
.exticr4
.modify(|r, w| unsafe { w.bits(r.bits() & reset | mask) }),
_ => unreachable!(),
}
exti.listen(Event::from_code(N), edge);
pin
}
}
Loading