S3C2440:第一个char device driver:RTC

刚刚开始学习驱动,首先接触了字符设备驱动。Hello World就直接pass了。

 

这个RTC驱动很简单,没有像linux附带中的S3CXX 系列有platform分层次的驱动程序。

code如下:

 

/*************************************

NAME:Baikal_rtc.c
COPYRIGHT:Baikal:dndxhej@sina.com.cn

*************************************/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/device.h>

#include <asm/io.h>
//#include <asm/plat-s3c/regs-rtc.h>


#define DEVICE_NAME    "Baikal-rtc"        /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define RTC_MAJOR    234            /* 主设备号 */

#define IOCTL_RTC_READ    1
#define IOCTL_RTC_WRITE    0


#define S3C2410_RTCCON        0x40         //寄存器的偏移量  后面联合ioremap进行物理地址到内存空间的映射

#define S3C2410_RTCSEC          0x70
#define S3C2410_RTCMIN          0x74
#define S3C2410_RTCHOUR          0x78
#define S3C2410_RTCDATE          0x7c
#define S3C2410_RTCDAY          0x80
#define S3C2410_RTCMON          0x84
#define S3C2410_RTCYEAR          0x88




static void __iomem *base;
static int rtc_open(struct inode *inode, struct file *file)
{

    return 0;
}

static int rtc_release(struct inode *inode, struct file *file)
{

    return 0;
}

static int rtc_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    long int year ;
    int month, day ,week;        // week
    int hour, minute, second ;

    if (arg > 4)
    {
        return -EINVAL;
    }

    switch(cmd)
    {
        case IOCTL_RTC_WRITE:
            // what is the time?
            writeb(1,base+S3C2410_RTCCON)   ;        //RTC read and write enable

            writeb(0x2000,base+S3C2410_RTCYEAR)       ;    //year
            writeb(0x11,base+S3C2410_RTCMON)        ;    //月
            writeb(0x15,base+S3C2410_RTCDATE)       ;    //日   
            writeb(0x05,base+S3C2410_RTCDAY)        ;    //星期
            writeb(0x12,base+S3C2410_RTCHOUR)       ;    //小时
            writeb(0x00,base+S3C2410_RTCMIN)        ;        //分
            writeb(0x00,base+S3C2410_RTCSEC)        ;    //秒
   
            writeb(0,base+S3C2410_RTCCON)  ;        //RTC read and write disable
            return 0;
        case IOCTL_RTC_READ:
            // the time is ......i don't kown
            writeb(1,base+S3C2410_RTCCON)   ;        //RTC read and write enable

            year    =   readb(base+S3C2410_RTCYEAR)  ;        //年
            month   =   readb(base+S3C2410_RTCMON)  ;        //月
            day     =   readb(base+S3C2410_RTCDATE)  ;        //日   
            week    =   readb(base+S3C2410_RTCDAY)  ;        //星期
            hour    =   readb(base+S3C2410_RTCHOUR)  ;        //小时
            minute  =   readb(base+S3C2410_RTCMIN)  ;        //分
            second  =   readb(base+S3C2410_RTCSEC)  ;        //秒
       
            writeb(0,base+S3C2410_RTCCON)  ;        //RTC read and write disable

            printk( "RTC time : %04x-%02x-%02x %02x:%02x:%02x  %2x/n", year, month, day, hour, minute, second ,week);
            return 0;
        default:
            return -EINVAL;
    }
   

    return 0;
}


/* Structure that declares the usual file */
/* access functions */
struct file_operations rtc_fops = {
.owner  =   THIS_MODULE,
.ioctl  =  rtc_ioctl,
.open   =  rtc_open,
.release = rtc_release,
};
static struct class *rtc_class;


static int __init rtc_init(void)
{
    int ret;

    /* 注册字符设备驱动程序
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
    
     */
base=ioremap(0x57000000,0x90);



    ret = register_chrdev(RTC_MAJOR, DEVICE_NAME, &rtc_fops);
    if (ret < 0)
    {
        printk(DEVICE_NAME " can't register major number/n");
        return ret;
    }

    //注册一个类,使mdev可以在"/dev/"目录下面建立设备节点
    rtc_class = class_create(THIS_MODULE, DEVICE_NAME);
    if(IS_ERR(rtc_class))
    {
        printk("Err: failed in rtc class. /n");
        return -1;
    }
    //创建一个设备节点,节点名为DEVICE_NAME
    class_device_create(rtc_class, NULL, MKDEV(RTC_MAJOR, 0), NULL, DEVICE_NAME);

    printk(DEVICE_NAME " initialized/n");
    return 0;
}




static void __exit rtc_exit(void)
{
    /* 卸载驱动程序 */
    unregister_chrdev(RTC_MAJOR, DEVICE_NAME);
    class_device_destroy(rtc_class, MKDEV(RTC_MAJOR, 0));    //删掉设备节点
    class_destroy(rtc_class);                    //注销类
}

/* Declaration of the init and exit functions */
module_init(rtc_init);
module_exit(rtc_exit);



MODULE_AUTHOR("Baikal");       
MODULE_DESCRIPTION("TQ2440 RTC Driver by Baikal");    // 一些描述信息
MODULE_LICENSE("GPL");                    // 遵循的协议

 

 

代码很简单,仅仅实现了rtc的写(还是固定写入的时间)和读。

 

insmod这个模块后,编写rtc.c程序对其进行测试,测试代码如下:

/*************************************

NAME:rtc.c
COPYRIGHT:Baikal:dndxhej@sina.com.cn

*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int writeorread;
    int fd;
    if (argc != 2 || sscanf(argv[1], "%d", &writeorread) != 1  ||
        writeorread < 0 || writeorread > 1 ) {
        fprintf(stderr, "Usage: rtc 0|1/n");
        exit(1);
    }
    fd = open("/dev/Baikal-rtc", 0);
    if (fd < 0) {
        perror("open device rtc");
        exit(1);
    }
    ioctl(fd, writeorread);
    close(fd);
    return 0;
}

 

 

利用rtc 0和rtc 1读写正常。想到网上关于rtc 的测试方法是:date和hwclock 。

这里,谈一下我对date 和 hwclock的理解。

date是系统时间,hwclock是硬件RTC的时间。hwclock可以实现系统时间和硬件时间的同步工作。

 

对于我这个驱动,用busybox中的hwclock命令,会报:/dev/misc/rtc 打不开之类的

 

稍微浏览下busybox中关于hwclock的实现:

int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
{
    int rtc;

    if (!*default_rtc) {
        *default_rtc = "/dev/rtc";
        rtc = open(*default_rtc, flags);
        if (rtc >= 0)
            return rtc;
        *default_rtc = "/dev/rtc0";
        rtc = open(*default_rtc, flags);
        if (rtc >= 0)
            return rtc;
        *default_rtc = "/dev/misc/rtc";

//add by Baikal
        rtc = open(*default_rtc, flags);
        if (rtc >= 0)
            return rtc;
        *default_rtc = "/dev/Baikal-rtc";
    }

    return xopen(*default_rtc, flags);
}

 

 

可以看到,他是默认打开"/dev/rtc"   "/dev/rtc0"  "/dev/misc/rtc" 三个默认的驱动文件,对于自己

写的这个非主流的rtc,他不认。你既然不认,我们就熟悉一下。于是,有了上面的add by Baikal这一段。

 

加了这一段,发现hwclock还是报错,原因是RTC_RD_TIME和RTC_SET_TIME不可用   

究其原因:还是自己写的模块是非主流的

 

再改:

修改busybox下面的rtc.c文件:

time_t FAST_FUNC rtc_read_time(int fd, int utc)
{
    struct tm tm;
//    char *oldtz = 0;
    time_t t = 0;
ioctl(fd, 1,&tm);//read
t = mktime(&tm);
return t;

 

再输入hwclock -r:

显示:Wed Dec 31 23:59:59 1969  0.000000 seconds

这个时间是linux系统时间的起始时间,还是不对,哥要读的是硬件RTC时间。

今天没时间改了,猜测下原因:因为返回的t值是mktime函数将&tm指向的结构体(结构体定义的就是年月日时分秒)转化为数字。后面

会将这个数字转化成字符串显示具体时间的。(ps:结构体转化为数字是有讲究的)

 

说了半天,为什么没读到硬件RTC时间呢,因为我的驱动模块中的ioctl还不完善。

 

明天继续完善,还有好多地方需要完善。RTC部分的datesheet也要仔细看一下。。。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值