ds1339驱动源码及RTC设备驱动简介

RTC

实时时钟,在嵌入式设备中以芯片来表现,一般有八个脚

芯片会被一直供电.所以里面的一些寄存器会一直存在值,且一直都是变化的.

linux中有两个时间,一个是rtc中的时间(一般由hwclock命令来控制),一个是linux系统中的时间(一般由date命令来控制)

在系统启动时,可以在/etc/init.d/rcS中写入命令将rtc中的时间拷贝到linux中.从而达到看起来像是时间没掉电的表象


RTC驱动

RTC设备属于一种字符设备,在一些文件中通过杂项设备的框架进行搭建驱动.RTC设备与主芯片的交互可能是任意方式(例如spi或者i2c),所以读写的时候要遵循相应的协议.

//杂项设备miscdevice结构体
static struct miscdevice ds1339_driver = {
    .minor      = MISC_DYNAMIC_MINOR,
    .name       = "rtc-ds1339",
    .fops       = &ds1339_fops,
};
//通过下面的语句被注册
misc_register(&ds1339_driver);
//操作指针
static struct file_operations ds1339_fops = {
    .owner      = THIS_MODULE,
    .unlocked_ioctl  = ds1339_ioctl,
    .open       = ds1339_open,
    .release    = ds1339_close
};
//一般的驱动都会实现读写
case CMD_SET_TIME:
    printk("Will set the ds1339 time and date\n");
    //拷贝进来
    copy_from_user(&wtime, (struct rtc_time_new *)arg, sizeof(struct rtc_time_new));//一般会检验
    //处理
    ds1339_time[0] = bin2bcd(wtime.tm_sec);
    ds1339_time[1] = bin2bcd(wtime.tm_min);
    ds1339_time[2] = bin2bcd(wtime.tm_hour);
    ds1339_time[3] = bin2bcd(wtime.tm_wday+1);
    ds1339_time[4] = bin2bcd(wtime.tm_mday);
    ds1339_time[5] = (bin2bcd(wtime.tm_month+1)|DS1339_BIT_CENTURY); 
    ds1339_time[6] = bin2bcd(wtime.tm_year%100);

    gpio_i2c_write(ADDRTC,0,0);//选中芯片,让芯片做准备
    for (temp=1;temp<7;temp++)
    {
        gpio_i2c_write(ADDRTC,temp,ds1339_time[temp]);//将数据写入芯片
    }
    break; 
case CMD_GET_TIME:
    printk("\nWill get the ds1339 time and date\n");

    for (temp=0;temp<7;temp++)
    {
        ds1339_time[temp]  = gpio_i2c_read(ADDRTC,temp);//将数据读出来
    }
    //处理
    wtime.tm_sec    = bcd2bin(ds1339_time[0] & 0x7f);
    wtime.tm_min    = bcd2bin(ds1339_time[1] & 0x7f);
    wtime.tm_hour   = bcd2bin(ds1339_time[2] & 0x3f);
    wtime.tm_wday   = bcd2bin(ds1339_time[3]& 0x07) - 1;
    wtime.tm_mday   = bcd2bin(ds1339_time[4]& 0x3f);
    wtime.tm_month  = bcd2bin(ds1339_time[5]& 0x1f) - 1;
    wtime.tm_year   = bcd2bin(ds1339_time[6]);
    //拷贝出去
    copy_to_user((void *)arg, &wtime, sizeof (struct rtc_time_new));
    break; 

注意:

  • 上例中的驱动,是杂项设备框架和i2c框架(没有展示代码)的结合.

源码下载

源码已经附到链接中,自己改的,和展示代码稍有不同,点我下载


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值