Skip to content

Commit 4817854

Browse files
authored
Merge pull request #3 from david-sawatzke/stm32f030
[WIP] Add implementation for stm32f030
2 parents 705bbb3 + ff670cf commit 4817854

File tree

9 files changed

+156
-25
lines changed

9 files changed

+156
-25
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ rust:
55
cache: cargo
66
env:
77
- MCU=stm32f042
8+
- MCU=stm32f030
9+
- MCU=stm32f030x6
10+
- MCU=stm32f030x8
11+
- MCU=stm32f030xc
812
matrix:
913
allow_failures:
1014
- rust: nightly

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ panic-halt = "0.2.0"
4444
[features]
4545
rt = ["stm32f0/rt"]
4646
stm32f042 = ["stm32f0/stm32f0x2"]
47+
stm32f030 = ["stm32f0/stm32f0x0"]
48+
stm32f030x4 = ["stm32f030x6"]
49+
stm32f030x6 = ["stm32f030"]
50+
stm32f030x8 = ["stm32f030", "stm32f0/stm32f0x0"]
51+
stm32f030xc = ["stm32f030", "stm32f0/stm32f0x0"]
4752

4853
[profile.dev]
4954
debug = true
@@ -52,3 +57,7 @@ debug = true
5257
debug = true
5358
lto = true
5459
opt-level = "s"
60+
61+
[[example]]
62+
name = "led_hal_button_irq"
63+
required-features = ["stm32f042"]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ API for the STMicro STM32F0xx family of microcontrollers. It replaces the
77

88
Currently supported configuration are:
99
* stm32f042
10+
* stm32f030
11+
* stm32f030x4
12+
* stm32f030x6
13+
* stm32f030x8
14+
* stm32f030xc
1015

1116
The idea behind this crate is to gloss over the slight differences in the
1217
various peripherals available on those MCUs so a HAL can be written for all

src/gpio.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ macro_rules! gpio {
450450
}
451451
}
452452

453+
#[cfg(any(feature = "stm32f042", feature = "stm32f030",))]
453454
gpio!(GPIOA, gpioa, iopaen, PA, [
454455
PA0: (pa0, 0, Input<Floating>),
455456
PA1: (pa1, 1, Input<Floating>),
@@ -469,6 +470,7 @@ gpio!(GPIOA, gpioa, iopaen, PA, [
469470
PA15: (pa15, 15, Input<Floating>),
470471
]);
471472

473+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
472474
gpio!(GPIOB, gpiob, iopben, PB, [
473475
PB0: (pb0, 0, Input<Floating>),
474476
PB1: (pb1, 1, Input<Floating>),
@@ -488,14 +490,53 @@ gpio!(GPIOB, gpiob, iopben, PB, [
488490
PB15: (pb15, 15, Input<Floating>),
489491
]);
490492

493+
#[cfg(feature = "stm32f042")]
491494
gpio!(GPIOC, gpioc, iopcen, PC, [
492495
PC13: (pc13, 13, Input<Floating>),
493496
PC14: (pc14, 14, Input<Floating>),
494497
PC15: (pc15, 15, Input<Floating>),
495498
]);
496499

500+
#[cfg(feature = "stm32f030")]
501+
gpio!(GPIOC, gpioc, iopcen, PC, [
502+
PC0: (pb0, 0, Input<Floating>),
503+
PC1: (pb1, 1, Input<Floating>),
504+
PC2: (pb2, 2, Input<Floating>),
505+
PC3: (pb3, 3, Input<Floating>),
506+
PC4: (pb4, 4, Input<Floating>),
507+
PC5: (pb5, 5, Input<Floating>),
508+
PC6: (pb6, 6, Input<Floating>),
509+
PC7: (pb7, 7, Input<Floating>),
510+
PC8: (pb8, 8, Input<Floating>),
511+
PC9: (pb9, 9, Input<Floating>),
512+
PC10: (pb10, 10, Input<Floating>),
513+
PC11: (pb11, 11, Input<Floating>),
514+
PC12: (pb12, 12, Input<Floating>),
515+
PC13: (pb13, 13, Input<Floating>),
516+
PC14: (pb14, 14, Input<Floating>),
517+
PC15: (pb15, 15, Input<Floating>),
518+
]);
519+
520+
// TODO Check if the bit is implemented yet
521+
// In the device crate the iopden bit is missing, so it won't compile
522+
// #[cfg(feature = "stm32f030")]
523+
// gpio!(GPIOD, gpiod, iopden, PD, [
524+
// PD2: (pd2, 2, Input<Floating>),
525+
// ]);
526+
527+
#[cfg(feature = "stm32f042")]
497528
gpio!(GPIOF, gpiof, iopfen, PF, [
498529
PF0: (pf0, 0, Input<Floating>),
499530
PF1: (pf1, 1, Input<Floating>),
500531
PF11: (pf11, 11, Input<Floating>),
501532
]);
533+
534+
#[cfg(feature = "stm32f030")]
535+
gpio!(GPIOF, gpiof, iopfen, PF, [
536+
PF0: (pf0, 0, Input<Floating>),
537+
PF1: (pf1, 1, Input<Floating>),
538+
PF4: (pf4, 4, Input<Floating>),
539+
PF5: (pf5, 5, Input<Floating>),
540+
PF6: (pf6, 5, Input<Floating>),
541+
PF7: (pf7, 5, Input<Floating>),
542+
]);

src/i2c.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
#[cfg(feature = "stm32f042")]
12
use crate::stm32::{I2C1, RCC};
23

34
use embedded_hal::blocking::i2c::{Write, WriteRead};
45

5-
use core::cmp;
6+
#[cfg(feature = "stm32f042")]
67
use crate::gpio::gpioa::{PA10, PA11, PA12, PA9};
8+
#[cfg(feature = "stm32f042")]
79
use crate::gpio::gpiob::{PB10, PB11, PB13, PB14, PB6, PB7, PB8, PB9};
10+
#[cfg(feature = "stm32f042")]
811
use crate::gpio::gpiof::{PF0, PF1};
12+
#[cfg(feature = "stm32f042")]
913
use crate::gpio::{Alternate, AF1, AF4, AF5};
1014
use crate::time::{KiloHertz, U32Ext};
15+
use core::cmp;
1116

1217
/// I2C abstraction
1318
pub struct I2c<I2C, PINS> {
@@ -17,12 +22,19 @@ pub struct I2c<I2C, PINS> {
1722

1823
pub trait Pins<I2c> {}
1924

25+
#[cfg(feature = "stm32f042")]
2026
impl Pins<I2C1> for (PA9<Alternate<AF4>>, PA10<Alternate<AF4>>) {}
27+
#[cfg(feature = "stm32f042")]
2128
impl Pins<I2C1> for (PA11<Alternate<AF5>>, PA12<Alternate<AF5>>) {}
29+
#[cfg(feature = "stm32f042")]
2230
impl Pins<I2C1> for (PB6<Alternate<AF1>>, PB7<Alternate<AF1>>) {}
31+
#[cfg(feature = "stm32f042")]
2332
impl Pins<I2C1> for (PB8<Alternate<AF1>>, PB9<Alternate<AF1>>) {}
33+
#[cfg(feature = "stm32f042")]
2434
impl Pins<I2C1> for (PB10<Alternate<AF1>>, PB11<Alternate<AF1>>) {}
35+
#[cfg(feature = "stm32f042")]
2536
impl Pins<I2C1> for (PB13<Alternate<AF5>>, PB14<Alternate<AF5>>) {}
37+
#[cfg(feature = "stm32f042")]
2638
impl Pins<I2C1> for (PF1<Alternate<AF1>>, PF0<Alternate<AF1>>) {}
2739

2840
#[derive(Debug)]
@@ -31,6 +43,7 @@ pub enum Error {
3143
NACK,
3244
}
3345

46+
#[cfg(feature = "stm32f042")]
3447
impl<PINS> I2c<I2C1, PINS> {
3548
pub fn i2c1(i2c: I2C1, pins: PINS, speed: KiloHertz) -> Self
3649
where
@@ -123,6 +136,7 @@ impl<PINS> I2c<I2C1, PINS> {
123136
}
124137
}
125138

139+
#[cfg(feature = "stm32f042")]
126140
impl<PINS> WriteRead for I2c<I2C1, PINS> {
127141
type Error = Error;
128142

@@ -200,6 +214,7 @@ impl<PINS> WriteRead for I2c<I2C1, PINS> {
200214
}
201215
}
202216

217+
#[cfg(feature = "stm32f042")]
203218
impl<PINS> Write for I2c<I2C1, PINS> {
204219
type Error = Error;
205220

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub use stm32f0;
1010
#[cfg(feature = "stm32f042")]
1111
pub use stm32f0::stm32f0x2 as stm32;
1212

13+
#[cfg(feature = "stm32f030")]
14+
pub use stm32f0::stm32f0x0 as stm32;
15+
1316
// Enable use of interrupt macro
1417
#[cfg(feature = "rt")]
1518
pub use stm32f0::interrupt;

src/rcc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use core::cmp;
22

3-
use cast::u32;
3+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
44
use crate::stm32::{FLASH, RCC};
5+
use cast::u32;
56

67
use crate::time::Hertz;
78

@@ -11,6 +12,7 @@ pub trait RccExt {
1112
fn constrain(self) -> Rcc;
1213
}
1314

15+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
1416
impl RccExt for RCC {
1517
fn constrain(self) -> Rcc {
1618
Rcc {
@@ -36,6 +38,7 @@ pub struct CFGR {
3638
sysclk: Option<u32>,
3739
}
3840

41+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
3942
impl CFGR {
4043
pub fn hclk<F>(mut self, freq: F) -> Self
4144
where

src/serial.rs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use embedded_hal::prelude::*;
66
use nb::block;
77
use void::Void;
88

9+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
910
use crate::stm32::{RCC, USART1, USART2};
1011

11-
use crate::gpio::gpioa::{PA10, PA14, PA15, PA2, PA3, PA9};
12-
use crate::gpio::gpiob::{PB6, PB7};
13-
use crate::gpio::{Alternate, AF0, AF1};
12+
use crate::gpio::*;
1413
use crate::rcc::Clocks;
1514
use crate::time::Bps;
1615

@@ -39,15 +38,42 @@ pub enum Error {
3938

4039
pub trait Pins<USART> {}
4140

42-
impl Pins<USART1> for (PA9<Alternate<AF1>>, PA10<Alternate<AF1>>) {}
43-
impl Pins<USART1> for (PB6<Alternate<AF0>>, PB7<Alternate<AF0>>) {}
44-
impl Pins<USART1> for (PA9<Alternate<AF1>>, PB7<Alternate<AF0>>) {}
45-
impl Pins<USART1> for (PB6<Alternate<AF0>>, PA10<Alternate<AF1>>) {}
46-
47-
impl Pins<USART2> for (PA2<Alternate<AF1>>, PA3<Alternate<AF1>>) {}
48-
impl Pins<USART2> for (PA2<Alternate<AF1>>, PA15<Alternate<AF1>>) {}
49-
impl Pins<USART2> for (PA14<Alternate<AF1>>, PA15<Alternate<AF1>>) {}
50-
impl Pins<USART2> for (PA14<Alternate<AF1>>, PA3<Alternate<AF1>>) {}
41+
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
42+
impl Pins<USART1> for (gpioa::PA9<Alternate<AF1>>, gpioa::PA10<Alternate<AF1>>) {}
43+
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
44+
impl Pins<USART1> for (gpiob::PB6<Alternate<AF0>>, gpiob::PB7<Alternate<AF0>>) {}
45+
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
46+
impl Pins<USART1> for (gpioa::PA9<Alternate<AF1>>, gpiob::PB7<Alternate<AF0>>) {}
47+
#[cfg(any(feature = "stm32f030", feature = "stm32f042"))]
48+
impl Pins<USART1> for (gpiob::PB6<Alternate<AF0>>, gpioa::PA10<Alternate<AF1>>) {}
49+
50+
#[cfg(feature = "stm32f030x6")]
51+
impl Pins<USART1> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
52+
53+
#[cfg(any(
54+
feature = "stm32f042",
55+
feature = "stm32f030x8",
56+
feature = "stm32f030xc",
57+
))]
58+
impl Pins<USART2> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
59+
#[cfg(any(
60+
feature = "stm32f042",
61+
feature = "stm32f030x8",
62+
feature = "stm32f030xc",
63+
))]
64+
impl Pins<USART2> for (gpioa::PA2<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>) {}
65+
#[cfg(any(
66+
feature = "stm32f042",
67+
feature = "stm32f030x8",
68+
feature = "stm32f030xc",
69+
))]
70+
impl Pins<USART2> for (gpioa::PA14<Alternate<AF1>>, gpioa::PA15<Alternate<AF1>>) {}
71+
#[cfg(any(
72+
feature = "stm32f042",
73+
feature = "stm32f030x8",
74+
feature = "stm32f030xc",
75+
))]
76+
impl Pins<USART2> for (gpioa::PA14<Alternate<AF1>>, gpioa::PA3<Alternate<AF1>>) {}
5177

5278
/// Serial abstraction
5379
pub struct Serial<USART, PINS> {
@@ -66,6 +92,7 @@ pub struct Tx<USART> {
6692
}
6793

6894
/// USART1
95+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
6996
impl<PINS> Serial<USART1, PINS> {
7097
pub fn usart1(usart: USART1, pins: PINS, baud_rate: Bps, clocks: Clocks) -> Self
7198
where
@@ -106,6 +133,7 @@ impl<PINS> Serial<USART1, PINS> {
106133
}
107134
}
108135

136+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
109137
impl embedded_hal::serial::Read<u8> for Rx<USART1> {
110138
type Error = Error;
111139

@@ -130,6 +158,7 @@ impl embedded_hal::serial::Read<u8> for Rx<USART1> {
130158
}
131159
}
132160

161+
#[cfg(any(feature = "stm32f042", feature = "stm32f030"))]
133162
impl embedded_hal::serial::Write<u8> for Tx<USART1> {
134163
type Error = Void;
135164

@@ -160,6 +189,11 @@ impl embedded_hal::serial::Write<u8> for Tx<USART1> {
160189
}
161190

162191
/// USART2
192+
#[cfg(any(
193+
feature = "stm32f042",
194+
feature = "stm32f030x8",
195+
feature = "stm32f030x8"
196+
))]
163197
impl<PINS> Serial<USART2, PINS> {
164198
pub fn usart2(usart: USART2, pins: PINS, baud_rate: Bps, clocks: Clocks) -> Self
165199
where
@@ -200,6 +234,11 @@ impl<PINS> Serial<USART2, PINS> {
200234
}
201235
}
202236

237+
#[cfg(any(
238+
feature = "stm32f042",
239+
feature = "stm32f030x8",
240+
feature = "stm32f030x8"
241+
))]
203242
impl embedded_hal::serial::Read<u8> for Rx<USART2> {
204243
type Error = Error;
205244

@@ -224,6 +263,11 @@ impl embedded_hal::serial::Read<u8> for Rx<USART2> {
224263
}
225264
}
226265

266+
#[cfg(any(
267+
feature = "stm32f042",
268+
feature = "stm32f030x8",
269+
feature = "stm32f030x8"
270+
))]
227271
impl embedded_hal::serial::Write<u8> for Tx<USART2> {
228272
type Error = Void;
229273

0 commit comments

Comments
 (0)