Created
November 19, 2019 18:59
-
-
Save teburd/67ccd1a2adafbf4c0d1d85e90f2377dd to your computer and use it in GitHub Desktop.
main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/#![deny(warnings)] | |
#![no_std] | |
#![no_main] | |
use cortex_m_semihosting::hprintln; | |
use panic_halt as _; | |
use rtfm::cyccnt::{Instant, U32Ext as _ }; | |
use stm32f4xx_hal::i2c::I2c; | |
use stm32f4xx_hal::prelude::*; | |
use stm32f4xx_hal::stm32; | |
use embedded_graphics::prelude::*; | |
use embedded_graphics::pixelcolor::BinaryColor; | |
use ssd1306::prelude::*; | |
use ssd1306::Builder; | |
const PERIOD: u32 = 8_000_000; | |
#[rtfm::app(device = stm32f4::stm32f446, peripherals = true, monotonic = rtfm::cyccnt::CYCCNT)] | |
const APP: () = { | |
#[init(schedule = [tick])] | |
fn init(mut cx: init::Context) { | |
// Initialize (enable) the monotonic timer (CYCCNT) | |
cx.core.DCB.enable_trace(); | |
// required on devices that software lock the DWT (e.g. STM32F7) | |
unsafe { cx.core.DWT.lar.write(0xC5ACCE55) } | |
cx.core.DWT.enable_cycle_counter(); | |
// semantically, the monotonic timer is frozen at time "zero" during `init` | |
let now = cx.start; // the start time of the system | |
// Schedule `tick` to run 8e6 cycles (clock cycles) in the future | |
cx.schedule.tick(now + PERIOD.cycles()).unwrap(); | |
hprintln!("init @ {:?}", now).unwrap(); | |
let mut flash = cx.device.FLASH.constrain(); | |
let mut rcc = cx.device.RCC.constrain(); | |
let clocks = rcc.cfgr.freeze(&mut flash.acr); | |
let mut gpiob = cx.device.GPIOB.split(&mut rcc.apb2); | |
let scl = gpiob.pb8.into_alternate_open_drain(&mut gpiob.crh); | |
let sda = gpiob.pb9.into_alternate_open_drain(&mut gpiob.crh); | |
let i2c = I2c::i2c1( | |
cx.device.I2C1, | |
(scl, sda), | |
400.khz(), | |
clocks | |
); | |
let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(i2c).into(); | |
disp.init().unwrap(); | |
let im: Image<BinaryColor> = | |
Image::new(include_bytes!("./rust.raw"), 64, 64).translate(Point::new(32, 0)); | |
disp.draw(im.into_iter()); | |
disp.flush().unwrap(); | |
} | |
#[task(schedule = [tick])] | |
fn tick(cx: tick::Context) { | |
let now = Instant::now(); | |
hprintln!("tick(scheduled = {:?}, now = {:?})", cx.scheduled, now).unwrap(); | |
cx.schedule.tick(cx.scheduled + PERIOD.cycles()).unwrap(); | |
} | |
extern "C" { | |
fn UART4(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment