首页 > 编程知识 正文

rx8025t时钟芯片示例代码,Linux写rtc周期

时间:2023-05-05 08:32:35 阅读:110335 作者:1100

硬件平台: mt7688软件平台:本机SDK mediatek _ a psoc _ SDK _ 4300 _ 2014 09 16 Linux-3.10.14内核arch/MIPS dev-I2C.c _ _ init const={ { I2C _ board _ info (rx 8025 t ',0x32 ),device name和从机地址. platform _ data=} void /* slave address*/__u16 flags; # define I2C _ m _ ten0x 0010/* thisisatenbitchipaddress */# define I2C _ m _ rd0x 0001/* read data, fromslavetomaster */# define I2C _ m _ stop0x 8000/* ifi2c _ func _ protocol _ mangling */# define I2C _ m _ no start ifi2c _ func _ protocol _ mangling */# define I2C _ m _ ignore _ nak0x 1000 d fine I2C _ m _ no _ rd _ ack0x 0800/* ifine /* pointer to msg data*/}; 2. 8025t的驱动程序drivers/char/I2C _ rx 8025 t.c:# include Linux/init.h # include Linux/fs.h # include Linux/module include Linux/kernel.h # include Linux/cdev.h # include Linux/DD inux slab.h # definerx 8025 t _ name ' rx 8025 t '/drivivilue static struct class* rtc_class=0; static struct cdev* rtc_dev=0; static struct i2c_client* g_cli=0; 状态void _ _ exit rx 8025 t _ exit (; staticintrx 8025 t _ probe (struct I2C _ client * CLI,const struct i2c_device_id* id ) {g_cli=cli; //传输时指定i2c_client,因此对于probe设备,使用全局变量进行保存并备用。

返回0; } staticintrx 8025 t _ remove (struct I2C _ client * CLI ) {g_cli=0; //删除设备时将i2c_client清除为return 0; } staticintrx 8025 t _ suspend (struct I2C _ client * CLI,pm_message_t mesg ) {return 0; } staticintrx 8025 t _ resume (struct I2C _ client * CLI ) {return 0; } staticstructi 2c _ device _ idrtc _ id [ ]={ { rx 8025 t _ name,0},{NULL,0}; static structi 2c _ driver driver _ RTC={.driver={.name=rx 8025 t _ name,}, id_table=rtc_id, probe=rx 8025 struct i2c_msg msg; msg.addr=g_cli-addr; msg.flags=(g _ CLI-flags I2C _ m _ ten )| I2C_M_RD; //I2C_M_RD读取模式。 没有该标志的默认表示//g_cli-flags I2C_M_TEN将I2C_M_TEN以外的位清零,不改变地址模式位的值,默认表示7位地址模式位msg.buf=kdata; g _ CLI-adapter-dev.platform _ data=g _ CLI-dev.platform _ data; //平台数据return设置() ret=I2C _ transfer (g _ CLI-adapter,msg,1 ) )=1)? len : ret; 开始//I2c传输} static ssize _ trtc _ write (char * kdata,int len ) {int ret; struct i2c_msg msg; msg.flags=g_cli-flags I2C_M_TEN; //默认写入msg.addr=g_cli-addr; msg.len=len; msg.buf=kdata; g _ CLI-adapter-dev.platform _ data=g _ CLI-dev.platform _ data; return((ret=I2c_transfer ) g_CLI-adapter,msg,1 ) )==1)? len : ret; 开始//I2C传输} staticintrx 8025 t _ open (struct inode * in,struct file* fi ) {return 0; } static { ssize_t Trx 8025 t _ read (struct file * fi,char __user *buf,size_t len,loff_t* ff ) ssize _ t; char kdata[KBUF_SIZE]; if(lenkbuf_size ) len=KBUF_SIZE; t=RTC_read(kdata,len ); if(t0 ) if ) len=t ) len=t; if(copy_to_user(buf,kdata,len ) ) printk ) kern_info'copydataerrorn ' ); 返回-故障; }}return t; } static ssize _ Trx 8025 t _ write (struct file * fi,const char __user *buf,size_t len,loff_t* ff ) charkdatatatatttttate 返回-故障; }if(copy_from_user(kdata,buf,len ) ) printk ) kern_info'getinputerror(n ' ); 返回-故障; }returnRTC_write(kdata,len ); } staticintrx 8025 t _ ioctl (struct file * fi,unsigned int cmd,unsigned long data ) {return -1; } staticstructfile _ operations file _ oper={.owner=this _ module, open=rx8025t_open, read=rx 8025 t _ read RTC _ class=class _ create (this _ module,' rx8025t_class ' ); //创建设备类if (is _ err ) RTC_class ) (printk ) ) error :故障诊断类别 n ' ); 返回- 1; }ret=alloc_chrdev_region(dev,199,1,RX8025T_NAME ); //使内核自动分配设备编号if(ret0) class _ destroy (RTC _ class ); printk (kern _ info ' alloc _ chrdev _ region fail (n ) ); 返回; }device_create(RTC_class,NULL,dev,NULL,' %s ',RX8025T_NAME ); //在类下创建设备节点rtc_dev=cdev_alloc (的if (! RTC _ dev ({ printk (kern _ info ' alloccdevfail (n ) } ); ret=-1; goto error_exit; }cdev_init(RTC_dev,file_oper ); file_operations结构rtc_dev-owner=THIS_MODULE; ret=cdev_add(RTC_dev,dev,1 ); //将字符设备添加到系统if(ret0) printk(Kern_info'cdevaddfailn ' ); goto error_exit; } return I2C _ add _ driver (driver _ RTC ); //I2注册cdrivererror _ exit : rx 8025 t _ exit (); 返回; } static void _ exit rx 8025 t _ exit (void ) if(RTC_dev ) cdev_del ) RTC_dev ); //删除字符设备if(RTC_class ) device _ destroy (RTC _ class,dev ); 丢弃//设备class_destroy(RTC_class ); //废弃类}unregister_chrdev_region(dev,1 ); //释放设备编号I2c_del_driver(driver_RTC )//I2C driver }删除module _ init (rx 8025 t _ init ) module _ exit (rx 8025 t ) module_license(GPL ); 模式_ alias (I2C rx 8025 t ); module _ description (rx 8025 ti2cslavedriver );

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。