Skip to content

Commit 11d418b

Browse files
jessexmbulislaw
authored andcommitted
mbed-os v5.9 RTC implementation
1 parent 7624a66 commit 11d418b

File tree

4 files changed

+26
-86
lines changed

4 files changed

+26
-86
lines changed

targets/TARGET_Maxim/TARGET_MAX32620C/rtc_api.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*******************************************************************************
3232
*/
3333

34+
#include <string.h>
3435
#include "rtc_api.h"
3536
#include "lp_ticker_api.h"
3637
#include "rtc.h"
@@ -83,64 +84,43 @@ static void init_rtc(void)
8384
static void overflow_handler(void)
8485
{
8586
MXC_RTCTMR->comp[1] += ((UINT32_MAX >> LOG2(LP_TIMER_RATE_HZ)) + 1);
86-
RTC_ClearFlags(MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS);
87+
RTC_ClearFlags(MXC_F_RTC_FLAGS_OVERFLOW);
8788
}
8889

8990
//******************************************************************************
9091
void rtc_init(void)
9192
{
92-
if (rtc_inited) {
93-
return;
94-
}
95-
9693
NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler);
9794
NVIC_EnableIRQ(RTC3_IRQn);
98-
// Enable wakeup on RTC overflow
99-
LP_ConfigRTCWakeUp(lp_ticker_inited, 0, 0, 1);
95+
96+
// Enable as LP wakeup source
97+
MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_FLAGS_RTC_ROLLOVER;
98+
10099
init_rtc();
101-
rtc_inited = 1;
102100
}
103101

104102
//******************************************************************************
105103
void rtc_free(void)
106104
{
107-
if (rtc_inited) {
108-
rtc_inited = 0;
109-
if (lp_ticker_inited) {
110-
RTC_DisableINT(MXC_F_RTC_FLAGS_OVERFLOW);
111-
} else {
112-
MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_CLEAR;
113-
RTC_Stop();
114-
}
115-
}
105+
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
116106
}
117107

118108
//******************************************************************************
119109
int rtc_isenabled(void)
120110
{
121-
return rtc_inited;
111+
return !!RTC_IsActive();
122112
}
123113

124114
//******************************************************************************
125115
void rtc_write(time_t t)
126116
{
127-
if (!rtc_inited) {
128-
rtc_init();
129-
}
130-
131117
MXC_RTCTMR->comp[1] = t - (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ));
132-
133-
// Wait for pending transactions
134118
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
135119
}
136120

137121
//******************************************************************************
138122
time_t rtc_read(void)
139123
{
140-
if (!rtc_inited) {
141-
rtc_init();
142-
}
143-
144124
return (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ)) + MXC_RTCTMR->comp[1];
145125
}
146126

targets/TARGET_Maxim/TARGET_MAX32625/rtc_api.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*******************************************************************************
3232
*/
3333

34+
#include <string.h>
3435
#include "rtc_api.h"
3536
#include "lp_ticker_api.h"
3637
#include "rtc.h"
@@ -83,64 +84,43 @@ static void init_rtc(void)
8384
static void overflow_handler(void)
8485
{
8586
MXC_RTCTMR->comp[1] += ((UINT32_MAX >> LOG2(LP_TIMER_RATE_HZ)) + 1);
86-
RTC_ClearFlags(MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS);
87+
RTC_ClearFlags(MXC_F_RTC_FLAGS_OVERFLOW);
8788
}
8889

8990
//******************************************************************************
9091
void rtc_init(void)
9192
{
92-
if (rtc_inited) {
93-
return;
94-
}
95-
9693
NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler);
9794
NVIC_EnableIRQ(RTC3_IRQn);
98-
// Enable wakeup on RTC overflow
99-
LP_ConfigRTCWakeUp(lp_ticker_inited, 0, 0, 1);
95+
96+
// Enable as LP wakeup source
97+
MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_FLAGS_RTC_ROLLOVER;
98+
10099
init_rtc();
101-
rtc_inited = 1;
102100
}
103101

104102
//******************************************************************************
105103
void rtc_free(void)
106104
{
107-
if (rtc_inited) {
108-
rtc_inited = 0;
109-
if (lp_ticker_inited) {
110-
RTC_DisableINT(MXC_F_RTC_FLAGS_OVERFLOW);
111-
} else {
112-
MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_CLEAR;
113-
RTC_Stop();
114-
}
115-
}
105+
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
116106
}
117107

118108
//******************************************************************************
119109
int rtc_isenabled(void)
120110
{
121-
return rtc_inited;
111+
return !!RTC_IsActive();
122112
}
123113

124114
//******************************************************************************
125115
void rtc_write(time_t t)
126116
{
127-
if (!rtc_inited) {
128-
rtc_init();
129-
}
130-
131117
MXC_RTCTMR->comp[1] = t - (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ));
132-
133-
// Wait for pending transactions
134118
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
135119
}
136120

137121
//******************************************************************************
138122
time_t rtc_read(void)
139123
{
140-
if (!rtc_inited) {
141-
rtc_init();
142-
}
143-
144124
return (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ)) + MXC_RTCTMR->comp[1];
145125
}
146126

targets/TARGET_Maxim/TARGET_MAX32630/rtc_api.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*******************************************************************************
3232
*/
3333

34+
#include <string.h>
3435
#include "rtc_api.h"
3536
#include "lp_ticker_api.h"
3637
#include "rtc.h"
@@ -83,64 +84,43 @@ static void init_rtc(void)
8384
static void overflow_handler(void)
8485
{
8586
MXC_RTCTMR->comp[1] += ((UINT32_MAX >> LOG2(LP_TIMER_RATE_HZ)) + 1);
86-
RTC_ClearFlags(MXC_F_RTC_FLAGS_ASYNC_CLR_FLAGS);
87+
RTC_ClearFlags(MXC_F_RTC_FLAGS_OVERFLOW);
8788
}
8889

8990
//******************************************************************************
9091
void rtc_init(void)
9192
{
92-
if (rtc_inited) {
93-
return;
94-
}
95-
9693
NVIC_SetVector(RTC3_IRQn, (uint32_t)overflow_handler);
9794
NVIC_EnableIRQ(RTC3_IRQn);
98-
// Enable wakeup on RTC overflow
99-
LP_ConfigRTCWakeUp(lp_ticker_inited, 0, 0, 1);
95+
96+
// Enable as LP wakeup source
97+
MXC_PWRSEQ->msk_flags |= MXC_F_PWRSEQ_FLAGS_RTC_ROLLOVER;
98+
10099
init_rtc();
101-
rtc_inited = 1;
102100
}
103101

104102
//******************************************************************************
105103
void rtc_free(void)
106104
{
107-
if (rtc_inited) {
108-
rtc_inited = 0;
109-
if (lp_ticker_inited) {
110-
RTC_DisableINT(MXC_F_RTC_FLAGS_OVERFLOW);
111-
} else {
112-
MXC_RTCTMR->ctrl |= MXC_F_RTC_CTRL_CLEAR;
113-
RTC_Stop();
114-
}
115-
}
105+
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
116106
}
117107

118108
//******************************************************************************
119109
int rtc_isenabled(void)
120110
{
121-
return rtc_inited;
111+
return !!RTC_IsActive();
122112
}
123113

124114
//******************************************************************************
125115
void rtc_write(time_t t)
126116
{
127-
if (!rtc_inited) {
128-
rtc_init();
129-
}
130-
131117
MXC_RTCTMR->comp[1] = t - (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ));
132-
133-
// Wait for pending transactions
134118
while (MXC_RTCTMR->ctrl & MXC_F_RTC_CTRL_PENDING);
135119
}
136120

137121
//******************************************************************************
138122
time_t rtc_read(void)
139123
{
140-
if (!rtc_inited) {
141-
rtc_init();
142-
}
143-
144124
return (MXC_RTCTMR->timer >> LOG2(LP_TIMER_RATE_HZ)) + MXC_RTCTMR->comp[1];
145125
}
146126

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@
28512851
"macros": ["__SYSTEM_HFX=96000000","TARGET=MAX32620","TARGET_REV=0x4332","OPEN_DRAIN_LEDS"],
28522852
"extra_labels": ["Maxim", "MAX32620C"],
28532853
"supported_toolchains": ["GCC_ARM", "IAR", "ARM"],
2854-
"device_has": ["ANALOGIN", "I2C", "INTERRUPTIN", "LPTICKER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "STDIO_MESSAGES", "USTICKER"],
2854+
"device_has": ["ANALOGIN", "I2C", "INTERRUPTIN", "LPTICKER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "STDIO_MESSAGES", "USTICKER"],
28552855
"release_versions": ["2", "5"]
28562856
},
28572857
"MAX32625_BASE": {
@@ -2895,7 +2895,7 @@
28952895
"macros": ["__SYSTEM_HFX=96000000", "TARGET=MAX32630", "TARGET_REV=0x4132", "BLE_HCI_UART", "OPEN_DRAIN_LEDS"],
28962896
"extra_labels": ["Maxim", "MAX32630"],
28972897
"supported_toolchains": ["GCC_ARM", "IAR", "ARM"],
2898-
"device_has": ["ANALOGIN", "I2C", "INTERRUPTIN", "LPTICKER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SPI", "STDIO_MESSAGES", "USTICKER"],
2898+
"device_has": ["ANALOGIN", "I2C", "INTERRUPTIN", "LPTICKER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SPI", "STDIO_MESSAGES", "USTICKER"],
28992899
"features": ["BLE"],
29002900
"release_versions": ["2", "5"]
29012901
},

0 commit comments

Comments
 (0)