It is complex API for RTC peripheral management. It is made with concept "minimal user interaction", it means that user using this API doesn't waste his time on setups or configuration. Contain some default settings for particular elements, but if you want to dive into some individual settings it gave you that opportunity. But if you want to just quickly setup clock that holds specified date or time, you just doing it with 2 lines of code.
List of features that are already available
- RTC Setup
- LSI - Low Speed Internal clock
- LSE - Low Speed External clock
- HSE - High Speed External clock (max 12 MHz)
- Time access/setup
- Date access/setup
- Delay in seconds
- Automatic Wake up Setup
- Alarms
- Time-stamps
- Tamper
- Daylight saving (Summer/Winter time)
This lib is designed to work with STM32 F3 family microcontrollers, especially with those described in RM0316
Library is wrapper for stm32f3xx-hal, but it use it minimal way, only for peripheral access.
[dependencies]
// Please pick feature that align with your device
stm32f3xx-hal = { version="0.10.1", features = ["stm32f303xc"]}
stm32f3-rtc = {version="0.1", features = ["stm32f303xc"]}
By default, rtc using LSI clock source that is built into microcontroller, but it is not accurate. If you don't need super accurate clock it is most of the time enough.
use stm32f3_rtc::rtc::Rtc;
use stm32f3xx_hal::pac;
let mut peripheral = pac::Peripherals::take().unwrap();
let rtc = Rtc::new(peripheral.RTC).start_clock(&mut peripheral.PWR, &mut peripheral.RCC);
use stm32f3_rtc::datetime::{Time, TimeAccess};
use stm32f3_rtc::rtc::Rtc;
use stm32f3xx_hal::pac;
use cortex_m_semihosting;
let mut peripheral = pac::Peripherals::take().unwrap();
let rtc = Rtc::new(peripheral.RTC).start_clock(&mut peripheral.PWR, &mut peripheral.RCC);
rtc.set_time(Time::from(12,30,0));
let time = rtc.time();
hprintln!("{}:{}:{}", time.hour, time.minute, time.second);
//Print: 12:30:0
use stm32f3_rtc::datetime::{Date, DateAccess};
use stm32f3_rtc::rtc::Rtc;
use stm32f3xx_hal::pac;
use cortex_m_semihosting::hprintln;
let mut peripheral = pac::Peripherals::take().unwrap();
let rtc = Rtc::new(peripheral.RTC).start_clock(&mut peripheral.PWR, &mut peripheral.RCC);
rtc.set_date(Date::from(1,1,2024));
let date = rtc.date();
hprintln!("{}.{}.{}", date.day, date.month, date.year);
//Print: 1.1.2024
This example shows how to run LSE clock with defoult prescalers for 32,768kHz frequency. You can pick your own prescalers by using set_prescalers() function. If you up to please read its documentation.
use stm32f3_rtc::rtc::{ClockSource, Rtc};
use stm32f3xx_hal::pac;
use cortex_m_semihosting::hprintln;
let mut peripheral = pac::Peripherals::take().unwrap();
let rtc = Rtc::new(peripheral.RTC)
.set_clock_source(ClockSource::LSE(true))
.start_clock(&mut peripheral.PWR, &mut peripheral.RCC);
use stm32f3_rtc::rtc::{ClockSource, Rtc};
use stm32f3xx_hal::pac;
use cortex_m_semihosting::hprintln;
let mut peripheral = pac::Peripherals::take().unwrap();
let rtc = Rtc::new(peripheral.RTC)
.start_clock(&mut peripheral.PWR, &mut peripheral.RCC);
rtc.set_time(Time::from(12,30,0));
loop {
rtc.delay(2);
hprintln!("This text will appear every 2 seconds");
}