I.MX6 I2C DS1337 disable square-wave output

                   linux I2C DS1337 disable square-wave output

                        \\\\\\\\\\\-*- 目录 -*-//////////
                        |   一、DS1337访问寄存器说明:
                        |   二、cat main.c
                        |   三、cat i2c_data.c
                        |   四、cat i2c_data.h
                        |   五、cat Android.mk
                        --------------------------------

一、DS1337控制寄存器说明:
    1. reference:
       1. DS1337 I2C Serial Real-Time Clock
       2. How to: wire the DS1337 RTC?
           http://forum.arduino.cc/index.php?topic=20937.02. SPECIAL-PURPOSE REGISTERS
       The DS1337 has two additional registers (control and status) that control the RTC, alarms, and square-wave output.
    3. Control Register (0Eh)
       +-------+-------+-------+-------+-------+-------+-------+-------+
       | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
       +-------+-------+-------+-------+-------+-------+-------+-------+
       | EOSC  | 0     | 0     | RS2   | RS1   | INTCN | A2IE  | A1IE  |
       +-------+-------+-------+-------+-------+-------+-------+-------+
       1. Bit 7: Enable Oscillator (EOSC). This active-low bit when set to logic 0 starts the oscillator. When this bit is set to logic 1, the oscillator is stopped. This bit is enabled (logic 0) when power is first applied.
       2. Bits 4 and 3: Rate Select (RS2 and RS1). These bits control the frequency of the square-wave output when the square wave has been enabled. The table below shows the square-wave frequencies that can be selected with the RS bits. These bits are both set to logic 1 (32kHz) when power is first applied.
       3. SQW/INTB Output:
           +------+-----+-----+-----------+------+
           | NTCN | RS2 | RS1 | SQW/INTB  | A2IE |
           |      |     |     |  OUTPUT   |      |
           +------+-----+-----+-----------+------+
           | 0    | 0   | 0   | 1Hz       | X    |
           +------+-----+-----+-----------+------+
           | 0    | 0   | 1   | 4.096kHz  | X    |
           +------+-----+-----+-----------+------+
           | 0    | 1   | 0   | 8.192kHz  | X    |
           +------+-----+-----+-----------+------+
           | 0    | 1   | 1   | 32.768kHz | X    |
           +------+-----+-----+-----------+------+
           | 1    | X   | X   | A2F       | 1    |
           +------+-----+-----+-----------+------+
       4. Bit 2: Interrupt Control (INTCN). This bit controls the relationship between the two alarms and the interrupt output  pins. When the INTCN bit is set to logic 1, a match between the timekeeping registers and the alarm 1 registers l activates the INTA pin (provided that the alarm is enabled) and a match between the timekeeping registers and the alarm 2 registers activates the SQW/INTB pin (provided that the alarm is enabled). When the INTCN bit is set to logic 0, a square wave is output on the SQW/INTB pin. This bit is set to logic 0 when power is first applied.
       5. Bit 1: Alarm 2 Interrupt Enable (A2IE). When set to logic 1, this bit permits the alarm 2 flag (A2F) bit in the status register to assert INTA (when INTCN = 0) or to assert SQW/INTB (when INTCN = 1). When the A2IE bit is set to logic 0, the A2F bit does not initiate an interrupt signal. The A2IE bit is disabled (logic 0) when power is first applied.
       6. Bit 0: Alarm 1 Interrupt Enable (A1IE). When set to logic 1, this bit permits the alarm 1 flag (A1F) bit in the status register to assert INTA. When the A1IE bit is set to logic 0, the A1F bit does not initiate the INTA signal. The A1IE bit is disabled (logic 0) when power is first applied. 

二、cat main.c
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include "i2c-dev.h"
    #include "i2c.h"
    #include "i2c_data.h"

    int main ( int argc, char ** argv ) {
        int fd,ret;

        fd = open( "/dev/i2c-2", O_RDWR );
        if ( fd < 0 ) {
            perror( "open error\n" );
        }

        unsigned char ch = 0;
        // 通过测试秒数来判断访问芯片DS1337没问题
        // terminal output: get data from ds1337 0x00: 54
        i2c_data_read_byte( fd, 0x68, 0x00, &ch );
        printf( "get data from ds1337 0x00: %x\n", ch );

        i2c_data_read_byte( fd, 0x68, 0x0e, &ch );
        // terminal output: get data from ds1337 0x0e: 18
        printf( "get data from ds1337 0x0e: %x\n", ch );

        // 关闭方波输出
        ch = 0x98;
        i2c_data_write_byte( fd, 0x68, 0x0e, ch );

        i2c_data_read_byte( fd, 0x68, 0x0e, &ch );
        // terminal output: get data from ds1337 0x0e: 98
        printf( "get data from ds1337 0x0e: %x\n", ch );

        i2c_data_read_byte( fd, 0x68, 0x0F, &ch );
        // terminal output: get data from ds1337 0x0f: 0
        printf( "get data from ds1337 0x0f: %x\n", ch );

        close( fd );

        return 0;
    }

三、cat i2c_data.c
    #include <stdio.h>
    #include <string.h>
    #include <linux/types.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <errno.h>
    #include "i2c-dev.h"
    #include "i2c.h"
    #include "i2c_data.h"

    // A demo for test
    int eepromDemo ( int argc, char **argv ) {
        int fd,ret;

        fd = open("/dev/i2c-3",O_RDWR);
        if ( fd < 0 ) {
            perror("open error\n");
        }

        unsigned char buf[] = {'x', 'x', 'x', '0', 'y', 'm', '\0'};
        i2c_data_write_byte( fd, 0x50, 0, 'z' );
        i2c_data_write_byte( fd, 0x50, 1, 'j' );
        i2c_data_write_byte( fd, 0x50, 2, 'f' );
        //printf("i2c function test: %s\n", buf);
        //i2c_data_write_byte (fd, 0x50, 3, buf[1]);
        //i2c_data_read_str (fd, 0x50, 0, buf, array_size(buf)-1);

        //unsigned char ch = 0;
        i2c_data_read_byte(fd, 0x50, 1, &ch);

        close(fd);

        return 0;
    }

    /*****************************************************************************
     * 函数说明:
     *     往i2c设备写数据,写一串字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    字节数组
     *     5. count:  字节数组长度
     * 返回值:
     *     正常返回写入的个数,如果出错,返回错误码
     ****************************************************************************/
    int i2c_data_write_str (int fd, int addr, int offset, unsigned char *buf, int count) {
        int ret = 0;
        struct i2c_rdwr_ioctl_data i2c_data;

        i2c_data.nmsgs = 1;     /* 每次只写一个字节 */
        i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));
        if(!i2c_data.msgs)
        {
            perror("i2c_data_write_str function malloc error.\n");
            return -1;
        }

        ioctl(fd, I2C_TIMEOUT, 1); /*超时时间*/
        ioctl(fd, I2C_RETRIES, 2); /*重复次数*/

        (i2c_data.msgs[0]).buf = (unsigned char*)malloc(2);

        int i = 0;
        for (i = 0; i < count; i++) {

            (i2c_data.msgs[0]).len      = 2;
            (i2c_data.msgs[0]).addr     = addr;     //i2c 设备地址
            (i2c_data.msgs[0]).flags    = 0;        //write
            (i2c_data.msgs[0]).buf[0]   = (unsigned char)offset+i;   // i2c 写入目标的地址
            (i2c_data.msgs[0]).buf[1]   = (unsigned char)buf[i];     //the data to write

            ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
            if(ret < 0) {
                perror("i2c_data_write_str ioctl error");
                return ret;
            }
        }   

        free((i2c_data.msgs[0]).buf);
        free(i2c_data.msgs);
        return i;
    }

    /************************************************************************
     * 函数说明:
     *     往i2c设备写数据,写一个字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    要写入的字节
     * 返回值:
     *     正常返回0,出错,返回错误码;
     ************************************************************************/
    int i2c_data_write_byte (int fd, int addr, int offset, unsigned char buf) {

        int ret = 0;
        struct i2c_rdwr_ioctl_data i2c_data;

        i2c_data.nmsgs = 1; /* 每次只写一个字节 */
        i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));
        if(!i2c_data.msgs)
        {
            perror("i2c_data_write_byte function malloc error.\n");
            return -1;
        }

        ioctl(fd,I2C_TIMEOUT,4); /*超时时间*/
        ioctl(fd,I2C_RETRIES,8); /*重复次数*/

        (i2c_data.msgs[0]).buf      = (unsigned char*)malloc(2);

        (i2c_data.msgs[0]).len      = 2;
        (i2c_data.msgs[0]).addr     = addr;     //i2c 设备地址
        (i2c_data.msgs[0]).flags    = 0;        //write
        (i2c_data.msgs[0]).buf[0]   = (unsigned char)offset;    // i2c 写入目标的地址
        (i2c_data.msgs[0]).buf[1]   = (unsigned char)buf;       //the data to write

        ret = ioctl(fd, 0x0707, (unsigned long)&i2c_data);
        if(ret < 0) {
            perror("i2c_data_write_byte ioctl error");
            return ret;
        }   

        free((i2c_data.msgs[0]).buf);
        free(i2c_data.msgs);
        return ret;
    }

    /*************************************************************************
     * 函数说明:
     *     从i2c设备读数据,读一个字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    保存读出的字节指针
     * 返回值:
     *     正常返回0,出错,返回错误码;
     ************************************************************************/
    int i2c_data_read_byte (int fd, int addr, int offset, unsigned char *buf) {
        int ret;
        struct i2c_rdwr_ioctl_data i2c_data;

        i2c_data.nmsgs = 2; /*读时序为2个开始信号*/
        i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));
        if(!i2c_data.msgs) {
            perror("i2c_data_read_byte functionmalloc error");
            exit(1);
        }

        ioctl(fd,I2C_TIMEOUT,1); /*超时时间*/
        ioctl(fd,I2C_RETRIES,2); /*重复次数*/

        (i2c_data.msgs[0]).len      = 1;        //i2c 目标数据的地址
        (i2c_data.msgs[0]).addr     = addr;     // i2c 设备地址
        (i2c_data.msgs[0]).flags    = 0;        //write
        (i2c_data.msgs[0]).buf      = (unsigned char*)malloc(1);
        (i2c_data.msgs[0]).buf[0]   = offset;   //i2c 数据地址

        (i2c_data.msgs[1]).len      = 1;        //读出的数据
        (i2c_data.msgs[1]).addr     = addr;     // i2c 设备地址
        (i2c_data.msgs[1]).flags    = I2C_M_RD; //read
        (i2c_data.msgs[1]).buf      = (unsigned char*)malloc(1);//存放返回值的地址。
        (i2c_data.msgs[1]).buf[0]   = 0;        //初始化读缓冲

        ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
        if(ret<0) {
            perror("i2c_data_read_byte ioctl error.\n");
            return ret;
        }

        //printf("buff[0] = %x\n",i2c_data.msgs[1].buf[0]);
        *buf = i2c_data.msgs[1].buf[0];

        free((i2c_data.msgs[0]).buf);
        free((i2c_data.msgs[1]).buf);
        free(i2c_data.msgs);

        return ret;
    }

    /**********************************************************************************
     * 函数说明:
     *     从i2c设备读数据,读一串字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    保存从i2c设备读出数据的字节数组指针
     *     5. count:  要读的字节个数
     * 返回值:
     *     正常返回0,如果出错,返回错误码
     **********************************************************************************/
    int i2c_data_read_str (int fd, int addr, int offset, unsigned char *buf, int count) {
        int ret;
        struct i2c_rdwr_ioctl_data i2c_data;

        i2c_data.nmsgs = 2;
        i2c_data.msgs  = (struct i2c_msg*)malloc(i2c_data.nmsgs*sizeof(struct i2c_msg));
        if(!i2c_data.msgs) {
            perror("i2c_data_read_byte functionmalloc error");
            exit(1);
        }

        ioctl(fd, I2C_TIMEOUT, 1); /*超时时间*/
        ioctl(fd, I2C_RETRIES, 2); /*重复次数*/

        (i2c_data.msgs[0]).len      = 1;        //i2c 目标数据的地址
        (i2c_data.msgs[0]).addr     = addr;     // i2c 设备地址
        (i2c_data.msgs[0]).flags    = 0;        //write
        (i2c_data.msgs[0]).buf      = (unsigned char*)malloc(1);
        (i2c_data.msgs[0]).buf[0]   = offset;   //i2c 数据地址

        (i2c_data.msgs[1]).len      = count;    //读出的数据
        (i2c_data.msgs[1]).addr     = addr;     // i2c 设备地址
        (i2c_data.msgs[1]).flags    = I2C_M_RD; //read
        (i2c_data.msgs[1]).buf      = buf;      //存放返回值的地址。

        ret = ioctl(fd, I2C_RDWR, (unsigned long)&i2c_data);
        if(ret<0) {
            perror("i2c_data_read_byte ioctl error.\n");
            return ret;
        }
        //buf[count] = 0;
        //printf("buf = %s\n", buf);

        free((i2c_data.msgs[0]).buf);
        free(i2c_data.msgs);

        return 0;
    }

四、cat i2c_data.h
    /**************************************************************************************
     * 声明:
     *     这个函数库主要用于读写i2c设备,提供了以下功能:
     *         1. 读一串字节:i2c_data_read_str()
     *         2. 读一个字节:i2c_data_read_byte()
     *         3. 写一串字节:i2c_data_write_str()
     *         4. 写一个字节:i2c_data_write_byte()
     *
     *
     *                                      write by zengjf     2015-4-28
     **************************************************************************************/
    #ifndef __I2C_DATA_H__
        #define __I2C_DATA_H__

    #define array_size(array_buffer) (sizeof(array_buffer)/sizeof(*array_buffer))

    /**********************************************************************************
     * 函数说明:
     *     往i2c设备写数据,写一串字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    字节数组
     *     5. count:  字节数组长度
     * 返回值:
     *     正常返回写入的个数,如果出错,返回错误码
     *********************************************************************************/
    int i2c_data_write_str (int fd, int addr, int offset, unsigned char *buf, int count);

    /**********************************************************************************
     * 函数说明:
     *     从i2c设备读数据,读一串字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    保存从i2c设备读出数据的字节数组指针
     *     5. count:  要读的字节个数
     * 返回值:
     *     正常返回0,如果出错,返回错误码
     **********************************************************************************/
    int i2c_data_read_str (int fd, int addr, int offset, unsigned char *buf, int count);

    /**********************************************************************************
     * 函数说明:
     *     往i2c设备写数据,写一个字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    要写入的字节
     * 返回值:
     *     正常返回0,出错,返回错误码;
     **********************************************************************************/
    int i2c_data_write_byte (int fd, int addr, int offset, unsigned char buf);

    /**********************************************************************************
     * 函数说明:
     *     从i2c设备读数据,读一个字节
     * 参数说明:
     *     1. fd:     文件描述符
     *     2. addr:   i2c设备地址
     *     3. offset: i2c设备内寄存器偏移地址
     *     4. buf:    保存读出的字节指针
     * 返回值:
     *     正常返回0,出错,返回错误码;
     **********************************************************************************/
    int i2c_data_read_byte (int fd, int addr, int offset, unsigned char *buf);

    #endif //__I2C_DATA_H__

五、cat Android.mk
    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)

    LOCAL_MODULE    := linuxTest
    LOCAL_SRC_FILES := linuxTest.c i2c_data.c

    include $(BUILD_EXECUTABLE)

 

时间: 2024-11-03 20:13:42

I.MX6 I2C DS1337 disable square-wave output的相关文章

UHD - USRP2 and N2x0 Series Device Manual

UHD - USRP2 and N2x0 Series Device Manual Comparative features list Hardware Capabilities: 1 transceiver card slot External PPS reference input External 10 MHz reference input MIMO cable shared reference Fixed 100 MHz clock rate Internal GPSDO option

RTPREEMPT rt实时补丁

RT PREEMPT HOWTO Intro Authors Luotao Fu (l.fu AT pengutronix DOT de), Pengutronix e.K., Kernel Development Group Robert Schwebel (r.schwebel AT pengutronix DOT de), Pengutronix e.K., Kernel Development Group Abstract This document describes the proc

linux rtc 接口【转】

转自:http://blog.csdn.net/goldfighter/article/details/6126178 Linux操作系统内核对RTC的编程详解   转自: http://xenyinzen.wikidot.com/reship:080225-2     Linux内核对RTC的编程   MC146818 RTC芯片(或其他兼容芯片,如DS12887)可以在IRQ8上产生周期性的中断,中断的频率在2HZ-8192HZ之间.与MC146818 RTC对应的设备驱动程序实现在incl

C语言使用libZPlay录制声音并写到文件的方法_C 语言

本文实例讲述了C语言使用libZPlay录制声音并写到文件的方法.分享给大家供大家参考.具体实现方法如下: /** * Record samples from line-in and save to out.mp3 * */ #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <olectl.h> #include <ole2.h> #include <stdio.h> #include <

DIY FSK RFID Reader

This page describes the construction of an RFID reader using only an Arduino (Nano 3.0 was tested, but others may work), a hand-wound wire coil, and some assorted low cost common components. Credits The hardware and software designs for this project

【工具】系统性能查看工具 dstat

今天学习到一款系统性能查看工具 dstat  获取方式: 1 yum install -y dstat 2 wget http://packages.sw.be/dstat/dstat-0.7.2-1.el5.rfx.noarch.rpm   rpm -ivh  dstat-0.7.2-1.el5.rfx.noarch.rpm Dstat的使用: 安装完成后,执行 dstat 命令,默认情况它会收集-cpu-.-disk-.-net-.-paging-.-system-的数据,一秒钟收集一次. 

lcd驱动程序之显示图片

以前没有写Lcd驱动程序,现在开始做项目了,才发现Lcd驱动程序必须认真学习,我总结所以驱动中LCD和网卡驱动是最难的,LCD主要是需要配置的寄存器太多了,还是老规矩先上代码. 内核版本:linux-3.4.2                   lcd:4.3 上代码之前我得讲解一些基本的知识点,LCD驱动我们只需要写硬件这一块的代码就可以了,下面有三个函数内核已经帮我们写好了,我们只需要调用就可以了,这几个函数实现了内核层和应用成数据的传递,有兴趣的朋友去分析一下源码,我里驱动写好了我们可以

神作:深入浅出傅里叶变换

一.什么是频域 从我们出生,我们看到的世界都以时间贯穿,股票的走势.人的身高.汽车的轨迹都会随着时间发生改变.这种以时间作为参照来观察动态世界的方法我们称其为时域分析.而我们也想当然的认为,世间万物都在随着时间不停的改变,并且永远不会静止下来.但如果我告诉你,用另一种方法来观察世界的话,你会发现世界是永恒不变的,你会不会觉得我疯了?我没有疯,这个静止的世界就叫做频域. 先举一个公式上并非很恰当,但意义上再贴切不过的例子: 在你的理解中,一段音乐是什么呢? 这是我们对音乐最普遍的理解,一个随着时间

傅里叶分析之掐死教程

傅里叶分析之掐死教程(完整版)更新于2014.06.06 Heinrich · 6 天前 作 者:韩 昊 知 乎:Heinrich 微 博:@花生油工人 知乎专栏:与时间无关的故事 谨以此文献给大连海事大学的吴楠老师,柳晓鸣老师,王新年老师以及张晶泊老师. 转载的同学请保留上面这句话,谢谢.如果还能保留文章来源就更感激不尽了. --更新于2014.6.6,想直接看更新的同学可以直接跳到第四章---- 我保证这篇文章和你以前看过的所有文章都不同,这是12年还在果壳的时候写的,但是当时没有来得及写完