(linux驱动学习 - 12). IIC 驱动实验

embedded/2024/10/21 17:47:47/

一.IIC 总线驱动相关结构体与函数

1.i2c_adapter 结构体

struct i2c_adapter 
{struct module *owner;unsigned int class;                 /* classes to allow probing for */const struct i2c_algorithm *algo;   /* 总线访问算法 */void *algo_data;/* data fields that are valid for all devices */struct rt_mutex bus_lock;int timeout;                        /* in jiffies */int retries;struct device dev;                  /* the adapter device */int nr;char name[48];struct completion dev_released;struct mutex userspace_clients_lock;struct list_head userspace_clients;struct i2c_bus_recovery_info *bus_recovery_info;const struct i2c_adapter_quirks *quirks;
};

2.i2c_algorithm 结构体

struct i2c_algorithm 
{/* I2C 适配器的传输函数,通过此函数与 IIC 设备进行通信  */int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs,int num);/* SMBUS 总线的传输函数 */int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,unsigned short flags, char read_write,u8 command, int size, union i2c_smbus_data *data);/* To determine what the adapter supports */u32 (*functionality) (struct i2c_adapter *);......
};

3.向系统注册设置好的 i2c_adapter 结构体 - i2c_add_adapter

/*** @description:            使用动态的总线号,向系统注册设置好的 i2c_adapter 结构体* @param - adapter     :   要添加到 Linux 内核中的 i2c_adapter,也就是 I2C 适配器* @return              :   成功则返回(0),失败则返回(负值)*/
int i2c_add_adapter(struct i2c_adapter *adapter)

4.向系统注册设置好的 i2c_adapter 结构体 - i2c_add_numbered_adapter

/*** @description:            使用静态的总线号,向系统注册设置好的 i2c_adapter 结构体       * @param - adap        :   要添加到 Linux 内核中的 i2c_adapter,也就是 I2C 适配器* @return              :   成功则返回(0),失败则返回(负值)     */
int i2c_add_numbered_adapter(struct i2c_adapter *adap)

5.删除 I2C 适配器 - i2c_del_adapter

/*** @description:            删除 I2C 适配器* @param - adap        :   要删除的 I2C 适配器* @return              :   无*/
void i2c_del_adapter(struct i2c_adapter *adap)

二.IIC 设备驱动相关结构体与函数

1. 描述设备信息 - i2c_client 结构体

struct i2c_client 
{unsigned short flags;           /* 标志 */unsigned short addr;            /* 芯片地址, 7 位,存在低 7 位*/......char name[I2C_NAME_SIZE];       /* 名字 */struct i2c_adapter *adapter;    /* 对应的 I2C 适配器 */struct device dev;              /* 设备结构体 */int irq;                        /* 中断 */struct list_head detected;......
};

2.i2c_driver 结构体

struct i2c_driver 
{unsigned int class;int (*attach_adapter)(struct i2c_adapter *) __deprecated;/*  当 I2C 设备和驱动匹配成功以后,probe 函数 就会执行 */int (*probe)(struct i2c_client *, const struct i2c_device_id *);int (*remove)(struct i2c_client *);void (*shutdown)(struct i2c_client *);void (*alert)(struct i2c_client *, unsigned int data);int (*command)(struct i2c_client *client, unsigned int cmd,void *arg);/* 若使用了设备树,需要设置 device_driver 的 of_match_table 成员变量,也就是驱动的兼容(compatible)属性 */struct device_driver driver;/* 未使用设备树时,用于匹配驱动和设备的 匹配ID表 */const struct i2c_device_id *id_table;int (*detect)(struct i2c_client *, struct i2c_board_info *);const unsigned short *address_list;struct list_head clients;
};

3.向内核注册 i2c_driver - i2c_register_driver

/*** @description:            向Linux内核注册 i2c_driver* @param - owner       :   一般为THIS_MODULE* @param - driver      :   要注册的 i2c_driver* @return              :   成功则返回(0),失败则返回(负值)*/
int i2c_register_driver(struct module *owner,struct i2c_driver *driver)/* 对 i2c_register_driver 函数的宏封装 */
#define i2c_add_driver(driver) i2c_register_driver(THIS_MODULE,driver)

4.注销 I2C 设备驱动 - i2c_del_driver

/*** @description:            注销 I2C 设备驱动* @param - driver      :   要注销的 i2c_driver* @return              :   无*/
void i2c_del_driver(struct i2c_driver *driver)

5.I2C 总线数据结构 - i2c_bus_type

struct bus_type i2c_bus_type = 
{.name = "i2c",.match = i2c_device_match,          //设备和驱动匹配函数.probe = i2c_device_probe,          .remove = i2c_device_remove,.shutdown = i2c_device_shutdown,
};

6.传输 I2C 数据 - i2c_transfer

/*** @description:            传输 I2C 数据* @param - adap        :   所使用的 I2C 适配器,i2c_client 会保存对应的 i2c_adapter* @param - msg         :   I2C 要发送的一个或多个消息* @param - num         :   要发送的消息的数量,也就是 msg 的数量* @return              :   成功则返回(发送的 msg 数量),失败则返回(负值)*/
int i2c_transfer(struct i2c_adapter *adap,struct i2c_msg *msg,int num)

7.i2c_msg 结构体

struct i2c_msg 
{__u16 addr;                 /* 从机地址 */__u16 flags;                /* 标志 */#define I2C_M_TEN 0x0010#define I2C_M_RD 0x0001#define I2C_M_STOP 0x8000#define I2C_M_NOSTART 0x4000#define I2C_M_REV_DIR_ADDR 0x2000#define I2C_M_IGNORE_NAK 0x1000#define I2C_M_NO_RD_ACK 0x0800#define I2C_M_RECV_LEN 0x0400__u16 len;                /* 消息(本 msg)长度 */__u8 *buf;                /* 消息数据 */
};

8.I2C 写数据

/*** @description:            I2C 数据发送函数,其本质是对 i2c_transfer 的封装* @param - client      :   I2C 设备对应的 i2c_client* @param - buf         :   要发送的数据* @param - count       :   要发送的数据的字节数,要小于 64KB* @return              :   成功则返回(发送的字节数),失败则返回(负值)*/
int i2c_master_send(const struct i2c_client *client,const char *buf,int count)

9.I2C 读数据

/*** @description:            I2C 数据接收函数,其本质是对 i2c_transfer 的封装* @param - client      :   I2C 设备对应的 i2c_client* @param - buf         :   要接收的数据* @param - count       :   要接收的字节数,要小于 64KB* @return              :   成功则返回(发送的字节数),失败则返回(负值)*/
int i2c_master_recv(const struct i2c_client *client,char *buf,int count)

三.I2C 驱动实验 - AP3216C

1.设备树部分

        由于Linux内核中已经有 I2C 驱动程序了,这里只需要在设备树中添加 oled 设备节点

(1).流程图

(2).设备树代码

        编译设备树,可以在开发板中的 /sys/bus/i2c/device/ 目录下看到 0-001e 这个设备,001e 就是 ap3216c 设备的地址

2.驱动程序

(1).流程图

(2).驱动代码

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include "ap3216creg.h"#define AP3216C_CNT     1
#define AP3216C_NAME    "ap3216c"/* ap3216c 设备结构体 */
struct ap3216c_dev
{dev_t devid;                    /* 设备号 */struct cdev cdev;               /* cdev */struct class *class;            /* 类 */struct device *device;          /* 设备 */struct device_node *nd;         /* 设备结点 */int major;                      /* 主设备号 */void *private_data;             /* 私有数据 */unsigned short ir,als,ps;       /* 三个光传感器数据 */
};static struct ap3216c_dev ap3216cdev;/*** @description:            从 ap3216c 读取多个寄存器数据* @param - dev     :       ap3216c 设备* @param - reg     :       要读取的寄存器首地址* @param - val     :       要读取的数据* @param - len     :       要读取的长度* @return          :       操作结果*/
static int ap3216c_read_regs(struct ap3216c_dev *dev,u8 reg,void *val,int len)
{int ret;struct i2c_msg msg[2];struct i2c_client *client = (struct i2c_client *)dev->private_data;/* msg[0] 为发送要读取的首地址 */msg[0].addr = client->addr;                 /* ap3216c 的设备地址 */msg[0].flags = 0;                           /* 标记为发送数据 */msg[0].buf = &reg;                          /* 要读取的寄存器首地址 */msg[0].len = 1;                             /* reg 的长度 *//* msg[1] 读取数据 */msg[1].addr = client->addr;                 /* ap3216c 的设备地址 */msg[1].flags = I2C_M_RD;                    /* 标记为读数据 */msg[1].buf = val;                           /* 读取数据缓冲区 */msg[1].len = len;                           /* 要读取的数据长度 */ret = i2c_transfer(client->adapter,msg,2);if(2 == ret){ret = 0;}           else{printk("i2c rd failed = %d ret = %06x len = %d\r\n",ret,reg,len);ret = -EREMOTEIO;}                    return ret;   
}static s32 ap3216c_write_regs(struct ap3216c_dev *dev,u8 reg,u8 *buf,u8 len)
{u8 b[256];struct i2c_msg msg;struct i2c_client *client = (struct i2c_client *)dev->private_data;b[0] = reg;                 /* 寄存器首地址 */memcpy(&b[1],buf,len);      /* 将要写入的数据拷贝到 数组b 里面 */msg.addr = client->addr;    /* ap3216c 地址 */msg.flags = 0;              /* 标记为写数据 */msg.buf = b;                /* 要写入的数据缓冲区 */msg.len = len + 1;          /* 要写入的数据长度(1个字节的目标寄存器地址 + 写入的数据长度) */return i2c_transfer(client->adapter,&msg,1);
}/*** @description:            读取 ap3216c 指定寄存器的值,读取一个寄存器* @param - dev     :       ap3216c 设备* @param - reg     :       要读取的寄存器* @return          :       读取到的寄存器值*/
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev,u8 reg)
{u8 data = 0;ap3216c_read_regs(dev,reg,&data,1);return data;
}/*** @description:            向 ap3216c指定寄存器写入指定的值,写一个寄存器* @param - dev     :       ap3216c 设备* @param - reg     :       要写入的寄存器* @param - data    :       要向寄存器写入的值*/
static void ap3216c_write_reg(struct ap3216c_dev *dev,u8 reg,u8 data)
{u8 buf = 0;buf = data;ap3216c_write_regs(dev,reg,&buf,1);
}/*** @description:            读取 ap3216c 的数据,读取原始数据,包括 ALS,PS 和 IR。同时打开 ALS,IR+PS 的话,两次数据读取的间隔要大于 112.5 ms* @param - ir      :       ir 数据* @param - ps      :       ps 数据* @param - als     :       als 数据*/
void ap3216c_readdata(struct ap3216c_dev *dev)
{unsigned char i = 0;unsigned char buf[6];/* 循环读取所有传感器数据 */for(i = 0; i < 6 ; i++){buf[i] = ap3216c_read_reg(dev,AP3216C_IRDATALOW + i);}/* IR_OF 位为1,则数据无效 */if(buf[0] & 0x80){dev->ir = 0;}else            //读取 IR 传感器的数据dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x03);  /* ALS 数据 */dev->als = ((unsigned short)buf[3] << 8) | buf[2];/* IR_OF 位为1,则数据无效 */if(buf[4] & 0x40){dev->ps = 0;        }elsedev->ps = ((unsigned short)(buf[5] & 0x3F) << 4) | (buf[4] & 0x0F);}/*** @description:            打开设备* @param - inode       :   传递给驱动的 inode* @param - filp        :   设备文件,在open函数里面,把 filp->private_data 指向 设备结构体* @return              :   0成功,其他失败*/
static int ap3216c_open(struct inode *inode,struct file *filp)
{/* 1.设置私有数据 */filp->private_data = &ap3216cdev;/* 2.初始化 AP3216C */ap3216c_write_reg(&ap3216cdev,AP3216C_SYSTEMCONG,0x04);mdelay(50);             //AP3216C 复位最少要 10msap3216c_write_reg(&ap3216cdev,AP3216C_SYSTEMCONG,0X03);return 0;
}/*** @description:            从设备读取数据* @param - filp    :       要打开的设备文件描述符* @param - buf     :       返回给用户空间的数据缓冲区* @param - cnt     :       要读取的字节数* @param - off     :       相对于文件首地址的偏移量* @return          :       成功则返回(读取到的字节数),失败则返回(负值)*/
static ssize_t ap3216c_read(struct file *filp,char __user *buf,size_t cnt,loff_t *off)
{   short data[8];long err = 0;struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;ap3216c_readdata(dev);data[0] = dev->ir;data[1] = dev->als;data[2] = dev->ps;err = copy_to_user(buf,data,sizeof(data));return 0;
}/*** @description:            关闭/释放设备* @param - filp        :*/
static int ap3216c_release(struct inode *inode,struct file *filp)
{return 0;
}/* 绑定 AP3216C 设备操作函数 */
static const struct file_operations ap3216c_fops = 
{.owner = THIS_MODULE,.open = ap3216c_open,.read = ap3216c_read,.release = ap3216c_release,
};/*** @description:            i2c 驱动的 probe 函数,当驱动与设备匹配以后就会执行此函数* @param - client      :   i2c 设备* @param - id          :   i2c 设备ID* @return              :   0成功,其他失败*/
static int ap3216c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{printk("driver and device was matched!\r\n");/* 1.构建设备号 */if(ap3216cdev.major)    //若有主设备号{ap3216cdev.devid = MKDEV(ap3216cdev.major,0);register_chrdev_region(ap3216cdev.devid,AP3216C_CNT,AP3216C_NAME);}else{alloc_chrdev_region(&ap3216cdev.devid,0,AP3216C_CNT,AP3216C_NAME);ap3216cdev.major = MAJOR(ap3216cdev.devid);}/* 2.注册 cdev */cdev_init(&ap3216cdev.cdev,&ap3216c_fops);cdev_add(&ap3216cdev.cdev,ap3216cdev.devid,AP3216C_CNT);/* 3.创建类 */ap3216cdev.class = class_create(THIS_MODULE,AP3216C_NAME);if(IS_ERR(ap3216cdev.class)){return PTR_ERR(ap3216cdev.class);}/* 4.创建设备 */ap3216cdev.device = device_create(ap3216cdev.class,NULL,ap3216cdev.devid,NULL,AP3216C_NAME);if(IS_ERR(ap3216cdev.device)){return PTR_ERR(ap3216cdev.device);}ap3216cdev.private_data = client;return 0;
}/*** @description:            i2c 驱动的 remove 函数,移除 i2c驱动时,此函数会执行* @param -client       :   i2c 设备* @return              :   0,成功;其他,失败*/
static int ap3216c_remove(struct i2c_client *client)
{/* 1.删除设备 */cdev_del(&ap3216cdev.cdev);unregister_chrdev_region(ap3216cdev.devid,AP3216C_CNT);/* 2.注销掉类和设备 */device_destroy(ap3216cdev.class,ap3216cdev.devid);class_destroy(ap3216cdev.class);return 0;
}/* 无设备树时的匹配方式- ID列表 */
static const struct i2c_device_id ap3216c_id[] = 
{{"alientek,ap3216c",0},{},
};/* 设备树匹配列表 */
static const struct of_device_id ap3216c_of_match[] = 
{{.compatible = "alientek,ap3216c"},{},
};/* i2c 驱动结构体 */
static struct i2c_driver ap3216c_driver = 
{.probe = ap3216c_probe,.remove = ap3216c_remove,.driver = {.owner = THIS_MODULE,.name = "ap3216c",/* 用于设备树下的匹配 */.of_match_table = ap3216c_of_match,},/* 用于无设备树时的匹配 */.id_table = ap3216c_id,
};/*** @description:            驱动入口函数* @param           :       无* @return          :       无*/
static int __init ap3216c_init(void)
{int ret = 0;ret = i2c_add_driver(&ap3216c_driver);return ret;
}/*** @description:            驱动出口函数*/
static void __exit ap3216c_exit(void)
{i2c_del_driver(&ap3216c_driver);
}module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kaneki");

四.IIC驱动实验 - OLED

1.设备树部分

(1).流程图

(2).设备树代码

2.驱动程序

(1).流程图

(2).设备树代码

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/i2c.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include "oledfont.h"#define OLED_CNT    1
#define OLED_NAME   "oled"#define OLED_CMD    0X00            //oled 写命令
#define OLED_DATA   0X40            //oled 写数据#define Max_Column  128             //oled 的x方向的像素个数//IIC从用户控件接收的数据格式
struct display_stru 
{int  x;             //x坐标int  y;             //y坐标char *buf;          //接收数据缓冲区
};/* 设备结构体 */
struct oled_dev
{dev_t devid;                    /* 设备号 */struct cdev cdev;               /* cdev */struct class *class;            /* 类 */struct device *device;          /* 设备 */struct device_node *nd;         /* 设备结点 */int major;                      /* 主设备号 */void *private_data;             /* 私有数据 */
};/* oled 设备 */
struct oled_dev oleddev;/*** @description:            向 OLED设备 写入若干个字节的数据* @param - dev     :       oled 设备* @param - reg     :       寄存器首地址* @param - buf     :       要写入的数据* @param - len     :       要写入的长度* @return          :       成功返回(发送的msg数量),失败返回(负值)*/
static s32 oled_write_regs(struct oled_dev *dev,u8 reg,u8 *buf,int len)
{u8 b[256];struct i2c_msg msg;struct i2c_client *client = (struct i2c_client *)dev->private_data;b[0] = reg;                     //填充寄存器的首地址memcpy(&b[1],buf,len);          //填充数据msg.addr = client->addr;        //oled 设备地址msg.buf = b;          msg.flags = 0;                  //标记为写msg.len = len + 1;               //一个字节的地址+len长度的数据return i2c_transfer(client->adapter,&msg,1);
}/*** @description:            向 OLED设备 写入单个字节的数据* @param - dev     :       oled 设备* @param - reg     :       寄存器首地址* @param - data    :       要写入的单字节数据* @return          :       成功返回(发送的msg数量),失败返回(负值)*/
static s32 oled_write_reg(struct oled_dev *dev,u8 reg,u8 data)
{u8 buf = 0;buf = data;return oled_write_regs(dev,reg,&buf,1);
}/*** @description:            oled 初始化* @param           :       无* @return          :       无*/
void oled_init(void)
{int ret;u8 i = 0;u8 data[] ={ 0xAE, 0x00, 0x10, 0x40, 0xB0, 0x81, 0xFF, 0xA1, 0xA6,0xA8, 0x3F, 0xC8, 0xD3, 0x00, 0xD5, 0x80, 0xD8, 0x05,0xD9, 0xF1, 0xDA, 0x12, 0xDB, 0x30, 0x8D, 0x14, 0xAF };for(i = 0 ; i < sizeof(data) ; i++){ret = oled_write_reg(&oleddev,OLED_CMD,data[i]);}
}/*** @description:            oled 清屏* @param           :       无* @return          :       无 */
void oled_clear(void)
{u8 i,n;for(i = 0 ; i < 8 ; i++){oled_write_reg(&oleddev,OLED_CMD,0XB0+i);               //设置页地址oled_write_reg(&oleddev,OLED_CMD,0x00);                 //设置显示位置-列低地址oled_write_reg(&oleddev,OLED_CMD,0x10);                 //设置显示位置-列高地址}for(n = 0 ; n < 128 ; n++){oled_write_reg(&oleddev,OLED_DATA,0x00);}
}/*** @description:            设置 OLED 某个点* @param - x       :       x坐标* @param - y       :       y坐标* @return          :       无*/
void oled_set_pos(u8 x, u8 y){  oled_write_reg(&oleddev,OLED_CMD, 0xb0 + y);oled_write_reg(&oleddev,OLED_CMD, ((x & 0xf0) >> 4) | 0x10);oled_write_reg(&oleddev,OLED_CMD, x & 0x0f);}/*** @description:            oled 显示单个字符* @param - x           :   x坐标* @param - y           :   y坐标* @param - chr         :   要显示的字符* @return              :   无*/
void oled_showchar(u8 x, u8 y, u8 chr){     u8 c=0, i=0;c = chr - ' ';                    if(x > Max_Column-1){x = 0;y = y + 2;}oled_set_pos(x, y);for(i=0; i<8; i++){oled_write_reg(&oleddev,OLED_DATA, F8X16[c*16+i]);}oled_set_pos(x,y+1);for(i=0; i<8; i++){oled_write_reg(&oleddev,OLED_DATA, F8X16[c*16+i+8]);  }}/*** @description:            oled显示字符串* @param - x       :       x坐标* @param - y       :       y坐标* @param - chr     :       字符串首地址* @return          :       无*/
void oled_showstring(u8 x, u8 y, u8 *chr){unsigned char j=0;while(chr[j] != '\0'){     oled_showchar(x, y, chr[j]);x += 8;if(x > 120){x = 0;y += 2;}j++;}
}/*** @description:            打开设备* @param - inode   :       传递给驱动的inode* @param - filp    :       设备文件,在open里面,将 filp->private_data 指向设备结构体* @return          :       0成功,其他失败*/
static int oled_open(struct inode *inode,struct file *filp)
{/* 1.设置私有数据 */filp->private_data = &oleddev;/* 2.初始化oled */oled_init();oled_clear();return 0;
}/*** @description:            向设备写数据* @param - filp    :       设备文件* @param - buf     :       用于空间的数据缓冲区* @param - cnt     :       要写入的数据大小* @param - offt    :       相对于文件首地址的偏移量* @return          :       成功则返回(写入的字节数),失败则返回(负值)*/
static ssize_t oled_write(struct file *filp,const char __user *buf,size_t cnt,loff_t *offt)
{int ret;struct display_stru display_buf;ret = copy_from_user(&display_buf,buf,cnt);if(0 > ret){printk("copy form user error!\r\n");return -EFAULT;}/* 用于调试,查看用户输入数据 */printk("display_buf.x = %d\r\n",display_buf.x);printk("display_buf.y = %d\r\n",display_buf.y);printk("display_buf.buf = %s\r\n",display_buf.buf);oled_showstring(display_buf.x,display_buf.y,display_buf.buf);return 0;
}/*** @description:        关闭/释放设备* @param - inode   :   传递给驱动的inode* @param - filp    :   要关闭的文件描述符*/
static int oled_release(struct inode *inode,struct file *filp)
{return 0;
}/* 绑定设备操作函数 */
struct file_operations oled_fops = 
{.owner = THIS_MODULE,.open = oled_open,.write = oled_write,.release = oled_release,
};/*** @description:            probe函数,当驱动与设备匹配成功后就会执行此函数* @param - client      :   i2c设备* @param - id          :   i2c 设备ID* @return              :   0成功,其他失败*/
static int oled_probe(struct i2c_client *client,const struct i2c_device_id *id)
{printk("oled driver and device was matched!\r\n");/* 1.构建设备号 */if(oleddev.major){oleddev.devid = MKDEV(oleddev.major,0);register_chrdev_region(oleddev.devid,OLED_CNT,OLED_NAME);}else{alloc_chrdev_region(&oleddev.devid,0,OLED_CNT,OLED_NAME);oleddev.major = MAJOR(oleddev.devid);}/* 2.注册cdev */cdev_init(&oleddev.cdev,&oled_fops);cdev_add(&oleddev.cdev,oleddev.devid,OLED_CNT);/* 3.创建类 */oleddev.class = class_create(THIS_MODULE,OLED_NAME);if(IS_ERR(oleddev.class)){return PTR_ERR(oleddev.class);}/* 4.创建设备 */oleddev.device = device_create(oleddev.class,NULL,oleddev.devid,NULL,OLED_NAME);if(IS_ERR(oleddev.device)){return PTR_ERR(oleddev.device);}oleddev.private_data = client;return 0;
}/*** @description:            i2c设备的 remove函数 ,移除 I2C驱动时,就会执行此函数*/
static int oled_remove(struct i2c_client *client,struct i2c_device_id *id)
{printk("oled_remove]r]n");/* 1.注销字符设备驱动 */cdev_del(&oleddev.cdev);unregister_chrdev_region(oleddev.devid,OLED_CNT);/* 2.销毁类和设备 */class_destroy(oleddev.class);device_destroy(oleddev.class,oleddev.devid);return 0;
}/* 无设备树时的匹配方式 - ID列表 */
static const struct i2c_device_id oled_id[] = 
{{"alientek,oled",0},{},
};/* 使用设备树时的匹配方式 - 匹配列表 */
static const struct of_device_id oled_of_match[] = 
{{.compatible = "alientek,oled"},            //compatible属性必须与设备树结点中的compatible一致{},
};/* i2c驱动结构体 */
static struct i2c_driver oled_driver = 
{.probe = oled_probe,.remove = oled_remove,.driver = {.owner = THIS_MODULE,.name = "oled",/* 用于设备树下的匹配 */.of_match_table = oled_of_match,},/* 用于无设备树时的匹配 */.id_table = oled_id,
};/*** @description:            驱动入口函数* @param           :       无* @return          :       无*/
static int __init oled_module_init(void)
{int ret;ret = i2c_add_driver(&oled_driver);return ret;
}/*** @description:            驱动出口函数* @param           :       无* @return          :       无*/
static void __exit oled_module_exit(void)
{i2c_del_driver(&oled_driver);
}module_init(oled_module_init);
module_exit(oled_module_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kaneki");

3.应用程序

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>//IIC从用户控件接收的数据格式
struct display_stru 
{int  x;             //x坐标int  y;             //y坐标char *buf;          //接收数据缓冲区
};int main(int argc, char  *argv[])
{int fd,ret;char *filename;struct display_stru display_buf;if(2 != argc){printf("Usage : ./%s <dev_path>\n",argv[0]);return -1;}filename = argv[1];fd = open(filename,O_WRONLY);if(0 > fd){perror("open dev error!");return -1;}display_buf.x = 0;display_buf.y = 0;display_buf.buf = "hello world!";ret = write(fd,&display_buf,sizeof(display_buf));close(fd);return 0;
}


http://www.ppmy.cn/embedded/129326.html

相关文章

如何使用gitlab切换分支

第一步&#xff0c;在gitlab上新建一个远程分支。选择New branch即可新建一个&#xff0c;但是注意往往是在当前分支下新建的分支&#xff0c;所以新分支里会有当前分支的内容。 第二步&#xff0c;在本地当前分支在运行这三行命令&#xff0c;即可得到一个空的新分支。 git c…

GB28181协议视频监控平台-核心信令的含义

核心信令&#xff08;Core Signaling&#xff09;在通信系统中指的是用于控制和管理通信过程的信令协议和机制。这些信令通常涉及到在网络中的设备&#xff08;如基站、交换机、路由器等&#xff09;之间传递控制信息&#xff0c;以便实现建立、维护和终止通信会话的功能。 核…

go 包相关知识

在Go语言中&#xff0c;包的引用和搜索路径是由环境变量GOPATH和GO111MODULE共同决定的。 GOPATH环境变量&#xff1a;这个变量定义了默认的工作目录&#xff0c;Go命令行工具将会在这个目录下查找包文件。这个目录通常包含三个子目录&#xff1a;src、bin和pkg。 src目录包含…

C语言[经典题——4×5矩形阵]

输出45的矩形阵 用for循环输出1&#xff5e;20数字摆出45的矩形阵 两个for循环嵌套使用&#xff0c;当外部的for循环输出一位&#xff0c;内部小的for循环就整个输出&#xff0c;直到外部的for循环输出完毕&#xff0c;在搭配if实现分行。 #include <stdio.h> int main()…

openSSL生成WEB SSL证书

1.使用openSSL生成凭证与私钥&#xff1a; openssl genrsa -out privatekey.pem 2048 openssl req -new -key privatekey.pem -out cert_req.pemCountry Name (2 letter code) [AU]:cn State or Province Name (full name) [Some-State]:sh Locality Name (eg, city) []:sh Or…

Artillery:强大的API负载测试工具

在现代软件开发中&#xff0c;API&#xff08;应用程序编程接口&#xff09;已成为连接不同服务和应用的核心组件。随着API的普及&#xff0c;确保其性能和稳定性变得至关重要。负载测试是评估API在高并发情况下表现的重要手段之一。在众多负载测试工具中&#xff0c;Artillery…

PPT分享:埃森哲-如何利用大数据进行数据挖掘与分析

PPT下载链接见文末~ 在当今信息爆炸的时代&#xff0c;大数据已成为企业决策、科学研究及社会发展的重要驱动力。数据挖掘与分析作为大数据应用的核心环节&#xff0c;能够帮助我们从海量数据中提取有价值的信息和知识。 本文将引导您了解如何利用大数据进行数据挖掘与分析&a…

通信基站类型、频段与网络标准

【1】通信基站类型 移动通信基站根据覆盖面积和功率大小主要可以分为以下几种类型&#xff1a; 宏基站&#xff08;宏站&#xff09;&#xff1a;这是最常见的基站类型&#xff0c;具有较大的发射功率和较广的覆盖范围&#xff0c;通常覆盖半径从1到25公里不等。宏基站主要用于…