1 /*******************************************************************************
2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of Maxim Integrated
23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
24 * Products, Inc. Branding Policy.
26 * The mere transfer of this software does not imply any licenses
27 * of trade secrets, proprietary technology, copyrights, patents,
28 * trademarks, maskwork rights, or any other form of intellectual
29 * property whatsoever. Maxim Integrated Products, Inc. retains all
31 *******************************************************************************
37 #include "pwrseq_regs.h"
38 #include "clkman_regs.h"
40 static int rtc_inited
= 0;
41 static volatile uint32_t overflow_cnt
= 0;
42 static uint32_t overflow_alarm
= 0;
44 //******************************************************************************
45 static void overflow_handler(void)
47 MXC_RTCTMR
->flags
= MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS
;
50 if (overflow_cnt
== overflow_alarm
) {
51 // Enable the comparator interrupt for the alarm
52 MXC_RTCTMR
->inten
|= MXC_F_RTC_INTEN_COMP0
;
56 //******************************************************************************
57 static void alarm_handler(void)
59 MXC_RTCTMR
->inten
&= ~MXC_F_RTC_INTEN_COMP0
;
60 MXC_RTCTMR
->flags
= MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS
;
63 //******************************************************************************
71 // Enable the clock to the synchronizer
72 MXC_CLKMAN
->clk_ctrl_13_rtc_int_sync
= MXC_E_CLKMAN_CLK_SCALE_ENABLED
;
74 // Enable the clock to the RTC
75 MXC_PWRSEQ
->reg0
|= MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN
;
77 // Set the divider from the 4kHz clock
78 MXC_RTCTMR
->prescale
= MXC_E_RTC_PRESCALE_DIV_2_0
;
80 // Enable the overflow interrupt
81 MXC_RTCTMR
->inten
|= MXC_F_RTC_FLAGS_OVERFLOW
;
83 // Prepare interrupt handlers
84 NVIC_SetVector(RTC0_IRQn
, (uint32_t)alarm_handler
);
85 NVIC_EnableIRQ(RTC0_IRQn
);
86 NVIC_SetVector(RTC3_IRQn
, (uint32_t)overflow_handler
);
87 NVIC_EnableIRQ(RTC3_IRQn
);
90 MXC_RTCTMR
->ctrl
|= MXC_F_RTC_CTRL_ENABLE
;
93 //******************************************************************************
96 if (MXC_RTCTMR
->ctrl
& MXC_F_RTC_CTRL_ENABLE
) {
97 // Clear and disable RTC
98 MXC_RTCTMR
->ctrl
|= MXC_F_RTC_CTRL_CLEAR
;
99 MXC_RTCTMR
->ctrl
&= ~MXC_F_RTC_CTRL_ENABLE
;
101 // Wait for pending transactions
102 while(MXC_RTCTMR
->ctrl
& MXC_F_RTC_CTRL_PENDING
);
105 // Disable the clock to the RTC
106 MXC_PWRSEQ
->reg0
&= ~(MXC_F_PWRSEQ_REG0_PWR_RTCEN_RUN
| MXC_F_PWRSEQ_REG0_PWR_RTCEN_SLP
);
108 // Disable the clock to the synchronizer
109 MXC_CLKMAN
->clk_ctrl_13_rtc_int_sync
= MXC_E_CLKMAN_CLK_SCALE_DISABLED
;
112 //******************************************************************************
113 int rtc_isenabled(void)
115 return (MXC_RTCTMR
->ctrl
& MXC_F_RTC_CTRL_ENABLE
);
118 //******************************************************************************
119 time_t rtc_read(void)
121 unsigned int shift_amt
;
122 uint32_t ovf_cnt_1
, ovf_cnt_2
, timer_cnt
;
124 // Account for a change in the default prescaler
125 shift_amt
= MXC_E_RTC_PRESCALE_DIV_2_12
- MXC_RTCTMR
->prescale
;
127 // Ensure coherency between overflow_cnt and timer
129 ovf_cnt_1
= overflow_cnt
;
130 timer_cnt
= MXC_RTCTMR
->timer
;
131 ovf_cnt_2
= overflow_cnt
;
132 } while (ovf_cnt_1
!= ovf_cnt_2
);
134 return (timer_cnt
>> shift_amt
) + (ovf_cnt_1
<< (32 - shift_amt
));
137 //******************************************************************************
138 uint64_t rtc_read_us(void)
140 unsigned int shift_amt
;
141 uint32_t ovf_cnt_1
, ovf_cnt_2
, timer_cnt
;
144 // Account for a change in the default prescaler
145 shift_amt
= MXC_E_RTC_PRESCALE_DIV_2_12
- MXC_RTCTMR
->prescale
;
147 // Ensure coherency between overflow_cnt and timer
149 ovf_cnt_1
= overflow_cnt
;
150 timer_cnt
= MXC_RTCTMR
->timer
;
151 ovf_cnt_2
= overflow_cnt
;
152 } while (ovf_cnt_1
!= ovf_cnt_2
);
154 currentUs
= (((uint64_t)timer_cnt
* 1000000) >> shift_amt
) + (((uint64_t)ovf_cnt_1
* 1000000) << (32 - shift_amt
));
159 //******************************************************************************
160 void rtc_write(time_t t
)
162 // Account for a change in the default prescaler
163 unsigned int shift_amt
= MXC_E_RTC_PRESCALE_DIV_2_12
- MXC_RTCTMR
->prescale
;
165 MXC_RTCTMR
->ctrl
&= ~MXC_F_RTC_CTRL_ENABLE
; // disable the timer while updating
166 MXC_RTCTMR
->timer
= t
<< shift_amt
;
167 overflow_cnt
= t
>> (32 - shift_amt
);
168 MXC_RTCTMR
->ctrl
|= MXC_F_RTC_CTRL_ENABLE
; // enable the timer while updating
171 //******************************************************************************
172 void rtc_set_wakeup(uint64_t wakeupUs
)
174 // Account for a change in the default prescaler
175 unsigned int shift_amt
= MXC_E_RTC_PRESCALE_DIV_2_12
- MXC_RTCTMR
->prescale
;
177 // Disable the alarm while it is prepared
178 MXC_RTCTMR
->inten
&= ~MXC_F_RTC_INTEN_COMP0
;
179 MXC_RTCTMR
->flags
= MXC_F_RTC_FLAGS_COMP0
; // clear interrupt
181 overflow_alarm
= (wakeupUs
>> (32 - shift_amt
)) / 1000000;
183 if (overflow_alarm
== overflow_cnt
) {
184 MXC_RTCTMR
->comp
[0] = (wakeupUs
<< shift_amt
) / 1000000;
185 MXC_RTCTMR
->inten
|= MXC_F_RTC_INTEN_COMP0
;
188 // Enable wakeup from RTC
189 MXC_PWRSEQ
->msk_flags
&= ~(MXC_F_PWRSEQ_MSK_FLAGS_RTC_ROLLOVER
| MXC_F_PWRSEQ_MSK_FLAGS_RTC_CMPR0
);