Skip to content

Commit c48c2ec

Browse files
authored
Merge pull request #7998 from NXPmicro/MIMXRT1050_Add_RTC
MIMXRT1050_EVK: Add RTC support
2 parents 9b0e534 + a8fca70 commit c48c2ec

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/rtc_api.c

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@
1717

1818
#if DEVICE_RTC
1919

20-
#include "fsl_snvs_hp.h"
20+
#include "fsl_snvs_lp.h"
21+
22+
static bool rtc_time_set = false;
2123

2224
void rtc_init(void)
2325
{
24-
snvs_hp_rtc_config_t snvsRtcConfig;
26+
snvs_lp_srtc_config_t snvsRtcConfig;
2527

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

29-
SNVS_HP_RTC_StartTimer(SNVS);
31+
SNVS_LP_SRTC_StartTimer(SNVS);
3032
}
3133

3234
void rtc_free(void)
3335
{
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 */
3540
}
3641

3742
/*
@@ -40,19 +45,31 @@ void rtc_free(void)
4045
*/
4146
int rtc_isenabled(void)
4247
{
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);
4462
}
4563

4664
time_t rtc_read(void)
4765
{
48-
uint64_t seconds = 0;
49-
uint64_t tmp = 0;
66+
uint32_t seconds = 0;
67+
uint32_t tmp = 0;
5068

5169
/* Do consecutive reads until value is correct */
52-
do
53-
{
70+
do {
5471
seconds = tmp;
55-
tmp = SNVS->HPRTCLR;
72+
tmp = (SNVS->LPSRTCMR << 17U) | (SNVS->LPSRTCLR >> 15U);
5673
} while (tmp != seconds);
5774

5875
return (time_t)seconds;
@@ -63,11 +80,15 @@ void rtc_write(time_t t)
6380
if (t == 0) {
6481
t = 1;
6582
}
66-
SNVS_HP_RTC_StopTimer(SNVS);
6783

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

70-
SNVS_HP_RTC_StartTimer(SNVS);
91+
rtc_time_set = true;
7192
}
7293

7394
#endif

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@
766766
"macros": ["CPU_MIMXRT1052DVL6B", "FSL_RTOS_MBED", "XIP_BOOT_HEADER_ENABLE=1", "XIP_EXTERNAL_FLASH=1", "XIP_BOOT_HEADER_DCD_ENABLE=1", "SKIP_SYSCLK_INIT"],
767767
"inherits": ["Target"],
768768
"detect_code": ["0227"],
769-
"device_has": ["SLEEP", "USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
769+
"device_has": ["RTC", "SLEEP", "USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
770770
"release_versions": ["2", "5"],
771771
"device_name": "MIMXRT1052"
772772
},

0 commit comments

Comments
 (0)