laoxiao

laoxiao

0个粉丝

6

问答

0

专栏

1

资料

laoxiao  发布于  2016-08-22 08:02:38
采纳率 0%
6个问答
4343

hi3520d的RTC和系统是时间设置,修改自sdk中的rtc_test.c

 
加载hi_rtc.ko,然后通过rtc_test.c来修改读取rtc时间,这个很多人都会了。
但是rtc时间怎么同步到系统时间,系统时间怎么同步到rtc上,就不那么方便了。
因此我参考网友的代码,在rtc_test.c上增加了2个参数。

建个启动文件/etc/init.d/S1000rtc,这样开机就能自动把rtc时间同步到系统上了

[code]#!/bin/sh
cd /mpp/ko //根据自己ko文件路径修改
./load3520D -i
rtctool -rtc2sys[/code]


Usage: ./test [options] [parameter1] ...
Options:
        -s(set)         Set time/alarm,     e.g '-s time 2012/7/15/13/37/59'
        -g(get)         Get time/alarm,     e.g '-g alarm'
        -w(write)       Write RTC register, e.g '-w '
        -sys2rtc        Read the system time and write it to the RTC
        -rtc2sys        Read the RTC and write it to the system time

        -r(ead)         Read RTC register,  e.g '-r '
        -a(alarm)       Alarm ON/OFF',      e.g '-a ON'
        -reset          RTC reset
        -c(ompensation) temperature compensation ON/OFF, eg '-c ON'
        -f(requency)    frequency precise adjustment, eg '-f '
        -m(mode)        Mode of temperature gather, e.g '-m , mode[  

[code]/*
* RTC sample&test code.
*/

#include
#include
#include
#include
#include
#include
#include
#include

#include "hi_rtc.h"

void usage(void)
{
        printf(
                        "\n"
                        "Usage: ./test [options] [parameter1] ...\n"
                        "Options: \n"
                        "        -s(set)                Set time/alarm,     e.g '-s time 2012/7/15/13/37/59'\n"
                        "        -g(get)                Get time/alarm,     e.g '-g alarm'\n"
                        "        -w(write)        Write RTC register, e.g '-w '\n"
                        "        -sys2rtc        Read the system time and write it to the RTC\n"
                        "        -rtc2sys        Read the RTC and write it to the system time\n"
                        "        -r(ead)                Read RTC register,  e.g '-r '\n"
                        "        -a(alarm)        Alarm ON/OFF',      e.g '-a ON'\n"
                        "        -reset                RTC reset\n"
                        "        -c(ompensation) temperature compensation ON/OFF, eg '-c ON'\n"
                        "        -f(requency)    frequency precise adjustment, eg '-f '\n"
                        "        -m(mode)        Mode of temperature gather, e.g '-m , mode[0-2]'\n"
                        "\n");
        exit(1);
}


static int _atoul(const char *str, unsigned char *pvalue)
{
        unsigned int result=0;

        while (*str)
        {
                if (isdigit((int)*str))
                {
                        if ((result<429496729) || ((result==429496729) && (*str<'6')))
                        {
                                result = result*10 + (*str)-48;
                        }
                        else
                        {
                                *pvalue = result;
                                return -1;
                        }
                }
                else
                {
                        *pvalue=result;
                        return -1;
                }
                str++;
        }
        *pvalue=result;
        return 0;
}


#define ASC2NUM(ch) (ch - '0')
#define HEXASC2NUM(ch) (ch - 'A' + 10)

static int  _atoulx(const char *str, unsigned char *pvalue)
{
        unsigned int   result=0;
        unsigned char  ch;

        while (*str)
        {
                ch=toupper(*str);
                if (isdigit(ch) || ((ch >= 'A') && (ch <= 'F' )))
                {
                        if (result < 0x10000000)
                        {
                                result = (result << 4) + ((ch<='9')?(ASC2NUM(ch)):(HEXASC2NUM(ch)));
                        }
                        else
                        {
                                *pvalue=result;
                                return -1;
                        }
                }
                else
                {
                        *pvalue=result;
                        return -1;
                }
                str++;
        }

        *pvalue=result;
        return 0;
}

/*used for convert hex value from string to int*/
static int str_to_num(const char *str, unsigned char *pvalue)
{
        if ( *str == '0' && (*(str+1) == 'x' || *(str+1) == 'X') ){
                if (*(str+2) == '\0'){
                        return -1;
                }
                else{
                        return _atoulx(str+2, pvalue);
                }
        }
        else {
                return _atoul(str,pvalue);
        }
}

/*used for convert time frome string to struct rtc_time_t*/
static int parse_string(char *string, rtc_time_t *p_tm)
{
        char *comma, *head;
        int value[10];
        int i;

        if (!string || !p_tm)
                return -1;

        if (!strchr(string, '/'))
                return -1;

        head = string;
        i = 0;
        comma = NULL;

        for(;;) {       
                comma = strchr(head, '/');

                if (!comma){
                        value[i++] = atoi(head);
                        break;
                }

                *comma = '\0';
                value[i++] = atoi(head);
                head = comma+1;       
        }
       
        if (i < 5)
                return -1;

        p_tm->year   = value[0];
        p_tm->month  = value[1];
        p_tm->date   = value[2];
        p_tm->hour   = value[3];
        p_tm->minute = value[4];
        p_tm->second = value[5];
        p_tm->weekday = 0;

        return 0;
}

int main(int argc, const char *argv[])
{
        rtc_time_t tm;
        reg_data_t regv;
        reg_temp_mode_t mode;
        int ret = -1;
        int fd = -1;
        const char *dev_name = "/dev/hi_rtc";
        char string[50];

        if (argc < 2){
                usage();
                return 0;
        }

        fd = open(dev_name, O_RDWR);
        if (!fd) {
                printf("open %s failed\n", dev_name);
                return -1;
        }

        if (!strcmp(argv[1],"-s")) {
                if (argc < 3) {
                        usage();
                        goto err1;       
                }

                if (!strcmp(argv[2], "time")) {

                        strcpy(string, argv[3]);

                        ret = parse_string(string, &tm);
                        if (ret < 0)
                        {
                                printf("parse time param failed\n");
                                goto err1;
                        }
                        printf("set time\n");
#if 1                       
                        /* code */
                        printf("year:%d\n", tm.year);
                        printf("month:%d\n",tm.month);
                        printf("date:%d\n", tm.date);
                        printf("hour:%d\n", tm.hour);
                        printf("minute:%d\n", tm.minute);
                        printf("second:%d\n", tm.second);
#endif                       
                        ret = ioctl(fd, HI_RTC_SET_TIME, &tm);
                        if (ret < 0) {
                                printf("ioctl: HI_RTC_SET_TIME failed\n");
                                goto err1;
                        }       
                } else if (!strcmp(argv[2], "alarm")) {

                        strcpy(string, argv[3]);

                        ret = parse_string(string, &tm);
                        if (ret < 0) {
                                printf("parse alarm param failed\n");
                                goto err1;
                        }
                        printf("set alarm\n");
#if 1                       
                        printf("year:%d\n", tm.year);
                        printf("month:%d\n",tm.month);
                        printf("date:%d\n", tm.date);
                        printf("hour:%d\n", tm.hour);
                        printf("minute:%d\n", tm.minute);
                        printf("second:%d\n", tm.second);
#endif                       
                        ret = ioctl(fd, HI_RTC_ALM_SET, &tm);
                        if (ret < 0) {
                                printf("ioctl: HI_RTC_ALM_SET failed\n");
                                goto err1;
                        }       
                 } else {
                        printf("unknown options %s\n", argv[2]);
                        goto err1;
                }
        } else if (!strcmp(argv[1],"-g")) {
                if (argc < 3) {
                        usage();
                        goto err1;       
                }

                if (!strcmp(argv[2], "time")) {

                        printf("[RTC_RD_TIME]\n");
                       
                        ret = ioctl(fd, HI_RTC_RD_TIME, &tm);
                        if (ret < 0) {
                                printf("ioctl: HI_RTC_RD_TIME failed\n");
                                goto err1;
                        }
                       
                        printf("Current time value: \n");
                } else if (!strcmp(argv[2], "alarm")) {
               
                        printf("[RTC_RD_ALM]\n");

                        ret = ioctl(fd, HI_RTC_ALM_READ, &tm);
                        if (ret < 0) {
                                printf("ioctl: HI_RTC_ALM_READ failed\n");
                                goto err1;
                        }
                       
                        printf("Current alarm value: \n");
                } else {
                        printf("unknow options %s\n", argv[2]);
                        goto err1;
                }

                printf("year %d\n", tm.year);
                printf("month %d\n", tm.month);
                printf("date %d\n", tm.date);
                printf("hour %d\n", tm.hour);
                printf("minute %d\n", tm.minute);
                printf("second %d\n", tm.second);
                printf("weekday %d\n", tm.weekday);
               
        } else if (!strcmp(argv[1],"-w")) {

                if (argc < 4) {
                        usage();
                        goto err1;
                }

                ret = str_to_num(argv[2], &(regv.reg));
                if (ret != 0) {
                        printf("reg 0x%08x invalid\n", regv.reg);
                        goto err1;
                }

                ret = str_to_num(argv[3], &(regv.val));
                if (ret != 0) {
                        printf("val 0x%08x invalid\n", regv.val);
                        goto err1;
                }
               
                printf("\n");
                printf("[RTC_REG_SET] reg:%02x, val:%02x\n", regv.reg, regv.val);
                printf("\n");

                ret = ioctl(fd, HI_RTC_REG_SET, ®v);
                if (ret < 0) {
                        printf("ioctl: HI_RTC_REG_SET failed\n");
                        goto err1;
                }

        } else if (!strcmp(argv[1],"-r")) {

                if (argc < 3) {
                        usage();
                        goto err1;
                }
               
                ret = str_to_num(argv[2], &(regv.reg));
                if (ret != 0) {
                        printf("reg 0x%08x invalid\n", regv.reg);
                        goto err1;
                }

                regv.val = 0;

                ret = ioctl(fd, HI_RTC_REG_READ, ®v);
                if (ret < 0) {
                        printf("ioctl: HI_RTC_REG_READ failed\n");
                        goto err1;
                }

                printf("\n");
                printf("[RTC_REG_GET] reg:0x%02x, val:0x%02x\n", regv.reg, regv.val);
                printf("\n");
        } else if (!strcmp(argv[1],"-a")) {

                if (argc < 3) {
                        usage();
                        goto err1;
                }

                if (!strcmp(argv[2], "ON")) {
                        ret = ioctl(fd, HI_RTC_AIE_ON);
                } else if (!strcmp(argv[2], "OFF")) {
                        ret = ioctl(fd, HI_RTC_AIE_OFF);
                }

                if (ret < 0) {
                        printf("ioctl: HI_RTC_AIE_ON/OFF failed\n");
                        goto err1;
                }

        } else if (!strcmp(argv[1],"-reset")) {

                printf("[RTC_RESET]\n");

                ret = ioctl(fd, HI_RTC_RESET);
                if(ret){
                        printf("reset err\n");
                        goto err1;
                }
        } else if (!strcmp(argv[1],"-sys2rtc")) {// add by xiaojianbin @2016/8/12

                printf("[Read the system time and write it to the RTC]\n");

                time_t sys_time;
                struct tm *p_time;
                rtc_time_t rtc_time;
                time(&sys_time);
                p_time = gmtime(&sys_time);

                printf("time year:%d\n"
                        "month :%d\n"
                        "day : %d\n"
                        "h :%d\n"
                        "min :%d\n"
                        "second %d\n",
                        p_time->tm_year+1900,
                        p_time->tm_mon+1,
                        p_time->tm_mday,
                        p_time->tm_hour + 8,
                        p_time->tm_min,
                        p_time->tm_sec);
                printf("system date :\r\n");
                system("date");

                rtc_time.year        = p_time->tm_year + 1900;
                rtc_time.month        = p_time->tm_mon + 1;
                rtc_time.date        = p_time->tm_mday;
                rtc_time.hour        = p_time->tm_hour;
                rtc_time.minute = p_time->tm_min;
                rtc_time.second        = p_time->tm_sec;

                ret = ioctl(fd, HI_RTC_SET_TIME, &rtc_time);
                if(ret < 0){
                        printf("set rtc of localtime error!\r\n");
                        goto err1;
                }
        } else if (!strcmp(argv[1],"-rtc2sys")) {// add by xiaojianbin @2016/8/12

                printf("[Read the RTC and write it to the system time]\n");

                rtc_time_t        rtc_time;
                struct timeval                val_time;
                struct tm        tm_time;
                // 1 get the time from rtc
                // ioctl HI_RTC_RD_TIME       

                ret = ioctl(fd, HI_RTC_RD_TIME, &rtc_time);
                if (ret < 0) {
                        printf("ioctl: HI_RTC_RD_TIME failed\n");
                        goto err1;
                }

                tm_time.tm_year        = rtc_time.year - 1900;
                tm_time.tm_mon        = rtc_time.month -1;
                tm_time.tm_mday        = rtc_time.date;
                tm_time.tm_hour        = rtc_time.hour;
                tm_time.tm_min        = rtc_time.minute;
                tm_time.tm_sec        = rtc_time.second;
                tm_time.tm_wday        = rtc_time.weekday;

                val_time.tv_sec                = mktime(&tm_time);
                val_time.tv_usec        = 0;
                settimeofday(&val_time,NULL);

                printf("the value will write to the system time is:"
                        "year:%d\n"
                        "month%d\n"
                        "day:%d\n"
                        "hour:%d\n"
                        "min:%d\n"
                        "sec:%d\n",
                        tm_time.tm_yday,
                        tm_time.tm_mon,
                        tm_time.tm_mday,
                        tm_time.tm_hour,
                        tm_time.tm_min,
                        tm_time.tm_sec);

        } else if (!strcmp(argv[1], "-c")) {

                if (argc < 3) {
                        usage();
                        goto err1;
                }

                if (!strcmp(argv[2], "ON")) {
                        //printf("RTC temperature compensation on!\n");
                        ret = ioctl(fd, HI_RTC_COMP_ON);
                } else if (!strcmp(argv[2], "OFF")) {
                        //printf("RTC temperature compensation off!\n");
                        ret = ioctl(fd, HI_RTC_COMP_OFF);
                }

                if (ret < 0) {
                        printf("ioctl: HI_RTC_COMP_ON/OFF failed\n");
                        goto err1;
                }
        } else if (!strcmp(argv[1], "-f")) {
               
                unsigned int freq;
                rtc_freq_t value;
               
                // print current frequency value
                if (argc  < 3)
                {
                        ret = ioctl(fd, HI_RTC_GET_FREQ, &value);
                        if (ret < 0)
                        {
                                printf("get current frequency failed\n");
                                goto err1;
                        }       
                       
                        freq = value.freq_l;
#if 0
                        if (freq > 3277000 || freq < 3276000)
                        {
                                printf("get invalid freq %d\n", freq);
                                goto err1;
                        }
#endif
                        printf("current frequency : %d\n", freq);       
                }       
                // set frequency
                else if (argc == 3)
                {
                        freq = atoi(argv[2]);
                       
                        if (freq > 3277000 || freq < 3276000)
                        {
                                printf("invalid freq %d\n", freq);
                                goto err1;
                        }
               
                        value.freq_l = freq;
                       
                        ret = ioctl(fd, HI_RTC_SET_FREQ, &value);
                        if (ret < 0)
                        {
                                printf("get current frequency failed\n");
                                goto err1;
                        }
                }

        } else if (!strcmp(argv[1], "-m")) {

                int mod, value;       
               
                if (argc < 3) {
                        usage();
                        goto err1;
                }

                mod = atoi(argv[2]);

                if (mod > 2 || mod < 0) {
                        printf("invalid mode %d\n", mod);
                        goto err1;
                }
               
                if (mod == 0) {
                        if (argc < 4) {
                                usage();
                                goto err1;
                        }

                        value = atoi(argv[3]);
                }
                else {
                        value = 0;
                }
               
                printf("[RTC_SET_TEMP_MODE] %d\n", mod);

                mode.mode = (enum temp_sel_mode)mod;       
                mode.value = value;

                ret = ioctl(fd, HI_RTC_SET_TEMP_MODE, &mode);
                if(ret) {
                        printf("ioctl: HI_RTC_SET_TEMP_MODE failed\n");
                        goto err1;
                }
        } else {
                printf("unknown download mode.\n");
                goto err1;
        }

err1:
        close(fd);

        return 0;
}
[/code]
我来回答
回答3个
时间排序
认可量排序

knaffe

0个粉丝

3

问答

0

专栏

0

资料

knaffe 2016-10-15 10:11:42
认可0
我加载./load3520D -i
SDK_TYPE type is 8d1
insmod: can't insert 'mmz.ko': File exists
******* Error!! Shell exit for error *****
这里会报错,无法使用 rtctool -rtc2sys
请教如何设置和同步系统时间?

qn1511230077

0个粉丝

3

问答

0

专栏

0

资料

qn1511230077 2017-12-25 15:24:00
认可0
本帖最后由 qn1511230077 于 2017-12-25 17:12 编辑

楼主你好,用您的代码,可以把rtc的时间同步到系统,但是系统时间同步到rtc总是出错

年月日不对 时分秒是正确的,求解!

qn1511230077

0个粉丝

3

问答

0

专栏

0

资料

qn1511230077 2017-12-25 17:31:13
认可0
多谢楼主的参考代码,在-sys2rtc的代码,在赋值的时候一定要给weekday赋值,不然会出错
或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
+ 添加网盘链接/附件

Markdown 语法

  • 加粗**内容**
  • 斜体*内容*
  • 删除线~~内容~~
  • 引用> 引用内容
  • 代码`代码`
  • 代码块```编程语言↵代码```
  • 链接[链接标题](url)
  • 无序列表- 内容
  • 有序列表1. 内容
  • 缩进内容
  • 图片![alt](url)
举报反馈

举报类型

  • 内容涉黄/赌/毒
  • 内容侵权/抄袭
  • 政治相关
  • 涉嫌广告
  • 侮辱谩骂
  • 其他

详细说明

易百纳技术社区