🐮_d6RHfJ · 2021年12月28日

【XR806开发板试用】xr806 RTC实验

一、例程编译、烧录确认
首先按照全志在线文档平台的点灯教程确保能正常编译、烧录和点灯:https://xr806.docs.aw-ol.com/...
确保例程没问题后,我们再改造例程,实现我们想要的功能
二、代码编写
我们将hello工程复制一份改文件夹名为rtc\_demo,目录结构如下

rtc\_demo
├── BUILD.gn
└── src
└── main.c
然后BUILD.gn内容增加头文件引用,部分配置如下:

import("//device/xradio/xr806/liteos_m/config.gni")

static_library("app_rtc") {
   configs = []

   sources = [
      "src/main.c",
   ]

   cflags = board_cflags

   include_dirs = board_include_dirs
   include_dirs += [
      "//kernel/liteos_m/kernel/arch/include",
   ]
}
~~~~
上一层的BUILD.gn内容为:

group("ohosdemo") {

deps = [
    #"hello_demo:app_hello",
    #"led_demo:app_led",
    #"iot_peripheral:app_peripheral",
    #"wlan_demo:app_WlanTest",
    #"http_demo:app_myhttp",
    #"tcp_demo:app_tcp",
    #"web_server_demo:app_web",
    #"uart_demo:app_uart",
    #"pwm_led_demo:app_pwm_led",
    "rtc_demo:app_rtc",
]

}

接下来是main.c,内容如下:

#include <stdio.h>
#include "ohos_init.h"
#include "kernel/os/os.h"
#include "driver/chip/hal_rtc.h"
#include <math.h>

#define OFFSET_YEAR                                      2000
#define GET_AD_YEAR(counterYear)                         (counterYear + OFFSET_YEAR)
#define GET_COUNTER_YEAR(adYear)                         (adYear - OFFSET_YEAR)
#define RTC_SET_LEAP_YEAR(isLeapYear)                    HAL_RTC_SetLeapYear(isLeapYear)
#define RTC_SET_DDHHMMSS(wday, hour, minute, second)     HAL_RTC_SetDDHHMMSS(wday, hour, minute, second)
#define RTC_SET_YYMMDD(isLeapYear, year, month, mday)    HAL_RTC_SetYYMMDD(isLeapYear, year, month, mday)
#define RTC_STOP_WDAY_ALARM(void)                        HAL_RTC_StopWDayAlarm(void)
#define RTC_STOP_SEC_ALARM(void)                         HAL_RTC_StopSecAlarm(void)

void rtc_get_yymmdd(uint8_t *isLeapYear, uint8_t *year, uint8_t *month, uint8_t *mday)
{
    HAL_RTC_GetYYMMDD(isLeapYear, year, month, mday);
}

void rtc_get_ddhhmmss(RTC_WeekDay *wday, uint8_t *hour, uint8_t *minute, uint8_t *second)
{
    HAL_RTC_GetDDHHMMSS(wday, hour, minute, second);
}

void analysis_wday(RTC_WeekDay wday, char *buf)
{
    if (wday == 0)
        sprintf(buf, "%s", "monday");
    else if (wday == 1)
        sprintf(buf, "%s", "tuesday");
    else if (wday == 2)
        sprintf(buf, "%s", "wednesday");
    else if (wday == 3)
        sprintf(buf, "%s", "thursday");
    else if (wday == 4)
        sprintf(buf, "%s", "friday");
    else if (wday == 5)
        sprintf(buf, "%s", "saturday");
    else if (wday == 6)
        sprintf(buf, "%s", "sunday");
}

/**
 * @brief Determine whether it is a leap year
 * @param[in] adYear : AD. year
 * @return if it is a leap year return 1 else 0
 */
uint8_t is_leap_year(uint16_t adYear)
{
    if ((adYear % 4 == 0 && adYear % 100 != 0)
        || (adYear % 400 == 0 && adYear % 3200 != 0)
        || (adYear % 172800 == 0))
        return 1;
    else
        return 0;
}

/**
 * @brief  transform real AD year to counter year and Set the RTC date,
 * including leaf year flag, year, month and month day
 * @param[in] adYear : real AD year
 * @return :  return value < 0 on erro
 *
 * @note adYear = counter year + OFFSET_YEAR, counter year  set to [0, 255]
 */
int rtc_set_yymmdd(uint16_t adYear, uint8_t month, uint8_t mday)
{
    if (GET_COUNTER_YEAR(adYear) < 0) {
        printf("\n\nERRO: adYear should be greater than OFFSET_YEAR!\n\n");
        return -1;
    } else if (GET_COUNTER_YEAR(adYear) > 255) {
        printf("\n\nERRO: adYear should be less than (OFFSET_YEAR+255)!\n\n");
        return -2;
    }

    RTC_SET_YYMMDD(is_leap_year(adYear), GET_COUNTER_YEAR(adYear), month, mday);

    return 0;
}

void rtc_set_time(void)
{
    /*set time : year mouth day hour minute second*/
    printf("set time : 2021-21-27, tuesday, 15:57:50\n");
    rtc_set_yymmdd(2021, 12, 27);
    RTC_SET_DDHHMMSS(RTC_WDAY_TUESDAY, 15, 57, 50);
}


void rtc_read_time(void)
{
    uint8_t leap, counterYear, mouth, mday;
    uint16_t adYear;

    RTC_WeekDay wday;
    uint8_t hour, minute, second;
    char buf[10];

    printf("read time:\n");
    rtc_get_yymmdd(&leap, &counterYear, &mouth, &mday);
    rtc_get_ddhhmmss(&wday, &hour, &minute, &second);

    analysis_wday(wday, buf);
    adYear = GET_AD_YEAR(counterYear);

    if (is_leap_year(adYear))
        printf("Is Leap Year\n");
    printf("%d-%d-%d, ", adYear, mouth, mday);
    printf("%s,%d:%d:%d\n", buf, hour, minute, second);
}

void rtc_reset_leap_year(void)
{
    uint8_t isLeapYear, counterYear, mouth, day;
    uint16_t adYear;

    rtc_get_yymmdd(&isLeapYear, &counterYear, &mouth, &day);
    adYear = GET_AD_YEAR(counterYear);
    isLeapYear = is_leap_year(adYear);
    RTC_SET_LEAP_YEAR(isLeapYear);
}

void wday_alarm_callback(void *arg)
{
    rtc_read_time();
    RTC_STOP_WDAY_ALARM();
    printf("wday alarm is arrive!!!\n\n");
}

void wday_alarm(void)
{
    RTC_WDayAlarmStartParam wday_param;

    printf("\nset wday alarm next day 0:0:20 \n");
    wday_param.alarmHour = 0;
    wday_param.alarmMinute = 0;
    wday_param.alarmSecond = 20;
    wday_param.alarmWDayMask = RTC_WDAY_ALARM_EN_BIT(RTC_WDAY_SUNDAY);
    wday_param.arg = NULL;
    wday_param.callback = wday_alarm_callback;

    HAL_RTC_StartWDayAlarm(&wday_param);
}

void sec_alarm_callback(void *arg)
{
    rtc_read_time();
    printf("second alarm is arrive!!!\n\n");
}

void sec_alarm(void)
{
    uint32_t alarmSecond;
    RTC_SecAlarmStartParam secParam;

    alarmSecond = 20;
    printf("set sec alarm %us after\n", alarmSecond);
    secParam.alarmSeconds = alarmSecond;
    secParam.arg = NULL;
    secParam.callback = sec_alarm_callback;
    HAL_RTC_StartSecAlarm(&secParam);
}

/**
 * @brief Read the time value (in microsecond) of the RTC's Free running counter
 *
 * Free running counter is a 48-bit counter which is driven by LFCLK and starts
 * to count as soon as the system reset is released and the LFCLK is ready.
 *
 * @note can't printf more than 32 bit digital
 */
void read_free_run_time(void)
{
    uint64_t freeRunTime = 0;

    freeRunTime = HAL_RTC_GetFreeRunTime();
    if (freeRunTime < pow(2, 32))
        printf("ReadFreeRunTime: %u uS\n", (uint32_t)freeRunTime);
}
void show_run_time(void)
{
    static int count = 40;
    uint8_t isShow = 1;

    printf("rtc show run time %d times start.\n", count);

    while (isShow) {
        if (count > 0) {
            read_free_run_time();
            count--;
        } else
            isShow = 0;
        OS_Sleep(1);
    }

    printf("rtc show run time over.\n");
}

int rtc_init(void)
{
    rtc_set_time();
    rtc_read_time();

    wday_alarm();
    sec_alarm();
    show_run_time();

    return 0;
}

void rtc_deinit(void)
{
    RTC_STOP_SEC_ALARM();
    RTC_STOP_WDAY_ALARM();
}

/* Run this example, please connect the uart0 */
int rtc_test(void)
{
    printf("rtc example started\n\n");
    /*waiting for  rtc stability*/
    OS_MSleep(200);

    rtc_init();

    while (1) {
        OS_Sleep(1);
        rtc_read_time();
        rtc_reset_leap_year();
    }
}

SYS_RUN(rtc_test);

要想实现设置一次,一直运行rtc的话板子没有后备电池,所以,我也没有将设置时间的标志位写进flash,所以每次启动都会设置时间,这个只是一个rtc的demo

推荐阅读
关注数
13823
内容数
139
全志XR806开发板相关的知识介绍以及应用专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息