Difference between revisions of "Matrix - RTC"

From FriendlyELEC WiKi
Jump to: navigation, search
(Download Matrix Source Code)
Line 3: Line 3:
 
==Introduction==
 
==Introduction==
 
[[File:Matrix-RTC.png|thumb|]]
 
[[File:Matrix-RTC.png|thumb|]]
*
+
*DS1307串行实时时钟是一种低功耗,完整的二进制编码的十进制(BCD)时钟/日历加56位字节的NV SRAM。地址和数据通过IIC串行传输,双向总线。
 +
*时钟/日历提供秒、分、时、日、星期、月和年的信息。月的最后一天自动调整月的日数少于31天,包括闰年的修正。时钟运行24小时或者12小时格式与AM/PM指标。
  
 
==Features==
 
==Features==
*
+
* I2C串口接口
 +
* 56字节、电池支持、通用的RAM和无限写道
 +
* 8-Pin DIP和8-Pin SO
 +
* 操作温度在-40度到85度
 +
* PCB尺寸(mm):24x32
 
[[File:Matrix-RTC_PCB.png|frameless|400px|]]
 
[[File:Matrix-RTC_PCB.png|frameless|400px|]]
  
Line 24: Line 29:
  
 
==Basic Device Operation==
 
==Basic Device Operation==
 +
 +
==硬件连接==
 +
===连接NanoPi M1===
 +
参考下图连接模块:<br>
 +
[[File:Matrix-RTC_nanopi_m1.jpg|frameless|600px|Matrix-RTC_nanopi_m1]]
 +
 +
连接说明:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-RTC || NanoPi M1     
 +
|-
 +
|SDA    || Pin3
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V    || Pin4
 +
|-
 +
|GND    || Pin6
 +
|}
 +
 +
===连接NanoPi 2===
 +
参考下图连接模块:<br>
 +
[[File:Matrix-RTC_nanopi_2.jpg|frameless|600px|Matrix-RTC_nanopi_2]]
 +
 +
连接说明:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-RTC || NanoPi 2
 +
|-
 +
|SDA    || Pin3
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V    || Pin4
 +
|-
 +
|GND    || Pin6
 +
|}
 +
 +
===连接NanoPi M2 / NanoPi 2 Fire===
 +
NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。<br>
 +
参考下图连接模块:<br>
 +
[[File:Matrix-RTC_nanopi_M2.jpg|frameless|600px|Matrix-RTC_nanopi_M2]]
 +
 +
连接说明:
 +
{| class="wikitable"
 +
|-
 +
| || NanoPi M2     
 +
|-
 +
|SDA    || Pin3
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V    || Pin4
 +
|-
 +
|GND    || Pin6
 +
|}
 +
 +
===连接NanoPC-T2===
 +
参考下图连接模块:<br>
 +
[[File:Matrix-RTC_NanoPC-T2.jpg|frameless|600px|Matrix-RTC_NanoPC-T2]]
 +
 +
连接说明:
 +
{| class="wikitable"
 +
|-
 +
|Matrix-RTC || NanoPC-T2
 +
|-
 +
|SDA    || Pin6
 +
|-
 +
|SCL    || Pin5
 +
|-
 +
|5V    || Pin29
 +
|-
 +
|GND    || Pin30
 +
|}
 +
 +
==编译运行测试程序==
 +
启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库:
 +
<syntaxhighlight lang="bash">
 +
$ apt-get update && apt-get install git
 +
$ git clone https://github.com/friendlyarm/matrix.git
 +
</syntaxhighlight>
 +
克隆完成后会得到一个名为matrix的目录。
 +
 +
编译并安装Matrix:
 +
<syntaxhighlight lang="bash">
 +
$ cd matrix
 +
$ make && make install
 +
</syntaxhighlight>
 +
 +
运行测试程序:
 +
<syntaxhighlight lang="bash">
 +
$ matrix-rtc
 +
</syntaxhighlight>
 +
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。<br>
 +
运行效果如下:<br>
 +
<syntaxhighlight lang="bash">
 +
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
 +
</syntaxhighlight>
 +
该程序只是简单的读写硬件RTC,如果想设置Debian的系统时间并将其保持在Matrix-RTC模块里,可执行以下命令,假设当前时间为"2016-11-17 17:26:01":
 +
<syntaxhighlight lang="bash">
 +
$ modprobe rtc-ds1307
 +
$ date -s "2016-11-17 17:26:01"
 +
$ hwclock -w -f /dev/rtc-ds1307
 +
</syntaxhighlight>
 +
然后修改/etc/modprobe.d/matrix-blacklist.conf,在"blacklist rtc_ds1307"前加上一个#,表示注释该行,这样下次开机就会自动加载驱动了。<br>
 +
重启系统,可以看到时间仍然是准确的:
 +
<syntaxhighlight lang="bash">
 +
$ hwclock -r -f /dev/rtc-ds1307
 +
2016年11月18日 星期五 08时29分48秒  -0.492649 seconds
 +
</syntaxhighlight>
 +
 +
==代码说明==
 +
所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rtc,内容如下:
 +
<syntaxhighlight lang="c">
 +
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;
 +
}
 +
</syntaxhighlight>
 +
API说明参考维基:[[Matrix API reference manual/zh|Matrix API reference manual]] <br>
 +
 +
 +
<!---
 
==Download Matrix Source Code==
 
==Download Matrix Source Code==
 
All the matrix modules' code samples are open source. They are maintained on GitHub - https://github.com/friendlyarm/matrix.git <br>
 
All the matrix modules' code samples are open source. They are maintained on GitHub - https://github.com/friendlyarm/matrix.git <br>
Line 302: Line 496:
  
 
==Connect to Arduino==
 
==Connect to Arduino==
 +
--->
  
 
==Resources==
 
==Resources==
 
[http://datasheets.maximintegrated.com/en/ds/DS1307.pdf DS1307.pdf]
 
[http://datasheets.maximintegrated.com/en/ds/DS1307.pdf DS1307.pdf]

Revision as of 00:04, 24 June 2016

查看中文

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 硬件连接

4.1 连接NanoPi M1

参考下图连接模块:
Matrix-RTC_nanopi_m1

连接说明:

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

4.2 连接NanoPi 2

参考下图连接模块:
Matrix-RTC_nanopi_2

连接说明:

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

4.3 连接NanoPi M2 / NanoPi 2 Fire

NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。
参考下图连接模块:
Matrix-RTC_nanopi_M2

连接说明:

NanoPi M2
SDA Pin3
SCL Pin5
5V Pin4
GND Pin6

4.4 连接NanoPC-T2

参考下图连接模块:
Matrix-RTC_NanoPC-T2

连接说明:

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