17
17
18
18
#if DEVICE_RTC
19
19
20
- #include "fsl_snvs_hp.h"
20
+ #include "fsl_snvs_lp.h"
21
+
22
+ static bool rtc_time_set = false;
21
23
22
24
void rtc_init (void )
23
25
{
24
- snvs_hp_rtc_config_t snvsRtcConfig ;
26
+ snvs_lp_srtc_config_t snvsRtcConfig ;
25
27
26
- SNVS_HP_RTC_GetDefaultConfig (& snvsRtcConfig );
27
- SNVS_HP_RTC_Init (SNVS , & snvsRtcConfig );
28
+ SNVS_LP_SRTC_GetDefaultConfig (& snvsRtcConfig );
29
+ SNVS_LP_SRTC_Init (SNVS , & snvsRtcConfig );
28
30
29
- SNVS_HP_RTC_StartTimer (SNVS );
31
+ SNVS_LP_SRTC_StartTimer (SNVS );
30
32
}
31
33
32
34
void rtc_free (void )
33
35
{
34
- SNVS_HP_RTC_Deinit (SNVS );
36
+ #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && \
37
+ defined(SNVS_LP_CLOCKS ))
38
+ CLOCK_DisableClock (kCLOCK_SnvsLp0 );
39
+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
35
40
}
36
41
37
42
/*
@@ -40,19 +45,31 @@ void rtc_free(void)
40
45
*/
41
46
int rtc_isenabled (void )
42
47
{
43
- return (int )((SNVS -> HPCR & SNVS_HPCR_RTC_EN_MASK ) >> SNVS_HPCR_RTC_EN_SHIFT );
48
+ #if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL ) && \
49
+ defined(SNVS_LP_CLOCKS ))
50
+ CLOCK_EnableClock (kCLOCK_SnvsLp0 );
51
+ #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
52
+
53
+
54
+ const bool rtc_init_done = ((SNVS -> LPCR & SNVS_LPCR_SRTC_ENV_MASK ) >> SNVS_LPCR_SRTC_ENV_SHIFT );
55
+
56
+ /* If RTC is not initialized, then disable the clock gate on exit. */
57
+ if (!rtc_init_done ) {
58
+ rtc_free ();
59
+ }
60
+
61
+ return (rtc_init_done & rtc_time_set );
44
62
}
45
63
46
64
time_t rtc_read (void )
47
65
{
48
- uint64_t seconds = 0 ;
49
- uint64_t tmp = 0 ;
66
+ uint32_t seconds = 0 ;
67
+ uint32_t tmp = 0 ;
50
68
51
69
/* Do consecutive reads until value is correct */
52
- do
53
- {
70
+ do {
54
71
seconds = tmp ;
55
- tmp = SNVS -> HPRTCLR ;
72
+ tmp = ( SNVS -> LPSRTCMR << 17U ) | ( SNVS -> LPSRTCLR >> 15U ) ;
56
73
} while (tmp != seconds );
57
74
58
75
return (time_t )seconds ;
@@ -63,11 +80,15 @@ void rtc_write(time_t t)
63
80
if (t == 0 ) {
64
81
t = 1 ;
65
82
}
66
- SNVS_HP_RTC_StopTimer (SNVS );
67
83
68
- SNVS -> HPRTCLR = (uint32_t )t ;
84
+ SNVS_LP_SRTC_StopTimer (SNVS );
85
+
86
+ SNVS -> LPSRTCMR = (uint32_t )(t >> 17U );
87
+ SNVS -> LPSRTCLR = (uint32_t )(t << 15U );
88
+
89
+ SNVS_LP_SRTC_StartTimer (SNVS );
69
90
70
- SNVS_HP_RTC_StartTimer ( SNVS ) ;
91
+ rtc_time_set = true ;
71
92
}
72
93
73
94
#endif
0 commit comments