diff --git a/src/application.rs b/src/application.rs new file mode 100644 index 0000000..a1ed4d8 --- /dev/null +++ b/src/application.rs @@ -0,0 +1,129 @@ +use arrayvec::ArrayString; +use core::convert::Infallible; +use core::fmt::Write; +use cortex_m::asm; +use cortex_m_rt::{entry, exception}; +use embedded_graphics::{ + drawable::Drawable, fonts::Text, image::Image, pixelcolor::BinaryColor, pixelcolor::Rgb565, + prelude::*, primitives::rectangle::Rectangle, style::PrimitiveStyleBuilder, + style::TextStyleBuilder, +}; +use embedded_hal::blocking::spi; +use embedded_hal::digital::v2::{InputPin, OutputPin}; +use profont::{ProFont12Point, ProFont9Point}; +use rtt_target::{rprintln, rtt_init_print}; +use st7735_lcd::Orientation; +use stm32f1xx_hal::{ + delay::Delay, + gpio, pac, + prelude::*, + qei::QeiOptions, + rcc, + spi::{Mode, Phase, Polarity, Spi, Spi1NoRemap}, + stm32, + timer::Timer, +}; +use tinybmp::Bmp; + +pub struct App +where + BLed: OutputPin, + SPI: spi::Transfer, + DispCS: OutputPin, + MaxCS: OutputPin, +{ + delay: Delay, + board_led: BLed, + spi: SPI, + disp_cs: DispCS, + max_cs: MaxCS, +} + +pub fn setup( + cp: cortex_m::peripheral::Peripherals, + dp: stm32::Peripherals, +) -> App< + gpio::gpioc::PC13>, + Spi< + pac::SPI1, + Spi1NoRemap, + ( + gpio::gpioa::PA5>, + gpio::gpioa::PA6>, + gpio::gpioa::PA7>, + ), + >, + gpio::gpioa::PA0>, + gpio::gpioa::PA9>, +> { + // Take ownership over the raw flash and rcc devices and convert them into the corresponding + // HAL structs + let mut flash = dp.FLASH.constrain(); + + let mut rcc = dp.RCC.constrain(); + + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in + // `clocks` + let clocks = rcc + .cfgr + .use_hse(8.mhz()) + .sysclk(72.mhz()) + .pclk1(36.mhz()) + .freeze(&mut flash.acr); + + // Acquire the GPIOC peripheral + let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); + let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); + + // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function + // in order to configure the port. For pins 0-7, crl should be passed instead. + let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); + + let mut delay = Delay::new(cp.SYST, clocks); + + let gpiob = dp.GPIOB.split(&mut rcc.apb2); + let mut afio = dp.AFIO.constrain(&mut rcc.apb2); + + let (_, _, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4); + + let qei = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1).qei( + (pb4, gpiob.pb5), + &mut afio.mapr, + QeiOptions::default(), + ); + + // SPI1 + let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl); + let miso = gpioa.pa6; + let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl); + + let mut disp_cs = gpioa.pa0.into_push_pull_output(&mut gpioa.crl); + let mut max_cs = gpioa.pa9.into_push_pull_output(&mut gpioa.crh); + + let mut rst = gpioa.pa1.into_push_pull_output(&mut gpioa.crl); + let mut dc = gpioa.pa4.into_push_pull_output(&mut gpioa.crl); + let mut disp_led = gpioa.pa8.into_push_pull_output(&mut gpioa.crh); + + disp_led.set_high().unwrap(); + + let mut spi = Spi::spi1( + dp.SPI1, + (sck, miso, mosi), + &mut afio.mapr, + Mode { + polarity: Polarity::IdleLow, + phase: Phase::CaptureOnFirstTransition, + }, + 16.mhz(), + clocks, + &mut rcc.apb2, + ); + + App { + delay: delay, + board_led: led, + spi: spi, + disp_cs: disp_cs, + max_cs: max_cs, + } +} diff --git a/src/main.rs b/src/main.rs index e03eed2..06f3f22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ use stm32f1xx_hal::{ }; use tinybmp::Bmp; +mod application; mod max6675; #[entry] @@ -36,68 +37,10 @@ fn main() -> ! { // Get access to the device specific peripherals from the peripheral access crate let dp = pac::Peripherals::take().unwrap(); - // Take ownership over the raw flash and rcc devices and convert them into the corresponding - // HAL structs - let mut flash = dp.FLASH.constrain(); + let app = application::setup(cp, dp); - let mut rcc = dp.RCC.constrain(); - - // Freeze the configuration of all the clocks in the system and store the frozen frequencies in - // `clocks` - let clocks = rcc - .cfgr - .use_hse(8.mhz()) - .sysclk(72.mhz()) - .pclk1(36.mhz()) - .freeze(&mut flash.acr); - - // Acquire the GPIOC peripheral - let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); - let mut gpioa = dp.GPIOA.split(&mut rcc.apb2); - - // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function - // in order to configure the port. For pins 0-7, crl should be passed instead. - let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); - - let mut delay = Delay::new(cp.SYST, clocks); - - let gpiob = dp.GPIOB.split(&mut rcc.apb2); - let mut afio = dp.AFIO.constrain(&mut rcc.apb2); - - let (_, _, pb4) = afio.mapr.disable_jtag(gpioa.pa15, gpiob.pb3, gpiob.pb4); - - let qei = Timer::tim3(dp.TIM3, &clocks, &mut rcc.apb1).qei( - (pb4, gpiob.pb5), - &mut afio.mapr, - QeiOptions::default(), - ); - - // SPI1 - let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl); - let miso = gpioa.pa6; - let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl); - - let mut disp_cs = gpioa.pa0.into_push_pull_output(&mut gpioa.crl); - let mut max_cs = gpioa.pa9.into_push_pull_output(&mut gpioa.crh); - - let mut rst = gpioa.pa1.into_push_pull_output(&mut gpioa.crl); - let mut dc = gpioa.pa4.into_push_pull_output(&mut gpioa.crl); - let mut disp_led = gpioa.pa8.into_push_pull_output(&mut gpioa.crh); - - disp_led.set_high().unwrap(); - - let mut spi = Spi::spi1( - dp.SPI1, - (sck, miso, mosi), - &mut afio.mapr, - Mode { - polarity: Polarity::IdleLow, - phase: Phase::CaptureOnFirstTransition, - }, - 16.mhz(), - clocks, - &mut rcc.apb2, - ); + loop {} + /* let mut disp = st7735_lcd::ST7735::new(spi, dc, rst, true, false, 160, 128); disp_cs.set_low().unwrap(); @@ -157,6 +100,7 @@ fn main() -> ! { delay.delay_ms(250u16); } + */ } #[exception]