我是靠谱客的博主 欣慰山水,这篇文章主要介绍DAC MCP4725 i2c 驱动(linux),现在分享给大家,希望可以做个参考。

mcp4725是一款低功耗,高精度 单通道,拥有EEPROM的12位的dac。

由于最近项目中使用到了该芯片所以贴出来给大家参考(步进电机电机芯片半流锁定。)

本贴呢非项目中使用的平台,主要是想在linux 下实现对该器件的使用,实现一个简单的i2c dac字符驱动。

对于i2c 基本原理通信协议呢不做过多描述。

1.mcp4725 地址

mcp4725官方默认地址是一般是b'1100 (96) a0 可通过外部上拉或者下拉决定。

 

2.数据流程

 

通过前面图可以看到mcp4725 控制起来是非常简单的 第一个字节主要是器件地址(7位)+读写位(1位)。第二个字节 高4位呢是控制指令,低4位呢主要配合后面的第三个字节,是第三个字节的高4位。

那么快速模式怎么使用呢?比如需要2v 电压。

假设VDD 为电源电压5V dac的压降为0.7v 那么 我们需要2v 的的电压 Vout=2/(5-0.7)*4095=1905 ,转换为2进制可以看到

 

高4位位0111 低7位为01110001  。那么对应给设备发送 就应当为 (16进制):0x60  0x07 0x71 那么我们的数据就写好了。

本环境在MT7628 openWRT 环境下,MT7628  只有1个i2c 总线接口他的地址为0x10000900

由于linux 使用设备树,其设备树定义为:

 

下面看一下linux 驱动代码的具体实现:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <linux/i2c.h> #include <linux/of.h> #include <linux/mod_devicetable.h> #include <linux/device.h> #include <linux/slab.h> #include <linux/cdev.h> #include <linux/fs.h> int major; struct class *cls; struct mcp4725_dev_t { struct cdev chrdev; struct device *device; struct i2c_client *cli; int index; }; int mcp4725_open(struct inode *inode,struct file *file) { struct mcp4725_dev_t *pri=container_of(inode->i_cdev,struct mcp4725_dev_t,chrdev);//container_of(inode->i_cdev,struct mcp4725_dev_t,chardev); struct i2c_client *cli; file->private_data=pri; cli=pri->cli; printk("%s->%dn",__func__,__LINE__); return 0; } int mcp4725_close(struct inode *inode,struct file *file) { printk("%s->%dn",__func__,__LINE__); return 0; } long mcp4725_ioctl(struct file * file, unsigned int cmd, unsigned long args) { printk("%s->%dn",__func__,__LINE__); return 0; } int mcp_write(struct i2c_client *cli,unsigned short val) { int ret=0; unsigned char buf[2]; struct i2c_msg msg; buf[0]=(unsigned char)(val>>8);//高位4位 buf[1]=(unsigned char)val;//低8位 msg.addr=cli->addr; msg.buf=buf; msg.flags=0; //msg. msg.len=2; ret=i2c_transfer(cli->adapter,&msg,1); if(ret!=1) { printk("%s->%d i2c_transfer ret%dn",__func__,__LINE__,ret); return -23; } printk("send %daddr:%d->%dn",__LINE__,cli->addr,val); return 0; } int mcp4725_write(struct file *file, const char __user *usr, size_t size, loff_t *loff) { int ret=0; unsigned short user_data; struct i2c_client *cli=NULL; struct mcp4725_dev_t *pri=file->private_data; cli=pri->cli; //char buf ret = copy_from_user(&user_data,usr,size); printk("addr:%d->%dn",cli->addr,user_data); if(ret<0) { printk("error:%s->%dn",__func__,__LINE__); return -20; } mcp_write(cli,user_data); printk("%s->%dn",__func__,__LINE__); return 0; } struct file_operations fops_mcp4725={ .open=mcp4725_open, .release=mcp4725_close, .write=mcp4725_write, .unlocked_ioctl=mcp4725_ioctl, }; int mcp4725_remove(struct i2c_client *cli) { /* data */ return 0; } struct i2c_device_id id_table[] = { }; int mcp4725_proble(struct i2c_client *cli,const struct i2c_device_id *id) { int ret=0; struct device_node *of_node=cli->dev.of_node; struct mcp4725_dev_t *mcp4725_pri=NULL; mcp4725_pri=kmalloc(sizeof(*mcp4725_pri),GFP_KERNEL); printk("addr:%d->%dn",cli->addr,__LINE__); printk("%s->%dn",__func__,__LINE__); memset(mcp4725_pri,0,sizeof(*mcp4725_pri)); cli->dev.platform_data=mcp4725_pri; mcp4725_pri->cli=cli; // ret=of_property_read_u32(of_node,"index",&mcp4725_pri->index); // if(ret<0) // { // printk("error:%s->%dn",__func__,__LINE__); // return -20; // } mcp4725_pri->index=5; printk("%s->%dn",__func__,__LINE__); cdev_init(&mcp4725_pri->chrdev,&fops_mcp4725); ret=cdev_add(&mcp4725_pri->chrdev,MKDEV(major,mcp4725_pri->index),1); if(ret<0) { printk("error:%s->%dn",__func__,__LINE__); return -21; } mcp4725_pri->device=device_create(cls,NULL,MKDEV(major,mcp4725_pri->index), NULL,"mcp4725xx%d",mcp4725_pri->index); if(mcp4725_pri->device==NULL) { printk("error:%s->%dn",__func__,__LINE__); return -22; } //mcp4725_pri->device-> printk("%s->%dn",__func__,__LINE__); return 0; } struct of_device_id of_match_table[]={ {.compatible="MCP4725",}, {} }; struct i2c_driver i2c_mcp4725={ .driver={ .name="i2c_MCP4725", .of_match_table=of_match_table, }, .id_table=id_table, .probe=mcp4725_proble, .remove=mcp4725_remove, } ; int __init mcp4725_init(void) { int ret=0; int devno; //alloc_chrdev_region ret=alloc_chrdev_region(&devno,0,255,"mcp4725 for chardev"); if(ret<0) { printk("%s-->%d",__func__,__LINE__); return -20; } major=MAJOR(devno); cls=class_create(THIS_MODULE,"pdev char class"); if(!cls) { printk("%s-->%d",__func__,__LINE__); return -20; } ret=i2c_add_driver(&i2c_mcp4725); if(ret<0) { printk("%s-->%d",__func__,__LINE__); return -20; } printk("%s-->%d",__func__,__LINE__); return 0; } void __exit mcp4725_exit(void) { i2c_del_driver(&i2c_mcp4725); class_destroy(cls); //unregister_chrdev_region unregister_chrdev_region(MKDEV(major,0),255); printk("%s->%d",__func__,__LINE__); return ; } module_init(mcp4725_init); module_exit(mcp4725_exit); MODULE_LICENSE("GPL");

测试APP 为:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdio.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> int main(int argc,char** argv) { char led1=0; char buf[10]; int data; int fd=open("/dev/mcp4725xx5",O_WRONLY); if(fd<0) { printf("open error rn"); return -1; } data=atoi(argv[1]); unsigned short val=(short)(data)/4.7*4095; printf("clc:%drn",val); write(fd,&val,sizeof(short)); printf("exitrn"); close(fd); return 1; }

 

 

 

最后

以上就是欣慰山水最近收集整理的关于DAC MCP4725 i2c 驱动(linux)的全部内容,更多相关DAC内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部