📄 rtctime.c
字号:
#include <linux/config.h>#include <linux/utsname.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/string.h>#include <linux/fcntl.h>#include <linux/timer.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/module.h>#include <linux/init.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/types.h>#include <linux/miscdevice.h>#include <linux/ioport.h>#include <linux/mc146818rtc.h>#include <linux/poll.h>#include <linux/proc_fs.h>#include <linux/spinlock.h>#include <linux/sysctl.h>#include <linux/pci.h>#include <asm/mach/irq.h>#include <asm/hardware.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/arch/io.h>#include "44b.h"
#define DEVICE_NAME "rtctime"typedef struct { int year; int month; int day; int weekday; int hour; int min; int sec;}RTCSTATE;static RTCSTATE _time;void Display_RTC(void);
void Set_year(int);void Set_mon(int);void Set_day(int);void Set_weekday(int);void Set_hour(int);void Set_min(int);
void Display_RTC(void) //显示当前时间(只需从寄存器中读取数值即可)
{
rRTCCON = 0x01;
_time.year = rBCDYEAR;
_time.month=rBCDMON;
_time.day=rBCDDAY;
_time.weekday=rBCDDATE;
_time.hour=rBCDHOUR;
_time.min=rBCDMIN;
_time.sec=rBCDSEC;
if(_time.sec==0) //秒数到零时,重新读一下其它寄存器,为了防止一秒误差
{ _time.year = rBCDYEAR;
_time.month=rBCDMON;
_time.day=rBCDDAY;
_time.weekday=rBCDDATE;
_time.hour=rBCDHOUR;
_time.min=rBCDMIN; }
//printk("\n%4d-%02x-%02x-%x-%02x:%02x:%02x\n",_time.year+2000,_time.month,_time.day,_time.weekday,_time.hour,_time.min,_time.sec);
rRTCCON = 0x0;
}void Set_year(int year) //设置年
{
rRTCCON = 0x01;
rBCDYEAR = ((year/10)<<4) + (year%10);
rRTCCON = 0x0;
}void Set_mon(int mon) //设置月
{
rRTCCON = 0x01;
rBCDMON = ((mon/10)<<4) + (mon%10);
rRTCCON = 0x0;
}void Set_day(int day) //设置日
{
rRTCCON = 0x01;
rBCDDAY = ((day/10)<<4) + (day%10);
rRTCCON = 0x0;
}void Set_weekday(int weekday) //设置星期
{
rRTCCON = 0x01;
rBCDDATE = ((weekday/10)<<4) + (weekday%10);
rRTCCON = 0x0;
}void Set_hour(int hour) //设置小时
{
rRTCCON = 0x01;
rBCDHOUR = ((hour/10)<<4) + (hour%10);
rRTCCON = 0x0;
}void Set_min(int min) //设置分钟
{
rRTCCON = 0x01;
rBCDMIN = ((min/10)<<4) + (min%10);
rRTCCON = 0x0;
}static int rtctime_open(struct inode *inode, struct file *filp){ return 0;}static int rtctime_release(struct inode *inode, struct file *filp){ return 0;}static int rtctime_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ switch(cmd) { case 0: Display_RTC(); return 0; case 1: Set_year(arg); return 0; case 2: Set_mon(arg); return 0; case 3: Set_day(arg); return 0; case 4: Set_weekday(arg); return 0; case 5: Set_hour(arg); return 0; case 6: Set_min(arg); return 0; default: printk("ERROR\n"); } return 0;}static int rtctime_read(struct file *fp, char *buf, size_t count)
{ put_user(_time.year, buf); //将寄存器数值存入缓冲区,以便上层read函数调用 put_user(_time.month, buf+4); put_user(_time.day, buf+8); put_user(_time.weekday, buf+12); put_user(_time.hour, buf+16); put_user(_time.min, buf+20); put_user(_time.sec, buf+24); return count;
}static struct file_operations rtctime_fops = { open: rtctime_open, ioctl: rtctime_ioctl, release: rtctime_release, read: rtctime_read,};int rtctime_init(void){ int ret; ret = register_chrdev(254, DEVICE_NAME, &rtctime_fops); if (ret < 0) { printk(DEVICE_NAME " can't get major number\n"); return ret; } else printk("OK\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -