GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_rtc.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_rtc.c
3  @brief Source file for stm32f103cb real time clock
4  @author Stuart Ianna
5  @version 0.1
6  @date June 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug
10 
11  @details
12 
13  @par Compilers
14  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
15 ******************************************************************************/
16 
17 #include "stm32f103cb_rtc.h"
18 
19 //This is the callback function used by systick during RTC_calibrate()
20 void rtc_callback(void);
21 static volatile uint8_t flag;
22 
23 /****************************************************************************/
24 // ISR FUNCTION POINTERS
25 /****************************************************************************/
26 static void (*rtc_alarm_handler)(void);
27 static void (*rtc_resolution_handler)(void);
28 static void (*rtc_overflow_handler)(void);
29 
30 /****************************************************************************/
31 // SETUP FUNCTIONS
32 /****************************************************************************/
33 mcu_error RTC_setup(uint32_t resolution){
34 
35  if((resolution > 0xFFFFF) && MCU_debugIsEnabled()){
36 
38  return E_RTC_PRELOAD;
39  }
40 
42  rtc_awake_from_off(RCC_LSI);
43  rtc_set_counter_val(0);
44  RTC_setPrescaler(resolution);
45 
46  return E_RTC_NOERROR;
47 }
48 
49 mcu_error RTC_setPrescaler(uint32_t resolution){
50 
51  if((resolution > 0xFFFFF) && MCU_debugIsEnabled()){
52 
54  return E_RTC_PRELOAD;
55  }
56 
57  rtc_set_prescale_val(resolution);
58 
59  return E_RTC_NOERROR;
60 }
61 
62 //Calibrate uses systick to check the actural clock count needed
63 //to get the resolution
64 mcu_error RTC_calibrate(uint32_t resolution){
65 
66  //Make sure resolution is non zero and not too large
67  if(((resolution > 0x3FFFF) || (!resolution) ) && MCU_debugIsEnabled()){
68 
70  return E_RTC_PRELOAD;
71  }
72 
73  SYSTICK_setup(resolution*1000,&rtc_callback);
74  rtc_set_counter_val(0);
75  rtc_set_prescale_val(0);
76  while(!flag);
77  rtc_set_prescale_val(rtc_get_counter_val()-4);
78  rtc_set_counter_val(0);
79  SYSTICK_stop();
80 
81  return E_RTC_NOERROR;
82 }
83 
84 void rtc_callback(void){
85 
86  flag = 1;
87 
88 }
89 
90 void RTC_setCount(uint32_t count){
91 
92  rtc_set_counter_val(count);
93 
94 }
95 
96 void RTC_clockEnable(void){
97 
98  rtc_awake_from_off(RCC_LSI);
99 }
100 
101 uint32_t RTC_getCount(void){
102 
103  return rtc_get_counter_val();
104 
105 }
106 
107 
108 /****************************************************************************/
109 // Resolution (second) ISR
110 /****************************************************************************/
111 void RTC_enableResolutionISR(void (*res_isr)(void)){
112 
114  rtc_resolution_handler = res_isr;
115  rtc_interrupt_enable(RTC_SEC);
116  nvic_enable_irq(NVIC_RTC_IRQ);
117 }
118 
120 
121  rtc_interrupt_disable(RTC_SEC);
122 }
123 
125 
126  return (RTC_CRH & RTC_CRH_SECIE);
127 }
128 
129 /****************************************************************************/
130 // ALARM ISR
131 /****************************************************************************/
132 
133 void RTC_enableAlarmISR(uint32_t count,void (*al_isr)(void)){
134 
136  rtc_alarm_handler = al_isr;
137  rtc_interrupt_enable(RTC_ALR);
138  rtc_set_alarm_time(count);
139  nvic_enable_irq(NVIC_RTC_IRQ);
140 }
141 
143 
144  rtc_interrupt_disable(RTC_ALR);
145 }
146 
147 uint8_t RTC_isAlarmEnabled(void){
148 
149  return (RTC_CRH & RTC_CRH_ALRIE);
150 }
151 
152 /****************************************************************************/
153 // OVERFLOW ISR
154 /****************************************************************************/
155 void RTC_enableOverflowISR(void (*ov_isr)(void)){
156 
158  rtc_overflow_handler = ov_isr;
159  rtc_interrupt_enable(RTC_OW);
160  nvic_enable_irq(NVIC_RTC_IRQ);
161 }
162 
164 
165  rtc_interrupt_disable(RTC_OW);
166 }
167 
168 uint8_t RTC_isOverflowEnabled(void){
169 
170  return (RTC_CRH & RTC_CRH_OWIE);
171 }
172 
173 /****************************************************************************/
174 // MAIN ISR
175 /****************************************************************************/
176 void rtc_isr(void){
177 
178  if(rtc_check_flag(RTC_OW)){
179 
180  rtc_clear_flag(RTC_OW);
181  rtc_overflow_handler();
182  }
183  else if(rtc_check_flag(RTC_ALR)){
184 
185  rtc_clear_flag(RTC_ALR);
186  rtc_alarm_handler();
187  }
188  else if(rtc_check_flag(RTC_SEC)){
189 
190  rtc_clear_flag(RTC_SEC);
191  rtc_resolution_handler();
192  }
193 }
uint32_t RTC_getCount(void)
Get the current count value.
mcu_error RTC_setPrescaler(uint32_t resolution)
Set the raw prescaler value of the RTC.
void RTC_clockEnable(void)
Enable the RTC clock.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
uint8_t RTC_isOverflowEnabled(void)
Determine if the overflow interrupt is enabled.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
Error RTC: Invalid period specified.
Error RTC: No error.
void RTC_setCount(uint32_t count)
Set the current count value.
void SYSTICK_stop(void)
Stop the systick counter.
uint8_t RTC_isResolutionEnabled(void)
Determine if the resolution (second) interrupt is enabled.
void RTC_disableAlarmISR(void)
disables the alarm ISR.
void RTC_enableAlarmISR(uint32_t count, void(*al_isr)(void))
Enables the alarm ISR.
void RTC_disableResolutionISR(void)
disables the resolution ISR.
void RTC_enableOverflowISR(void(*ov_isr)(void))
Enables the overflow ISR.
mcu_error RTC_setup(uint32_t resolution)
Initialize the Real time clock.
void RTC_disableOverflowISR(void)
disables the overflow ISR.
Header file for stm32f103cb real time clock (RTC)
void RTC_enableResolutionISR(void(*res_isr)(void))
Enables the resolution ISR.
uint8_t RTC_isAlarmEnabled(void)
Determine if the alarm interrupt is enabled.
mcu_error RTC_calibrate(uint32_t resolution)
Calibrate the RTC to a given resolution in milliseconds.
mcu_error SYSTICK_setup(uint32_t timeout, void(*handler)(void))
Sets up systick and handler.
mcu_error
Error enumerators for the Debug peripheral.