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

目录

一.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).驱动程序代码

#ifndef __OLEDFONT_H
#define __OLEDFONT_H


//常用ASCII表
//偏移量32
//ASCII字符集
//偏移量32
//大小:12*6

/************************************6*8的点阵************************************/
const unsigned char F6x8[][6] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};
/****************************************8*16的点阵************************************/
const unsigned char F8X16[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};


#endif
#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)
{
    printk("oled_remove]r]n");

    /* 1.注销字符设备驱动 */
    cdev_del(&oleddev.cdev);
    unregister_chrdev_region(oleddev.devid,OLED_CNT);

    /* 2.销毁类和设备 */
    device_destroy(oleddev.class,oleddev.devid);
    class_destroy(oleddev.class);
    

    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值