1#![no_std]
12#![allow(unused_imports)]
13
14extern crate cortex_m;
15pub extern crate stm32l0x1_hal as hal;
16
17use cortex_m::peripheral::syst::SystClkSource;
18use hal::common::Constrain;
19use hal::flash::{self, *};
20use hal::gpio::{self, *};
21use hal::i2c::{self, *};
22use hal::power::{self, *};
23use hal::rcc::clocking::*;
24use hal::rcc::{self, *};
25use hal::serial::{self, *};
26use hal::time::{self, *};
27
28use stm32l0::stm32l0x1;
29use stm32l0::stm32l0x1::*;
30
31pub type Led = gpio::PB3<Output<PushPull, Floating>>;
33
34pub struct Pins {
36 pub d1: PA9<Analog>,
39 pub d0: PA10<Analog>,
41 pub d2: PA12<Analog>,
43 pub d3: PB0<Analog>,
45 pub d4: PB7<Analog>,
47 pub d5: PB6<Analog>,
49 pub d6: PB1<Analog>,
51 pub d7: PC14<Analog>,
53 pub d8: PC15<Analog>,
55 pub d9: PA8<Analog>,
57 pub d10: PA11<Analog>,
59 pub d11: PB5<Analog>,
61 pub d12: PB4<Analog>,
63
64 pub d13: PB3<Analog>,
67 pub a0: PA0<Analog>,
69 pub a1: PA1<Analog>,
71 pub a2: PA3<Analog>,
73 pub a3: PA4<Analog>,
75 pub a4: PA5<Analog>,
77 pub a5: PA6<Analog>,
79 pub a6: PA7<Analog>,
81 pub a7: PA2<Analog>,
83}
84
85pub struct Board<VDD, VCORE, RTC> {
87 pub pwr: Power<VDD, VCORE, RTC>,
89 pub flash: Flash,
91 pub rcc: Rcc,
93}
94
95pub fn init<VCORE>(pwr: PWR, flash: FLASH, rcc: RCC) -> Board<VddHigh, VCORE, RtcDis>
97where
98 VCORE: Vos + FreqLimit + Latency,
99{
100 let pwr: Power<VddHigh, VCoreRange2, RtcDis> = pwr.constrain();
101 let pwr = pwr.into_vcore_range::<VCORE>();
102 let flash = flash.constrain();
103 let rcc = hal::rcc::as_default(rcc);
104
105 Board { pwr, flash, rcc }
106}
107
108impl<VDD, VCORE, RTC> Board<VDD, VCORE, RTC> {
109 pub fn pins(&mut self, gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC) -> Pins {
111 let gpioa = gpio::A::new(gpioa, &mut self.rcc.iop);
112 let gpiob = gpio::B::new(gpiob, &mut self.rcc.iop);
113 let gpioc = gpio::C::new(gpioc, &mut self.rcc.iop);
114
115 Pins {
116 d1: gpioa.PA9,
117 d0: gpioa.PA10,
118 d2: gpioa.PA12,
119 d3: gpiob.PB0,
120 d4: gpiob.PB7,
121 d5: gpiob.PB6,
122 d6: gpiob.PB1,
123 d7: gpioc.PC14,
124 d8: gpioc.PC15,
125 d9: gpioa.PA8,
126 d10: gpioa.PA11,
127 d11: gpiob.PB5,
128 d12: gpiob.PB4,
129
130 d13: gpiob.PB3,
131 a0: gpioa.PA0,
132 a1: gpioa.PA1,
133 a2: gpioa.PA3,
134 a3: gpioa.PA4,
135 a4: gpioa.PA5,
136 a5: gpioa.PA6,
137 a6: gpioa.PA7,
138 a7: gpioa.PA2,
139 }
140 }
141
142 pub fn systick_start(&mut self, syst: &mut SYST, src: SystClkSource, ticks: u32) {
144 syst.set_clock_source(src);
145 syst.set_reload(ticks);
146 syst.clear_current();
147 syst.enable_counter();
148 syst.enable_interrupt();
149 }
150
151 pub fn user_led<T>(&mut self, d13: PB3<T>) -> Led {
153 d13.into_output::<PushPull, Floating>()
154 }
155
156 pub fn vcp_usart<T>(
158 &mut self,
159 usart: USART2,
160 tx_pin_a7: PA2<T>,
161 clk_src: USARTClkSource,
162 ) -> Serial<USART2, (PA2<AF::AF4>, PA15<AF::AF4>)> {
163 let gpioa = gpio::A::new(
165 unsafe { stm32l0x1::Peripherals::steal() }.GPIOA,
166 &mut self.rcc.iop,
167 );
168 let rx_pin = gpioa.PA15;
169
170 let vcp_tx: PA2<AF::AF4> = tx_pin_a7
171 .into_output::<PushPull, Floating>()
172 .into_alt_fun::<AF::AF4>();
173 vcp_tx.set_pin_speed(PinSpeed::VeryHigh);
174
175 let vcp_rx: PA15<AF::AF4> = rx_pin
176 .into_output::<PushPull, Floating>()
177 .into_alt_fun::<AF::AF4>();
178 vcp_rx.set_pin_speed(PinSpeed::VeryHigh);
179
180 usart2::rs232(
181 usart,
182 (vcp_tx, vcp_rx),
183 Bps(115200),
184 clk_src,
185 self.rcc.cfgr.context().unwrap(),
186 &mut self.rcc.apb1,
187 &mut self.rcc.ccipr,
188 )
189 }
190
191 pub fn i2c1<C, A>(
193 &mut self,
194 i2c1: I2C1,
195 pins: (PB6<C>, PB7<A>),
196 ) -> I2c<I2C1, (PB6<AF::AF1>, PB7<AF::AF1>)> {
197 let i2c_sda = pins
198 .1
199 .into_output::<OpenDrain, PullUp>()
200 .into_alt_fun::<AF::AF1>();
201 i2c_sda.set_pin_speed(PinSpeed::VeryHigh);
202
203 let i2c_scl = pins
204 .0
205 .into_output::<OpenDrain, PullUp>()
206 .into_alt_fun::<AF::AF1>();
207 i2c_scl.set_pin_speed(PinSpeed::VeryHigh);
208
209 i2c::I2c::i2c1(
210 i2c1,
211 (i2c_scl, i2c_sda),
212 i2c::I2cClkSrc::HSI16,
214 0x00303D5B, &mut self.rcc.apb1,
217 &mut self.rcc.ccipr,
218 )
219 }
220}