From f84333ae9f65d627918c73cf259fd4802d19b7d7 Mon Sep 17 00:00:00 2001 From: Jonathan 'theJPster' Pallant Date: Sun, 27 Jun 2021 19:53:56 +0100 Subject: [PATCH 1/2] Add support for power button. Short press turns PSU on. Long press turns PSU off. --- Cargo.toml | 2 +- src/main.rs | 152 ++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 131 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 13821f4..c113ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/main.rs b/src/main.rs index 0fb50f6..0bb0aef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,8 @@ 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, @@ -22,19 +23,47 @@ use cortex_m::interrupt::free as disable_interrupts; static VERSION: &'static str = include_str!(concat!(env!("OUT_DIR"), "/version.txt")); -const PERIOD_MS: u16 = 1000; +const LED_PERIOD_MS: u16 = 1000; +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>, - uart_rts: PA12>, + /// The power LED (D1101) led_power: PB0>, + /// The status LED (D1102) led_status: PB1>, + /// The FTDI UART header (J105) serial: serial::Serial>, PA10>>, + /// The Clear-To-Send line on the FTDI UART header (which the serial object can't handle) + uart_cts: PA11>, + /// The Ready-To-Receive line on the FTDI UART header (which the serial object can't handle) + uart_rts: PA12>, + /// The power button + button_power: PF0>, + /// The reset button + button_reset: PF1>, + /// Tracks power button state for short presses. 75ms x 2 = 150ms is a short press + button_power_short_press: debouncr::Debouncer, + /// Tracks power button state for long presses. 75ms x 16 = 1200ms is a long press + button_power_long_press: debouncr::Debouncer, + /// Tracks DC power state + dc_power_enabled: DcPowerState, } - #[init(spawn = [led_status_blink])] + #[init(spawn = [led_status_blink, button_poll])] fn init(ctx: init::Context) -> init::LateResources { defmt::info!("Neotron BMC version {:?} booting", VERSION); @@ -55,17 +84,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..."); @@ -76,7 +116,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!"); @@ -86,15 +128,20 @@ 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, } } #[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()); } } @@ -116,7 +163,7 @@ const APP: () = { // 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(); @@ -125,11 +172,72 @@ 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(); } + #[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(); From fe93a16c9319c1ddf21322b4c284cbb036e27a69 Mon Sep 17 00:00:00 2001 From: Jonathan 'theJPster' Pallant Date: Sun, 27 Jun 2021 20:05:27 +0100 Subject: [PATCH 2/2] Updating docs. --- src/main.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0bb0aef..159f73a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,15 @@ #![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}, @@ -19,11 +20,16 @@ use stm32f0xx_hal::{ 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")); +/// 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 @@ -63,6 +69,12 @@ const APP: () = { dc_power_enabled: DcPowerState, } + /// 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); @@ -136,6 +148,10 @@ const APP: () = { } } + /// 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(_ctx: idle::Context) -> ! { defmt::info!("Idle is running..."); @@ -145,6 +161,10 @@ const APP: () = { } } + /// 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. @@ -158,6 +178,10 @@ 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 @@ -177,6 +201,9 @@ const APP: () = { 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 @@ -244,11 +271,7 @@ const APP: () = { } }; -#[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),