历史上的今天
今天是:2024年10月12日(星期六)
2018年10月12日 | Tiny210 IIC驱动之at24cxx访问
2018-10-12 来源:eefocus
at24cxx_dev.c驱动源码:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/slab.h"
static struct i2c_board_info at24cxx_info = {
I2C_BOARD_INFO("at24c08", 0x50),
};
static struct i2c_client *at24cxx_client;
static int at24cxx_dev_init(void)
{
struct i2c_adapter *i2c_adap;
i2c_adap = i2c_get_adapter(0);
at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
i2c_put_adapter(i2c_adap);
return 0;
}
static void at24cxx_dev_exit(void)
{
i2c_unregister_device(at24cxx_client);
}
module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE("GPL");
===================================================================
at24cxx_drv.c驱动源码:
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/platform_device.h"
#include "linux/i2c.h"
#include "linux/err.h"
#include "linux/slab.h"
#include "linux/fs.h"
#include "asm/uaccess.h"
static int major;
static struct class *class;
static struct i2c_client *at24cxx_client;
// 传入: buf[0] : addr
// 输出: buf[0] : data
static ssize_t at24cxx_read(struct file * file, char __user *buf, size_t count, loff_t *off)
{
unsigned char addr, data;
copy_from_user(&addr, buf, 1);
data = i2c_smbus_read_byte_data(at24cxx_client, addr);
copy_to_user(buf, &data, 1);
return 1;
}
// buf[0] : addr
// buf[1] : data
static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t count, loff_t *off)
{
unsigned char ker_buf[2];
unsigned char addr, data;
copy_from_user(ker_buf, buf, 2);
addr = ker_buf[0];
data = ker_buf[1];
printk("addr = 0xx, data = 0xx\n", addr, data);
if (!i2c_smbus_write_byte_data(at24cxx_client, addr, data))
return 2;
else
return -EIO;
}
static struct file_operations at24cxx_fops = {
.owner = THIS_MODULE,
.read = at24cxx_read,
.write = at24cxx_write,
};
static int __devinit at24cxx_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
at24cxx_client = client;
//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
major = register_chrdev(0, "at24cxx", &at24cxx_fops);
class = class_create(THIS_MODULE, "at24cxx");
device_create(class, NULL, MKDEV(major, 0), NULL, "at24cxx"); // /dev/at24cxx
return 0;
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
//printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
device_destroy(class, MKDEV(major, 0));
class_destroy(class);
unregister_chrdev(major, "at24cxx");
return 0;
}
static const struct i2c_device_id at24cxx_id_table[] = {
{ "at24c08", 0 },
{}
};
// 1. 分配/设置i2c_driver
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "100ask",
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_id_table,
};
static int at24cxx_drv_init(void)
{
// 2. 注册i2c_driver
i2c_add_driver(&at24cxx_driver);
return 0;
}
static void at24cxx_drv_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE("GPL");
=====================================================================
i2c_test.c测试程序:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
// i2c_test r addr
// i2c_test w addr val
void print_usage(char *file)
{
printf("%s r addr\n", file);
printf("%s w addr val\n", file);
}
int main(int argc, char **argv)
{
int fd;
unsigned char buf[2];
if ((argc != 3) && (argc != 4))
{
print_usage(argv[0]);
return -1;
}
fd = open("/dev/at24cxx", O_RDWR);
if (fd < 0)
{
printf("can't open /dev/at24cxx\n");
return -1;
}
if (strcmp(argv[1], "r") == 0)
{
buf[0] = strtoul(argv[2], NULL, 0);
read(fd, buf, 1);
printf("data: %c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
}
else if ((strcmp(argv[1], "w") == 0) && (argc == 4))
{
buf[0] = strtoul(argv[2], NULL, 0);
buf[1] = strtoul(argv[3], NULL, 0);
if (write(fd, buf, 2) != 2)
printf("write err, addr = 0xx, data = 0xx\n", buf[0], buf[1]);
}
else
{
print_usage(argv[0]);
return -1;
}
return 0;
}
下一篇:Tiny210驱动之虚拟网卡驱动
史海拾趣
|
Altium Protel DXP是第一套完整的板卡级设计系统,真正实现在单个应用程序中的集成。设计从一开始的目的就是为了支持整个设计过程,Protel DXP让你可以选择最适当的设计途径来按你想要的方式工作。Protel DXP PCB线路图设计系统完全利用了Windows X ...… 查看全部问答> |
|
我前几天下了个Cadence软件,主要是查看芯片封装信息的,其中主要有两个文件,一个是pad格式,可以打开,另一个是psm格式的文件 怎么也打不开。气死了,现在很急,希望哪位高手可以帮助我。。。:Q… 查看全部问答> |
|
程序如下 #include unsigned int count=0; unsigned int count1=0; sbit P15=P1^5 ;//蜂鸣器 main() { TMOD=0X01;//选择定时器0方式1 TH0=(65536-50000)/256;//定时器置初值,定时50ms ...… 查看全部问答> |
|
Rt_thread中如何用FinSH shell查看一个线程使用的栈最大深度 请教了:RT中定义堆栈是一个麻烦事,我们要怎样用FinSH shell来查看一个线程使用的栈的最大深度喃? 哪位说说看。。。。。… 查看全部问答> |
|
为啥用msp430驱动不了呢?用51就可以呢?msp430用的是默认的时钟!各位帮帮忙啊! /**************宏定义***************/ #define DataDir0 P4DIR //LCD1602 #define DataPort0 ...… 查看全部问答> |
|
在能源紧缺的背景下,可以说节约能源是我们面临的重要问题,LED一直被称为新一代绿色光源。它的节能、环保、寿命长等优点,使LED照明产业成为各国重点关注的绿色产业。鉴于LED照明技术的日益成熟以及飞速发展,为了规范LED照明这一新兴市场,全球一 ...… 查看全部问答> |
|
本帖最后由 yhye2world 于 2016-10-27 15:11 编辑 见附图,为 《STM32库开发实战指南》(刘火良 版)封面截图。 请教: 哪位 有这本书的 光盘资料 呀 ?给小弟 共享下 ! 先谢谢了 ! … 查看全部问答> |




