hi3515 rtc驱动(ds1307/1339)驱动和示例

将驱动放入/extdrv中编译

部分驱动如下:

#include <linux/module.h>

#include <linux/miscdevice.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include <linux/rtc.h>
#include <linux/bcd.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include <asm/system.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/string.h>
#include <linux/list.h>
#include <asm/semaphore.h>
#include <asm/delay.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <asm/hardware.h>
#include <asm/bitops.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include "gpio_i2c.h"
#include "gpio_i2c1.h"
#include "rtc-ds1339.h"
#define ENABLE_TEST

#define DS1339_BIT_CENTURY 0x80/* in REG_MONTH */

static unsigned char rtc1339_byte_read(unsigned char chip_addr, unsigned char addr);


static unsigned char rtc1339_byte_write(unsigned char chip_addr,
                       unsigned char addr, unsigned char data) 
{
    #ifdef ENABLE_TEST
    unsigned char reg_val;
    #endif
    
    gpio_i2c_write(chip_addr, addr, data);  
    #ifdef ENABLE_TEST
    reg_val = rtc1339_byte_read(chip_addr, addr);
    if(reg_val != data)
    {
        printk("ec6214_byte_write error: addr:%x, reg:%x, val:%x, read:%x\r\n", chip_addr, addr, data, reg_val);
    }
    #endif
    return 0;
}


static unsigned char rtc1339_byte_read(unsigned char chip_addr, unsigned char addr)
{   
    unsigned char reg_val;
    reg_val = gpio_i2c_read(chip_addr, addr);
    return reg_val;
}




///////////////////////////////////////////////////////////////////////////////
//***********open the ds1339*****************************************//
int ds1339_open(struct inode * inode,struct file *file)
{
 return 0;
}
///////////////////////////////////////////////////////////////////////////////
//***********close the ds1339*****************************************//
int ds1339_close(struct inode * inode,struct file *file)
{
 return 0;
}

... ...

 

示例如下:

#include <stdio.h>
#include <stdlib.h> /* getenv() */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <error.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <linux/rtc.h>
#include "rtc-ds1339.h"

int Init()
{
int fd;
//open device file
fd = open("/dev/rtc-ds1339",O_RDWR);
if(fd < 0)
{
printf("device open fail\n");
return -1;
}


return fd;
}

int main(int agc,char **agv)
{
int fd,ch;
struct rtc_time 
{
  unsigned int tm_sec;
  unsigned int tm_min;
  unsigned int tm_hour;
  unsigned int tm_wday;
  unsigned int tm_date;
  unsigned int tm_month;
  unsigned int tm_year;
};
struct rtc_time tm;


tm.tm_year=2013;
tm.tm_month=03;
tm.tm_date=14;
tm.tm_wday=4;
tm.tm_hour=16;
tm.tm_min=18;


fd=Init();
if (fd>0)
{
if (ioctl(fd,CMD_SET_TIME,&tm)<0)
printf ("ioctl eo\n");
while(1)
{
ioctl(fd,CMD_GET_TIME,&tm);
printf("%02d.%02d.%02d,%01d,%02d:%02d:%02d\n"
,tm.tm_year,tm.tm_month,tm.tm_date,tm.tm_wday,tm.tm_hour,tm.tm_min,tm.tm_sec);
sleep(1);
}
close(fd);
}


return 0;
}

运行结果如下:

 

posted @ 2015-12-12 14:58  苏博  阅读(1669)  评论(0编辑  收藏  举报