1、使用linux内核里面的i2c子系统来驱动i2c oled 屏幕
代码如下:
#include "linux/i2c.h"
#include <linux/module.h>
#include <linux/poll.h>#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include "font.h"
typedef struct i2cmsg
{int row;int col;int buf_len;void *buf;
} i2cmsg_type;
struct i2c_client *g_client;
static int major = 0;
struct class *i2c_cls;
#define OLED_I2C_ADDRESS 0x3c
#define OLED_CMD 0x00
#define OLED_DATA 0x40
static void Oled_Clear(struct i2c_client *client);
static int i2c_transfer_char(struct i2c_client *client, unsigned char data, unsigned char oled_ctrl);
static void Oled_Show_Str(char row, char col, char *str);
static long i2c_oled_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{return 0;
}
static ssize_t i2c_oled_write(struct file *file, const char __user *buf, size_t size, loff_t *filp)
{int ret;void *kernel_buf;// 分配内存i2cmsg_type *msg = kzalloc(sizeof(i2cmsg_type), GFP_KERNEL);if (!msg) {printk(KERN_ERR "Failed to allocate memory for i2cmsg_type\n");return -ENOMEM;}// 从用户空间复制 i2cmsg_type 结构体ret