blue_pill_more/src/main.rs

215 lines
6.7 KiB
Rust

#![no_std]
#![cfg_attr(not(doc), no_main)]
use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};
use cortex_m_rt::entry;
use cortex_m::interrupt::Mutex;
use stm32f1xx_hal::{
afio,
device::{EXTI, NVIC},
gpio::{
gpioa::{PA8, PA9},
Edge, ExtiPin, Floating, Input,
},
i2c::{BlockingI2c, DutyCycle, Mode},
pac::{interrupt, CorePeripherals, Interrupt, Peripherals},
prelude::*,
};
use switch_hal::{InputSwitch, IntoSwitch, OutputSwitch, ToggleableOutputSwitch};
use ssd1306::{prelude::*, I2CDisplayInterface, Ssd1306};
use core::cell::RefCell;
mod rotary;
use rotary::{Direction, Rotary};
mod hello_screen;
use hello_screen::{HelloEvent, HelloDisplay};
type ClkPin = PA8<Input<Floating>>;
type DtPin = PA9<Input<Floating>>;
static CLK_PIN: Mutex<RefCell<Option<ClkPin>>> = Mutex::new(RefCell::new(None));
static DT_PIN: Mutex<RefCell<Option<DtPin>>> = Mutex::new(RefCell::new(None));
static COUNT: Mutex<RefCell<i32>> = Mutex::new(RefCell::new(0));
const DISPLAY_W: i32 = 128;
const DISPLAY_H: i32 = 64;
#[entry]
fn main() -> ! {
// Init buffers for debug printing
rtt_init_print!();
// Get access to the core peripherals from the cortex-m crate
let mut core_periph = CorePeripherals::take().unwrap();
// Get access to the device specific peripherals from the peripheral access crate
let dev_periph = Peripherals::take().unwrap();
// RCC is the primary clock control peripheral, but FLASH is also involved in setting
// up clock speed because the wait states must be increased at higher clock rates
let rcc = dev_periph.RCC.constrain();
let mut flash = dev_periph.FLASH.constrain();
let mut afio = dev_periph.AFIO.constrain();
// Peripherals often need to know the clock settings to be properly configured, so
// we configure the clock and "freeze" the configuration so we can pass it
let clocks = rcc
.cfgr
.use_hse(8.MHz()) // Use High Speed External 8Mhz crystal oscillator
.sysclk(72.MHz()) // Use the PLL to multiply SYSCLK to 72MHz
.hclk(72.MHz()) // Leave AHB prescaler at /1
.pclk1(36.MHz()) // Use the APB1 prescaler to divide the clock to 36MHz (max supported)
.pclk2(72.MHz()) // Leave the APB2 prescaler at /1
.adcclk(12.MHz()) // ADC prescaler of /6 (max speed of 14MHz, but /4 gives 18MHz)
.freeze(&mut flash.acr);
// In order to have precisely-timed delays, we can use the core SysTick clock as a
// delay provider
let mut delay = core_periph.SYST.delay(&clocks);
// Acquire the necessary gpio peripherals
let mut gpioa = dev_periph.GPIOA.split();
let mut gpiob = dev_periph.GPIOB.split();
let mut gpioc = dev_periph.GPIOC.split();
// Configure the rotary encoder pins A8, A9 and switch pin A10
let clk = gpioa.pa8.into_floating_input(&mut gpioa.crh);
let dt = gpioa.pa9.into_floating_input(&mut gpioa.crh);
let sw = gpioa
.pa10
.into_floating_input(&mut gpioa.crh)
.into_active_low_switch();
// Set up the rotary encoder pins for use with the interrupt
init_encoder_pins(clk, dt, &mut afio, &dev_periph.EXTI);
// Configure pin C13 to drive the "PC13" LED as an active-low switch
let mut led = gpioc
.pc13
.into_push_pull_output(&mut gpioc.crh)
.into_active_low_switch();
// Configure the I2C pins we are using for the display to the correct mode
let scl = gpiob.pb10.into_alternate_open_drain(&mut gpiob.crh);
let sda = gpiob.pb11.into_alternate_open_drain(&mut gpiob.crh);
// Very brief delay before starting up i2c; otherwise the startup process
// could hang.
delay.delay_us(10_u8);
// Configure the I2C peripheral itself
let i2c = BlockingI2c::i2c2(
dev_periph.I2C2,
(scl, sda),
Mode::Fast {
frequency: 400_000.Hz(),
duty_cycle: DutyCycle::Ratio2to1,
},
clocks,
1000,
10,
1000,
1000,
);
// Initialize the display
let interface = I2CDisplayInterface::new(i2c);
let mut display = Ssd1306::new(interface, DisplaySize128x64, DisplayRotation::Rotate0)
.into_buffered_graphics_mode();
display.init().unwrap();
// Every set of commands to the display is buffered until we flush it
display.flush().unwrap();
// Enable interrupts
unsafe {
core_periph.NVIC.set_priority(Interrupt::EXTI9_5, 1);
NVIC::unmask(Interrupt::EXTI9_5);
}
NVIC::unpend(Interrupt::EXTI9_5);
rprintln!("Finished init, starting main loop");
// Create the hello UI
let mut hello: HelloDisplay<DISPLAY_W, DISPLAY_H> = HelloDisplay::new();
// Turn the LED on via the OutputPin trait
led.on().unwrap();
let mut button_last = false;
let mut counter_last = 0;
// Microcontroller programs never exit main, so we must loop!
loop {
hello.draw(&mut display).unwrap();
display.flush().unwrap();
hello.event(HelloEvent::Tick);
// Check our inputs
let button = sw.is_active().unwrap();
if button_last && !button {
led.toggle().unwrap();
hello.event(HelloEvent::Button);
}
button_last = button;
let counter = cortex_m::interrupt::free(|cs| *COUNT.borrow(cs).borrow());
if counter_last != counter {
hello.event(HelloEvent::Knob(counter - counter_last));
}
counter_last = counter;
}
}
fn init_encoder_pins(
mut clk: PA8<Input<Floating>>,
dt: PA9<Input<Floating>>,
afio: &mut afio::Parts,
exti: &EXTI,
) {
cortex_m::interrupt::free(|cs| {
clk.make_interrupt_source(afio);
clk.trigger_on_edge(exti, Edge::RisingFalling);
clk.enable_interrupt(exti);
CLK_PIN.borrow(cs).replace(Some(clk));
DT_PIN.borrow(cs).replace(Some(dt));
});
}
#[interrupt]
fn EXTI9_5() {
static mut ENC: Option<Rotary<ClkPin, DtPin>> = None;
let enc = ENC.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
let clk = CLK_PIN.borrow(cs).replace(None).unwrap();
let dt = DT_PIN.borrow(cs).replace(None).unwrap();
Rotary::new(clk, dt)
})
});
if enc.pin_a.check_interrupt() {
match enc.update().unwrap() {
Direction::Clockwise => cortex_m::interrupt::free(|cs| {
COUNT.borrow(cs).replace_with(|count| count.wrapping_add(1));
}),
Direction::CounterClockwise => cortex_m::interrupt::free(|cs| {
COUNT
.borrow(cs)
.replace_with(|count| count.wrapping_add(-1));
}),
Direction::None => {}
};
enc.pin_a.clear_interrupt_pending_bit();
}
}