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

将驱动放入/extdrv中编译

部分驱动如下:

#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
#include /* getenv() */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;
}

运行结果如下:



需源码的  QQ:156722569


你可能感兴趣的:(LINUX)