GlobLib
HAL and API libraries for MCUs and hardware.

This module contains functions for working with the real time clock. More...

Files

file  stm32f103cb_rtc.h
 Header file for stm32f103cb real time clock (RTC)
 

Functions

mcu_error RTC_setup (uint32_t resolution)
 Initialize the Real time clock. More...
 
mcu_error RTC_calibrate (uint32_t resolution)
 Calibrate the RTC to a given resolution in milliseconds. More...
 
mcu_error RTC_setPrescaler (uint32_t resolution)
 Set the raw prescaler value of the RTC. More...
 
uint8_t RTC_isAlarmEnabled (void)
 Determine if the alarm interrupt is enabled. More...
 
uint8_t RTC_isOverflowEnabled (void)
 Determine if the overflow interrupt is enabled. More...
 
uint8_t RTC_isResolutionEnabled (void)
 Determine if the resolution (second) interrupt is enabled. More...
 
uint32_t RTC_getCount (void)
 Get the current count value. More...
 
void RTC_setCount (uint32_t count)
 Set the current count value. More...
 
void RTC_enableResolutionISR (void(*res_isr)(void))
 Enables the resolution ISR. More...
 
void RTC_enableAlarmISR (uint32_t count, void(*al_isr)(void))
 Enables the alarm ISR. More...
 
void RTC_enableOverflowISR (void(*ov_isr)(void))
 Enables the overflow ISR. More...
 
void RTC_disableResolutionISR (void)
 disables the resolution ISR.
 
void RTC_disableAlarmISR (void)
 disables the alarm ISR.
 
void RTC_disableOverflowISR (void)
 disables the overflow ISR.
 
void RTC_clockEnable (void)
 Enable the RTC clock. More...
 

Detailed Description

This module contains functions for working with the real time clock.

The real time clock works with the internal LSI oscillator, this runs at approximately 40kHz. This clock functions seperate from the main clock and can be used in standby mode to wake the core from sleep state. This is acheived by setting the alarm to the desired wakeup time.

Both the counter and alarm registers are 32 bits wide, for a 1 second tick this is approximately 136 years before an overflow event occurs! At 1ms it is 7 weeks. The prescaler is 20 bits wide, giving a meer 3600 years as a maximum value.

The accuracy of the RTC increases with increasing resolution approximated as

and so on. These values assume RTC_calibrate() was used to determine the preload value.

There are three interrupts which can be generated with the RTC

Author
Stuart Ianna
Version
0.1
Date
June 2018
Bug:
None
Todo:
Warning
RTC_configure() requires a one-shot usage of systick. This means that this function should be called before any other systick usage.
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Working example.
void alarm(void);
void resolution(void);
void overflow(void);
int main(void){
//Coms for terminal info
//The value passed here is meaningless as RTC_calibrate is called next.
RTC_setup(500);
//This uses systick!
//This takes 500ms to complete!
//(smaller resolution, shorter time, less accurate)
//Systick could be used here again if needed
//Good to reset the count after so everything starts at 0
//Call alarm after 2 resultion counts (1 second)
RTC_enableAlarmISR(2,&alarm);
//Call resolution every period. (500ms)
//Call this after a couple of years.
//Done
while(1);
return 0;
}
void alarm(void){
printl("Alarm");
//The clock continues to count.
//Set the alarm again after 2 counts (1 second)
}
void resolution(void){
printl("Resolution");
}
void overflow(void){
printl("Overflow");
}

Function Documentation

mcu_error RTC_calibrate ( uint32_t  resolution)

Calibrate the RTC to a given resolution in milliseconds.

This function uses SYSTICK to calculate the required RTC prescaler. SYSTICK must not be used for any other function while this is being called. After complete SYSTICK can be utalised again.

This function will take as long as the resultion to complete!

Parameters
resolutionThe required resolution of the RTC in milliseconds.
Warning
Read description.
Returns
Error number as defined in mcu_error, E_RTC_NOERROR if ok or E_RTC_PRESCALER if an error with prescaler value has occured.

Definition at line 64 of file stm32f103cb_rtc.c.

void RTC_clockEnable ( void  )

Enable the RTC clock.

This can be used to renable the clock after system has been in stop mode.

Definition at line 96 of file stm32f103cb_rtc.c.

void RTC_enableAlarmISR ( uint32_t  count,
void(*)(void)  al_isr 
)

Enables the alarm ISR.

This interrupt occurs when the counter value matches the alarm value.

Parameters
countThe value at which the count register triggers the interrupt.
al_isrThe function to call on interrupt.

Definition at line 133 of file stm32f103cb_rtc.c.

void RTC_enableOverflowISR ( void(*)(void)  ov_isr)

Enables the overflow ISR.

This interrupt occurs when the counter register overflows (never)

Parameters
ov_isrThe function to call on interrupt.

Definition at line 155 of file stm32f103cb_rtc.c.

void RTC_enableResolutionISR ( void(*)(void)  res_isr)

Enables the resolution ISR.

This interrupt occurs each resoultion time value.

Parameters
res_isrThe function to call on interrupt.

Definition at line 111 of file stm32f103cb_rtc.c.

uint32_t RTC_getCount ( void  )

Get the current count value.

Returns
The current value (Of units respect to resolution)

Definition at line 101 of file stm32f103cb_rtc.c.

uint8_t RTC_isAlarmEnabled ( void  )

Determine if the alarm interrupt is enabled.

Returns
  • 0 if the alarm is disabled
  • Non-zero if the alarm is enabled

Definition at line 147 of file stm32f103cb_rtc.c.

uint8_t RTC_isOverflowEnabled ( void  )

Determine if the overflow interrupt is enabled.

Returns
  • 0 if the overflow is disabled
  • Non-zero if the overflow is enabled

Definition at line 168 of file stm32f103cb_rtc.c.

uint8_t RTC_isResolutionEnabled ( void  )

Determine if the resolution (second) interrupt is enabled.

Returns
  • 0 if the second is disabled
  • Non-zero if the second is enabled

Definition at line 124 of file stm32f103cb_rtc.c.

void RTC_setCount ( uint32_t  count)

Set the current count value.

Parameters
countThe current value to set the count to

Definition at line 90 of file stm32f103cb_rtc.c.

mcu_error RTC_setPrescaler ( uint32_t  resolution)

Set the raw prescaler value of the RTC.

This value determines the period of the count incrementation. Usage of RTC_calabrate() superseeds the need for this function.

Parameters
resolutionThe value written the the RTC prescaler register.
Returns
Error number as defined in mcu_error, E_RTC_NOERROR if ok or E_RTC_PRESCALER if an error with prescaler value has occured.

Definition at line 49 of file stm32f103cb_rtc.c.

mcu_error RTC_setup ( uint32_t  resolution)

Initialize the Real time clock.

This must be called before any other feature is used.

The resolution here is loaded directly to the prescalar register. The final period of the clock can be estimated using

period (ms) = (resolution/4)

This doesn't produce a very accurate result, and if possible RTC_calibrate should be called following this funcition. This automatically sets the period to the desired value.

Parameters
resolutionThe value written to the prescaler register
Returns
Error number as defined in mcu_error, E_RTC_NOERROR if ok or E_RTC_PRESCALER if an error with prescaler value has occured.

Definition at line 33 of file stm32f103cb_rtc.c.