Matrix - RTC

From FriendlyELEC WiKi
Revision as of 00:07, 24 June 2016 by Yftan (Talk | contribs) (硬件连接)

Jump to: navigation, search

查看中文

1 Introduction

Matrix-RTC.png
  • DS1307串行实时时钟是一种低功耗,完整的二进制编码的十进制(BCD)时钟/日历加56位字节的NV SRAM。地址和数据通过IIC串行传输,双向总线。
  • 时钟/日历提供秒、分、时、日、星期、月和年的信息。月的最后一天自动调整月的日数少于31天,包括闰年的修正。时钟运行24小时或者12小时格式与AM/PM指标。

2 Features

  • I2C串口接口
  • 56字节、电池支持、通用的RAM和无限写道
  • 8-Pin DIP和8-Pin SO
  • 操作温度在-40度到85度
  • PCB尺寸(mm):24x32

Matrix-RTC PCB.png

  • Pin Description:
Pin Description
SDA I2C SDA
SCL I2C SCL
5V Supply Voltage 5V
GND Ground

3 Basic Device Operation

4 Applications

4.1 Connect to NanoPi M1

Refer to the following connection diagram to connect the module to the NanoPi M1:
Matrix-RTC_nanopi_m1

Connection Details:

Matrix-RTC NanoPi M1
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

4.2 Connect to NanoPi 2

Refer to the following connection diagram to connect the module to the NanoPi 2:
Matrix-RTC_nanopi_2

Connection Details:

Matrix-RTC NanoPi 2
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

4.3 Connect to NanoPi M2 / NanoPi 2 Fire

Refer to the following connection diagram to connect the module to the NanoPi M2/ NanoPi 2 Fire:
Matrix-RTC_nanopi_M2

Connection Details:

NanoPi M2
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

4.4 Connect to NanoPC-T2

Refer to the following connection diagram to connect the module to the NanoPC-T2:
Matrix-RTC_NanoPC-T2

Connection Details:

Matrix-RTC NanoPC-T2
SDA Pin6
SCL Pin5
5V Pin29
GND Pin30

5 编译运行测试程序

启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库:

$ apt-get update && apt-get install git
$ git clone https://github.com/friendlyarm/matrix.git

克隆完成后会得到一个名为matrix的目录。

编译并安装Matrix:

$ cd matrix
$ make && make install

运行测试程序:

$ matrix-rtc

注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
运行效果如下:

RTC Driver Test Example.
Set RTC date/time is 9-15-2015, 01:01:01.
Read RTC date/time is 9-15-2015, 01:01:01.
Test complete

该程序只是简单的读写硬件RTC,如果想设置Debian的系统时间并将其保持在Matrix-RTC模块里,可执行以下命令,假设当前时间为"2016-11-17 17:26:01":

$ modprobe rtc-ds1307
$ date -s "2016-11-17 17:26:01"
$ hwclock -w -f /dev/rtc-ds1307

然后修改/etc/modprobe.d/matrix-blacklist.conf,在"blacklist rtc_ds1307"前加上一个#,表示注释该行,这样下次开机就会自动加载驱动了。
重启系统,可以看到时间仍然是准确的:

$ hwclock -r -f /dev/rtc-ds1307
20161118日 星期五 08时2948秒  -0.492649 seconds

6 代码说明

所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rtc,内容如下:

int main(int argc, char **argv)
{
    int fd, retval, board;
    struct rtc_time rtc_tm;
    const char *rtc = default_rtc;
    const char *date_time = default_date_time;
 
    if ((board = boardInit()) < 0) {
        printf("Fail to init board\n");
        return -1;
    }
 
    switch (argc) {
    case 3:
        rtc = argv[1];
        date_time = argv[2];
        break;
    case 1:
        break;
    default:
        fprintf(stderr, "usage:  rtctest [rtcdev] [year mon day hour min sec]\n");
        return 1;
    }
    system("modprobe "DRIVER_MODULE);
    fd = open(rtc, O_RDONLY);
    if (fd ==  -1) {
        perror(rtc);
        goto err;
    }
    fprintf(stderr, "RTC Driver Test Example.\n");
 
    sscanf(date_time, "%d %d %d %d %d %d",
            &rtc_tm.tm_year,
            &rtc_tm.tm_mon,
            &rtc_tm.tm_mday,
            &rtc_tm.tm_hour,
            &rtc_tm.tm_min,
            &rtc_tm.tm_sec); 
    rtc_tm.tm_year -= 1900;
    rtc_tm.tm_mon -= 1;
    retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
    if (retval == -1) {
        perror("RTC_SET_TIME ioctl");
        goto err;
    }
 
    fprintf(stderr, "Set RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
            rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_year + 1900,
            rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
 
    /* Read the RTC time/date */
    retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
    if (retval == -1) {
        perror("RTC_RD_TIME ioctl");
        goto err;
    }
 
    fprintf(stderr, "Read RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",
            rtc_tm.tm_mon + 1, rtc_tm.tm_mday, rtc_tm.tm_year + 1900,
            rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
 
    fprintf(stderr, "Test complete\n");
    close(fd);
err:
    system("rmmod "DRIVER_MODULE);
    return 0;
}

API说明参考维基:Matrix API reference manual


7 Resources

DS1307.pdf