Skip to content

Commit 3db793e

Browse files
committed
First release of stm32f0xx-hal based on stm32f042-hal
Signed-off-by: Daniel Egger <[email protected]>
0 parents  commit 3db793e

26 files changed

+2686
-0
lines changed

.cargo/config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[target.thumbv6m-none-eabi]
2+
runner = 'arm-none-eabi-gdb'
3+
rustflags = [
4+
"-C", "link-arg=-Tlink.x",
5+
]
6+
7+
[build]
8+
target = "thumbv6m-none-eabi"

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
**/*.orig
3+
**/*.rs.bk
4+
Cargo.lock
5+
*.txt

Cargo.toml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[package]
2+
authors = ["Daniel Egger <[email protected]>"]
3+
categories = [
4+
"embedded",
5+
"hardware-support",
6+
"no-std",
7+
]
8+
description = "Peripheral access API for STM32F0 series microcontrollers"
9+
documentation = "https://docs.rs/stm32f0xx-hal"
10+
keywords = [
11+
"arm",
12+
"cortex-m",
13+
"stm32f0xx",
14+
"hal",
15+
]
16+
license = "0BSD"
17+
name = "stm32f0xx-hal"
18+
readme = "README.md"
19+
repository = "https://github.com/stm32-rs/stm32f0xx-hal"
20+
version = "0.7.0"
21+
22+
[dependencies]
23+
bare-metal = { version = "0.2.4", features = ["const-fn"] }
24+
cortex-m = "0.5.8"
25+
cortex-m-rt = "0.6.5"
26+
nb = "0.1.1"
27+
void = { version = "1.0.2", default-features = false }
28+
stm32f0 = "0.4.0"
29+
30+
[dependencies.cast]
31+
default-features = false
32+
version = "0.2.2"
33+
34+
[dependencies.embedded-hal]
35+
features = ["unproven"]
36+
version = "0.2.2"
37+
38+
[dev-dependencies]
39+
ina260 = "0.2.3"
40+
numtoa = "0.2.3"
41+
panic-halt = "0.2.0"
42+
43+
[features]
44+
rt = ["stm32f0/rt"]
45+
stm32f042 = ["stm32f0/stm32f0x2"]
46+
47+
[profile.dev]
48+
debug = true
49+
50+
[profile.release]
51+
debug = true
52+
lto = true
53+
opt-level = "s"

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
stm32f0xx-hal
2+
=============
3+
4+
_stm32f0xx-hal_ contains a hardware abstraction on top of the peripheral access
5+
API for the STMicro STM32F0xx family of microcontrollers. It replaces the
6+
[stm32f042-hal][] by a more ubiqitous version suitable for additional families.
7+
8+
Currently supported configuration are:
9+
* stm32f042
10+
11+
The idea behind this crate is to gloss over the slight differences in the
12+
various peripherals available on those MCUs so a HAL can be written for all
13+
chips in that same family without having to cut and paste crates for every
14+
single model.
15+
16+
Collaboration on this crate is highly welcome as are pull requests!
17+
18+
This crate relies on Adam Greigs fantastic [stm32f0][] crate to provide
19+
appropriate register definitions and implements a partial set of the
20+
[embedded-hal][] traits.
21+
22+
Some of the implementation was shamelessly adapted from the [stm32f103xx-hal][]
23+
crate by Jorge Aparicio.
24+
25+
[stm32f0]: https://crates.io/crates/stm32f0
26+
[stm32f042-hal]: https://github.com/therealprof/stm32f042-hal
27+
[stm32f103xx-hal]: https://github.com/japaric/stm32f103xx-hal
28+
[embedded-hal]: https://github.com/japaric/embedded-hal.git
29+
30+
License
31+
-------
32+
33+
[0-clause BSD license](LICENSE-0BSD.txt).

examples/blinky.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m_rt;
5+
extern crate panic_halt;
6+
7+
extern crate stm32f0xx_hal as hal;
8+
9+
use hal::prelude::*;
10+
use hal::stm32;
11+
12+
use cortex_m_rt::entry;
13+
14+
#[entry]
15+
fn main() -> ! {
16+
if let Some(p) = stm32::Peripherals::take() {
17+
let gpioa = p.GPIOA.split();
18+
19+
/* (Re-)configure PA1 as output */
20+
let mut led = gpioa.pa1.into_push_pull_output();
21+
22+
loop {
23+
/* Turn PA1 on a million times in a row */
24+
for _ in 0..1_000_000 {
25+
led.set_high();
26+
}
27+
/* Then turn PA1 off a million times in a row */
28+
for _ in 0..1_000_000 {
29+
led.set_low();
30+
}
31+
}
32+
}
33+
34+
loop {
35+
continue;
36+
}
37+
}

examples/blinky_delay.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m;
5+
extern crate cortex_m_rt;
6+
extern crate panic_halt;
7+
8+
extern crate stm32f0xx_hal as hal;
9+
10+
use hal::delay::Delay;
11+
use hal::prelude::*;
12+
use hal::stm32;
13+
14+
use cortex_m::peripheral::Peripherals;
15+
use cortex_m_rt::entry;
16+
17+
#[entry]
18+
fn main() -> ! {
19+
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
20+
let gpioa = p.GPIOA.split();
21+
22+
/* (Re-)configure PA1 as output */
23+
let mut led = gpioa.pa1.into_push_pull_output();
24+
25+
/* Constrain clocking registers */
26+
let mut rcc = p.RCC.constrain();
27+
28+
/* Configure clock to 8 MHz (i.e. the default) and freeze it */
29+
let clocks = rcc.cfgr.sysclk(8.mhz()).freeze();
30+
31+
/* Get delay provider */
32+
let mut delay = Delay::new(cp.SYST, clocks);
33+
34+
loop {
35+
led.set_high();
36+
delay.delay_ms(1_000_u16);
37+
38+
led.set_low();
39+
delay.delay_ms(1_000_u16);
40+
}
41+
}
42+
43+
loop {
44+
continue;
45+
}
46+
}

examples/flash_systick.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
extern crate cortex_m;
5+
extern crate cortex_m_rt;
6+
extern crate panic_halt;
7+
8+
extern crate stm32f0xx_hal as hal;
9+
10+
use hal::gpio::*;
11+
use hal::prelude::*;
12+
use hal::stm32;
13+
14+
use cortex_m::interrupt::Mutex;
15+
use cortex_m::peripheral::syst::SystClkSource::Core;
16+
use cortex_m::peripheral::Peripherals;
17+
use cortex_m_rt::{entry, exception};
18+
19+
use core::cell::RefCell;
20+
use core::ops::DerefMut;
21+
22+
static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
23+
24+
#[entry]
25+
fn main() -> ! {
26+
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
27+
let gpioa = p.GPIOA.split();
28+
let mut rcc = p.RCC.constrain();
29+
let _ = rcc.cfgr.sysclk(48.mhz()).freeze();
30+
let mut syst = cp.SYST;
31+
32+
/* (Re-)configure PA1 as output */
33+
let mut led = gpioa.pa1.into_push_pull_output();
34+
35+
cortex_m::interrupt::free(move |cs| {
36+
*GPIO.borrow(cs).borrow_mut() = Some(led);
37+
});
38+
39+
/* Initialise SysTick counter with a defined value */
40+
unsafe { syst.cvr.write(1) };
41+
42+
/* Set source for SysTick counter, here full operating frequency (== 8MHz) */
43+
syst.set_clock_source(Core);
44+
45+
/* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
46+
syst.set_reload(4_000_000 - 1);
47+
48+
/* Start counter */
49+
syst.enable_counter();
50+
51+
/* Start interrupt generation */
52+
syst.enable_interrupt();
53+
}
54+
55+
loop {
56+
continue;
57+
}
58+
}
59+
60+
/* Define an exception, i.e. function to call when exception occurs. Here if our SysTick timer
61+
* trips the flash function will be called and the specified stated passed in via argument */
62+
//, flash, state: u8 = 1);
63+
#[exception]
64+
fn SysTick() -> ! {
65+
static mut state: u8 = 1;
66+
67+
/* Enter critical section */
68+
cortex_m::interrupt::free(|cs| {
69+
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
70+
/* Check state variable, keep LED off most of the time and turn it on every 10th tick */
71+
if *state < 10 {
72+
/* If set turn off the LED */
73+
led.set_low();
74+
75+
/* And now increment state variable */
76+
*state += 1;
77+
} else {
78+
/* If not set, turn on the LED */
79+
led.set_high();
80+
81+
/* And set new state variable back to 0 */
82+
*state = 0;
83+
}
84+
}
85+
});
86+
}

0 commit comments

Comments
 (0)