Skip to content

Commit 30e39ee

Browse files
author
Cruz Monrreal
authored
Merge pull request #6852 from mprse/issue_5308_fix
Fix for issue #5308 - RTC set/get time issue on NCS36510
2 parents 4318caa + 23127af commit 30e39ee

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

TESTS/mbed_drivers/rtc/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void test_attach_RTC_stub_funtions()
144144
TEST_ASSERT_EQUAL(false, rtc_init_called);
145145

146146
/* Check if time has been successfully set and retrieved. */
147-
TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
147+
TEST_ASSERT_UINT32_WITHIN(RTC_DELTA, CUSTOM_TIME_1, seconds);
148148
}
149149

150150
/* This test verifies if attach_rtc provides availability to
@@ -183,7 +183,7 @@ void test_attach_RTC_org_funtions()
183183
TEST_ASSERT_EQUAL(false, rtc_init_called);
184184

185185
/* Check if time has been successfully set and retrieved. */
186-
TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
186+
TEST_ASSERT_UINT32_WITHIN(RTC_DELTA, CUSTOM_TIME_1, seconds);
187187
}
188188

189189
/* This test verifies if time() function returns
@@ -430,7 +430,7 @@ void test_functional_set()
430430
set_time(timeValue);
431431

432432
/* Get current time and verify that new value has been set. */
433-
TEST_ASSERT_EQUAL(timeValue, time(NULL));
433+
TEST_ASSERT_UINT32_WITHIN(1, timeValue, time(NULL));
434434
}
435435

436436
/* This test verifies if RTC counts seconds.

targets/TARGET_ONSEMI/TARGET_NCS36510/rtc_api.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@
3939
#include "rtc.h"
4040
#include "cmsis_nvic.h"
4141

42+
#define US_PER_SEC 1000000
43+
44+
static time_t m_time_base;
45+
46+
static uint32_t rtc_seconds_get()
47+
{
48+
return (uint32_t)((fRtcRead() / US_PER_SEC) & 0xFFFFFFFF);
49+
}
50+
4251
/* See rtc_apc.h for description */
4352

4453
void rtc_init(void)
@@ -61,13 +70,20 @@ int rtc_isenabled(void)
6170
/* See rtc_apc.h for description */
6271
time_t rtc_read(void)
6372
{
64-
return (uint32_t)(fRtcRead() & 0xFFFFFFFF); /* TODO Truncating 64 bit value to 32 bit */
73+
return m_time_base + rtc_seconds_get();
6574
}
6675

6776
/* See rtc_apc.h for description */
6877
void rtc_write(time_t t)
6978
{
70-
fRtcWrite(t);
79+
uint32_t seconds;
80+
do {
81+
seconds = rtc_seconds_get();
82+
m_time_base = t - seconds;
83+
/* If the number of seconds indicated by the counter changed during the
84+
update of the time base, just repeat the update, now using the new
85+
number of seconds. */
86+
} while (seconds != rtc_seconds_get());
7187
}
7288

7389
#endif /* DEVICE_RTC */

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3753,7 +3753,7 @@
37533753
"post_binary_hook": {"function": "NCS36510TargetCode.ncs36510_addfib"},
37543754
"macros": ["CM3", "CPU_NCS36510", "TARGET_NCS36510", "LOAD_ADDRESS=0x3000"],
37553755
"supported_toolchains": ["GCC_ARM", "ARM", "IAR"],
3756-
"device_has": ["ANALOGIN", "SERIAL", "I2C", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "LOWPOWERTIMER", "TRNG", "SPISLAVE"],
3756+
"device_has": ["ANALOGIN", "SERIAL", "I2C", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "LOWPOWERTIMER", "TRNG", "SPISLAVE", "RTC"],
37573757
"release_versions": ["2", "5"]
37583758
},
37593759
"NUMAKER_PFM_M453": {

0 commit comments

Comments
 (0)