文章目錄
- 前言
- 一、IIC驅動程式架構分析
- 二、源碼執行個體分析
- 三、實驗結果
前言
本文基于S3C2440開發闆。
一、IIC驅動程式架構分析
核心中 I2C 的處理已經做好了,我們隻需要做裝置驅動程式相關的内容。總線處理好了 I2C 協定,即總線知道如何收發資料,而不知道資料的含義,我們要做的隻是裝置相關層的代碼 , 例如提供裝置的位址和要存取的資料和位址 。I2C 協定中,先發出 7bit“裝置位址”,然後是 1 位“寫”或“讀”的标志位。然後接着是每發出 8 位資料有一個 ACK 位。
總線層: 知道裝置如何讀寫。晶片廠家會幫我們做好。操作寄存器drivers\i2c\busses
裝置層驅動層:知道資料的含義。drivers\i2c\chips
程式編寫步驟:
- 1,配置設定某結構體:
i2c_driver
-
2,設定結構體。
主要是提供2個函數:
.attach_adapter = at24cxx_attach,
.detach_client = at24cxx_detach,
- 3,注冊結構體
。i2c_add_driver(&at24cxx_driver);
- 4,硬體相關操作。
二、源碼執行個體分析
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
static unsigned short ignore[] = { I2C_CLIENT_END };
static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; /* 位址值是7位 */
/* 改為0x60的話, 由于不存在裝置位址為0x60的裝置, 是以at24cxx_detect不被調用 */
static unsigned short force_addr[] = {ANY_I2C_BUS, 0x60, I2C_CLIENT_END};
static unsigned short * forces[] = {force_addr, NULL};
static struct i2c_client_address_data addr_data = {
.normal_i2c = normal_addr, /* 要發出S信号和裝置位址并得到ACK信号,才能确定存在這個裝置 */
.probe = ignore,
.ignore = ignore,
//.forces = forces, /* 強制認為存在這個裝置 */
};
static struct i2c_driver at24cxx_driver;
static int major;
static struct class *cls;
struct i2c_client *at24cxx_client;
static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)
{
unsigned char address;
unsigned char data;
struct i2c_msg msg[2];
int ret;
/* address = buf[0]
* data = buf[1]
*/
if (size != 1)
return -EINVAL;
copy_from_user(&address, buf, 1);
/* 資料傳輸三要素: 源,目的,長度 */
/* 讀AT24CXX時,要先把要讀的存儲空間的位址發給它 */
msg[0].addr = at24cxx_client->addr; /* 目的 */
msg[0].buf = &address; /* 源 */
msg[0].len = 1; /* 位址=1 byte */
msg[0].flags = 0; /* 表示寫 */
/* 然後啟動讀操作 */
msg[1].addr = at24cxx_client->addr; /* 源 */
msg[1].buf = &data; /* 目的 */
msg[1].len = 1; /* 資料=1 byte */
msg[1].flags = I2C_M_RD; /* 表示讀 */
ret = i2c_transfer(at24cxx_client->adapter, msg, 2);
if (ret == 2)
{
copy_to_user(buf, &data, 1);
return 1;
}
else
return -EIO;
}
static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
unsigned char val[2];
struct i2c_msg msg[1];
int ret;
/* address = buf[0]
* data = buf[1]
*/
if (size != 2)
return -EINVAL;
copy_from_user(val, buf, 2);
/* 資料傳輸三要素: 源,目的,長度 */
msg[0].addr = at24cxx_client->addr; /* 目的 */
msg[0].buf = val; /* 源 */
msg[0].len = 2; /* 位址+資料=2 byte */
msg[0].flags = 0; /* 表示寫 */
ret = i2c_transfer(at24cxx_client->adapter, msg, 1);
if (ret == 1)
return 2;
else
return -EIO;
}
static struct file_operations at24cxx_fops = {
.owner = THIS_MODULE,
.read = at24cxx_read,
.write = at24cxx_write,
};
static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind)
{
printk("at24cxx_detect\n");
/* 構構一個i2c_client結構體: 以後收發資料時會用到它 */
at24cxx_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
at24cxx_client->addr = address;
at24cxx_client->adapter = adapter;
at24cxx_client->driver = &at24cxx_driver;
strcpy(at24cxx_client->name, "at24cxx");
i2c_attach_client(at24cxx_client);
major = register_chrdev(0, "at24cxx", &at24cxx_fops);
cls = class_create(THIS_MODULE, "at24cxx");
class_device_create(cls, NULL, MKDEV(major, 0), NULL, "at24cxx"); /* /dev/at24cxx */
return 0;
}
static int at24cxx_attach(struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, at24cxx_detect);
}
static int at24cxx_detach(struct i2c_client *client)
{
printk("at24cxx_detach\n");
class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "at24cxx");
i2c_detach_client(client);
kfree(i2c_get_clientdata(client));
return 0;
}
/* 1. 配置設定一個i2c_driver結構體 */
/* 2. 設定i2c_driver結構體 */
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = "at24cxx",
},
.attach_adapter = at24cxx_attach,
.detach_client = at24cxx_detach,
};
static int at24cxx_init(void)
{
i2c_add_driver(&at24cxx_driver);
return 0;
}
static void at24cxx_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}
module_init(at24cxx_init);
module_exit(at24cxx_exit);
MODULE_LICENSE("GPL");
代碼分析:
①當我們用insmod加載驅動,執行i2c_add_driver後,就會從總線層的adapter連結清單中取出"擴充卡",調用
attach_adapter
,執行
at24cxx_attach
函數中的
i2c_probe(adapter, &addr_data, at24cxx_detect);
,使用擴充卡中的
.smbus_xfer
函數發“start”信号、發裝置位址等等,如果等到裝置的回應信号就會調用
at24cxx_detect
函數。
②在
at24cxx_detect
函數中,我們就可以得到具體的擴充卡和我們最先寫的at24cxx_driver 的比對項,這時需要再構造一個結構體
struct i2c_client *at24cxx_client;
,這個結構體的内容主要是填充我們的相比對的擴充卡和驅動,同時也填充i2c裝置的位址,這個結構體在資料的收發時需要用到,和在解除安裝驅動程式時會調用at24cxx_driver裡的.detach_client = at24cxx_detach。這裡同時建立一個字元裝置。
③如何與i2c裝置進行通訊?這裡直接用核心提供的函數;
i2c_transfer()
,需要的參數主要由擴充卡,資料包,包長;寫資料的話,我們隻需要發送一個包,資料包的格式主要有源,目的,長度,和讀寫标志位。讀資料的話需要先寫要讀的位址,然後再讀,需要兩個包。
測試驅動代碼:
#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)
{
buf[0] = strtoul(argv[2], NULL, 0);
buf[1] = strtoul(argv[3], NULL, 0);
write(fd, buf, 2);
}
else
{
print_usage(argv[0]);
return -1;
}
return 0;
}