drv_rtc.c 2.8 KB
Newer Older
A
ACM32_MCU 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date         Author        Notes
 * 2021-08-31   AisinoChip    first add to bsp
 */

#include "board.h"
#include <sys/time.h>
#include <rtdevice.h>

#ifdef BSP_USING_RTC

static RTC_ConfigTypeDef RTC_Handle;

rt_inline rt_uint8_t dec2hex(rt_uint8_t dec)
{
    return ((dec / 10) << 4) + (dec % 10);
}

rt_inline rt_uint8_t hex2dec(rt_uint8_t hex)
{
    return ((hex / 16) * 10) + (hex % 16);
}

static time_t get_rtc_timestamp(void)
{
    RTC_TimeTypeDef RTC_TimeStruct = {0};
    RTC_DateTypeDef RTC_DateStruct = {0};
    struct tm tm_new = {0};

    HAL_RTC_GetTime(&RTC_TimeStruct);
    HAL_RTC_GetDate(&RTC_DateStruct);

    tm_new.tm_sec  = hex2dec(RTC_TimeStruct.u8_Seconds);
    tm_new.tm_min  = hex2dec(RTC_TimeStruct.u8_Minutes);
    tm_new.tm_hour = hex2dec(RTC_TimeStruct.u8_Hours);
    tm_new.tm_mday = hex2dec(RTC_DateStruct.u8_Date);
    tm_new.tm_mon  = hex2dec(RTC_DateStruct.u8_Month) - 1;
    tm_new.tm_year = hex2dec(RTC_DateStruct.u8_Year) + 100;

    return timegm(&tm_new);
}

static rt_err_t set_rtc_time_stamp(time_t time_stamp)
{
    RTC_TimeTypeDef RTC_TimeStruct = {0};
    RTC_DateTypeDef RTC_DateStruct = {0};
    struct tm *p_tm;

    p_tm = gmtime(&time_stamp);
    if (p_tm->tm_year < 100)
    {
        return -RT_ERROR;
    }

    RTC_TimeStruct.u8_Seconds = dec2hex(p_tm->tm_sec);
    RTC_TimeStruct.u8_Minutes = dec2hex(p_tm->tm_min);
    RTC_TimeStruct.u8_Hours   = dec2hex(p_tm->tm_hour);
    RTC_DateStruct.u8_Date    = dec2hex(p_tm->tm_mday);
    RTC_DateStruct.u8_Month   = dec2hex(p_tm->tm_mon + 1);
    RTC_DateStruct.u8_Year    = dec2hex(p_tm->tm_year - 100);
    RTC_DateStruct.u8_WeekDay = dec2hex(p_tm->tm_wday) + 1;

    HAL_RTC_SetTime(&RTC_TimeStruct);
    HAL_RTC_SetDate(&RTC_DateStruct);

    return RT_EOK;
}

static rt_err_t _rtc_init(void)
{
    RTC_Handle.u32_ClockSource       = RTC_CLOCK_XTL;
    RTC_Handle.u32_Compensation      = COMPENSATION_INCREASE;
    RTC_Handle.u32_CompensationValue = 0x05;

    HAL_RTC_Config(&RTC_Handle);

    return RT_EOK;
}

static rt_err_t _rtc_get_secs(void *args)
{
    *(rt_uint32_t *)args = get_rtc_timestamp();
    return RT_EOK;
}

static rt_err_t _rtc_set_secs(void *args)
{
    rt_err_t result = RT_EOK;
    if (set_rtc_time_stamp(*(rt_uint32_t *)args))
    {
        result = -RT_ERROR;
    }

    return result;
}

static const struct rt_rtc_ops acm32_rtc_ops =
{
    _rtc_init,
    _rtc_get_secs,
    _rtc_set_secs,
    RT_NULL,
    RT_NULL,
    RT_NULL,
    RT_NULL,
};

static rt_rtc_dev_t acm32_rtc_dev;

static int rt_hw_rtc_init(void)
{
    acm32_rtc_dev.ops = &acm32_rtc_ops;

    return rt_hw_rtc_register(&acm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /* BSP_USING_RTC */