📄 ds1337.c
字号:
i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_MONTH, buf[5]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_YEAR, buf[6]); } break; case DS1337_GET_TIME: { u8 buf[7]; u8 val; struct date_time dt; memset( buf, 0, sizeof(buf)); ds1337_read( ds1337_client, DS1337_REG_SECOND, &buf[0]); ds1337_read( ds1337_client, DS1337_REG_MINUTE, &buf[1]); ds1337_read( ds1337_client, DS1337_REG_HOUR, &buf[2]); ds1337_read( ds1337_client, DS1337_REG_DAY, &buf[3]); ds1337_read( ds1337_client, DS1337_REG_DATE, &buf[4]); ds1337_read( ds1337_client, DS1337_REG_MONTH, &buf[5]); ds1337_read( ds1337_client, DS1337_REG_YEAR, &buf[6]); dt.tm_sec = BCD2BIN(buf[0]); dt.tm_min = BCD2BIN(buf[1]); dt.tm_hour = BCD2BIN(buf[2]); dt.tm_wday = BCD2BIN(buf[3]); dt.tm_mday = BCD2BIN(buf[4]); val = buf[5] & 0x7f; dt.tm_mon = BCD2BIN(val); dt.tm_year = BCD2BIN(buf[6]); if (buf[5] & 0x80) dt.tm_year += 100; copy_to_user((void*)arg, &dt, sizeof(struct date_time)); } break; /*mode:1=enable alarm,0=disable alarm,others=wrong input*/ case DS1337_SET_ALARM1_MODE: { u8 mode; u8 control; u8 status; ds1337_read( ds1337_client, DS1337_REG_STATUS, &status); status &=~A1F; i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_STATUS, status ); copy_from_user( &mode ,(void*)arg,sizeof(unsigned char)); ds1337_read( ds1337_client, DS1337_REG_CONTROL, &control); if( mode == 1 ) { control |= A1IE; } else if( mode ==0) { control &=~A1IE; } else { printk("error mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_CONTROL, control ); } break; case DS1337_GET_ALARM1_MODE: { u8 mode; u8 control; ds1337_read( ds1337_client, DS1337_REG_CONTROL, &control); if( control & A1IE ) { mode = 1; } else { mode = 0; } copy_to_user( (void*)arg, &mode, sizeof(unsigned char) ); } break; case DS1337_SET_ALARM1_TIME: { u8 buf[4]; struct alarm1_time dt; copy_from_user(&dt, (void*) arg, sizeof(struct alarm1_time)); buf[0] = BIN2BCD(dt.tm_sec); buf[1] = BIN2BCD(dt.tm_min); buf[2] = BIN2BCD(dt.tm_hour); buf[3] = BIN2BCD(dt.tm_day); //writes registers i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_SECOND, buf[0]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_MINUTE, buf[1]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_HOUR, buf[2]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_DAY_DATE, buf[3]); } break; case DS1337_GET_ALARM1_TIME: { u8 buf[4]; struct alarm1_time dt; ds1337_read( ds1337_client, DS1337_REG_A1M_SECOND, &buf[0]); ds1337_read( ds1337_client, DS1337_REG_A1M_MINUTE, &buf[1]); ds1337_read( ds1337_client, DS1337_REG_A1M_HOUR, &buf[2]); ds1337_read( ds1337_client, DS1337_REG_A1M_DAY_DATE, &buf[3]); dt.tm_sec = BCD2BIN(buf[0]); dt.tm_min = BCD2BIN(buf[1]); dt.tm_hour = BCD2BIN(buf[2]); dt.tm_day =BCD2BIN(buf[3]); copy_to_user((void*)arg, &dt, sizeof(struct alarm1_time)); } break; /*mode:1=enable alarm,0=disable alarm,others=wrong input*/ case DS1337_SET_ALARM2_MODE: { u8 mode; u8 control; u8 status; ds1337_read( ds1337_client, DS1337_REG_STATUS, &status); status &=~A2F; i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_STATUS, status ); copy_from_user( &mode ,(void*)arg,sizeof(unsigned char)); ds1337_read( ds1337_client, DS1337_REG_CONTROL, &control); if( mode == 1 ) { control |= A2IE; } else if( mode ==0) { control &=~A2IE; } else { printk("error mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_CONTROL, control ); } break; case DS1337_GET_ALARM2_MODE: { u8 mode; u8 control; ds1337_read( ds1337_client, DS1337_REG_CONTROL, &control); if( control & A2IE ) { mode = 1; } else { mode = 0; } copy_to_user( (void*)arg, &mode, sizeof(unsigned char) ); } break; //set the time for alarm2,match min,hour,date..... case DS1337_SET_ALARM2_TIME: { u8 buf[3]; struct alarm2_time dt; copy_from_user(&dt, (void*) arg, sizeof(struct alarm2_time)); buf[0] = BIN2BCD(dt.tm_min); buf[1] = BIN2BCD(dt.tm_hour); buf[2] = BIN2BCD(dt.tm_day); //writes registers i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A2M_MINUTE, buf[0]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A2M_HOUR, buf[1]); i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A2M_DAY_DATE, buf[2]); } break; case DS1337_GET_ALARM2_TIME: { u8 buf[3]; struct alarm2_time dt; ds1337_read( ds1337_client, DS1337_REG_A2M_MINUTE, &buf[0]); ds1337_read( ds1337_client, DS1337_REG_A2M_HOUR, &buf[1]); ds1337_read( ds1337_client, DS1337_REG_A2M_DAY_DATE, &buf[2]); dt.tm_min = BCD2BIN(buf[0]); dt.tm_hour = BCD2BIN(buf[1]); dt.tm_day =BCD2BIN(buf[2]); copy_to_user((void*)arg, &dt, sizeof(struct alarm2_time)); } break;/* //mode=1:12hour-mode;mode=0:24hour-mode case DS1337_SET_HOUR_MODE: { u8 mode,hour; copy_from_user( &mode, (void*)arg, sizeof(unsigned char) ); ds1337_read( ds1337_client , DS1337_REG_HOUR, &hour); if( mode==1 ) { hour |=MODE_12; } else if( mode == 0 ) { hour &=~MODE_12; } else { printk("the wrong input mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_HOUR, hour); } case DS1337_GET_HOUR_MODE: { u8 hour,mode; ds1337_read( ds1337_client , DS1337_REG_HOUR, &hour); if( hour & MODE_12 ) { mode =1; } else { mode = 0; } copy_to_user((void*)arg, &mode , sizeof(unsigned char) ); } case DS1337_SET_ALARM1_HOUR_MODE: { u8 mode,hour; copy_from_user( &mode, (void*)arg, sizeof(unsigned char) ); ds1337_read( ds1337_client , DS1337_REG_A1M_HOUR, &hour); if( mode==1 ) { hour |=MODE_12; } else if( mode == 0 ) { hour &=~MODE_12; } else { printk("the wrong input mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_HOUR, hour); } case DS1337_GET_ALARM1_HOUR_MODE: { u8 hour,mode; ds1337_read( ds1337_client , DS1337_REG_A1M_HOUR, &hour); if( hour & MODE_12 ) { mode =1; } else { mode = 0; } copy_to_user((void*)arg, &mode , sizeof(unsigned char) ); }*/ //mode =1:match day of the week;mode=0:match the day of the month case DS1337_SET_ALARM1_DAY_MODE: { u8 mode,day; copy_from_user( &mode, (void*)arg, sizeof(unsigned char) ); ds1337_read( ds1337_client , DS1337_REG_A1M_DAY_DATE, &day); if( mode==1 ) { day |=MATCHWEEK; } else if( mode == 0 ) { day &=~MATCHWEEK; } else { printk("the wrong input mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A1M_DAY_DATE, day ); } case DS1337_GET_ALARM1_DAY_MODE: { u8 day,mode; ds1337_read( ds1337_client , DS1337_REG_A1M_DAY_DATE, &day); if( day & MATCHWEEK ) { mode = 1; } else { mode = 0; } copy_to_user((void*)arg, &mode , sizeof(unsigned char) ); }/* case DS1337_SET_ALARM2_HOUR_MODE: { u8 mode,hour; copy_from_user( &mode, (void*)arg, sizeof(unsigned char) ); ds1337_read( ds1337_client , DS1337_REG_A2M_HOUR, &hour); if( mode==1 ) { hour |=MODE_12; } else if( mode == 0 ) { hour &=~MODE_12; } else { printk("the wrong input mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A2M_HOUR, hour); } case DS1337_GET_ALARM2_HOUR_MODE: { u8 hour,mode; ds1337_read( ds1337_client , DS1337_REG_A2M_HOUR, &hour); if( hour & MODE_12 ) { mode =1; } else { mode = 0; } copy_to_user((void*)arg, &mode , sizeof(unsigned char) ); }*/ //mode =1:match day of the week;mode=0:match the day of the month case DS1337_SET_ALARM2_DAY_MODE: { u8 mode,day; copy_from_user( &mode, (void*)arg, sizeof(unsigned char) ); ds1337_read( ds1337_client , DS1337_REG_A2M_DAY_DATE, &day); if( mode==1 ) { day |=MATCHWEEK; } else if( mode == 0 ) { day &=~MATCHWEEK; } else { printk("the wrong input mode value \n"); goto out; } i2c_smbus_write_byte_data(ds1337_client, DS1337_REG_A2M_DAY_DATE, day ); } case DS1337_GET_ALARM2_DAY_MODE: { u8 day,mode; ds1337_read( ds1337_client , DS1337_REG_A2M_DAY_DATE, &day); if( day & MATCHWEEK ) { mode = 1; } else { mode = 0; } copy_to_user((void*)arg, &mode , sizeof(unsigned char) ); } default: break; }out: return ret;}/* End File Ops */static struct file_operations ds1337_fops = { .open = ds1337_open, .release = ds1337_release, .ioctl = ds1337_ioctl,};static int __init ds1337_init(void){ int retval,status=0; printk(KERN_INFO "\t" MOD_DESC "\n"); if (register_chrdev(DS1337_MAJOR, "ds1337", &ds1337_fops) != 0) { printk(KERN_ERR "%s Couldn't register ds1337 device.\n", pname); goto out; } if ((status = i2c_add_driver(&ds1337_driver)) < 0) { printk(KERN_INFO "%s Couldn't register DS1337 I2C driver.\n", pname); goto out; } //GIO_RTC_INTA GIO15 // '0' indicate have a realtime clock interrupt. if(request_gio(GIO_RTC_INTA)) printk(KERN_ERR "Request of GIO:%d for touchscreen failed.\n", GIO_RTC_INTA); gio_set_dir(GIO_RTC_INTA, bit_hi); //input //for test. printk("ds1337-->gio_get_bitset(GIO_RTC_INTA) = %d\n", gio_get_bitset(GIO_RTC_INTA)); retval = request_irq(IRQ_GIO15, ds1337_isr, SA_INTERRUPT, "ds1337_rtc", (void *)GIO_RTC_INTA); if(retval) { printk("Unable to get interrupt in 'ds1337.c' --> ds1337_init()\n"); return -ENODEV; } set_irq_type(IRQ_GIO15, IRQT_FALLING); gio_enable_irq(GIO_RTC_INTA, GIO_FALLING_EDGE); out: return status;}static void __exit ds1337_exit(void){ unrequest_gio(GIO_RTC_INTA); gio_disable_irq(GIO_RTC_INTA); free_irq(IRQ_GIO15, (void *)GIO_RTC_INTA); i2c_del_driver(&ds1337_driver); unregister_chrdev(DS1337_MAJOR, "ds1337");}MODULE_DESCRIPTION("DS1337 RTC driver");MODULE_LICENSE("GPL");module_init(ds1337_init);module_exit(ds1337_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -