Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
108 lines (94 sloc) 3.11 KB
//! Stores the date and time on a MCP7940N real-time clock (RTC).
//! Then continuously print the date and time.
//!
//! This example is runs on the STM32F3 Discovery board using I2C1.
//!
//! ```
//! F3 <-> MCP7940N <-> Display
//! GND <-> GND <-> GND
//! +3.3V <-> +3.3V
//! +5V <-> +5V
//! PB7 <-> SDA <-> SDA
//! PB6 <-> SCL <-> SCL
//! ```
//!
//! Beware that the MCP7940N runs on 3.3V but PB6 and PB7 run on 5V level
//! so make sure to put a logic level shifter in between.
//!
//! Run with:
//! `cargo run --example mcp7940n-rtc-display-f3 --target thumbv7em-none-eabihf`,
#![deny(unsafe_code)]
#![no_std]
#![no_main]
extern crate embedded_graphics;
// panic handler
extern crate panic_semihosting;
use core::fmt::Write;
use cortex_m_rt::entry;
use embedded_graphics::fonts::Font6x8;
use embedded_graphics::prelude::*;
use f3::{
hal::{delay::Delay, i2c::I2c, prelude::*, stm32f30x},
led::Led,
};
use mcp794xx::{DateTime, Hours, Mcp794xx, Rtcc};
use ssd1306::prelude::*;
use ssd1306::Builder;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32f30x::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut led: Led = gpioe
.pe9
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper)
.into();
let mut delay = Delay::new(cp.SYST, clocks);
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);
let manager = shared_bus::BusManager::<cortex_m::interrupt::Mutex<_>, _>::new(i2c);
let mut disp: GraphicsMode<_> = Builder::new().connect_i2c(manager.acquire()).into();
disp.init().unwrap();
disp.flush().unwrap();
let mut rtc = Mcp794xx::new_mcp7940n(manager.acquire());
let begin = DateTime {
year: 2019,
month: 1,
day: 2,
weekday: 3,
hour: Hours::H24(4),
minute: 5,
second: 6,
};
rtc.set_datetime(&begin).unwrap();
rtc.enable().unwrap();
loop {
// Blink LED 0 to check that everything is actually running.
// If the LED 0 is off, something went wrong.
led.on();
delay.delay_ms(50_u8);
led.off();
delay.delay_ms(50_u8);
let now = rtc.get_datetime().unwrap();
let mut buffer: heapless::String<heapless::consts::U32> = heapless::String::new();
if let Hours::H24(h) = now.hour {
write!(
buffer,
"{}-{}-{} {} {}:{}:{} ",
now.year, now.month, now.day, now.weekday, h, now.minute, now.second
)
.unwrap();
disp.draw(
Font6x8::render_str(&buffer)
.with_stroke(Some(1u8.into()))
.into_iter(),
);
}
disp.flush().unwrap();
}
}
You can’t perform that action at this time.