2021-02-04 13:51:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021, Bluetrum Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-01-28 greedyhao first version
|
2021-03-19 11:08:00 +08:00
|
|
|
* 2021-03-19 iysheng modify just set time first power up
|
2021-03-26 23:26:17 +08:00
|
|
|
* 2021-03-26 iysheng add alarm and 1s interrupt support
|
2021-02-04 13:51:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "board.h"
|
2021-02-07 21:08:41 +08:00
|
|
|
#include <sys/time.h>
|
2021-02-04 13:51:55 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_ONCHIP_RTC
|
|
|
|
|
2021-11-08 10:19:08 +08:00
|
|
|
#if RTTHREAD_VERSION < 40004
|
|
|
|
#error "RTTHREAD_VERSION is less than 4.0.4"
|
|
|
|
#endif
|
|
|
|
|
2021-02-04 13:51:55 +08:00
|
|
|
//#define DRV_DEBUG
|
|
|
|
#define LOG_TAG "drv.rtc"
|
|
|
|
#include <drv_log.h>
|
|
|
|
|
|
|
|
static struct rt_device rtc;
|
|
|
|
|
|
|
|
/************** HAL Start *******************/
|
|
|
|
#define IRTC_ENTER_CRITICAL() uint32_t cpu_ie = PICCON & BIT(0); PICCONCLR = BIT(0);
|
2021-10-11 15:56:02 +08:00
|
|
|
#define IRTC_EXIT_CRITICAL() PICCON |= cpu_ie
|
2021-02-04 13:51:55 +08:00
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t get_weekday(struct tm *const _tm)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t weekday;
|
2021-02-07 21:08:41 +08:00
|
|
|
time_t secs = timegm(_tm);
|
2021-02-04 13:51:55 +08:00
|
|
|
|
|
|
|
weekday = (secs / 86400 + 4) % 7;
|
|
|
|
return weekday;
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
void irtc_write(rt_uint32_t cmd)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
RTCDAT = cmd;
|
|
|
|
while (RTCCON & RTC_CON_TRANS_DONE);
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t irtc_read(void)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
RTCDAT = 0x00;
|
|
|
|
while (RTCCON & RTC_CON_TRANS_DONE);
|
2021-10-11 15:56:02 +08:00
|
|
|
return (rt_uint8_t)RTCDAT;
|
2021-02-04 13:51:55 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
void irtc_time_write(rt_uint32_t cmd, rt_uint32_t dat)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
IRTC_ENTER_CRITICAL();
|
|
|
|
RTCCON |= RTC_CON_CHIP_SELECT;
|
|
|
|
irtc_write(cmd | RTC_WR);
|
2021-10-11 15:56:02 +08:00
|
|
|
irtc_write((rt_uint8_t)(dat >> 24));
|
|
|
|
irtc_write((rt_uint8_t)(dat >> 16));
|
|
|
|
irtc_write((rt_uint8_t)(dat >> 8));
|
|
|
|
irtc_write((rt_uint8_t)(dat >> 0));
|
2021-02-04 13:51:55 +08:00
|
|
|
RTCCON &= ~RTC_CON_CHIP_SELECT;
|
|
|
|
IRTC_EXIT_CRITICAL();
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint32_t irtc_time_read(rt_uint32_t cmd)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint32_t rd_val;
|
2021-02-04 13:51:55 +08:00
|
|
|
IRTC_ENTER_CRITICAL();
|
|
|
|
RTCCON |= RTC_CON_CHIP_SELECT;
|
|
|
|
irtc_write(cmd | RTC_RD);
|
2021-10-11 15:56:02 +08:00
|
|
|
*((rt_uint8_t *)&rd_val + 3) = irtc_read();
|
|
|
|
*((rt_uint8_t *)&rd_val + 2) = irtc_read();
|
|
|
|
*((rt_uint8_t *)&rd_val + 1) = irtc_read();
|
|
|
|
*((rt_uint8_t *)&rd_val + 0) = irtc_read();
|
2021-02-04 13:51:55 +08:00
|
|
|
RTCCON &= ~RTC_CON_CHIP_SELECT;
|
|
|
|
IRTC_EXIT_CRITICAL();
|
|
|
|
return rd_val;
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
void irtc_sfr_write(rt_uint32_t cmd, rt_uint8_t dat)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
IRTC_ENTER_CRITICAL();
|
|
|
|
RTCCON |= RTC_CON_CHIP_SELECT;
|
|
|
|
irtc_write(cmd | RTC_WR);
|
|
|
|
irtc_write(dat);
|
|
|
|
RTCCON &= ~RTC_CON_CHIP_SELECT;
|
|
|
|
IRTC_EXIT_CRITICAL();
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t irtc_sfr_read(rt_uint32_t cmd)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t rd_val;
|
2021-02-04 13:51:55 +08:00
|
|
|
IRTC_ENTER_CRITICAL();
|
|
|
|
RTCCON |= RTC_CON_CHIP_SELECT;
|
|
|
|
irtc_write(cmd | RTC_RD);
|
|
|
|
rd_val = irtc_read();
|
|
|
|
RTCCON &= ~RTC_CON_CHIP_SELECT;
|
|
|
|
IRTC_EXIT_CRITICAL();
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:21:01 +08:00
|
|
|
static void _init_rtc_clock(void)
|
|
|
|
{
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t rtccon0;
|
|
|
|
rt_uint8_t rtccon2;
|
2021-03-23 10:21:01 +08:00
|
|
|
|
|
|
|
rtccon0 = irtc_sfr_read(RTCCON0_CMD);
|
|
|
|
rtccon2 = irtc_sfr_read(RTCCON2_CMD);
|
|
|
|
#ifdef RTC_USING_INTERNAL_CLK
|
|
|
|
rtccon0 &= ~RTC_CON0_XOSC32K_ENABLE;
|
|
|
|
rtccon0 |= RTC_CON0_INTERNAL_32K;
|
|
|
|
rtccon2 | RTC_CON2_32K_SELECT;
|
|
|
|
#else
|
|
|
|
rtccon0 |= RTC_CON0_XOSC32K_ENABLE;
|
|
|
|
rtccon0 &= ~RTC_CON0_INTERNAL_32K;
|
|
|
|
rtccon2 & ~RTC_CON2_32K_SELECT;
|
|
|
|
#endif
|
|
|
|
irtc_sfr_write(RTCCON0_CMD, rtccon0);
|
|
|
|
irtc_sfr_write(RTCCON2_CMD, rtccon2);
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:51:55 +08:00
|
|
|
void hal_rtc_init(void)
|
|
|
|
{
|
|
|
|
time_t sec = 0;
|
|
|
|
struct tm tm_new = {0};
|
2021-10-11 15:56:02 +08:00
|
|
|
rt_uint8_t temp;
|
2021-02-04 13:51:55 +08:00
|
|
|
|
2021-03-23 10:21:01 +08:00
|
|
|
_init_rtc_clock();
|
2021-02-04 13:51:55 +08:00
|
|
|
temp = irtc_sfr_read(RTCCON0_CMD);
|
2021-03-19 11:08:00 +08:00
|
|
|
if (temp & RTC_CON0_PWRUP_FIRST) {
|
|
|
|
temp &= ~RTC_CON0_PWRUP_FIRST;
|
2021-02-04 13:51:55 +08:00
|
|
|
irtc_sfr_write(RTCCON0_CMD, temp); /* First power on */
|
2021-03-19 11:08:00 +08:00
|
|
|
tm_new.tm_mday = 29;
|
|
|
|
tm_new.tm_mon = 1 - 1;
|
|
|
|
tm_new.tm_year = 2021 - 1900;
|
|
|
|
sec = timegm(&tm_new);
|
2021-02-04 13:51:55 +08:00
|
|
|
|
2021-03-19 11:08:00 +08:00
|
|
|
irtc_time_write(RTCCNT_CMD, sec);
|
|
|
|
}
|
2021-03-26 23:26:17 +08:00
|
|
|
#ifdef RT_USING_ALARM
|
|
|
|
RTCCON |= RTC_CON_ALM_INTERRUPT;
|
|
|
|
#ifdef RTC_USING_1S_INT
|
|
|
|
RTCCON |= RTC_CON_1S_INTERRUPT;
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-02-04 13:51:55 +08:00
|
|
|
}
|
|
|
|
/************** HAL End *******************/
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_err_t ab32_rtc_get_secs(void *args)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-08-08 16:32:56 +08:00
|
|
|
*(rt_uint32_t *)args = irtc_time_read(RTCCNT_CMD);
|
|
|
|
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
|
2021-03-26 23:26:17 +08:00
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
return RT_EOK;
|
2021-02-04 13:51:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_err_t ab32_rtc_set_secs(void *args)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-08-08 16:32:56 +08:00
|
|
|
irtc_time_write(RTCCNT_CMD, *(rt_uint32_t *)args);
|
2021-02-04 13:51:55 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_err_t ab32_rtc_get_alarm(void *args)
|
2021-03-26 23:26:17 +08:00
|
|
|
{
|
2021-08-08 16:32:56 +08:00
|
|
|
*(rt_uint32_t *)args = irtc_time_read(RTCALM_CMD);
|
2021-03-26 23:26:17 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_err_t ab32_rtc_set_alarm(void *args)
|
2021-03-26 23:26:17 +08:00
|
|
|
{
|
2021-08-08 16:32:56 +08:00
|
|
|
irtc_time_write(RTCALM_CMD, *(rt_uint32_t *)args);
|
2021-03-26 23:26:17 +08:00
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
return RT_EOK;
|
2021-03-26 23:26:17 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_err_t ab32_rtc_init(void)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
hal_rtc_init();
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
return RT_EOK;
|
2021-02-04 13:51:55 +08:00
|
|
|
}
|
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static const struct rt_rtc_ops ab32_rtc_ops =
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
2021-08-08 16:32:56 +08:00
|
|
|
ab32_rtc_init,
|
|
|
|
ab32_rtc_get_secs,
|
|
|
|
ab32_rtc_set_secs,
|
|
|
|
ab32_rtc_get_alarm,
|
|
|
|
ab32_rtc_set_alarm,
|
2021-02-04 13:51:55 +08:00
|
|
|
RT_NULL,
|
|
|
|
RT_NULL,
|
|
|
|
};
|
2021-03-26 23:26:17 +08:00
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static rt_rtc_dev_t ab32_rtc_dev;
|
2021-03-26 23:26:17 +08:00
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
static int rt_hw_rtc_init(void)
|
2021-02-04 13:51:55 +08:00
|
|
|
{
|
|
|
|
rt_err_t result;
|
2021-03-26 23:26:17 +08:00
|
|
|
|
2021-08-08 16:32:56 +08:00
|
|
|
ab32_rtc_dev.ops = &ab32_rtc_ops;
|
|
|
|
result = rt_hw_rtc_register(&ab32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
|
2021-02-04 13:51:55 +08:00
|
|
|
if (result != RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_E("rtc register err code: %d", result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
LOG_D("rtc init success");
|
2021-08-08 16:32:56 +08:00
|
|
|
|
2021-02-04 13:51:55 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
|
|
|
|
|
|
|
#endif /* BSP_USING_ONCHIP_RTC */
|