Skip to content

Commit 3b4e718

Browse files
committed
Set up peripherals.
Need to amend HAL to support SPI Peripheral mode (it is Controller only at the moment).
1 parent bf1d3d1 commit 3b4e718

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

build.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// This is the build-script for the Neotron BMC.
22
///
3-
/// It just copies the memory.x file somewhere Cargo can fine it.
3+
/// It just copies the memory.x file somewhere Cargo can find it, then generates a version header.
44
use std::env;
55
use std::fs::File;
66
use std::io::Write;
@@ -15,4 +15,14 @@ fn main() {
1515
.unwrap();
1616
println!("cargo:rustc-link-search={}", out.display());
1717
println!("cargo:rerun-if-changed=memory.x");
18+
19+
20+
let version_output = std::process::Command::new("git")
21+
.current_dir(env::var_os("CARGO_MANIFEST_DIR").unwrap())
22+
.args(&["describe", "--tags", "--all", "--dirty"])
23+
.output()
24+
.expect("running git-describe");
25+
assert!(version_output.status.success());
26+
27+
std::fs::write(out.join("version.txt"), version_output.stdout).expect("writing version file");
1828
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use core::sync::atomic::{AtomicUsize, Ordering};
44

55
use defmt_rtt as _; // global logger
66
use stm32f0xx_hal as _; // memory layout
7-
8-
use panic_probe as _;
7+
use panic_probe as _; // panic handler
98

109
// same panicking *behavior* as `panic-probe` but doesn't print a panic message
1110
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked

src/main.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
11
#![no_main]
22
#![no_std]
33

4-
use neotron_bmc as _; // global logger + panicking-behavior + memory layout
4+
use neotron_bmc as _;
5+
6+
use stm32f0xx_hal as hal;
7+
use hal::{prelude::*, pac, spi, i2c, serial};
8+
9+
static VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/version.txt"));
10+
11+
const MODE_0: spi::Mode = spi::Mode {
12+
polarity: spi::Polarity::IdleLow,
13+
phase: spi::Phase::CaptureOnFirstTransition,
14+
};
515

616
#[cortex_m_rt::entry]
717
fn main() -> ! {
8-
defmt::info!("Hello, world!");
18+
defmt::info!("Neotron BMC version {:?} booting", VERSION);
19+
20+
let p = pac::Peripherals::take().unwrap();
21+
22+
let mut flash = p.FLASH;
23+
let mut rcc = p.RCC.configure().freeze(&mut flash);
24+
25+
let gpioa = p.GPIOA.split(&mut rcc);
26+
let gpiob = p.GPIOB.split(&mut rcc);
27+
28+
let (sck, miso, mosi, scl, sda, tx, rx) = cortex_m::interrupt::free(move |cs| {
29+
(
30+
// SPI pins
31+
gpioa.pa5.into_alternate_af0(cs),
32+
gpioa.pa6.into_alternate_af0(cs),
33+
gpioa.pa7.into_alternate_af0(cs),
34+
// I²C pins
35+
gpioa.pa9.into_alternate_af4(cs),
36+
gpioa.pa10.into_alternate_af4(cs),
37+
// USART pins
38+
gpiob.pb6.into_alternate_af0(cs),
39+
gpiob.pb7.into_alternate_af0(cs),
40+
)
41+
});
42+
43+
// Configure SPI with 1MHz rate
44+
let spi = spi::Spi::spi1(p.SPI1, (sck, miso, mosi), MODE_0, 1.mhz(), &mut rcc);
45+
let serial = serial::Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
46+
let i2c = i2c::I2c::i2c1(p.I2C1, (scl, sda), 100.khz(), &mut rcc);
947

1048
neotron_bmc::exit()
1149
}

0 commit comments

Comments
 (0)