Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmt-rtt = "0.2.0"
cortex-m-rtic = "0.5"
panic-probe = { version = "0.2.0", features = ["print-defmt"] }
stm32f0xx-hal = { version = "0.17", features = ["stm32f031", "rt"] }

debouncr = "0.2"

[features]
# set logging levels here
Expand Down
201 changes: 166 additions & 35 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,81 @@
#![no_main]
#![no_std]

use neotron_bmc as _;

use neotron_bmc::monotonic::{Instant, Tim3Monotonic, U16Ext};

use cortex_m_rt::exception;

///! Neotron BMC Firmware
///!
///! This is the firmware for the Neotron Board Management Controller (BMC). It controls the power, reset, UART and PS/2 ports on a Neotron mainboard.
///! For more details, see the `README.md` file.
///!
///! # Licence
///! This source code as a whole is licensed under the GPL v3. Third-party crates are covered by their respective licences.
use cortex_m::interrupt::free as disable_interrupts;
use rtic::app;

use stm32f0xx_hal::{
gpio::gpioa::{PA10, PA11, PA12, PA9},
gpio::gpiob::{PB0, PB1},
gpio::{Alternate, Output, PushPull, AF1},
gpio::gpiof::{PF0, PF1},
gpio::{Alternate, Input, Output, PullUp, PushPull, AF1},
pac,
prelude::*,
serial,
};

use cortex_m::interrupt::free as disable_interrupts;
use neotron_bmc as _;
use neotron_bmc::monotonic::{Instant, Tim3Monotonic, U16Ext};

/// Version string auto-generated by git.
static VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/version.txt"));

const PERIOD_MS: u16 = 1000;
/// At what rate do we blink the status LED when we're running?
const LED_PERIOD_MS: u16 = 1000;

/// How often we poll the power and reset buttons in milliseconds.
const DEBOUNCE_POLL_INTERVAL_MS: u16 = 75;

/// The states we can be in controlling the DC power
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(u8)]
pub enum DcPowerState {
/// We've just enabled the DC power (so ignore any incoming long presses!)
Starting = 1,
/// We are now fully on. Look for a long press to turn off.
On = 2,
/// We are fully off.
Off = 0,
}

#[app(device = crate::pac, peripherals = true, monotonic = crate::Tim3Monotonic)]
const APP: () = {
struct Resources {
uart_cts: PA11<Alternate<AF1>>,
uart_rts: PA12<Alternate<AF1>>,
/// The power LED (D1101)
led_power: PB0<Output<PushPull>>,
/// The status LED (D1102)
led_status: PB1<Output<PushPull>>,
/// The FTDI UART header (J105)
serial: serial::Serial<pac::USART1, PA9<Alternate<AF1>>, PA10<Alternate<AF1>>>,
/// The Clear-To-Send line on the FTDI UART header (which the serial object can't handle)
uart_cts: PA11<Alternate<AF1>>,
/// The Ready-To-Receive line on the FTDI UART header (which the serial object can't handle)
uart_rts: PA12<Alternate<AF1>>,
/// The power button
button_power: PF0<Input<PullUp>>,
/// The reset button
button_reset: PF1<Input<PullUp>>,
/// Tracks power button state for short presses. 75ms x 2 = 150ms is a short press
button_power_short_press: debouncr::Debouncer<u8, debouncr::Repeat2>,
/// Tracks power button state for long presses. 75ms x 16 = 1200ms is a long press
button_power_long_press: debouncr::Debouncer<u16, debouncr::Repeat16>,
/// Tracks DC power state
dc_power_enabled: DcPowerState,
}

#[init(spawn = [led_status_blink])]
/// The entry point to our application.
///
/// Sets up the hardware and spawns the regular tasks.
///
/// * Task `led_status_blink` - blinks the LED
/// * Task `button_poll` - checks the power and reset buttons
#[init(spawn = [led_status_blink, button_poll])]
fn init(ctx: init::Context) -> init::LateResources {
defmt::info!("Neotron BMC version {:?} booting", VERSION);

Expand All @@ -55,17 +96,28 @@ const APP: () = {
defmt::info!("Creating pins...");
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);
let (uart_tx, uart_rx, uart_cts, uart_rts, mut led_power, led_status) =
disable_interrupts(|cs| {
(
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
gpioa.pa11.into_alternate_af1(cs),
gpioa.pa12.into_alternate_af1(cs),
gpiob.pb0.into_push_pull_output(cs),
gpiob.pb1.into_push_pull_output(cs),
)
});
let gpiof = dp.GPIOF.split(&mut rcc);
let (
uart_tx,
uart_rx,
uart_cts,
uart_rts,
mut led_power,
led_status,
button_power,
button_reset,
) = disable_interrupts(|cs| {
(
gpioa.pa9.into_alternate_af1(cs),
gpioa.pa10.into_alternate_af1(cs),
gpioa.pa11.into_alternate_af1(cs),
gpioa.pa12.into_alternate_af1(cs),
gpiob.pb0.into_push_pull_output(cs),
gpiob.pb1.into_push_pull_output(cs),
gpiof.pf0.into_pull_up_input(cs),
gpiof.pf1.into_pull_up_input(cs),
)
});

defmt::info!("Creating UART...");

Expand All @@ -76,7 +128,9 @@ const APP: () = {

ctx.spawn.led_status_blink().unwrap();

led_power.set_high().unwrap();
ctx.spawn.button_poll().unwrap();

led_power.set_low().unwrap();

defmt::info!("Init complete!");

Expand All @@ -86,18 +140,31 @@ const APP: () = {
uart_rts,
led_power,
led_status,
button_power,
button_reset,
button_power_short_press: debouncr::debounce_2(false),
button_power_long_press: debouncr::debounce_16(false),
dc_power_enabled: DcPowerState::Off,
}
}

/// Our idle task.
///
/// This task is called when there is nothing else to do. We
/// do a little logging, then put the CPU to sleep waiting for an interrupt.
#[idle(resources = [])]
fn idle(_: idle::Context) -> ! {
fn idle(_ctx: idle::Context) -> ! {
defmt::info!("Idle is running...");
loop {
cortex_m::asm::wfi();
defmt::info!("It is now {}", crate::Instant::now().counts());
defmt::trace!("It is now {}", crate::Instant::now().counts());
}
}

/// This is the USART1 task.
///
/// It fires whenever there is new data received on USART1. We should flag to the host
/// that data is available.
#[task(binds = USART1, resources=[serial])]
fn usart1_interrupt(ctx: usart1_interrupt::Context) {
// Reading the register clears the RX-Not-Empty-Interrupt flag.
Expand All @@ -111,12 +178,16 @@ const APP: () = {
}
}

/// This is the LED blink task.
///
/// This task is called periodically. We check whether the status LED is currently on or off,
/// and set it to the opposite. This makes the LED blink.
#[task(schedule = [led_status_blink], resources = [led_status])]
fn led_status_blink(ctx: led_status_blink::Context) {
// Use the safe local `static mut` of RTIC
static mut LED_STATE: bool = false;

defmt::info!("blink time {}", ctx.scheduled.counts());
defmt::trace!("blink time {}", ctx.scheduled.counts());

if *LED_STATE {
ctx.resources.led_status.set_low().unwrap();
Expand All @@ -125,22 +196,82 @@ const APP: () = {
ctx.resources.led_status.set_high().unwrap();
*LED_STATE = true;
}
let next = ctx.scheduled + PERIOD_MS.millis();
defmt::info!("Next blink at {}", next.counts());
let next = ctx.scheduled + LED_PERIOD_MS.millis();
defmt::trace!("Next blink at {}", next.counts());
ctx.schedule.led_status_blink(next).unwrap();
}

/// This task polls our power and reset buttons.
///
/// We poll them rather than setting up an interrupt as we need to debounce them, which involves waiting a short period and checking them again. Given that we have to do that, we might as well not bother with the interrupt.
#[task(schedule = [button_poll], resources = [led_power, button_power, button_power_short_press, button_power_long_press, dc_power_enabled])]
fn button_poll(ctx: button_poll::Context) {
// Poll button
let pressed: bool = ctx.resources.button_power.is_low().unwrap();

// Update state
let short_edge = ctx.resources.button_power_short_press.update(pressed);
let long_edge = ctx.resources.button_power_long_press.update(pressed);

// Dispatch event
if short_edge == Some(debouncr::Edge::Rising) {
defmt::trace!(
"Power short press in! {}",
*ctx.resources.dc_power_enabled as u8
);
if *ctx.resources.dc_power_enabled == DcPowerState::Off {
*ctx.resources.dc_power_enabled = DcPowerState::Starting;
ctx.resources.led_power.set_high().unwrap();
defmt::info!("Power on!");
// TODO: Enable DC PSU here
// TODO: Start monitoring 3.3V and 5.0V rails here
// TODO: Take system out of reset when 3.3V and 5.0V are good
}
} else if short_edge == Some(debouncr::Edge::Falling) {
defmt::trace!(
"Power short press out! {}",
*ctx.resources.dc_power_enabled as u8
);
match *ctx.resources.dc_power_enabled {
DcPowerState::Starting => {
*ctx.resources.dc_power_enabled = DcPowerState::On;
}
DcPowerState::On => {
// TODO: Tell host that power off was requested
}
DcPowerState::Off => {
// Ignore
}
}
}

if long_edge == Some(debouncr::Edge::Rising) {
defmt::trace!(
"Power long press in! {}",
*ctx.resources.dc_power_enabled as u8
);
if *ctx.resources.dc_power_enabled == DcPowerState::On {
*ctx.resources.dc_power_enabled = DcPowerState::Off;
ctx.resources.led_power.set_low().unwrap();
defmt::info!("Power off!");
// TODO: Put system in reset here
// TODO: Disable DC PSU here
}
}

// Re-schedule the timer interrupt
ctx.schedule
.button_poll(ctx.scheduled + DEBOUNCE_POLL_INTERVAL_MS.millis())
.unwrap();
}

// Let it use the USB interrupt as a generic software interrupt.
extern "C" {
fn USB();
}
};

#[exception]
unsafe fn DefaultHandler(value: i16) {
defmt::panic!("DefaultHandler({})", value);
}

// TODO: Pins we haven't used yet
// SPI pins
// spi_clk: gpioa.pa5.into_alternate_af0(cs),
// spi_cipo: gpioa.pa6.into_alternate_af0(cs),
Expand Down