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

news2024/10/27 12:01:32

目录

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

1.i2c_adapter 结构体

2.i2c_algorithm 结构体

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

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

5.删除 I2C 适配器 - i2c_del_adapter

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

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

2.i2c_driver 结构体

3.向内核注册 i2c_driver - i2c_register_driver

4.注销 I2C 设备驱动 - i2c_del_driver

5.I2C 总线数据结构 - i2c_bus_type

6.传输 I2C 数据 - i2c_transfer

7.i2c_msg 结构体

8.I2C 写数据

9.I2C 读数据

三.I2C 驱动实验 - AP3216C

1.设备树部分

(1).流程图

(2).设备树代码

2.驱动程序

(1).流程图

(2).驱动代码

四.IIC驱动实验 - OLED

1.设备树部分

(1).流程图

(2).设备树代码

2.驱动程序

(1).流程图

​编辑

(2).设备树代码

3.应用程序


一.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;        
    }
    else
        dev->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 复位最少要 10ms
    ap3216c_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.coloradmin.cn/o/2224645.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

影刀RPA实战:验证码识别功能指令

1.影刀官方验证码识别 1.1 介绍 功能&#xff1a;基于AI引擎提供的验证码识别服务&#xff0c;使用影刀内置的AI引擎来识别验证码&#xff0c;使用第三图鉴账号来识别验证码&#xff0c;选填写用户名及密码&#xff0c; 可识别的验证码类型&#xff1a; 纯数字&#xff1a;适…

python学习-第一个小游戏(vscode环境)

学习小甲鱼的视频&#xff0c;写了一个小游戏&#xff0c;vscode环境 运行结果 源码地址&#xff1a; python小游戏-猜数字源码

Visual Studio2022 Profile 工具使用

本篇研究下Visual Studio自带的性能分析工具&#xff0c;针对C代码&#xff0c;基于Visual Studio2022 文章目录 CPU使用率检测并发可视化工具使用率视图线程视图内核视图并发可视化工具SDK 参考资料 CPU使用率 对于CPU密集型程序&#xff0c;我们可以通过分析程序的CPU使用率…

系统架构设计师教程 第2章 2.5 计算机网络 笔记

2.5计算机网络 ★☆☆☆☆ 2.5.1网络的基本概念 1.计算机网络的发展 计算机网络发展&#xff0c;其大致经历了诞生、形成、互联互通和高速发展等4个阶段。 2.计算机网络的功能 1)数据通信 数据通信是依照一定的通信协议&#xff0c;利用数据传 输技术在两个通信结点之间传…

【Spring Boot】元注解

元注解 1.元注解1.1 Target1.2 Retention1.3 Inherited1.4 Documented1.5 interface 2.自定义注解2.1 创建自定义注解类2.2 实现业务逻辑2.3 使用自定义注解 1.元注解 元注解就是定义注解的注解&#xff0c;是 Java 提供的用于定义注解的基本注解。 注解 说明 Retention是注解…

linux中级(NFS服务器)

NFS&#xff1a;用于在NNIX/Linux主机之间进行文件共享的协议 流程&#xff1a;首先服务端开启RPC服务&#xff0c;并开启111端口&#xff0c;服务器端启动NFS服务&#xff0c;并向RPC注册端口信息&#xff0c;客户端启动RPC&#xff0c;向服务器RPC服务请求NFS端口&#xff0…

Matlab数字信号处理——基于改进小波变换的图像去噪方法(7种去噪算法)

1.基于小波变换的阈值收缩法去噪 该方法利用小波变换分离出信号中的噪声成分&#xff0c;并通过设置合适的阈值对小波系数进行收缩&#xff0c;保留主要信息的同时&#xff0c;去除噪声。 %基于小波变换的阈值收缩法去噪算法 clear clc Iimread(nana.png); X im2double(I); …

深入理解C语言中的静态库与动态库 —— 原理与实践

引言 在 C 语言编程中&#xff0c;库是预编译的代码集合&#xff0c;用于实现特定功能&#xff0c;以供其他程序使用。库可以分为静态库和动态库两种主要类型。静态库在编译阶段被链接到目标程序中&#xff0c;而动态库则是在运行时被加载。本文旨在深入探讨这两种库的工作原理…

渗透测试-百日筑基—SQL注入篇时间注入绕过HTTP数据编码绕过—下

day8-渗透测试sql注入篇&时间注入&绕过&HTTP数据编码绕过 一、时间注入 SQL注入时间注入&#xff08;也称为延时注入&#xff09;是SQL注入攻击的一种特殊形式&#xff0c;它属于盲注&#xff08;Blind SQL Injection&#xff09;的一种。在盲注中&#xff0c;攻击…

Android 在github网站下载项目:各种很慢怎么办?比如gradle下载慢;访问github慢;依赖下载慢

目录 访问github慢gradle下载慢依赖下载慢 前言 大家好&#xff0c;我是前期后期&#xff0c;在网上冲浪的一名程序员。 为什么要看这篇文章呢&#xff1f;问题是什么&#xff1f; 我们在Github上面看到一些好的项目的时候&#xff0c;想下载下来研究学习一下。但经常遇到各…

外面卖几百的Ai数字人软件 说123456生成视频 去授权版本

下载&#xff1a;https://pan.quark.cn/s/27a0cff98eae 可以无限制使用。

网络拓扑视角下的IP地址管理优化

前言 对IP地址进行有效管理&#xff0c;好处是能 提升网络运行效率&#xff0c;还可以保障网络安全和稳定。网络拓扑结构本身作为网络描述中各节点联系的主要角色&#xff0c;为IP地址管理提供了一些优化策略。 网络拓扑和IP地址管理的关系 网络拓扑结构描述了网络中各节点&a…

【Spring MVC】响应结果和设置

​ 我的主页&#xff1a;2的n次方_ 1. 返回静态页面 先创建一个 html 页面 ​ 如果还按照之前的方式进行返回的话&#xff0c;返回的并不是一个 html 页面 RequestMapping("/response") RestController public class ResponseController {RequestMapping(&quo…

线程同步(互斥锁条件变量)

线程同步 互斥锁(互斥量)条件变量生产/消费者模型 一、互斥锁 C11提供了四种互斥锁&#xff1a; mutex&#xff1a;互斥锁。timed_mutex&#xff1a;带超时机制的互斥锁。recursive_mutex&#xff1a;递归互斥锁。recursive_timed_mutex&#xff1a;带超时机制的递归互斥锁…

命名管道和共享内存

命名管道 管道应用的一个限制就是只能在具有共同祖先的进程间通信&#xff0c;如果我们想在不相关的进程之间交换数据&#xff0c;可以使用FIFO文件来做这项工作&#xff0c;它被称为命名管道&#xff0c;命名管道是一种特殊类型的文件 创建命名管道 int main(int argc, cha…

落地台灯什么牌子好?口碑最好的落地灯品牌

落地台灯什么牌子好&#xff1f;随着落地台灯被越来越多的人认可&#xff0c;如今市场中护眼大路灯品牌类型五花八门&#xff0c;质量存在较大差距。很多网红、明星代言等产品&#xff0c;入行时间短&#xff0c;关注市场营销&#xff0c;而忽视产品核心技术的提升&#xff0c;…

Codeforces Round 982 div2 个人题解(A~D2)

Codeforces Round 982 div2 个人题解(A~D2) Dashboard - Codeforces Round 982 (Div. 2) - Codeforces 火车头 #define _CRT_SECURE_NO_WARNINGS 1#include <algorithm> #include <array> #include <bitset> #include <cmath> #include <cstdio&…

软件系统建设方案书(word参考模板)

1 引言 1.1 编写目的 1.2 项目概述 1.3 名词解释 2 项目背景 3 业务分析 3.1 业务需求 3.2 业务需求分析与解决思路 3.3 数据需求分析【可选】 4 项目建设总体规划【可选】 4.1 系统定位【可选】 4.2 系统建设规划 5 建设目标 5.1 总体目标 5.2 分阶段目标【可选】 5.2.1 业务目…

简记 Vue3(一)—— setup、ref、reactive、toRefs、toRef

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…

API网关的作用--为什么微服务需要一个API网关?

微服务网关核心作用就是协议转换、安全隔离和流量控制 微服务架构中&#xff0c;API网关作为系统的入口点&#xff0c;可以统一处理所有客户端请求。 1&#xff09;协议转换&#xff1a;它能够支持多种通信协议&#xff08;如HTTP、gRPC等&#xff09;之间的相互转换&#xff…