Skip to content

MIMXRT1050_EVK: Add RTC support #7998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions targets/TARGET_NXP/TARGET_MCUXpresso_MCUS/TARGET_IMX/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@

#if DEVICE_RTC

#include "fsl_snvs_hp.h"
#include "fsl_snvs_lp.h"

static bool rtc_time_set = false;

void rtc_init(void)
{
snvs_hp_rtc_config_t snvsRtcConfig;
snvs_lp_srtc_config_t snvsRtcConfig;

SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig);
SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig);
SNVS_LP_SRTC_GetDefaultConfig(&snvsRtcConfig);
SNVS_LP_SRTC_Init(SNVS, &snvsRtcConfig);

SNVS_HP_RTC_StartTimer(SNVS);
SNVS_LP_SRTC_StartTimer(SNVS);
}

void rtc_free(void)
{
SNVS_HP_RTC_Deinit(SNVS);
#if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
defined(SNVS_LP_CLOCKS))
CLOCK_DisableClock(kCLOCK_SnvsLp0);
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
}

/*
Expand All @@ -40,19 +45,31 @@ void rtc_free(void)
*/
int rtc_isenabled(void)
{
return (int)((SNVS->HPCR & SNVS_HPCR_RTC_EN_MASK) >> SNVS_HPCR_RTC_EN_SHIFT);
#if (!(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && \
defined(SNVS_LP_CLOCKS))
CLOCK_EnableClock(kCLOCK_SnvsLp0);
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */


const bool rtc_init_done = ((SNVS->LPCR & SNVS_LPCR_SRTC_ENV_MASK) >> SNVS_LPCR_SRTC_ENV_SHIFT);

/* If RTC is not initialized, then disable the clock gate on exit. */
if(!rtc_init_done) {
rtc_free();
}

return (rtc_init_done & rtc_time_set);
}

time_t rtc_read(void)
{
uint64_t seconds = 0;
uint64_t tmp = 0;
uint32_t seconds = 0;
uint32_t tmp = 0;

/* Do consecutive reads until value is correct */
do
{
do {
seconds = tmp;
tmp = SNVS->HPRTCLR;
tmp = (SNVS->LPSRTCMR << 17U) | (SNVS->LPSRTCLR >> 15U);
} while (tmp != seconds);

return (time_t)seconds;
Expand All @@ -63,11 +80,15 @@ void rtc_write(time_t t)
if (t == 0) {
t = 1;
}
SNVS_HP_RTC_StopTimer(SNVS);

SNVS->HPRTCLR = (uint32_t)t;
SNVS_LP_SRTC_StopTimer(SNVS);

SNVS->LPSRTCMR = (uint32_t)(t >> 17U);
SNVS->LPSRTCLR = (uint32_t)(t << 15U);

SNVS_LP_SRTC_StartTimer(SNVS);

SNVS_HP_RTC_StartTimer(SNVS);
rtc_time_set = true;
}

#endif
2 changes: 1 addition & 1 deletion targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@
"macros": ["CPU_MIMXRT1052DVL6B", "FSL_RTOS_MBED", "XIP_BOOT_HEADER_ENABLE=1", "XIP_EXTERNAL_FLASH=1", "XIP_BOOT_HEADER_DCD_ENABLE=1", "SKIP_SYSCLK_INIT"],
"inherits": ["Target"],
"detect_code": ["0227"],
"device_has": ["SLEEP", "USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"device_has": ["RTC", "SLEEP", "USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "ERROR_RED", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
"release_versions": ["2", "5"],
"device_name": "MIMXRT1052"
},
Expand Down