Skip to content

marekjastrzebski/stm32f3-rtc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stm32f3-rtc

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.

Features

List of features that are already available

  1. RTC Setup
  2. LSI - Low Speed Internal clock
  3. LSE - Low Speed External clock
  4. HSE - High Speed External clock (max 12 MHz)
  5. Time access/setup
  6. Date access/setup
  7. Delay in seconds
  8. Automatic Wake up Setup
  9. Alarms
  10. Time-stamps
  11. Tamper
  12. Daylight saving (Summer/Winter time)

Compatibility

This lib is designed to work with STM32 F3 family microcontrollers, especially with those described in RM0316

Dependencies

Library is wrapper for stm32f3xx-hal, but it use it minimal way, only for peripheral access.

Basic Usage

Cargo.toml

[dependencies]
// Please pick feature that align with your device
stm32f3xx-hal = { version="0.10.1", features = ["stm32f303xc"]}
stm32f3-rtc = {version="0.1", features = ["stm32f303xc"]}

Code usage:

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.

1. Creating RTC instance

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);

2. Setup and read time:

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

3. Setup and read date:

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

4. Setup different clock source:

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);

5. Using delay:

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");
}

About

API for RTC STM32

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages