Skip to content

S1SBP6A fix RTC range #13880

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
Nov 12, 2020
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
29 changes: 29 additions & 0 deletions targets/TARGET_Samsung/TARGET_SIDK_S1SBP6A/device/s1sbp6a_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ uint32_t bp6a_rtc_bin2bcd(uint8_t bin)
return ((bin / 10u) << 4u) + (bin % 10);
}

void bp6a_rtc_offset_write(uint32_t offset, uint32_t flag)
{
uint8_t i;

bp6a_rtc_unlock(true);
for (i = 0; i < 4; i++)
putreg32(0x40019000 + 0xB8 + i * 4, ((offset >> (i * 8)) & 0xFF));

putreg32(0x40019000 + 0x90, flag);
bp6a_rtc_unlock(false);
}

uint32_t bp6a_rtc_read_offset(uint32_t *flag)
{
uint8_t i;
uint32_t offset = 0;

bp6a_rtc_unlock(true);

for (i = 0; i < 4; i++)
offset |= getreg32(0x40019000 + 0xB8 + i * 4) << (i * 8);

*flag = getreg32(0x40019000 + 0x90);

bp6a_rtc_unlock(false);

return offset;
}

void bp6a_rtc_getdatetime(struct rtc_bcd_s *rtc)
{
bp6a_rtc_unlock(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ void bp6a_rtc_getdatetime(struct rtc_bcd_s *rtc);
void bp6a_rtc_setdatetime(struct rtc_bcd_s *rtc);
void bp6a_rtc_init(void);
void bp6a_set_rtc_delay(uint32_t delay);

uint32_t bp6a_rtc_read_offset(uint32_t *flag);
void bp6a_rtc_offset_write(uint32_t offset, uint32_t flag);
#endif /*__S1SBP6A_RTC_H */
12 changes: 12 additions & 0 deletions targets/TARGET_Samsung/TARGET_SIDK_S1SBP6A/rtc_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
#include "mbed_debug.h"

#define BP6A_RTC_START_TIME 946684800
#define BP6A_MAX_REG 0x0FFFFFFF
static bool rtc_initialized = false;
static bool g_before2000 = false;
static time_t g_rtc_offset = 0;

void rtc_init(void)
{
Expand All @@ -40,6 +42,7 @@ void rtc_init(void)

bp6a_set_rtc_delay((uint32_t)((float)sys_clk * 2 / 32789));
bp6a_rtc_init();
g_rtc_offset = bp6a_rtc_read_offset(&g_before2000);
rtc_initialized = true;
}
}
Expand Down Expand Up @@ -73,6 +76,8 @@ time_t rtc_read(void)
return 0;
}

t += g_rtc_offset;

if (g_before2000) {
t -= BP6A_RTC_START_TIME;
}
Expand All @@ -84,6 +89,12 @@ void rtc_write(time_t t)
struct rtc_bcd_s rtc_val;
struct tm timeinfo;

if (t > BP6A_MAX_REG) {
g_rtc_offset = t;
t = 0;
} else
g_rtc_offset = 0;

/*BP6A : The implicit number of thousands place is 20.*/
if (t < BP6A_RTC_START_TIME) {
g_before2000 = true;
Expand All @@ -92,6 +103,7 @@ void rtc_write(time_t t)
g_before2000 = false;
}

bp6a_rtc_offset_write(g_rtc_offset, (uint32_t)g_before2000);

if (_rtc_localtime(t, &timeinfo, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
return;
Expand Down