#![no_std]
#![allow(unused_imports)]
extern crate cortex_m;
pub extern crate stm32l0x1_hal as hal;
use cortex_m::peripheral::syst::SystClkSource;
use hal::common::Constrain;
use hal::flash::{self, *};
use hal::gpio::{self, *};
use hal::i2c::{self, *};
use hal::power::{self, *};
use hal::rcc::clocking::*;
use hal::rcc::{self, *};
use hal::serial::{self, *};
use hal::time::{self, *};
use stm32l0::stm32l0x1;
use stm32l0::stm32l0x1::*;
pub type Led = gpio::PB3<Output<PushPull, Floating>>;
pub struct Pins {
pub d1: PA9<Analog>,
pub d0: PA10<Analog>,
pub d2: PA12<Analog>,
pub d3: PB0<Analog>,
pub d4: PB7<Analog>,
pub d5: PB6<Analog>,
pub d6: PB1<Analog>,
pub d7: PC14<Analog>,
pub d8: PC15<Analog>,
pub d9: PA8<Analog>,
pub d10: PA11<Analog>,
pub d11: PB5<Analog>,
pub d12: PB4<Analog>,
pub d13: PB3<Analog>,
pub a0: PA0<Analog>,
pub a1: PA1<Analog>,
pub a2: PA3<Analog>,
pub a3: PA4<Analog>,
pub a4: PA5<Analog>,
pub a5: PA6<Analog>,
pub a6: PA7<Analog>,
pub a7: PA2<Analog>,
}
pub struct Board<VDD, VCORE, RTC> {
pub pwr: Power<VDD, VCORE, RTC>,
pub flash: Flash,
pub rcc: Rcc,
}
pub fn init<VCORE>(pwr: PWR, flash: FLASH, rcc: RCC) -> Board<VddHigh, VCORE, RtcDis>
where
VCORE: Vos + FreqLimit + Latency,
{
let pwr: Power<VddHigh, VCoreRange2, RtcDis> = pwr.constrain();
let pwr = pwr.into_vcore_range::<VCORE>();
let flash = flash.constrain();
let rcc = hal::rcc::as_default(rcc);
Board { pwr, flash, rcc }
}
impl<VDD, VCORE, RTC> Board<VDD, VCORE, RTC> {
pub fn pins(&mut self, gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC) -> Pins {
let gpioa = gpio::A::new(gpioa, &mut self.rcc.iop);
let gpiob = gpio::B::new(gpiob, &mut self.rcc.iop);
let gpioc = gpio::C::new(gpioc, &mut self.rcc.iop);
Pins {
d1: gpioa.PA9,
d0: gpioa.PA10,
d2: gpioa.PA12,
d3: gpiob.PB0,
d4: gpiob.PB7,
d5: gpiob.PB6,
d6: gpiob.PB1,
d7: gpioc.PC14,
d8: gpioc.PC15,
d9: gpioa.PA8,
d10: gpioa.PA11,
d11: gpiob.PB5,
d12: gpiob.PB4,
d13: gpiob.PB3,
a0: gpioa.PA0,
a1: gpioa.PA1,
a2: gpioa.PA3,
a3: gpioa.PA4,
a4: gpioa.PA5,
a5: gpioa.PA6,
a6: gpioa.PA7,
a7: gpioa.PA2,
}
}
pub fn systick_start(&mut self, syst: &mut SYST, src: SystClkSource, ticks: u32) {
syst.set_clock_source(src);
syst.set_reload(ticks);
syst.clear_current();
syst.enable_counter();
syst.enable_interrupt();
}
pub fn user_led<T>(&mut self, d13: PB3<T>) -> Led {
d13.into_output::<PushPull, Floating>()
}
pub fn vcp_usart<T>(
&mut self,
usart: USART2,
tx_pin_a7: PA2<T>,
clk_src: USARTClkSource,
) -> Serial<USART2, (PA2<AF::AF4>, PA15<AF::AF4>)> {
let gpioa = gpio::A::new(
unsafe { stm32l0x1::Peripherals::steal() }.GPIOA,
&mut self.rcc.iop,
);
let rx_pin = gpioa.PA15;
let vcp_tx: PA2<AF::AF4> = tx_pin_a7
.into_output::<PushPull, Floating>()
.into_alt_fun::<AF::AF4>();
vcp_tx.set_pin_speed(PinSpeed::VeryHigh);
let vcp_rx: PA15<AF::AF4> = rx_pin
.into_output::<PushPull, Floating>()
.into_alt_fun::<AF::AF4>();
vcp_rx.set_pin_speed(PinSpeed::VeryHigh);
usart2::rs232(
usart,
(vcp_tx, vcp_rx),
Bps(115200),
clk_src,
self.rcc.cfgr.context().unwrap(),
&mut self.rcc.apb1,
&mut self.rcc.ccipr,
)
}
pub fn i2c1<C, A>(
&mut self,
i2c1: I2C1,
pins: (PB6<C>, PB7<A>),
) -> I2c<I2C1, (PB6<AF::AF1>, PB7<AF::AF1>)> {
let i2c_sda = pins
.1
.into_output::<OpenDrain, PullUp>()
.into_alt_fun::<AF::AF1>();
i2c_sda.set_pin_speed(PinSpeed::VeryHigh);
let i2c_scl = pins
.0
.into_output::<OpenDrain, PullUp>()
.into_alt_fun::<AF::AF1>();
i2c_scl.set_pin_speed(PinSpeed::VeryHigh);
i2c::I2c::i2c1(
i2c1,
(i2c_scl, i2c_sda),
i2c::I2cClkSrc::HSI16,
0x00303D5B,
&mut self.rcc.apb1,
&mut self.rcc.ccipr,
)
}
}