LubanCat driver初始源码

This commit is contained in:
Yu Xifeng 2024-11-19 11:37:38 +08:00
commit 5144b762b4
96 changed files with 15685 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.*.cmd
*.mod*
*.o
*odule*.*
bin/

View File

@ -0,0 +1,128 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#define DEV_NAME "EmbedCharDev"
#define DEV_CNT (2)
#define BUFF_SIZE 128
//定义字符设备的设备号
static dev_t devno;
//定义字符设备结构体chr_dev
static struct cdev chr_dev;
//数据缓冲区
static char vbuf1[BUFF_SIZE];
static char vbuf2[BUFF_SIZE];
static int chr_dev_open(struct inode *inode, struct file *filp);
static int chr_dev_release(struct inode *inode, struct file *filp);
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos);
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos);
static struct file_operations chr_dev_fops =
{
.owner = THIS_MODULE,
.open = chr_dev_open,
.release = chr_dev_release,
.write = chr_dev_write,
.read = chr_dev_read,
};
static int chr_dev_open(struct inode *inode, struct file *filp)
{
printk("\nopen\n ");
switch(MINOR(inode->i_rdev))
{
case 0 :
{
filp->private_data = vbuf1;
break;
}
case 1 :
{
filp->private_data = vbuf2;
break;
}
}
return 0;
}
static int chr_dev_release(struct inode *inode, struct file *filp)
{
printk("\nrelease\n");
return 0;
}
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
char *vbuf = filp->private_data;
int tmp = count ;
if(p > BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_from_user(vbuf, buf, tmp);
*ppos += tmp;
return tmp;
}
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
int tmp = count ;
char *vbuf = filp->private_data;
if(p >= BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_to_user(buf, vbuf+p, tmp);
*ppos +=tmp;
return tmp;
}
static int __init chrdev_init(void)
{
int ret = 0;
printk("chrdev init\n");
//第一步
//采用动态分配的方式,获取设备编号,次设备号为0,
//设备名称为EmbedCharDev,可通过命令cat /proc/devices查看
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk("fail to alloc devno\n");
goto alloc_err;
}
//第二步
//关联字符设备结构体cdev与文件操作结构体file_operations
cdev_init(&chr_dev, &chr_dev_fops);
//第三步
//添加设备至cdev_map散列表中
ret = cdev_add(&chr_dev, devno, DEV_CNT);
if(ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
return 0;
add_err:
//添加设备失败时,需要注销设备号
unregister_chrdev_region(devno, DEV_CNT);
alloc_err:
return ret;
}
module_init(chrdev_init);
static void __exit chrdev_exit(void)
{
printk("chrdev exit\n");
unregister_chrdev_region(devno, DEV_CNT);
cdev_del(&chr_dev);
}
module_exit(chrdev_exit);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,14 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := 1_SupportMoreDev.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,130 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#define DEV_NAME "EmbedCharDev"
#define DEV_CNT (2)
#define BUFF_SIZE 128
//定义字符设备的设备号
static dev_t devno;
//虚拟字符设备
struct chr_dev{
struct cdev dev;
char vbuf[BUFF_SIZE];
};
//字符设备1
static struct chr_dev vcdev1;
//字符设备2
static struct chr_dev vcdev2;
static int chr_dev_open(struct inode *inode, struct file *filp);
static int chr_dev_release(struct inode *inode, struct file *filp);
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos);
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos);
static struct file_operations chr_dev_fops = {
.owner = THIS_MODULE,
.open = chr_dev_open,
.release = chr_dev_release,
.write = chr_dev_write,
.read = chr_dev_read,
};
static int chr_dev_open(struct inode *inode, struct file *filp)
{
printk("open\n");
filp->private_data = container_of(inode->i_cdev, struct chr_dev, dev);
return 0;
}
static int chr_dev_release(struct inode *inode, struct file *filp)
{
printk("\nrelease\n");
return 0;
}
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
//获取文件的私有数据
struct chr_dev *dev = filp->private_data;
char *vbuf = dev->vbuf;
int tmp = count ;
if(p > BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_from_user(vbuf, buf, tmp);
*ppos += tmp;
return tmp;
}
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
int tmp = count ;
//获取文件的私有数据
struct chr_dev *dev = filp->private_data;
char *vbuf = dev->vbuf;
if(p >= BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_to_user(buf, vbuf+p, tmp);
*ppos +=tmp;
return tmp;
}
static int __init chrdev_init(void)
{
int ret;
printk("chrdev init\n");
ret = alloc_chrdev_region(&devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0)
goto alloc_err;
//关联第一个设备:vdev1
cdev_init(&vcdev1.dev, &chr_dev_fops);
ret = cdev_add(&vcdev1.dev, devno+0, 1);
if(ret < 0){
printk("fail to add vcdev1 ");
goto add_err1;
}
//关联第二个设备:vdev2
cdev_init(&vcdev2.dev, &chr_dev_fops);
ret = cdev_add(&vcdev2.dev, devno+1, 1);
if(ret < 0){
printk("fail to add vcdev2 ");
goto add_err2;
}
return 0;
add_err2:
cdev_del(&(vcdev1.dev));
add_err1:
unregister_chrdev_region(devno, DEV_CNT);
alloc_err:
return ret;
}
module_init(chrdev_init);
static void __exit chrdev_exit(void)
{
printk("chrdev exit\n");
unregister_chrdev_region(devno, DEV_CNT);
cdev_del(&(vcdev1.dev));
cdev_del(&(vcdev2.dev));
}
module_exit(chrdev_exit);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,14 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := 2_SupportMoreDev.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,17 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := chrdev.o
out = chrdev_test
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) main.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm $(out)

View File

@ -0,0 +1,112 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#define DEV_NAME "EmbedCharDev"
#define DEV_CNT (1)
#define BUFF_SIZE 128
//定义字符设备的设备号
static dev_t devno;
//定义字符设备结构体chr_dev
static struct cdev chr_dev;
//数据缓冲区
static char vbuf[BUFF_SIZE];
static int chr_dev_open(struct inode *inode, struct file *filp);
static int chr_dev_release(struct inode *inode, struct file *filp);
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos);
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos);
static struct file_operations chr_dev_fops =
{
.owner = THIS_MODULE,
.open = chr_dev_open,
.release = chr_dev_release,
.write = chr_dev_write,
.read = chr_dev_read,
};
static int chr_dev_open(struct inode *inode, struct file *filp)
{
printk("\nopen\n");
return 0;
}
static int chr_dev_release(struct inode *inode, struct file *filp)
{
printk("\nrelease\n");
return 0;
}
static ssize_t chr_dev_write(struct file *filp, const char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
int tmp = count ;
if(p > BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_from_user(vbuf, buf, tmp);
*ppos += tmp;
return tmp;
}
static ssize_t chr_dev_read(struct file *filp, char __user * buf, size_t count, loff_t *ppos)
{
unsigned long p = *ppos;
int ret;
int tmp = count ;
if(p >= BUFF_SIZE)
return 0;
if(tmp > BUFF_SIZE - p)
tmp = BUFF_SIZE - p;
ret = copy_to_user(buf, vbuf+p, tmp);
*ppos +=tmp;
return tmp;
}
static int __init chrdev_init(void)
{
int ret = 0;
printk("chrdev init\n");
//第一步
//采用动态分配的方式,获取设备编号,次设备号为0,
//设备名称为EmbedCharDev,可通过命令cat /proc/devices查看
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk("fail to alloc devno\n");
goto alloc_err;
}
//第二步
//关联字符设备结构体cdev与文件操作结构体file_operations
cdev_init(&chr_dev, &chr_dev_fops);
//第三步
//添加设备至cdev_map散列表中
ret = cdev_add(&chr_dev, devno, DEV_CNT);
if(ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
return 0;
add_err:
//添加设备失败时,需要注销设备号
unregister_chrdev_region(devno, DEV_CNT);
alloc_err:
return ret;
}
module_init(chrdev_init);
static void __exit chrdev_exit(void)
{
printk("chrdev exit\n");
unregister_chrdev_region(devno, DEV_CNT);
cdev_del(&chr_dev);
}
module_exit(chrdev_exit);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
char *wbuf = "Hello World\n";
char rbuf[128];
int main(void)
{
printf("EmbedCharDev test\n");
//打开文件
int fd = open("/dev/chrdev", O_RDWR);
//写入数据
write(fd, wbuf, strlen(wbuf));
//写入完毕,关闭文件
close(fd);
//打开文件
fd = open("/dev/chrdev", O_RDWR);
//读取文件内容
read(fd, rbuf, 128);
//打印读取的内容
printf("The content : %s \n", rbuf);
//读取完毕,关闭文件
close(fd);
return 0;
}

18
I2c_MPU6050/Makefile Normal file
View File

@ -0,0 +1,18 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := i2c_mpu6050.o
out = 6050_test_app
all:
$(MAKE) EXTRA_CFLAGS=-fno-pic -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm $(out)

313
I2c_MPU6050/i2c_mpu6050.c Normal file
View File

@ -0,0 +1,313 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/i2c.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include "i2c_mpu6050.h"
/*------------------字符设备内容----------------------*/
#define DEV_NAME "I2C1_mpu6050"
#define DEV_CNT (1)
/*定义 led 资源结构体,保存获取得到的节点信息以及转换后的虚拟寄存器地址*/
static dev_t mpu6050_devno; //定义字符设备的设备号
static struct cdev mpu6050_chr_dev; //定义字符设备结构体chr_dev
struct class *class_mpu6050; //保存创建的类
struct device *device_mpu6050; // 保存创建的设备
struct device_node *mpu6050_device_node; //rgb_led的设备树节点结构体
/*------------------IIC设备内容----------------------*/
struct i2c_client *mpu6050_client = NULL; //保存mpu6050设备对应的i2c_client结构体,匹配成功后由.prob函数带回。
/*通过i2c 向mpu6050写入数据
*mpu6050_client:mpu6050的i2c_client结构体。
*address, 数据要写入的地址,
*data, 要写入的数据
*返回值,错误,-1。成功,0
*/
static int i2c_write_mpu6050(struct i2c_client *mpu6050_client, u8 address, u8 data)
{
int error = 0;
u8 write_data[2];
struct i2c_msg send_msg; //要发送的数据结构体
/*设置要发送的数据*/
write_data[0] = address;
write_data[1] = data;
/*发送 iic要写入的地址 reg*/
send_msg.addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
send_msg.flags = 0; //标记为发送数据
send_msg.buf = write_data; //写入的首地址
send_msg.len = 2; //reg长度
/*执行发送*/
error = i2c_transfer(mpu6050_client->adapter, &send_msg, 1);
if (error != 1)
{
printk(KERN_DEBUG "\n i2c_transfer error \n");
return -1;
}
return 0;
}
/*通过i2c 向mpu6050写入数据
*mpu6050_client:mpu6050的i2c_client结构体。
*address, 要读取的地址,
*data,保存读取得到的数据
*length,读长度
*返回值,错误,-1。成功,0
*/
static int i2c_read_mpu6050(struct i2c_client *mpu6050_client, u8 address, void *data, u32 length)
{
int error = 0;
u8 address_data = address;
struct i2c_msg mpu6050_msg[2];
/*设置读取位置msg*/
mpu6050_msg[0].addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
mpu6050_msg[0].flags = 0; //标记为发送数据
mpu6050_msg[0].buf = &address_data; //写入的首地址
mpu6050_msg[0].len = 1; //写入长度
/*设置读取位置msg*/
mpu6050_msg[1].addr = mpu6050_client->addr; //mpu6050在 iic 总线上的地址
mpu6050_msg[1].flags = I2C_M_RD; //标记为读取数据
mpu6050_msg[1].buf = data; //读取得到的数据保存位置
mpu6050_msg[1].len = length; //读取长度
error = i2c_transfer(mpu6050_client->adapter, mpu6050_msg, 2);
if (error != 2)
{
printk(KERN_DEBUG "\n i2c_read_mpu6050 error \n");
return -1;
}
return 0;
}
/*初始化i2c
*返回值,成功,返回0。失败,返回 -1
*/
static int mpu6050_init(void)
{
int error = 0;
/*配置mpu6050*/
error += i2c_write_mpu6050(mpu6050_client, PWR_MGMT_1, 0X00);
error += i2c_write_mpu6050(mpu6050_client, SMPLRT_DIV, 0X07);
error += i2c_write_mpu6050(mpu6050_client, CONFIG, 0X06);
error += i2c_write_mpu6050(mpu6050_client, ACCEL_CONFIG, 0X01);
if (error < 0)
{
/*初始化错误*/
printk(KERN_DEBUG "\n mpu6050_init error \n");
return -1;
}
return 0;
}
/*字符设备操作函数集,open函数实现*/
static int mpu6050_open(struct inode *inode, struct file *filp)
{
// printk("\n mpu6050_open \n");
/*向 mpu6050 发送配置数据,让mpu6050处于正常工作状态*/
mpu6050_init();
return 0;
}
/*字符设备操作函数集,.read函数实现*/
static ssize_t mpu6050_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
char data_H;
char data_L;
int error;
short mpu6050_result[6]; //保存mpu6050转换得到的原始数据
// printk("\n mpu6050_read \n");
i2c_read_mpu6050(mpu6050_client, ACCEL_XOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_XOUT_L, &data_L, 1);
mpu6050_result[0] = data_H << 8;
mpu6050_result[0] += data_L;
i2c_read_mpu6050(mpu6050_client, ACCEL_YOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_YOUT_L, &data_L, 1);
mpu6050_result[1] = data_H << 8;
mpu6050_result[1] += data_L;
i2c_read_mpu6050(mpu6050_client, ACCEL_ZOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, ACCEL_ZOUT_L, &data_L, 1);
mpu6050_result[2] = data_H << 8;
mpu6050_result[2] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_XOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_XOUT_L, &data_L, 1);
mpu6050_result[3] = data_H << 8;
mpu6050_result[3] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_YOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_YOUT_L, &data_L, 1);
mpu6050_result[4] = data_H << 8;
mpu6050_result[4] += data_L;
i2c_read_mpu6050(mpu6050_client, GYRO_ZOUT_H, &data_H, 1);
i2c_read_mpu6050(mpu6050_client, GYRO_ZOUT_L, &data_L, 1);
mpu6050_result[5] = data_H << 8;
mpu6050_result[5] += data_L;
// printk("AX=%d, AY=%d, AZ=%d \n",(int)mpu6050_result[0],(int)mpu6050_result[1],(int)mpu6050_result[2]);
// printk("GX=%d, GY=%d, GZ=%d \n \n",(int)mpu6050_result[3],(int)mpu6050_result[4],(int)mpu6050_result[5]);
/*将读取得到的数据拷贝到用户空间*/
error = copy_to_user(buf, mpu6050_result, cnt);
if(error != 0)
{
printk("copy_to_user error!");
return -1;
}
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int mpu6050_release(struct inode *inode, struct file *filp)
{
// printk("\n mpu6050_release \n");
/*向mpu6050发送命令,使mpu6050进入关机状态*/
return 0;
}
/*字符设备操作函数集*/
static struct file_operations mpu6050_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = mpu6050_open,
.read = mpu6050_read,
.release = mpu6050_release,
};
/*----------------平台驱动函数集-----------------*/
static int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int ret = -1; //保存错误状态码
printk(KERN_EMERG "\t match successed \n");
/*---------------------注册 字符设备部分-----------------*/
//采用动态分配的方式,获取设备编号,次设备号为0,
//设备名称为rgb-leds,可通过命令cat /proc/devices查看
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&mpu6050_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
printk("fail to alloc mpu6050_devno\n");
goto alloc_err;
}
//关联字符设备结构体cdev与文件操作结构体file_operations
mpu6050_chr_dev.owner = THIS_MODULE;
cdev_init(&mpu6050_chr_dev, &mpu6050_chr_dev_fops);
// 添加设备至cdev_map散列表中
ret = cdev_add(&mpu6050_chr_dev, mpu6050_devno, DEV_CNT);
if (ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
/*创建类 */
class_mpu6050 = class_create(THIS_MODULE, DEV_NAME);
/*创建设备 DEV_NAME 指定设备名,*/
device_mpu6050 = device_create(class_mpu6050, NULL, mpu6050_devno, NULL, DEV_NAME);
mpu6050_client = client;
return 0;
add_err:
// 添加设备失败时,需要注销设备号
unregister_chrdev_region(mpu6050_devno, DEV_CNT);
printk("\n error! \n");
alloc_err:
return -1;
}
static int mpu6050_remove(struct i2c_client *client)
{
/*删除设备*/
device_destroy(class_mpu6050, mpu6050_devno); //清除设备
class_destroy(class_mpu6050); //清除类
cdev_del(&mpu6050_chr_dev); //清除设备号
unregister_chrdev_region(mpu6050_devno, DEV_CNT); //取消注册字符设备
return 0;
}
/*定义ID 匹配表*/
static const struct i2c_device_id gtp_device_id[] = {
{"fire,i2c_mpu6050", 0},
{}};
/*定义设备树匹配表*/
static const struct of_device_id mpu6050_of_match_table[] = {
{.compatible = "fire,i2c_mpu6050"},
{/* sentinel */}};
/*定义i2c总线设备结构体*/
struct i2c_driver mpu6050_driver = {
.probe = mpu6050_probe,
.remove = mpu6050_remove,
.id_table = gtp_device_id,
.driver = {
.name = "fire,i2c_mpu6050",
.owner = THIS_MODULE,
.of_match_table = mpu6050_of_match_table,
},
};
/*
*驱动初始化函数
*/
static int __init mpu6050_driver_init(void)
{
int ret;
pr_info("mpu6050_driver_init\n");
ret = i2c_add_driver(&mpu6050_driver);
return ret;
}
/*
*驱动注销函数
*/
static void __exit mpu6050_driver_exit(void)
{
pr_info("mpu6050_driver_exit\n");
i2c_del_driver(&mpu6050_driver);
}
module_init(mpu6050_driver_init);
module_exit(mpu6050_driver_exit);
MODULE_LICENSE("GPL");

35
I2c_MPU6050/i2c_mpu6050.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef I2C_MPU6050_H
#define I2C_MPU6050_H
//宏定义
#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B
#define WHO_AM_I 0x75
#define SlaveAddress 0xD0
#define Address 0x68 //MPU6050地址
#define I2C_RETRIES 0x0701
#define I2C_TIMEOUT 0x0702
#define I2C_SLAVE 0x0703 //IIC从器件的地址设置
#define I2C_BUS_MODE 0x0780
#endif

41
I2c_MPU6050/test_app.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
short resive_data[6]; //保存收到的 mpu6050转换结果数据,依次为 AX(x轴角度), AY, AZ 。GX(x轴加速度), GY ,GZ
/*打开文件*/
int fd = open("/dev/I2C1_mpu6050", O_RDWR);
if(fd < 0)
{
printf("open file : %s failed !\n", argv[0]);
return -1;
}
/*读取数据*/
int error = read(fd,resive_data,12);
if(error < 0)
{
printf("write file error! \n");
close(fd);
/*判断是否关闭成功*/
}
/*打印数据*/
printf("AX=%d, AY=%d, AZ=%d ",(int)resive_data[0],(int)resive_data[1],(int)resive_data[2]);
printf(" GX=%d, GY=%d, GZ=%d \n \n",(int)resive_data[3],(int)resive_data[4],(int)resive_data[5]);
/*关闭文件*/
error = close(fd);
if(error < 0)
{
printf("close file error! \n");
}
return 0;
}

24
README.md Normal file
View File

@ -0,0 +1,24 @@
# LubanCat_RK系列驱动教程配套例程
## 使用
把该例程linux_driver放到内核源码的同级目录下:
.
├── kernel
└── linux_driver
linux_driver中内核模块makefile的KERNEL_DIR都是指向kernel/build。
## 环境
PC系统:ubuntu18.04
板卡系统:ubuntu20.04
## 配套教程
**《 嵌入式Linux驱动开发实战指南——基于LubanCat_RK系列板卡 》**

15
SPI_OLED/Makefile Normal file
View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := spi_oled.o
all:
$(MAKE) EXTRA_CFLAGS=-fno-pic -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2022 - All Rights Reserved by
* EmbedFire LubanCat
*/
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/clock/rk3568-cru.h>
#include <dt-bindings/interrupt-controller/irq.h>
&spi3{
status = "okay";
pinctrl-names = "default", "high_speed";
pinctrl-0 = <&spi3m1_cs0 &spi3m1_pins>;
pinctrl-1 = <&spi3m1_cs0 &spi3m1_pins_hs>;
cs-gpios = <&gpio4 RK_PC6 GPIO_ACTIVE_LOW>;
spi_oled@0 {
compatible = "fire,spi_oled";
reg = <0>; //chip select 0:cs0 1:cs1
spi-max-frequency = <24000000>; //spi output clock
dc_control_pin = <&gpio3 RK_PA7 GPIO_ACTIVE_HIGH>;
pinctrl-names = "default";
pinctrl-0 = <&spi_oled_pin>;
};
};
&pinctrl {
spi_oled {
spi_oled_pin: spi_oled_pin {
rockchip,pins = <3 RK_PA7 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};

416
SPI_OLED/spi_oled.c Normal file
View File

@ -0,0 +1,416 @@
/*
* Copyright (C) 2022 - All Rights Reserved by
* EmbedFire LubanCat
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/uaccess.h>
#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>
#include "spi_oled.h"
/*------------------字符设备内容----------------------*/
#define DEV_NAME "spi_oled"
#define DEV_CNT (1)
static dev_t oled_devno; //定义字符设备的设备号
static struct cdev oled_chr_dev; //定义字符设备结构体chr_dev
struct class *class_oled; //保存创建的类
struct device *device_oled; // 保存创建的设备
/*------------------IIC设备内容----------------------*/
struct spi_device *oled_spi_device; //保存oled设备对应的spi_device结构体,匹配成功后由.prob函数带回。
struct device_node *oled_device_node; //spi_oled的设备树节点结构体
int oled_control_pin_number; // 保存oled D/C控制引脚的引脚号(从对应设备树节点中获取)
u8 oled_init_data[] = {
0xae, 0xae, 0x00, 0x10, 0x40,
0x81, 0xcf, 0xa1, 0xc8, 0xa6,
0xa8, 0x3f, 0xd3, 0x00, 0xd5,
0x80, 0xd9, 0xf1, 0xda, 0x12,
0xdb, 0x40, 0x20, 0x02, 0x8d,
0x14, 0xa4, 0xa6, 0xaf};
/*
*函数功能:向oled发送一个命令
*spi_device oled设备驱动对应的spi_device结构体。
*command 要发送的数据。
*返回值:成功,返回0 失败返回负数。
*/
static int oled_send_command(struct spi_device *spi_device, u8 command)
{
int error = 0;
u8 tx_data = command;
struct spi_message message; //定义发送的消息
struct spi_transfer *transfer; //定义传输结构体
/*设置 D/C引脚为低电平*/
gpio_direction_output(oled_control_pin_number, 0);
/*申请空间*/
transfer = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
/*填充message和transfer结构体*/
transfer->tx_buf = &tx_data;
transfer->len = 1;
spi_message_init(&message);
spi_message_add_tail(transfer, &message);
error = spi_sync(spi_device, &message);
kfree(transfer);
if (error != 0)
{
printk("spi_sync error! \n");
return -1;
}
gpio_direction_output(oled_control_pin_number, 1);
return 0;
}
/*
*函数功能:向oled发送一组命令
*spi_device oled设备驱动对应的spi_device结构体。
*commands 要发送的数据。
*返回值:成功,返回0 失败返回负数。
*/
static int oled_send_commands(struct spi_device *spi_device, u8 *commands, u16 lenght)
{
int error = 0;
int index ;
for(index = 0; index < lenght; index++)
{
error = oled_send_command(spi_device, *(commands++));
if(error < 0)
return -1;
}
return 0;
}
/*
*向 oled 发送一个字节
*spi_device,指定oled 设备驱动的spi 结构体
*data, 要发送数据
*/
static int oled_send_one_u8(struct spi_device *spi_device, u8 data)
{
int error = 0;
u8 tx_data = data;
struct spi_message message; //定义发送的消息
struct spi_transfer *transfer; //定义传输结构体
/*设置 D/C引脚为高电平*/
gpio_direction_output(oled_control_pin_number, 1);
/*申请空间*/
transfer = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
/*填充message和transfer结构体*/
transfer->tx_buf = &tx_data;
transfer->len = 1;
spi_message_init(&message);
spi_message_add_tail(transfer, &message);
error = spi_sync(spi_device, &message);
kfree(transfer);
if (error != 0)
{
printk("spi_sync error! \n");
return -1;
}
return 0;
}
/*
*向 oled 发送数据
*spi_device,指定oled 设备驱动的spi 结构体
*data, 要发送数据的地址
*lenght,发送的数据长度
*/
static int oled_send_data(struct spi_device *spi_device, u8 *data, u16 lenght)
{
int error = 0;
int index ;
for(index = 0; index < lenght; index++)
{
error = oled_send_one_u8(spi_device, *(data++));
if(error < 0)
return -1;
}
return 0;
}
/*
* 填充整个OLED 显示屏
* bmp_dat, 填充的值
*/
void oled_fill(unsigned char bmp_dat)
{
u8 y, x;
for (y = 0; y < 8; y++)
{
oled_send_command(oled_spi_device, 0xb0 + y);
oled_send_command(oled_spi_device, 0x01);
oled_send_command(oled_spi_device, 0x10);
for (x = 0; x < 128; x++)
{
oled_send_one_u8(oled_spi_device, bmp_dat);
}
}
}
/*
*向 oled 发送要显示的数据,x, y 指定显示的起始位置,支持自动换行
*spi_device,指定oled 设备驱动的spi 结构体
*display_buffer, 数据地址
*x, y,起始坐标。
*length, 发送长度
*/
static int oled_display_buffer(u8 *display_buffer, u8 x, u8 y, u16 length)
{
u16 index = 0;
int error = 0;
do
{
/*设置写入的起始坐标*/
error += oled_send_command(oled_spi_device, 0xb0 + y);
error += oled_send_command(oled_spi_device, ((x & 0xf0) >> 4) | 0x10);
error += oled_send_command(oled_spi_device, (x & 0x0f) | 0x01);
if (length > (X_WIDTH - x))
{
error += oled_send_data(oled_spi_device, display_buffer + index, X_WIDTH - x);
length -= (X_WIDTH - x);
index += (X_WIDTH - x);
x = 0;
y++;
}
else
{
error += oled_send_data(oled_spi_device, display_buffer + index, length);
index += length;
// x += length;
length = 0;
}
} while (length > 0);
if (error != 0)
{
/*发送错误*/
printk("oled_display_buffer error! %d \n",error);
return -1;
}
return index;
}
/*oled 初始化函数*/
void spi_oled_init(void)
{
/*初始化oled*/
oled_send_commands(oled_spi_device, oled_init_data, sizeof(oled_init_data));
/*清屏*/
oled_fill(0x00);
}
/*字符设备操作函数集,open函数实现*/
static int oled_open(struct inode *inode, struct file *filp)
{
spi_oled_init(); //初始化显示屏
return 0;
}
/*字符设备操作函数集,.write函数实现*/
static ssize_t oled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *off)
{
int copy_number=0;
/*申请内存*/
oled_display_struct *write_data;
write_data = (oled_display_struct*)kzalloc(cnt, GFP_KERNEL);
copy_number = copy_from_user(write_data, buf,cnt);
oled_display_buffer(write_data->display_buffer, write_data->x, write_data->y, write_data->length);
/*释放内存*/
kfree(write_data);
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int oled_release(struct inode *inode, struct file *filp)
{
oled_send_command(oled_spi_device, 0xae);//关闭显示
return 0;
}
/*字符设备操作函数集*/
static struct file_operations oled_chr_dev_fops = {
.owner = THIS_MODULE,
.open = oled_open,
.write = oled_write,
.release = oled_release};
/*----------------平台驱动函数集-----------------*/
static int oled_probe(struct spi_device *spi)
{
struct device_node *node=spi->dev.of_node;
printk(KERN_EMERG " match successed \n");
/*获取 oled 的 D/C 控制引脚并设置为输出,默认高电平*/
oled_control_pin_number = of_get_named_gpio(node, "dc_control_pin", 0);
printk("oled_control_pin_number = %d,\n ", oled_control_pin_number);
gpio_request(oled_control_pin_number, "dc_control_pin");
gpio_direction_output(oled_control_pin_number, 1);
/*初始化spi*/
oled_spi_device = spi;
oled_spi_device->mode = SPI_MODE_0;
oled_spi_device->max_speed_hz = 2000000;
spi_setup(oled_spi_device);
/*打印oled_spi_device 部分内容*/
printk("max_speed_hz = %d\n", oled_spi_device->max_speed_hz);
printk("chip_select = %d\n", (int)oled_spi_device->chip_select);
printk("bits_per_word = %d\n", (int)oled_spi_device->bits_per_word);
printk("mode = %02X\n", oled_spi_device->mode);
printk("cs_gpio = %02X\n", oled_spi_device->cs_gpio);
return 0;
}
static int oled_remove(struct spi_device *spi)
{
return 0;
}
/*传统的方式指定 ID 匹配表*/
static const struct spi_device_id oled_device_id[] = {
{"fire,spi_oled", 0},
{}};
/*指定设备树匹配表*/
static const struct of_device_id oled_of_match_table[] = {
{.compatible = "fire,spi_oled"},
{}};
/*spi 总线设备结构体*/
struct spi_driver oled_driver = {
.probe = oled_probe,
.remove = oled_remove,
.id_table = oled_device_id,
.driver = {
.name = "spi_oled",
.of_match_table = of_match_ptr(oled_of_match_table),
},
};
/*
*驱动初始化函数
*/
static int __init oled_driver_init(void)
{
int error;
int ret = -1; //保存错误状态码
pr_info("oled_driver_init\n");
/*---------------------注册 字符设备部分-----------------*/
//采用动态分配的方式,获取设备编号,次设备号为0,
//设备名称spi_oled,可通过命令cat /proc/devices查看
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&oled_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
printk("fail to alloc oled_devno\n");
goto alloc_err;
}
//关联字符设备结构体cdev与文件操作结构体file_operations
oled_chr_dev.owner = THIS_MODULE;
cdev_init(&oled_chr_dev, &oled_chr_dev_fops);
// 添加设备至cdev_map散列表中
ret = cdev_add(&oled_chr_dev, oled_devno, DEV_CNT);
if (ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
/*创建类 */
class_oled = class_create(THIS_MODULE, DEV_NAME);
/*创建设备 DEV_NAME 指定设备名,*/
device_oled = device_create(class_oled, NULL, oled_devno, NULL, DEV_NAME);
error = spi_register_driver(&oled_driver);
if (error < 0) {
device_destroy(class_oled, oled_devno); //清除设备
class_destroy(class_oled); //清除类
cdev_del(&oled_chr_dev); //清除设备号
unregister_chrdev_region(oled_devno, DEV_CNT); //取消注册字符设备
}
return error;
add_err:
// 添加设备失败时,需要注销设备号
unregister_chrdev_region(oled_devno, DEV_CNT);
printk(" error! \n");
alloc_err:
return -1;
}
/*
*驱动注销函数
*/
static void __exit oled_driver_exit(void)
{
pr_info("oled_driver_exit\n");
spi_unregister_driver(&oled_driver);
gpio_free(oled_control_pin_number);
/*删除设备*/
device_destroy(class_oled, oled_devno); //清除设备
class_destroy(class_oled); //清除类
cdev_del(&oled_chr_dev); //清除设备号
unregister_chrdev_region(oled_devno, DEV_CNT); //取消注册字符设备
}
module_init(oled_driver_init);
module_exit(oled_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Embedfire");

21
SPI_OLED/spi_oled.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef ECSPI_OLED_H
#define ECSPI_OLED_H
#include <linux/types.h>
//宏定义
//oled显示屏列数
#define X_WIDTH 128
//oled显示屏行数
#define Y_WIDTH 64
/*数据发送结构体*/
typedef struct oled_display_struct
{
u8 x;
u8 y;
u32 length;
u8 display_buffer[];
}oled_display_struct;
#endif

View File

@ -0,0 +1,11 @@
out_file_name = "test_app"
all: test_app.c oled_code_table.c
aarch64-linux-gnu-gcc $^ -o $(out_file_name)
.PHONY: clean
clean:
rm $(out_file_name)

View File

@ -0,0 +1,304 @@
/**
******************************************************************************
* @brief 128*64点阵的OLED显示测试工程,仅适用于SD1306驱动IIC通信方式显示屏
******************************************************************************
* @attention *
*
* 1. 128*64点整OLED模块功能演示程序的字表,仅适用heltec.taobao.com所售产品;
* 2. 字表由打包资料中的"取字软件"计算得出;
* 3. 取字方式 -- 共阴、列行式、逆向输出
*
*
******************************************************************************
*/
extern unsigned char F16x16[];
extern unsigned char F6x8[][6];
extern unsigned char F8x16[][16];
extern unsigned char BMP1[];
/***************************16*16的点阵字体取模方式:共阴——列行式——逆向输出*********/
unsigned char F16x16[] =
{
0x00,0xFE,0x92,0x92,0xFE,0x92,0x92,0xFE,0x00,0x42,0x4A,0xD2,0x6A,0x46,0xC0,0x00,
0x40,0xC4,0x44,0x44,0x3F,0x24,0x24,0x24,0x00,0x40,0x80,0x7F,0x00,0x01,0x00,0x00,/*"野",0*/
0x00,0x00,0xC0,0x38,0x00,0x00,0x00,0xFF,0x00,0x00,0x40,0x20,0x18,0x00,0x00,0x00,
0x80,0x81,0x40,0x20,0x10,0x0C,0x03,0x00,0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00,/*"火",1*/
0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,
0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00,/*"科",2*/
0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00,
0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00/*"技",3*/
};
/************************************6*8的点阵************************************/
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的点阵************************************/
unsigned char F8x16[][16]=
{
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
};
unsigned char BMP1[] =
{
0x00,0x03,0x05,0x09,0x11,0xFF,0x11,0x89,0x05,0xC3,0x00,0xE0,0x00,0xF0,0x00,0xF8,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x44,0x28,0xFF,0x11,0xAA,0x44,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x83,0x01,0x38,0x44,0x82,0x92,
0x92,0x74,0x01,0x83,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x44,0xC7,0x01,0x7D,
0x7D,0x7D,0x7D,0x01,0x7D,0x7D,0x7D,0x7D,0x01,0x7D,0x7D,0x7D,0x7D,0x01,0xFF,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0x00,0x00,
0x6D,0x6D,0x6D,0x6D,0x6D,0x00,0x00,0x60,0x60,0x60,0x60,0x60,0x00,0x00,0x40,0x40,
0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDB,0xDB,0xDB,0xDB,0xDB,0x00,0x00,
0xDB,0xDB,0xDB,0xDB,0xDB,0x00,0x00,0xDB,0xDB,0xDB,0xDB,0xDB,0x00,0x00,0xDB,0xDB,
0xDB,0xDB,0xDB,0x00,0x00,0xDA,0xDA,0xDA,0xDA,0xDA,0x00,0x00,0xD8,0xD8,0xD8,0xD8,
0xD8,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,
0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x80,
0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x00,0x00,
0x06,0x06,0x06,0x06,0x06,0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x00,0x00,0x06,0x06,
0x06,0x06,0x06,0x00,0x00,0x06,0x06,0x06,0xE6,0x66,0x20,0x00,0x06,0x06,0x86,0x06,
0x06,0x00,0x00,0x06,0x06,0x06,0x06,0x86,0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x00,
0x00,0x86,0x86,0x86,0x86,0x86,0x80,0x80,0x86,0x86,0x06,0x86,0x86,0xC0,0xC0,0x86,
0x86,0x86,0x06,0x06,0xD0,0x30,0x76,0x06,0x06,0x06,0x06,0x00,0x00,0x06,0x06,0x06,
0x06,0x06,0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x00,0x00,0x06,0x06,0x06,0x06,0x06,
0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x1C,0x00,0xFE,0x00,0x01,
0x02,0x00,0xC4,0x18,0x20,0x02,0x9E,0x63,0xB2,0x0E,0x00,0xFF,0x81,0x81,0xFF,0x00,
0x00,0x80,0x40,0x30,0x0F,0x00,0x00,0x00,0x00,0xFF,0x00,0x23,0xEA,0xAA,0xBF,0xAA,
0xEA,0x03,0x3F,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x0C,0x08,0x00,0x00,0x01,0x01,0x01,
0x01,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x81,0x80,0x80,0x81,0x80,
0x81,0x80,0x80,0x80,0x80,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x01,0x01,0x09,0x0C,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,
0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,
0x00,0x1E,0x21,0x40,0x40,0x50,0x21,0x5E,0x00,0x1E,0x21,0x40,0x40,0x50,0x21,0x5E,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xC1,0xC1,0xFF,
0xFF,0xC1,0xC1,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x80,0xFC,0xF3,0xEF,0xF3,0xFC,
0x80,0xFF,0x80,0xEE,0xEE,0xEE,0xF5,0xFB,0xFF,0x9C,0xBE,0xB6,0xB6,0x88,0xFF,0x00,/*"D:\DreamSpark\OLED\MP3_UI.bmp",0*/
};

BIN
SPI_OLED/test_app/test_app Executable file

Binary file not shown.

View File

@ -0,0 +1,278 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "test_app.h"
/*点阵数据*/
extern unsigned char F16x16[];
extern unsigned char F6x8[][6];
extern unsigned char F8x16[][16];
extern unsigned char BMP1[];
int main(int argc, char *argv[])
{
int error = -1;
/*打开文件*/
int fd = open("/dev/spi_oled", O_RDWR);
if (fd < 0)
{
printf("open file : %s failed !\n", argv[0]);
return -1;
}
while(1)
{
/*显示图片*/
show_bmp(fd, 0, 0, BMP1, X_WIDTH*Y_WIDTH/8);
sleep(2);
oled_fill(fd, 0, 0, 127, 7, 0x00); //清屏
oled_show_F16X16_letter(fd,0, 0, F16x16, 4); //显示汉字
oled_show_F8X16_string(fd,0,2,"F8X16:THIS IS SPI TEST APP");
oled_show_F6X8_string(fd, 0, 6,"F6X8:THIS IS SPI TEST APP");
sleep(2);
oled_fill(fd, 0, 0, 127, 7, 0x00); //清屏
oled_show_F8X16_string(fd,0,0,"Testing is completed");
sleep(2);
oled_fill(fd, 0, 0, 127, 7, 0x00); //清屏
}
/*关闭文件*/
error = close(fd);
if(error < 0)
{
printf("close file error! \n");
}
return 0;
}
/*显示图片
*fd,打开文件描述符
*x, 图片显示起始位置x坐标,常规图片要将x设置为0,或者将图片逐行显示,
*y, 图片显示起始位置y坐标
*buffer,要显示的数据地址
*length,数据长度
*/
int show_bmp(int fd, u8 x, u8 y, u8 *buffer, u32 length)
{
int error;
struct oled_display_struct *display_struct;
/*申请内存*/
display_struct = malloc(sizeof(oled_display_struct) + length);
/*填充发送结构体*/
display_struct->x = x;
display_struct->y = y;
display_struct->length = length;
memcpy(display_struct->display_buffer, buffer, display_struct->length);
/*执行写入*/
error = write(fd, display_struct, sizeof(oled_display_struct) + display_struct->length);
/*释放内存*/
free(display_struct);
return error;
}
/*显示色块(清屏或刷屏)
*fd,打开文件描述符
*x,y, 起始坐标
*end_x,end_y 结束坐标
*data, 要显示的数据(0x00 黑屏,0xFF全亮。其他...)
*/
int oled_fill(int fd, u8 start_x, u8 start_y, u8 end_x, u8 end_y, u8 data)
{
struct oled_display_struct *display_struct = NULL;
int error;
if ((end_x < start_x) || (end_y < start_y))
{
return -1;
}
/*填充要发送的数据*/
display_struct = malloc(sizeof(oled_display_struct) + end_x - start_x + 1);
if (display_struct == NULL)
{
printf("oled_fill malloc error \n");
return -1;
}
display_struct->length = end_x - start_x + 1;
memset(display_struct->display_buffer, data, display_struct->length);
do
{
/*设置写入位置*/
display_struct->x = start_x;
display_struct->y = start_y;
/*执行写入*/
write(fd, display_struct, sizeof(oled_display_struct) + display_struct->length);
start_y++;
} while (start_y <= end_y);
/*释放内存*/
free(display_struct);
return 0;
}
/*向 oled 写入一个字符
*fd,打开文件描述符
*x,y, 起始坐标
*width, 字符宽度
*high, 字符高度
*data,字符数据地址
*/
int oled_show_one_letter(int fd, u8 x, u8 y, u8 width, u8 high, u8 *data)
{
struct oled_display_struct *display_struct = NULL;
if ((high % 8) != 0)
{
printf("oled_show_one_letter \"high\" set error! \n");
return -1;
}
high = high/8;
/*申请空间*/
display_struct = malloc(sizeof(oled_display_struct) + width * high);
if (display_struct == NULL)
{
printf("oled_fill malloc error \n");
return -1;
}
do
{
display_struct->x = x;
display_struct->y = y;
display_struct->length = width;
memcpy(display_struct->display_buffer, data, display_struct->length);
write(fd, display_struct, sizeof(oled_display_struct) + display_struct->length);
data += display_struct->length;
high--;
y++;
} while (high > 0);
/*释放空间*/
free(display_struct);
return 0;
}
/*写入F16X16字符
*fd,打开文件描述符
*x,y, 起始坐标
*data,字符数据地址
*length,字符个数
*/
int oled_show_F16X16_letter(int fd, u8 x, u8 y, u8 *data, u32 length)
{
/*判断数据是否有效*/
if ((x > (127 - 16)) || (y > (7 - 2)))
{
printf("oled_show_F16X16_letter \"x\" or \"y\" set error! \n");
return -1;
}
do
{
oled_show_one_letter(fd, x, y, 16, 16, data);
/* 32 = 16(width) * 16(high) / 8(一个字节8位)*/
data += 32;
length--;
x += 16;
if (x > 127-16)
{
/*清除未写入的区域*/
/*清除未写入的区域*/
// oled_fill(fd, x, y, 127, y+1, 0x00);
x = 0;
y += 2;
}
} while (length>0);
return 0;
}
/*写入F6x8字符串
*fd,打开文件描述符
*x,y, 起始坐标
*data,字符数据地址
*length,字符个数
*/
int oled_show_F6X8_string(int fd, u8 x, u8 y, u8 *string)
{
u32 i = 0;
u8 value = 0;
/*判断数据是否有效*/
if ((x > (127 - 6)) || (y > 7)||(string[0] == '\0'))
{
printf("oled_show_F16X16_letter \"x\" or \"y\" set error! \n");
return -1;
}
do
{
value = string[i] - 32;
oled_show_one_letter(fd, x, y, 6, 8, F6x8[value]);
/* 32 = 16(width) * 16(high) / 8(一个字节8位)*/
x += 6;
i++;
if (x > (127-6))
{
/*清除未写入的区域*/
// oled_fill(fd, x, y, 127, y, 0x00);
x = 0;
y++;
}
} while ((y<=7)&&(string[i] != '\0'));
return 0;
}
/*写入F8X16字符串
*fd,打开文件描述符
*x,y, 起始坐标
*data,字符数据地址
*length,字符个数
*/
int oled_show_F8X16_string(int fd, u8 x, u8 y, u8 *string)
{
u32 i = 0;
u8 value = 0;
/*判断数据是否有效*/
if ((x > (127 - 8)) || (y > (7 - 1))||(string[0] == '\0'))
{
printf("oled_show_F16X16_letter \"x\" or \"y\" set error! \n");
return -1;
}
do
{
value = string[i] - 32;
oled_show_one_letter(fd, x, y, 8, 16, F8x16[value]);
/* 32 = 16(width) * 16(high) / 8(一个字节8位)*/
x += 8;
i++;
if (x > (127-8))
{
/*清除未写入的区域*/
// oled_fill(fd, x, y, 127, y, 0x00);
x = 0;
y += 2;
}
} while ((y<=6)&&(string[i] != '\0'));
return 0;
}

View File

@ -0,0 +1,30 @@
#ifndef TEST_APP_H
#define TEST_APP_H
//宏定义
#define X_WIDTH 128 //oled显示屏列数
#define Y_WIDTH 64 //oled显示屏行数
#define u8 unsigned char
#define u32 unsigned int
/*数据发送结构体*/
typedef struct oled_display_struct
{
u8 x;
u8 y;
u32 length;
u8 display_buffer[];
}oled_display_struct;
int show_bmp(int fd, u8 x, u8 y,u8* buffer, u32 length);
int oled_fill(int fd, u8 start_x, u8 start_y,u8 end_x, u8 end_y,u8 data);
int oled_show_one_letter(int fd, u8 x, u8 y, u8 width, u8 high, u8 *data);
int oled_show_F16X16_letter(int fd, u8 x, u8 y, u8 *data, u32 length);
int oled_show_F6X8_string(int fd, u8 x, u8 y, u8 *string);
int oled_show_F8X16_string(int fd, u8 x, u8 y, u8 *string);
#endif

16
Sysfs/Makefile Normal file
View File

@ -0,0 +1,16 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := sys_test.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

67
Sysfs/sys_test.c Normal file
View File

@ -0,0 +1,67 @@
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include<linux/sysfs.h>
#include<linux/kobject.h>
#include <linux/err.h>
volatile int test_value = 0;
struct kobject *kobj_ref;
/*该函数被调用在sysfs文件被读时*/
static ssize_t sysfs_show(struct kobject *kobj,
struct kobj_attribute *attr, char *buf)
{
pr_info("读sysfs!\n");
return sprintf(buf, "test_value = %d\n", test_value);
}
/* 该函数被调用在sysfs文件被写时*/
static ssize_t sysfs_store(struct kobject *kobj,
struct kobj_attribute *attr,const char *buf, size_t count)
{
pr_info("写sysfs!\n");
sscanf(buf,"%d",&test_value);
return count;
}
struct kobj_attribute sysfs_test_attr = __ATTR(test_value, 0664, sysfs_show, sysfs_store);
/*模块初始化函数*/
static int __init sysfs_test_driver_init(void)
{
/*创建一个目录在/sys下 */
kobj_ref = kobject_create_and_add("sysfs_test",NULL);
/*在sysfs_test目录下创建一个文件*/
if(sysfs_create_file(kobj_ref,&sysfs_test_attr.attr)){
pr_err("创建sysfs文件失败.....\n");
goto error_sysfs;
}
pr_info("驱动模块初始化完成!\n");
return 0;
error_sysfs:
kobject_put(kobj_ref);
sysfs_remove_file(kernel_kobj, &sysfs_test_attr.attr);
return -1;
}
/*模块退出函数*/
static void __exit sysfs_test_driver_exit(void)
{
kobject_put(kobj_ref);
sysfs_remove_file(kernel_kobj, &sysfs_test_attr.attr);
pr_info("设备驱动模块移除!\n");
}
module_init(sysfs_test_driver_init);
module_exit(sysfs_test_driver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Embedfire");
MODULE_DESCRIPTION("一个简单的使用sysfs的驱动程序");

View File

View File

@ -0,0 +1,17 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := interrupt.o
out = test_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm test_app

View File

@ -0,0 +1,202 @@
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>
#include <linux/types.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "button"
#define DEV_CNT (1)
static dev_t button_devno; //定义字符设备的设备号
static struct cdev button_chr_dev; //定义字符设备结构体chr_dev
struct class *class_button; //保存创建的类
struct device *device_button; // 保存创建的设备
struct device_node *button_device_node = NULL; //定义按键设备节点结构体
unsigned button_GPIO_number = 0; //保存button使用的GPIO引脚编号
u32 interrupt_number = 0; // button 引脚中断编号
atomic_t button_status = ATOMIC_INIT(0); //定义整型原子变量,保存按键状态 ,设置初始值为0
static irqreturn_t button_irq_hander(int irq, void *dev_id)
{
atomic_inc(&button_status);
return IRQ_HANDLED;
}
static int button_open(struct inode *inode, struct file *filp)
{
int error = -1;
/*获取按键 设备树节点*/
button_device_node = of_find_node_by_path("/button_interrupt");
if(NULL == button_device_node)
{
printk("of_find_node_by_path error!");
return -1;
}
/*获取按键使用的GPIO*/
button_GPIO_number = of_get_named_gpio(button_device_node ,"button-gpios", 0);
if(!button_GPIO_number)
{
printk("of_get_named_gpio error");
return -1;
}
printk("button_GPIO_number = %d \n", button_GPIO_number);
/*申请GPIO , 记得释放*/
error = gpio_request(button_GPIO_number, "button_gpios");
if(error < 0)
{
printk("gpio_request error");
gpio_free(button_GPIO_number);
return -1;
}
error = gpio_direction_input(button_GPIO_number);//设置引脚为输入模式
/*获取中断号*/
interrupt_number = irq_of_parse_and_map(button_device_node, 0);
printk("\n irq_of_parse_and_map = %d \n",interrupt_number);
/*申请中断, 记得释放*/
error = request_irq(interrupt_number,button_irq_hander,IRQF_TRIGGER_FALLING,"button_interrupt",NULL);
if(error != 0)
{
printk("request_irq error");
free_irq(interrupt_number, NULL);
return -1;
}
/*申请之后已经开启了,切记不要再次打开,否则运行时报错*/
// // enable_irq(interrupt_number);
return 0;
}
static ssize_t button_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int error = -1;
int button_countervc = 0;
/*读取按键状态值*/
button_countervc = atomic_read(&button_status);
/*结果拷贝到用户空间*/
error = copy_to_user(buf, &button_countervc, sizeof(button_countervc));
if(error < 0)
{
printk("copy_to_user error");
return -1;
}
/*清零按键状态值*/
atomic_set(&button_status,0);
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int button_release(struct inode *inode, struct file *filp)
{
/*释放申请的引脚,和中断*/
printk("button_release\n");
free_irq(interrupt_number, NULL);
gpio_free(button_GPIO_number);
return 0;
}
/*字符设备操作函数集*/
static struct file_operations button_chr_dev_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release
};
/*
*驱动初始化函数
*/
static int __init button_driver_init(void)
{
int error = -1;
/*采用动态分配的方式,获取设备编号,次设备号为0,*/
error = alloc_chrdev_region(&button_devno, 0, DEV_CNT, DEV_NAME);
if (error < 0)
{
printk("fail to alloc button_devno\n");
goto alloc_err;
}
/*关联字符设备结构体cdev与文件操作结构体file_operations*/
button_chr_dev.owner = THIS_MODULE;
cdev_init(&button_chr_dev, &button_chr_dev_fops);
/*添加设备至cdev_map散列表中*/
error = cdev_add(&button_chr_dev, button_devno, DEV_CNT);
if (error < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
class_button = class_create(THIS_MODULE, DEV_NAME); //创建类
device_button = device_create(class_button, NULL, button_devno, NULL, DEV_NAME);//创建设备 DEV_NAME 指定设备名,
return 0;
add_err:
unregister_chrdev_region(button_devno, DEV_CNT); // 添加设备失败时,需要注销设备号
printk("\n error! \n");
alloc_err:
return -1;
}
/*
*驱动注销函数
*/
static void __exit button_driver_exit(void)
{
pr_info("button_driver_exit\n");
/*删除设备*/
device_destroy(class_button, button_devno); //清除设备
class_destroy(class_button); //清除类
cdev_del(&button_chr_dev); //清除设备号
unregister_chrdev_region(button_devno, DEV_CNT); //取消注册字符设备
}
module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int error = -20;
int button_status = 0;
/*打开文件*/
int fd = open("/dev/button", O_RDWR);
if (fd < 0)
{
printf("open file : /dev/button error!\n");
return -1;
}
printf("wait button down... \n");
do
{
/*读取按键状态*/
error = read(fd, &button_status, sizeof(button_status));
if (error < 0)
{
printf("read file error! \n");
}
usleep(1000 * 100); //延时100毫秒
} while (0 == button_status);
printf("button Down !\n");
/*关闭文件*/
error = close(fd);
if (error < 0)
{
printf("close file error! \n");
}
return 0;
}

View File

@ -0,0 +1,17 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := interrupt.o
out = test_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm test_app

View File

@ -0,0 +1,224 @@
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "button"
#define DEV_CNT (1)
static dev_t button_devno; //定义字符设备的设备号
static struct cdev button_chr_dev; //定义字符设备结构体chr_dev
struct class *class_button; //保存创建的类
struct device *device_button; // 保存创建的设备
struct device_node *button_device_node = NULL; //定义按键设备节点结构体
unsigned button_GPIO_number = 0; //保存button使用的GPIO引脚编号
u32 interrupt_number = 0; // button 引脚中断编号
atomic_t button_status = ATOMIC_INIT(0); //定义整型原子变量,保存按键状态 ,设置初始值为0
struct tasklet_struct button_tasklet;
void button_tasklet_hander(unsigned long data)
{
int counter = 1;
mdelay(200);
printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "button_tasklet_hander counter = %d \n", counter++);
}
static irqreturn_t button_irq_hander(int irq, void *dev_id)
{
printk(KERN_ERR "button_irq_hander----------inter");
/*按键状态加一*/
atomic_inc(&button_status);
tasklet_schedule(&button_tasklet);
printk(KERN_ERR "button_irq_hander-----------exit");
return IRQ_HANDLED;
}
static int button_open(struct inode *inode, struct file *filp)
{
int error = -1;
/*获取按键 设备树节点*/
button_device_node = of_find_node_by_path("/button_interrupt");
if(NULL == button_device_node)
{
printk("of_find_node_by_path error!");
return -1;
}
/*获取按键使用的GPIO*/
button_GPIO_number = of_get_named_gpio(button_device_node ,"button-gpios", 0);
if(0 == button_GPIO_number)
{
printk("of_get_named_gpio error");
return -1;
}
/*申请GPIO , 记得释放*/
error = gpio_request(button_GPIO_number, "button_gpio");
if(error < 0)
{
printk("gpio_request error");
gpio_free(button_GPIO_number);
return -1;
}
error = gpio_direction_input(button_GPIO_number);//设置引脚为输入模式
/*获取中断号*/
interrupt_number = irq_of_parse_and_map(button_device_node, 0);
printk("irq_of_parse_and_map = %d \n",interrupt_number);
/*申请中断, 记得释放*/
error = request_irq(interrupt_number,button_irq_hander,IRQF_TRIGGER_RISING,"button_interrupt",device_button);
if(error != 0)
{
printk("request_irq error");
free_irq(interrupt_number, device_button);
return -1;
}
/*初始化button_tasklet*/
tasklet_init(&button_tasklet,button_tasklet_hander,0);
/*申请之后已经开启了,切记不要再次打开,否则运行时报错*/
// // enable_irq(interrupt_number);
return 0;
}
static ssize_t button_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int error = -1;
int button_countervc = 0;
/*读取按键状态值*/
button_countervc = atomic_read(&button_status);
/*结果拷贝到用户空间*/
error = copy_to_user(buf, &button_countervc, sizeof(button_countervc));
if(error < 0)
{
printk("copy_to_user error");
return -1;
}
/*清零按键状态值*/
atomic_set(&button_status,0);
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int button_release(struct inode *inode, struct file *filp)
{
/*释放申请的引脚,和中断*/
free_irq(interrupt_number, device_button);
gpio_free(button_GPIO_number);
return 0;
}
/*字符设备操作函数集*/
static struct file_operations button_chr_dev_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release
};
/*
*驱动初始化函数
*/
static int __init button_driver_init(void)
{
int error = -1;
/*采用动态分配的方式,获取设备编号,次设备号为0,*/
error = alloc_chrdev_region(&button_devno, 0, DEV_CNT, DEV_NAME);
if (error < 0)
{
printk("fail to alloc button_devno\n");
goto alloc_err;
}
/*关联字符设备结构体cdev与文件操作结构体file_operations*/
button_chr_dev.owner = THIS_MODULE;
cdev_init(&button_chr_dev, &button_chr_dev_fops);
/*添加设备至cdev_map散列表中*/
error = cdev_add(&button_chr_dev, button_devno, DEV_CNT);
if (error < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
class_button = class_create(THIS_MODULE, DEV_NAME); //创建类
device_button = device_create(class_button, NULL, button_devno, NULL, DEV_NAME);//创建设备 DEV_NAME 指定设备名,
return 0;
add_err:
unregister_chrdev_region(button_devno, DEV_CNT); // 添加设备失败时,需要注销设备号
printk("\n error! \n");
alloc_err:
return -1;
}
/*
*驱动注销函数
*/
static void __exit button_driver_exit(void)
{
pr_info("button_driver_exit\n");
/*删除设备*/
device_destroy(class_button, button_devno); //清除设备
class_destroy(class_button); //清除类
cdev_del(&button_chr_dev); //清除设备号
unregister_chrdev_region(button_devno, DEV_CNT); //取消注册字符设备
}
module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("embedfire lubuncat , interrupt tasklet");

View File

@ -0,0 +1,9 @@
#ifndef INTERRUPT_H
#define INTERRUPT_H
#include <linux/types.h>
#endif

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int error = -20;
int button_status = 0;
/*打开文件*/
int fd = open("/dev/button", O_RDWR);
if (fd < 0)
{
printf("open file : /dev/button error!\n");
return -1;
}
printf("wait button down... \n");
do
{
/*读取按键状态*/
error = read(fd, &button_status, sizeof(button_status));
if (error < 0)
{
printf("read file error! \n");
}
usleep(1000 * 100); //延时100毫秒
} while (0 == button_status);
printf("button Down !\n");
/*关闭文件*/
error = close(fd);
if (error < 0)
{
printf("close file error! \n");
}
return 0;
}

View File

@ -0,0 +1,17 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := interrupt.o
out = test_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm test_app

View File

@ -0,0 +1,218 @@
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/of_irq.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "button"
#define DEV_CNT (1)
static dev_t button_devno; //定义字符设备的设备号
static struct cdev button_chr_dev; //定义字符设备结构体chr_dev
struct class *class_button; //保存创建的类
struct device *device_button; // 保存创建的设备
struct device_node *button_device_node = NULL; //定义按键设备节点结构体
unsigned button_GPIO_number = 0; //保存button使用的GPIO引脚编号
u32 interrupt_number = 0; // button 引脚中断编号
atomic_t button_status = ATOMIC_INIT(0); //定义整型原子变量,保存按键状态 ,设置初始值为0
struct work_struct button_work; //定义工作结构体
/*定义工作执行函数*/
void work_hander(struct work_struct *work)
{
int counter = 1;
mdelay(200);
printk(KERN_ERR "work_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "work_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "work_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "work_hander counter = %d \n", counter++);
mdelay(200);
printk(KERN_ERR "work_hander counter = %d \n", counter++);
}
/*定义按键中断服务函数*/
static irqreturn_t button_irq_hander(int irq, void *dev_id)
{
/*按键状态加一*/
atomic_inc(&button_status);
schedule_work(&button_work); //触发工作
return IRQ_HANDLED;
}
static int button_open(struct inode *inode, struct file *filp)
{
int error = -1;
/*获取按键 设备树节点*/
button_device_node = of_find_node_by_path("/button_interrupt");
if(NULL == button_device_node)
{
printk("of_find_node_by_path error!");
return -1;
}
/*获取按键使用的GPIO*/
button_GPIO_number = of_get_named_gpio(button_device_node ,"button-gpios", 0);
if(0 == button_GPIO_number)
{
printk("of_get_named_gpio error");
return -1;
}
/*申请GPIO , 记得释放*/
error = gpio_request(button_GPIO_number, "button_gpio");
if(error < 0)
{
printk("gpio_request error");
gpio_free(button_GPIO_number);
return -1;
}
error = gpio_direction_input(button_GPIO_number);//设置引脚为输入模式
/*获取中断号*/
interrupt_number = irq_of_parse_and_map(button_device_node, 0);
printk("irq_of_parse_and_map = %d \n",interrupt_number);
/*申请中断, 记得释放*/
error = request_irq(interrupt_number,button_irq_hander,IRQF_TRIGGER_RISING,"button_interrupt",device_button);
if(error != 0)
{
printk("request_irq error");
free_irq(interrupt_number, device_button);
return -1;
}
/*初始化button_work*/
INIT_WORK(&button_work, work_hander);
/*申请之后已经开启了,切记不要再次打开,否则运行时报错*/
// // enable_irq(interrupt_number);
return 0;
}
static ssize_t button_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int error = -1;
int button_countervc = 0;
/*读取按键状态值*/
button_countervc = atomic_read(&button_status);
/*结果拷贝到用户空间*/
error = copy_to_user(buf, &button_countervc, sizeof(button_countervc));
if(error < 0)
{
printk("copy_to_user error");
return -1;
}
/*清零按键状态值*/
atomic_set(&button_status,0);
return 0;
}
/*字符设备操作函数集,.release函数实现*/
static int button_release(struct inode *inode, struct file *filp)
{
/*释放申请的引脚,和中断*/
gpio_free(button_GPIO_number);
free_irq(interrupt_number, device_button);
return 0;
}
/*字符设备操作函数集*/
static struct file_operations button_chr_dev_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release};
/*
*驱动初始化函数
*/
static int __init button_driver_init(void)
{
int error = -1;
/*采用动态分配的方式,获取设备编号,次设备号为0,*/
error = alloc_chrdev_region(&button_devno, 0, DEV_CNT, DEV_NAME);
if (error < 0)
{
printk("fail to alloc button_devno\n");
goto alloc_err;
}
/*关联字符设备结构体cdev与文件操作结构体file_operations*/
button_chr_dev.owner = THIS_MODULE;
cdev_init(&button_chr_dev, &button_chr_dev_fops);
/*添加设备至cdev_map散列表中*/
error = cdev_add(&button_chr_dev, button_devno, DEV_CNT);
if (error < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
class_button = class_create(THIS_MODULE, DEV_NAME); //创建类
device_button = device_create(class_button, NULL, button_devno, NULL, DEV_NAME);//创建设备 DEV_NAME 指定设备名,
return 0;
add_err:
unregister_chrdev_region(button_devno, DEV_CNT); // 添加设备失败时,需要注销设备号
printk("\n error! \n");
alloc_err:
return -1;
}
/*
*驱动注销函数
*/
static void __exit button_driver_exit(void)
{
pr_info("button_driver_exit\n");
/*删除设备*/
device_destroy(class_button, button_devno); //清除设备
class_destroy(class_button); //清除类
cdev_del(&button_chr_dev); //清除设备号
unregister_chrdev_region(button_devno, DEV_CNT); //取消注册字符设备
}
module_init(button_driver_init);
module_exit(button_driver_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("embedfire lubuncat2_RK , interrupt work queue");

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int error = -20;
int button_status = 0;
/*打开文件*/
int fd = open("/dev/button", O_RDWR);
if (fd < 0)
{
printf("open file : /dev/button error!\n");
return -1;
}
printf("wait button down... \n");
do
{
/*读取按键状态*/
error = read(fd, &button_status, sizeof(button_status));
if (error < 0)
{
printf("read file error! \n");
}
usleep(1000 * 100); //延时100毫秒
} while (0 == button_status);
printf("button Down !\n");
usleep(1000 * 5000);
/*关闭文件*/
error = close(fd);
if (error < 0)
{
printf("close file error! \n");
}
return 0;
}

View File

@ -0,0 +1,26 @@
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/interrupt-controller/irq.h>
&{/} {
button_interrupt: button_interrupt {
status = "okay";
compatible = "button_interrupt";
pinctrl-names = "default";
pinctrl-0 = <&button_interrupt_pin>;
interrupt-parent = <&gpio0>;
interrupts = <RK_PB0 IRQ_TYPE_LEVEL_LOW>;
button-gpios = <&gpio0 RK_PB0 GPIO_ACTIVE_HIGH>;
};
};
&{/pinctrl} {
button_interrupt {
button_interrupt_pin: button_interrupt_pin {
rockchip,pins = <0 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};

View File

@ -0,0 +1,11 @@
config TOUCHSCREEN_HYNITRON_CST816X
tristate "Hynitron CST816X touchscreen support"
depends on I2C
help
Say Y here if you have a touchscreen using a Hynitron
CST816X touchscreen controller.
If unsure, say N.
To compile this driver as a module, choose M here: the
module will be called hynitron-cst816x.

View File

@ -0,0 +1,20 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=/opt/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
export ARCH CROSS_COMPILE
obj-m += hynitron-cst816x.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm $(out)

View File

@ -0,0 +1,259 @@
# Linux Kernel driver for CST816S Touchscreen controller based on 1.28 Inch Round LCD display
This guide will help you build, install, and set up your kernel module and CST816S Touch Screen device tree overlay for a Raspberry Pi.
<img src="https://github.com/kuzhylol/cst816x-driver/raw/main/pictures/cst816s-ts.png" width="240" height="180">
## Documentation
**Raspberry Pi x Round display pinout**
| Module Pin | Raspberry Pi (BCM) |
|------------|--------------------|
| VCC | 3.3V |
| GND | GND |
| MISO | 9 |
| MOSI | 10 |
| SCLK | 11 |
| LCS_CS | 8 |
| LCS_DC | 25 |
| LCS_RST | 27 |
| LCS_BL | 18 |
| TP_SDA | 2 |
| TP_SCL | 3 |
| TP_INT | 4 |
| TP_RST | 17 |
Follow the [Detailed guide](https://www.waveshare.com/wiki/1.28inch_Touch_LCD) provided by Waveshare to set up hardware connection.
**Input device Capabilities Description**
| Event Code | Description |
|--------------------|-----------------------|
| BTN_LEFT | Gesture Left |
| BTN_RIGHT | Gesture Right |
| BTN_FORWARD | Gesture Up |
| BTN_BACK | Gesture Down |
| BTN_TOUCH | Single Touch |
| BTN_TOOL_TRIPLETAP | Long press (hold) |
**Testing environment**
1. [GC9A01 FBTFT overlay](https://github.com/juliannojungle/gc9a01-overlay) to draw graphics
2. [LVGL demo](https://github.com/lvgl/lv_port_linux) to test Touschreen functionality.
Follow the steps to enable LVGL demo:
**Enable Frame buffer**
Need to enable Frame Buffer functionality inside LVGL header config (lv_conf.h).
```c
#define LV_USE_LINUX_FBDEV 1
```
**Initialize Frame buffer**
```c
const char *device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fbN");
lv_display_t *disp = lv_linux_fbdev_create();
lv_linux_fbdev_set_file(disp, device);
```
**Initialize Input Device (Linux Touchscreen Interface)**
```c
lv_indev_t *input = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/eventN");
lv_indev_set_display(input, disp);
```
** Initialize and start LWFS **
```c
ui_init();
while(1) {
format_linux_date();
lv_timer_handler();
usleep(1000);
}
```
## Kernel Module Setup Instructions
### Build driver as External Kernel Module
1. **Navigate to the kernel module directory**:
```sh
cd cst816x-driver/
```
2. **Build the module using the default kernel headers**:
```sh
make
```
Alternatively, specify the kernel source directory manually:
```sh
make -C <local_path_to_kernel_sources> M=$PWD
```
### Install driver as External Kernel Module
1. **Install the module using the default kernel headers**:
```sh
make install
```
Alternatively, specify the kernel source directory manually:
```sh
make -C <local_path_to_kernel_sources> M=$PWD install
```
### Clean the Build Files
1. **Clean the build files using the default kernel headers**:
```sh
make clean
```
Alternatively, specify the kernel source directory manually:
```sh
make -C /home/pi/linux-6.9.y M=$PWD clean
```
### Install as External Module
1. **Insert the module into the kernel**:
```sh
sudo insmod hynitron-cst816x.ko
```
2. **Check the module status**:
```sh
dmesg
```
Example output:
```plaintext
[ 1593.107576] hynitron_cst816x: loading out-of-tree module taints kernel.
[ 1593.283284] input: Hynitron CST816X Touchscreen as /devices/platform/soc/fe804000.i2c/i2c-1/1-0015/input/inputN
```
### Built-in Kernel driver
1. Append CST816X entry to Kernel Kconfig for input/touchscreen section: **drivers/input/touchscreen/Kconfig**
```plaintext
config TOUCHSCREEN_HYNITRON_CST816X
tristate "Hynitron CST816X touchscreen support"
depends on I2C
help
Say Y here if you have a touchscreen using a Hynitron
CST816X touchscreen controller.
If unsure, say N.
To compile this driver as a module, choose M here: the
module will be called hynitron-cst816x.
```
2. Append module entry to Makefile: **drivers/input/touchscreen/Makefile**
```plaintext
obj-$(CONFIG_TOUCHSCREEN_GOODIX_BERLIN_I2C) += goodix_berlin_i2c.o
obj-$(CONFIG_TOUCHSCREEN_GOODIX_BERLIN_SPI) += goodix_berlin_spi.o
obj-$(CONFIG_TOUCHSCREEN_HIDEEP) += hideep.o
+obj-$(CONFIG_TOUCHSCREEN_HYNITRON_CST816X) += hynitron-cst816x.o
obj-$(CONFIG_TOUCHSCREEN_ILI210X) += ili210x.o
obj-$(CONFIG_TOUCHSCREEN_ILITEK) += ilitek_ts_i2c.o
```
3. Build Kernel, driver will included as built-in module
## Device Tree setup steps
### Device Tree approach
Include the following content in your device tree file (e.g., `hynitron-cst816s.dts`):
```dts
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>
i2c {
#address-cells = <1>;
#size-cells = <0>;
touchscreen@15 {
compatible = "hynitron,cst816s";
reg = <0x15>;
interrupt-parent = <&gpio0>;
interrupts = <4 IRQ_TYPE_EDGE_RISING>;
reset-gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
};
};
```
### Raspberry Pi DTS overlay approach
1. **Navigate to the directory containing your device tree source file**:
```sh
cd cst816x-driver/rpi4b/
```
2. **Compile the device tree overlay**:
```sh
dtc -@ -I dts -O dtb -o cst816s.dtbo hynitron-cst816s.dts
```
3. **Copy the compiled overlay to the boot overlays directory**:
```sh
sudo cp cst816s.dtbo /boot/overlays/
```
2. **Add the overlay to the `config.txt` file**:
```sh
sudo sh -c 'echo "dtoverlay=cst816s" >> /boot/config.txt'
```
### Reboot the Device
**Reboot your Raspberry Pi to apply the overlay**:
```sh
sudo reboot
```
Following these steps will build and install your kernel module and device tree overlay on a Raspberry Pi. Make sure to adjust any paths to match your specific environment.
### Verify Input Device Capabilities
**You can find available capabilities showing that device operate properly**
```plaintext
evtest /dev/input/eventN
Input driver version is 1.0.1
Input device ID: bus 0x18 vendor 0x0 product 0x0 version 0x0
Input device name: "Hynitron CST816X Touchscreen"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 272 (BTN_LEFT)
Event code 273 (BTN_RIGHT)
Event code 277 (BTN_FORWARD)
Event code 278 (BTN_BACK)
Event code 330 (BTN_TOUCH)
Event code 334 (BTN_TOOL_TRIPLETAP)
Event type 3 (EV_ABS)
Event code 0 (ABS_X)
Value 0
Min 0
Max 240
Event code 1 (ABS_Y)
Value 0
Min 0
Max 240
```

View File

@ -0,0 +1,57 @@
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
%YAML 1.2
---
$id: http://devicetree.org/schemas/input/touchscreen/hynitron,cst816s.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#
title: Hynitron CST816S Touchscreen controller
description:
Hynitron CST816S Touchscreen controller for 1.28-inch 240x240 Resolution
Touch LCD Display Module
maintainers:
- Oleh Kuzhylnyi <kuzhylol@gmail.com>
allOf:
- $ref: touchscreen.yaml#
properties:
compatible:
enum:
- hynitron,cst816s
reg:
maxItems: 1
interrupts:
maxItems: 1
reset-gpios:
maxItems: 1
additionalProperties: false
required:
- compatible
- reg
- interrupts
- reset-gpios
examples:
- |
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/interrupt-controller/irq.h>
i2c {
#address-cells = <1>;
#size-cells = <0>;
touchscreen@15 {
compatible = "hynitron,cst816s";
reg = <0x15>;
interrupt-parent = <&gpio0>;
interrupts = <4 IRQ_TYPE_EDGE_RISING>;
reset-gpios = <&gpio 17 GPIO_ACTIVE_LOW>;
};
};
...

View File

@ -0,0 +1,229 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Driver for I2C connected Hynitron CST816X Touchscreen
*
* Copyright (C) 2024 Oleh Kuzhylnyi <kuzhylol@gmail.com>
*/
#include <asm/unaligned.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#define CST816X_FRAME 0x01
struct cst816x_touch_info {
u8 gesture;
u8 touch;
__be16 abs_x;
__be16 abs_y;
} __packed;
struct cst816x_event_map {
u8 gesture;
u16 code;
};
struct cst816x_priv {
struct i2c_client *client;
struct gpio_desc *reset;
struct input_dev *input;
struct cst816x_event_map event_map[16];
};
static int cst816x_parse_dt_event_map(struct device *dev,
struct cst816x_priv *priv)
{
struct device_node *np = dev->of_node;
struct device_node *ts, *child;
u32 gesture, code;
u8 index;
ts = of_get_child_by_name(np, "touchscreen");
if (!ts)
return -ENOENT;
for_each_child_of_node(ts, child) {
if (of_property_read_u32(child, "cst816x,gesture", &gesture))
continue;
if (of_property_read_u32(child, "linux,code", &code))
continue;
index = gesture & 0x0F;
priv->event_map[index].gesture = gesture;
priv->event_map[index].code = code;
}
return 0;
}
static int cst816x_i2c_read_register(struct cst816x_priv *priv, u8 reg,
void *buf, size_t len)
{
int rc;
struct i2c_msg xfer[] = {
{
.addr = priv->client->addr,
.flags = 0,
.buf = &reg,
.len = sizeof(reg),
},
{
.addr = priv->client->addr,
.flags = I2C_M_RD,
.buf = buf,
.len = len,
},
};
rc = i2c_transfer(priv->client->adapter, xfer, ARRAY_SIZE(xfer));
if (rc != ARRAY_SIZE(xfer)) {
rc = rc < 0 ? rc : -EIO;
dev_err(&priv->client->dev, "i2c rx err: %d\n", rc);
return rc;
}
return 0;
}
static bool cst816x_process_touch(struct cst816x_priv *priv,
struct cst816x_touch_info *info)
{
if (cst816x_i2c_read_register(priv, CST816X_FRAME, info, sizeof(*info)))
return false;
info->abs_x = get_unaligned_be16(&info->abs_x) & GENMASK(11, 0);
info->abs_y = get_unaligned_be16(&info->abs_y) & GENMASK(11, 0);
dev_dbg(&priv->client->dev, "x: %d, y: %d, t: %d, g: 0x%x\n",
info->abs_x, info->abs_y, info->touch, info->gesture);
return true;
}
static int cst816x_register_input(struct cst816x_priv *priv)
{
int i;
priv->input = devm_input_allocate_device(&priv->client->dev);
if (!priv->input)
return -ENOMEM;
priv->input->name = "Hynitron CST816X Touchscreen";
priv->input->phys = "input/ts";
priv->input->id.bustype = BUS_I2C;
input_set_drvdata(priv->input, priv);
for (i = 0; i < ARRAY_SIZE(priv->event_map); i++)
input_set_capability(priv->input, EV_KEY,
priv->event_map[i].code);
input_set_abs_params(priv->input, ABS_X, 0, 240, 0, 0);
input_set_abs_params(priv->input, ABS_Y, 0, 240, 0, 0);
return input_register_device(priv->input);
}
static void cst816x_reset(struct cst816x_priv *priv)
{
gpiod_set_value_cansleep(priv->reset, 1);
msleep(50);
gpiod_set_value_cansleep(priv->reset, 0);
msleep(100);
}
static irqreturn_t cst816x_irq_cb(int irq, void *cookie)
{
struct cst816x_priv *priv = cookie;
struct cst816x_touch_info info;
if (!cst816x_process_touch(priv, &info))
return IRQ_HANDLED;
if (info.touch) {
input_report_abs(priv->input, ABS_X, info.abs_x);
input_report_abs(priv->input, ABS_Y, info.abs_y);
input_report_key(priv->input, BTN_TOUCH, 1);
}
if (info.gesture) {
input_report_key(priv->input,
priv->event_map[info.gesture & 0x0F].code,
info.touch);
if (!info.touch)
input_report_key(priv->input, BTN_TOUCH, 0);
}
input_sync(priv->input);
return IRQ_HANDLED;
}
static int cst816x_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct cst816x_priv *priv;
struct device *dev = &client->dev;
int error;
(void)id;
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->client = client;
priv->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(priv->reset))
return dev_err_probe(dev, PTR_ERR(priv->reset),
"gpio reset request failed\n");
if (priv->reset)
cst816x_reset(priv);
error = cst816x_parse_dt_event_map(dev, priv);
if (error)
dev_warn(dev, "no gestures specified in dt\n");
error = cst816x_register_input(priv);
if (error)
return dev_err_probe(dev, error, "input register failed\n");
error = devm_request_threaded_irq(dev, client->irq, NULL, cst816x_irq_cb,
IRQF_ONESHOT, dev->driver->name, priv);
if (error)
return dev_err_probe(dev, error, "irq request failed\n");
return 0;
}
static const struct i2c_device_id cst816x_id[] = {
{ .name = "cst816s", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, cst816x_id);
static const struct of_device_id cst816x_of_match[] = {
{ .compatible = "hynitron,cst816s", },
{ }
};
MODULE_DEVICE_TABLE(of, cst816x_of_match);
static struct i2c_driver cst816x_driver = {
.driver = {
.name = "cst816x",
.of_match_table = cst816x_of_match,
},
.id_table = cst816x_id,
.probe = cst816x_probe,
};
module_i2c_driver(cst816x_driver);
MODULE_AUTHOR("Oleh Kuzhylnyi <kuzhylol@gmail.com>");
MODULE_DESCRIPTION("Hynitron CST816X Touchscreen Driver");
MODULE_LICENSE("GPL");

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 KiB

View File

@ -0,0 +1,49 @@
/dts-v1/;
/plugin/;
/ {
fragment@0 {
target = <&i2c1>;
__overlay__ {
cst816s: cst816s@15 {
compatible = "hynitron,cst816s";
reg = <0x15>;
interrupt-parent = <&gpio>;
interrupts = <4 1>; /* IRQF_TRIGGER_RISING */
reset-gpios = <&gpio 17 1>; /* GPIO_ACTIVE_LOW */
touchscreen {
swipe_up {
cst816x,gesture = <0x1>;
linux,code = <0x115>; // BTN_FORWARD
};
swipe_down {
cst816x,gesture = <0x2>;
linux,code = <0x116>; // BTN_BACK
};
swipe_left {
cst816x,gesture = <0x3>;
linux,code = <0x110>; // BTN_LEFT
};
swipe_right {
cst816x,gesture = <0x4>;
linux,code = <0x111>; // BTN_RIGHT
};
single_tap {
cst816x,gesture = <0x5>;
linux,code = <0x14A>; // BTN_TOUCH
};
long_press {
cst816x,gesture = <0xC>;
linux,code = <0x14E>; // BTN_TOOL_TRIPLETAP
};
};
};
};
};
};

15
device_tree/Makefile Normal file
View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := get_dts_info.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

1
device_tree/README.txt Normal file
View File

@ -0,0 +1 @@
设备树获取节点属性实验

105
device_tree/get_dts_info.c Normal file
View File

@ -0,0 +1,105 @@
/*
* Copyright (C) 2022 - All Rights Reserved by
* EmbedFire LubanCat
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
struct device_node *led_test_device_node; //设备树节点
struct device_node *led_device_node; //节点
struct property *led_property; //定义属性结构体指针
int size = 0 ;
unsigned int out_values[18]; //保存读取得到 属性值
static const struct of_device_id of_gpio_leds_match[] = {
{.compatible = "get_dts_info_test"},
{},
};
static int get_dts_info_probe(struct platform_device *pdev)
{
int error_status = -1;
pr_info("%s\n",__func__);
/*获取DTS属性信息*/
led_test_device_node = of_find_node_by_path("/get_dts_info_test");
if(led_test_device_node == NULL)
{
printk(KERN_ALERT "get led_device_node failed ! \n");
return -1;
}
/*根据 led_test_device_node 设备节点结构体输出节点的基本信息*/
printk(KERN_ALERT "name: %s",led_test_device_node->name); //输出节点名
printk(KERN_ALERT "child name: %s",led_test_device_node->child->name); //输出子节点的节点名
/*获取 led_device_node 的子节点*/
led_device_node = of_get_next_child(led_test_device_node,NULL);
if(led_device_node == NULL)
{
printk(KERN_ALERT "\n get led_device_node failed ! \n");
return -1;
}
printk(KERN_ALERT "name: %s",led_device_node->name); //输出节点名
printk(KERN_ALERT "parent name: %s",led_device_node->parent->name); //输出父节点的节点名
/*获取 led_device_node 节点 的"compatible" 属性 */
led_property = of_find_property(led_device_node,"compatible",&size);
if(led_property == NULL)
{
printk(KERN_ALERT "get led_property failed ! \n");
return -1;
}
printk(KERN_ALERT "size = : %d",size); //实际读取得到的长度
printk(KERN_ALERT "name: %s",led_property->name); //输出属性名
printk(KERN_ALERT "length: %d",led_property->length); //输出属性长度
printk(KERN_ALERT "value : %s",(char*)led_property->value); //属性值
/*获取 reg 地址属性*/
error_status = of_property_read_u32_array(led_device_node,"reg",out_values, 2);
if(error_status != 0)
{
printk(KERN_ALERT "get out_values failed ! \n");
return -1;
}
printk(KERN_ALERT"0x%08X ", out_values[0]);
printk(KERN_ALERT"0x%08X ", out_values[1]);
return 0;
}
static int get_dts_info_remove(struct platform_device *pdev)
{
pr_info("%s\n",__func__);
return 0;
}
static struct platform_driver get_dts_info_driver = {
.probe = get_dts_info_probe,
.remove = get_dts_info_remove,
.driver = {
.name = "get_dts_info_test",
.of_match_table = of_match_ptr(of_gpio_leds_match),
},
};
module_platform_driver(get_dts_info_driver);
MODULE_DESCRIPTION("平台驱动:一个简单获取设备树属性的实验");
MODULE_LICENSE("GPL");

File diff suppressed because it is too large Load Diff

3622
device_tree/rk3568.dtsi Normal file

File diff suppressed because it is too large Load Diff

20
device_tree_led/Makefile Normal file
View File

@ -0,0 +1,20 @@
# 指定内核目录,根据实际编译内核时指定的输出目录,编译时没有指定,就是内核源码目录
KERNEL_DIR=../../kernel
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := led_test.o
out = test_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm $(out)

View File

@ -0,0 +1,3 @@
## Linux设备树——LED灯实验
参考教程https://doc.embedfire.com/linux/rk356x/driver/zh/latest/linux_driver/base_device_tree_rgb_led.html

227
device_tree_led/led_test.c Normal file
View File

@ -0,0 +1,227 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "led_test"
#define DEV_CNT (1)
/*定义 led 资源结构体,保存获取得到的节点信息以及转换后的虚拟寄存器地址*/
struct led_resource
{
struct device_node *device_node; //led的设备树节点
void __iomem *va_DR;
void __iomem *va_DDR;
};
static dev_t led_devno; //定义字符设备的设备号
static struct cdev led_chr_dev; //定义字符设备结构体chr_dev
struct class *class_led; //保存创建的类
struct device *device; // 保存创建的设备
struct device_node *led_test_device_node; //led_test的设备树节点结构体
/*定义led灯的led_resource 结构体,保存获取得到的节点信息*/
struct led_resource led_res;
/*字符设备操作函数集,open函数*/
static int led_chr_dev_open(struct inode *inode, struct file *filp)
{
printk("\n led_chr_dev_open \n");
return 0;
}
/*字符设备操作函数集,write函数*/
static ssize_t led_chr_dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
unsigned int register_data = 0; //暂存读取得到的寄存器数据
unsigned char write_data; //用于保存接收到的数据
int error = copy_from_user(&write_data, buf, cnt);
if (error < 0)
{
return -1;
}
/*设置led引脚 输出电平*/
if (write_data)
{
register_data |= ((unsigned int)0x1 << (23));
register_data |= ((unsigned int)0x1 << (7));
writel(register_data, led_res.va_DR); // GPIO0_C7引脚输出高电平,红灯灭
}
else
{
register_data |= ((unsigned int)0x1 << (23));
register_data &= ~((unsigned int)0x1 << (7));
writel(register_data, led_res.va_DR); // GPIO0_C7引脚输出低电平,红灯亮
}
return 0;
}
/*字符设备操作函数集*/
static struct file_operations led_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = led_chr_dev_open,
.write = led_chr_dev_write,
};
/*----------------平台驱动函数集-----------------*/
static int led_probe(struct platform_device *pdv)
{
int ret = -1; //保存错误状态码
unsigned int register_data = 0;
printk(KERN_EMERG "\t match successed \n");
/*获取led_test的设备树节点*/
led_test_device_node = of_find_node_by_path("/led_test");
if (led_test_device_node == NULL)
{
printk(KERN_ERR "\t get led_test failed! \n");
return -1;
}
/*获取led节点*/
led_res.device_node = of_find_node_by_name(led_test_device_node,"led");
if (led_res.device_node == NULL)
{
printk(KERN_ERR "\n get led_device_node failed ! \n");
return -1;
}
/*获取 reg 属性并转化为虚拟地址*/
led_res.va_DR = of_iomap(led_res.device_node, 0);
if(led_res.va_DR == NULL){
printk("of_iomap is error \n");
return -1;
}
led_res.va_DDR = of_iomap(led_res.device_node, 1);
if(led_res.va_DDR == NULL){
printk("of_iomap is error \n");
return -1;
}
// 设置模式寄存器:输出模式
register_data = readl(led_res.va_DDR);
register_data |= ((unsigned int)0x1 << (23));
register_data |= ((unsigned int)0x1 << (7));
writel(register_data,led_res.va_DDR);
// 设置置位寄存器:默认输出高电平
register_data = readl(led_res.va_DR);
register_data |= ((unsigned int)0x1 << (23));
register_data |= ((unsigned int)0x1 << (7));
writel(register_data, led_res.va_DR);
/*---------------------注册 字符设备部分-----------------*/
//第一步
//采用动态分配的方式,获取设备编号,次设备号为0,
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&led_devno, 0, DEV_CNT, DEV_NAME);
if (ret < 0)
{
printk("fail to alloc led_devno\n");
goto alloc_err;
}
//第二步
//关联字符设备结构体cdev与文件操作结构体file_operations
led_chr_dev.owner = THIS_MODULE;
cdev_init(&led_chr_dev, &led_chr_dev_fops);
//第三步
//添加设备至cdev_map散列表中
ret = cdev_add(&led_chr_dev, led_devno, DEV_CNT);
if (ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
//第四步
/*创建类 */
class_led = class_create(THIS_MODULE, DEV_NAME);
/*创建设备*/
device = device_create(class_led, NULL, led_devno, NULL, DEV_NAME);
return 0;
add_err:
//添加设备失败时,需要注销设备号
unregister_chrdev_region(led_devno, DEV_CNT);
printk("\n error! \n");
alloc_err:
return -1;
}
static const struct of_device_id led_ids[] = {
{.compatible = "fire,led_test"},
{/* sentinel */}
};
/*定义平台设备结构体*/
struct platform_driver led_platform_driver = {
.probe = led_probe,
.driver = {
.name = "leds-platform",
.owner = THIS_MODULE,
.of_match_table = led_ids,
}};
/*
*驱动初始化函数
*/
static int __init led_platform_driver_init(void)
{
int DriverState;
DriverState = platform_driver_register(&led_platform_driver);
printk(KERN_EMERG "\tDriverState is %d\n", DriverState);
return 0;
}
/*
*驱动注销函数
*/
static void __exit led_platform_driver_exit(void)
{
/*取消物理地址映射到虚拟地址*/
iounmap(led_res.va_DR);
iounmap(led_res.va_DDR);
/*删除设备*/
device_destroy(class_led, led_devno); //清除设备
class_destroy(class_led); //清除类
cdev_del(&led_chr_dev); //清除设备号
unregister_chrdev_region(led_devno, DEV_CNT); //取消注册字符设备
/*注销字符设备*/
platform_driver_unregister(&led_platform_driver);
printk(KERN_EMERG "dts test led exit!\n");
}
module_init(led_platform_driver_init);
module_exit(led_platform_driver_exit);
MODULE_LICENSE("GPL");

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf("led test_app\n");
/*判断输入的命令是否合法*/
if(argc != 2)
{
printf(" command error ! \n");
printf(" usage : sudo test_app num [num can be 0 or 1]\n");
return -1;
}
/*打开文件*/
int fd = open("/dev/led_test", O_RDWR);
if(fd < 0)
{
printf("open file : %s failed !\n", argv[0]);
return -1;
}
unsigned char commend = atoi(argv[1]); //将接收值转化为数字;
/*写入命令*/
int error = write(fd,&commend,sizeof(commend));
if(error < 0)
{
printf("write file error! \n");
close(fd);
/*判断是否关闭成功*/
}
/*关闭文件*/
error = close(fd);
if(error < 0)
{
printf("close file error! \n");
}
return 0;
}

View File

@ -0,0 +1,293 @@
/*
* LEDs driver for GPIOs
*
* Copyright (C) 2007 8D Technologies inc.
* Raphael Assenat <raph@8d.com>
* Copyright (C) 2008 Freescale Semiconductor, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
*/
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
struct gpio_led_data {
struct led_classdev cdev;
struct gpio_desc *gpiod;
u8 can_sleep;
u8 blinking;
gpio_blink_set_t platform_gpio_blink_set;
};
static inline struct gpio_led_data *
cdev_to_gpio_led_data(struct led_classdev *led_cdev)
{
return container_of(led_cdev, struct gpio_led_data, cdev);
}
static void gpio_led_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
int level;
if (value == LED_OFF)
level = 0;
else
level = 1;
if (led_dat->blinking) {
led_dat->platform_gpio_blink_set(led_dat->gpiod, level,
NULL, NULL);
led_dat->blinking = 0;
} else {
if (led_dat->can_sleep)
gpiod_set_value_cansleep(led_dat->gpiod, level);
else
gpiod_set_value(led_dat->gpiod, level);
}
}
static int gpio_led_set_blocking(struct led_classdev *led_cdev,
enum led_brightness value)
{
gpio_led_set(led_cdev, value);
return 0;
}
static int gpio_blink_set(struct led_classdev *led_cdev,
unsigned long *delay_on, unsigned long *delay_off)
{
struct gpio_led_data *led_dat = cdev_to_gpio_led_data(led_cdev);
led_dat->blinking = 1;
return led_dat->platform_gpio_blink_set(led_dat->gpiod, GPIO_LED_BLINK,
delay_on, delay_off);
}
static int create_gpio_led(const struct gpio_led *template,
struct gpio_led_data *led_dat, struct device *parent,
struct device_node *np, gpio_blink_set_t blink_set)
{
int ret, state;
led_dat->gpiod = template->gpiod;
if (!led_dat->gpiod) {
/*
* This is the legacy code path for platform code that
* still uses GPIO numbers. Ultimately we would like to get
* rid of this block completely.
*/
unsigned long flags = GPIOF_OUT_INIT_LOW;
/* skip leds that aren't available */
if (!gpio_is_valid(template->gpio)) {
dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
template->gpio, template->name);
return 0;
}
if (template->active_low)
flags |= GPIOF_ACTIVE_LOW;
ret = devm_gpio_request_one(parent, template->gpio, flags,
template->name);
if (ret < 0)
return ret;
led_dat->gpiod = gpio_to_desc(template->gpio);
if (!led_dat->gpiod)
return -EINVAL;
}
led_dat->cdev.name = template->name;
led_dat->cdev.default_trigger = template->default_trigger;
led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
if (!led_dat->can_sleep)
led_dat->cdev.brightness_set = gpio_led_set;
else
led_dat->cdev.brightness_set_blocking = gpio_led_set_blocking;
led_dat->blinking = 0;
if (blink_set) {
led_dat->platform_gpio_blink_set = blink_set;
led_dat->cdev.blink_set = gpio_blink_set;
}
if (template->default_state == LEDS_GPIO_DEFSTATE_KEEP) {
state = gpiod_get_value_cansleep(led_dat->gpiod);
if (state < 0)
return state;
} else {
state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
}
led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
if (!template->retain_state_suspended)
led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
if (template->panic_indicator)
led_dat->cdev.flags |= LED_PANIC_INDICATOR;
if (template->retain_state_shutdown)
led_dat->cdev.flags |= LED_RETAIN_AT_SHUTDOWN;
ret = gpiod_direction_output(led_dat->gpiod, state);
if (ret < 0)
return ret;
return devm_of_led_classdev_register(parent, np, &led_dat->cdev);
}
struct gpio_leds_priv {
int num_leds;
struct gpio_led_data leds[];
};
static inline int sizeof_gpio_leds_priv(int num_leds)
{
return sizeof(struct gpio_leds_priv) +
(sizeof(struct gpio_led_data) * num_leds);
}
static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct fwnode_handle *child;
struct gpio_leds_priv *priv;
int count, ret;
count = device_get_child_node_count(dev);
if (!count)
return ERR_PTR(-ENODEV);
priv = devm_kzalloc(dev, sizeof_gpio_leds_priv(count), GFP_KERNEL);
if (!priv)
return ERR_PTR(-ENOMEM);
device_for_each_child_node(dev, child) {
struct gpio_led_data *led_dat = &priv->leds[priv->num_leds];
struct gpio_led led = {};
const char *state = NULL;
struct device_node *np = to_of_node(child);
ret = fwnode_property_read_string(child, "label", &led.name);
if (ret && IS_ENABLED(CONFIG_OF) && np)
led.name = np->name;
if (!led.name) {
fwnode_handle_put(child);
return ERR_PTR(-EINVAL);
}
led.gpiod = devm_fwnode_get_gpiod_from_child(dev, NULL, child,
GPIOD_ASIS,
led.name);
if (IS_ERR(led.gpiod)) {
fwnode_handle_put(child);
return ERR_CAST(led.gpiod);
}
fwnode_property_read_string(child, "linux,default-trigger",
&led.default_trigger);
if (!fwnode_property_read_string(child, "default-state",
&state)) {
if (!strcmp(state, "keep"))
led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
else if (!strcmp(state, "on"))
led.default_state = LEDS_GPIO_DEFSTATE_ON;
else
led.default_state = LEDS_GPIO_DEFSTATE_OFF;
}
if (fwnode_property_present(child, "retain-state-suspended"))
led.retain_state_suspended = 1;
if (fwnode_property_present(child, "retain-state-shutdown"))
led.retain_state_shutdown = 1;
if (fwnode_property_present(child, "panic-indicator"))
led.panic_indicator = 1;
ret = create_gpio_led(&led, led_dat, dev, np, NULL);
if (ret < 0) {
fwnode_handle_put(child);
return ERR_PTR(ret);
}
led_dat->cdev.dev->of_node = np;
priv->num_leds++;
}
return priv;
}
static const struct of_device_id of_gpio_leds_match[] = {
{ .compatible = "gpio-leds", },
{},
};
MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
static int gpio_led_probe(struct platform_device *pdev)
{
struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
struct gpio_leds_priv *priv;
int i, ret = 0;
if (pdata && pdata->num_leds) {
priv = devm_kzalloc(&pdev->dev,
sizeof_gpio_leds_priv(pdata->num_leds),
GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->num_leds = pdata->num_leds;
for (i = 0; i < priv->num_leds; i++) {
ret = create_gpio_led(&pdata->leds[i], &priv->leds[i],
&pdev->dev, NULL,
pdata->gpio_blink_set);
if (ret < 0)
return ret;
}
} else {
priv = gpio_leds_create(pdev);
if (IS_ERR(priv))
return PTR_ERR(priv);
}
platform_set_drvdata(pdev, priv);
return 0;
}
static void gpio_led_shutdown(struct platform_device *pdev)
{
struct gpio_leds_priv *priv = platform_get_drvdata(pdev);
int i;
for (i = 0; i < priv->num_leds; i++) {
struct gpio_led_data *led = &priv->leds[i];
if (!(led->cdev.flags & LED_RETAIN_AT_SHUTDOWN))
gpio_led_set(&led->cdev, LED_OFF);
}
}
static struct platform_driver gpio_led_driver = {
.probe = gpio_led_probe,
.shutdown = gpio_led_shutdown,
.driver = {
.name = "leds-gpio",
.of_match_table = of_gpio_leds_match,
},
};
module_platform_driver(gpio_led_driver);
MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
MODULE_DESCRIPTION("GPIO LED driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:leds-gpio");

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 - All Rights Reserved by
* EmbedFire LubanCat
*/
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
&{/leds} {
status = "okay";
led_test: led_test{
label = "led_test";
default-state = "on";
//gpios = <&gpio0 RK_PC7 GPIO_ACTIVE_LOW>;
gpios = <&gpio0 RK_PA6 GPIO_ACTIVE_HIGH>;
pinctrl-names = "default";
pinctrl-0 = <&led_test_pin>;
};
};
&{/pinctrl} {
led_test {
led_test_pin: led_test_pin {
rockchip,pins = <0 RK_PA6 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};

1
fbtft-st7796-5.10/Kbuild Normal file
View File

@ -0,0 +1 @@
obj-m += fb_st7796.o

View File

@ -0,0 +1,10 @@
KDIR ?= /lib/modules/`uname -r`/build
default:
$(MAKE) -C $(KDIR) M=$$PWD
install:
$(MAKE) -C $(KDIR) M=$$PWD modules_install
clean:
$(MAKE) -C $(KDIR) M=$$PWD clean

View File

@ -0,0 +1,13 @@
# FBTFT driver for ST7796 (?)
Tested with linux kernel 5.10.160
**Caution**: The store I bought the display from lists the panel as ST7796U, but when I try to use [initialization sequence for ST7796](https://github.com/birdtechstep/tinydrm/blob/master/st7796.c#L112-L196) it didn't work. However I used the sequence for ST7789 and it worked, but I still had to use [ST7796's screen orientation settings](https://github.com/birdtechstep/tinydrm/blob/master/st7796.c#L179-L196), so I'm not sure which controller it actually is. If it doesn't work for you, you can override it in `init` property.
## Usage
```sh
make
make install
depmod -A
```

View File

@ -0,0 +1,279 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* FB driver for the ST7796 LCD Controller
*
* Copyright (C) 2015 Dennis Menschel
*/
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <video/mipi_display.h>
#include "fbtft.h"
#define DRVNAME "fb_st7796"
#define DEFAULT_GAMMA \
"70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25\n" \
"70 2C 2E 15 10 09 48 33 53 0B 19 18 20 25"
#define HSD20_IPS_GAMMA \
"D0 05 0A 09 08 05 2E 44 45 0F 17 16 2B 33\n" \
"D0 05 0A 09 08 05 2E 43 45 0F 16 16 2B 33"
#define HSD20_IPS 1
/**
* enum st7796_command - st7796 display controller commands
*
* @PORCTRL: porch setting
* @GCTRL: gate control
* @VCOMS: VCOM setting
* @VDVVRHEN: VDV and VRH command enable
* @VRHS: VRH set
* @VDVS: VDV set
* @VCMOFSET: VCOM offset set
* @PWCTRL1: power control 1
* @PVGAMCTRL: positive voltage gamma control
* @NVGAMCTRL: negative voltage gamma control
*
* The command names are the same as those found in the datasheet to ease
* looking up their semantics and usage.
*
* Note that the st7796 display controller offers quite a few more commands
* which have been omitted from this list as they are not used at the moment.
* Furthermore, commands that are compliant with the MIPI DCS have been left
* out as well to avoid duplicate entries.
*/
enum st7796_command {
PORCTRL = 0xB2,
GCTRL = 0xB7,
VCOMS = 0xBB,
VDVVRHEN = 0xC2,
VRHS = 0xC3,
VDVS = 0xC4,
VCMOFSET = 0xC5,
PWCTRL1 = 0xD0,
PVGAMCTRL = 0xE0,
NVGAMCTRL = 0xE1,
};
#define MADCTL_BGR BIT(3) /* bitmask for RGB/BGR order */
#define MADCTL_MV BIT(5) /* bitmask for page/column order */
#define MADCTL_MX BIT(6) /* bitmask for column address order */
#define MADCTL_MY BIT(7) /* bitmask for page address order */
/**
* init_display() - initialize the display controller
*
* @par: FBTFT parameter object
*
* Most of the commands in this init function set their parameters to the
* same default values which are already in place after the display has been
* powered up. (The main exception to this rule is the pixel format which
* would default to 18 instead of 16 bit per pixel.)
* Nonetheless, this sequence can be used as a template for concrete
* displays which usually need some adjustments.
*
* Return: 0 on success, < 0 if error occurred.
*/
static int init_display(struct fbtft_par *par)
{
/* turn off sleep mode */
write_reg(par, MIPI_DCS_EXIT_SLEEP_MODE);
mdelay(120);
/* set pixel format to RGB-565 */
write_reg(par, MIPI_DCS_SET_PIXEL_FORMAT, MIPI_DCS_PIXEL_FMT_16BIT);
if (HSD20_IPS)
write_reg(par, PORCTRL, 0x05, 0x05, 0x00, 0x33, 0x33);
else
write_reg(par, PORCTRL, 0x08, 0x08, 0x00, 0x22, 0x22);
/*
* VGH = 13.26V
* VGL = -10.43V
*/
if (HSD20_IPS)
write_reg(par, GCTRL, 0x75);
else
write_reg(par, GCTRL, 0x35);
/*
* VDV and VRH register values come from command write
* (instead of NVM)
*/
write_reg(par, VDVVRHEN, 0x01, 0xFF);
/*
* VAP = 4.1V + (VCOM + VCOM offset + 0.5 * VDV)
* VAN = -4.1V + (VCOM + VCOM offset + 0.5 * VDV)
*/
if (HSD20_IPS)
write_reg(par, VRHS, 0x13);
else
write_reg(par, VRHS, 0x0B);
/* VDV = 0V */
write_reg(par, VDVS, 0x20);
/* VCOM = 0.9V */
if (HSD20_IPS)
write_reg(par, VCOMS, 0x22);
else
write_reg(par, VCOMS, 0x20);
/* VCOM offset = 0V */
write_reg(par, VCMOFSET, 0x20);
/*
* AVDD = 6.8V
* AVCL = -4.8V
* VDS = 2.3V
*/
write_reg(par, PWCTRL1, 0xA4, 0xA1);
write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
if (HSD20_IPS)
write_reg(par, MIPI_DCS_ENTER_INVERT_MODE);
return 0;
}
/**
* set_var() - apply LCD properties like rotation and BGR mode
*
* @par: FBTFT parameter object
*
* Return: 0 on success, < 0 if error occurred.
*/
static int set_var(struct fbtft_par *par)
{
u8 madctl_par = 0;
if (par->bgr)
madctl_par |= MADCTL_BGR;
switch (par->info->var.rotate) {
case 0:
madctl_par |= MADCTL_MX;
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
break;
case 90:
madctl_par |= MADCTL_MV;
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
break;
case 180:
madctl_par |= MADCTL_MY;
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
break;
case 270:
madctl_par |= (MADCTL_MV | MADCTL_MX | MADCTL_MY);
write_reg(par, MIPI_DCS_SET_ADDRESS_MODE, madctl_par);
break;
}
return 0;
}
/**
* set_gamma() - set gamma curves
*
* @par: FBTFT parameter object
* @curves: gamma curves
*
* Before the gamma curves are applied, they are preprocessed with a bitmask
* to ensure syntactically correct input for the display controller.
* This implies that the curves input parameter might be changed by this
* function and that illegal gamma values are auto-corrected and not
* reported as errors.
*
* Return: 0 on success, < 0 if error occurred.
*/
static int set_gamma(struct fbtft_par *par, u32 *curves)
{
int i;
int j;
int c; /* curve index offset */
/*
* Bitmasks for gamma curve command parameters.
* The masks are the same for both positive and negative voltage
* gamma curves.
*/
static const u8 gamma_par_mask[] = {
0xFF, /* V63[3:0], V0[3:0]*/
0x3F, /* V1[5:0] */
0x3F, /* V2[5:0] */
0x1F, /* V4[4:0] */
0x1F, /* V6[4:0] */
0x3F, /* J0[1:0], V13[3:0] */
0x7F, /* V20[6:0] */
0x77, /* V36[2:0], V27[2:0] */
0x7F, /* V43[6:0] */
0x3F, /* J1[1:0], V50[3:0] */
0x1F, /* V57[4:0] */
0x1F, /* V59[4:0] */
0x3F, /* V61[5:0] */
0x3F, /* V62[5:0] */
};
for (i = 0; i < par->gamma.num_curves; i++) {
c = i * par->gamma.num_values;
for (j = 0; j < par->gamma.num_values; j++)
curves[c + j] &= gamma_par_mask[j];
write_reg(par, PVGAMCTRL + i,
curves[c + 0], curves[c + 1], curves[c + 2],
curves[c + 3], curves[c + 4], curves[c + 5],
curves[c + 6], curves[c + 7], curves[c + 8],
curves[c + 9], curves[c + 10], curves[c + 11],
curves[c + 12], curves[c + 13]);
}
return 0;
}
/**
* blank() - blank the display
*
* @par: FBTFT parameter object
* @on: whether to enable or disable blanking the display
*
* Return: 0 on success, < 0 if error occurred.
*/
static int blank(struct fbtft_par *par, bool on)
{
if (on)
write_reg(par, MIPI_DCS_SET_DISPLAY_OFF);
else
write_reg(par, MIPI_DCS_SET_DISPLAY_ON);
return 0;
}
static struct fbtft_display display = {
.regwidth = 8,
.width = 320,
.height = 480,
.gamma_num = 2,
.gamma_len = 14,
.gamma = HSD20_IPS_GAMMA,
.fbtftops = {
.init_display = init_display,
.set_var = set_var,
.set_gamma = set_gamma,
.blank = blank,
},
};
FBTFT_REGISTER_DRIVER(DRVNAME, "sitronix,st7796", &display);
MODULE_ALIAS("spi:" DRVNAME);
MODULE_ALIAS("platform:" DRVNAME);
MODULE_ALIAS("spi:st7796");
MODULE_ALIAS("platform:st7796");
MODULE_DESCRIPTION("FB driver for the ST7796 LCD Controller");
MODULE_AUTHOR("Dennis Menschel");
MODULE_LICENSE("GPL");

423
fbtft-st7796-5.10/fbtft.h Normal file
View File

@ -0,0 +1,423 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/* Copyright (C) 2013 Noralf Tronnes */
#ifndef __LINUX_FBTFT_H
#define __LINUX_FBTFT_H
#include <linux/fb.h>
#include <linux/spinlock.h>
#include <linux/spi/spi.h>
#include <linux/platform_device.h>
#define FBTFT_ONBOARD_BACKLIGHT 2
#define FBTFT_GPIO_NO_MATCH 0xFFFF
#define FBTFT_GPIO_NAME_SIZE 32
#define FBTFT_MAX_INIT_SEQUENCE 512
#define FBTFT_GAMMA_MAX_VALUES_TOTAL 128
#define FBTFT_OF_INIT_CMD BIT(24)
#define FBTFT_OF_INIT_DELAY BIT(25)
/**
* struct fbtft_gpio - Structure that holds one pinname to gpio mapping
* @name: pinname (reset, dc, etc.)
* @gpio: GPIO number
*
*/
struct fbtft_gpio {
char name[FBTFT_GPIO_NAME_SIZE];
struct gpio_desc *gpio;
};
struct fbtft_par;
/**
* struct fbtft_ops - FBTFT operations structure
* @write: Writes to interface bus
* @read: Reads from interface bus
* @write_vmem: Writes video memory to display
* @write_reg: Writes to controller register
* @set_addr_win: Set the GRAM update window
* @reset: Reset the LCD controller
* @mkdirty: Marks display lines for update
* @update_display: Updates the display
* @init_display: Initializes the display
* @blank: Blank the display (optional)
* @request_gpios_match: Do pinname to gpio matching
* @request_gpios: Request gpios from the kernel
* @free_gpios: Free previously requested gpios
* @verify_gpios: Verify that necessary gpios is present (optional)
* @register_backlight: Used to register backlight device (optional)
* @unregister_backlight: Unregister backlight device (optional)
* @set_var: Configure LCD with values from variables like @rotate and @bgr
* (optional)
* @set_gamma: Set Gamma curve (optional)
*
* Most of these operations have default functions assigned to them in
* fbtft_framebuffer_alloc()
*/
struct fbtft_ops {
int (*write)(struct fbtft_par *par, void *buf, size_t len);
int (*read)(struct fbtft_par *par, void *buf, size_t len);
int (*write_vmem)(struct fbtft_par *par, size_t offset, size_t len);
void (*write_register)(struct fbtft_par *par, int len, ...);
void (*set_addr_win)(struct fbtft_par *par,
int xs, int ys, int xe, int ye);
void (*reset)(struct fbtft_par *par);
void (*mkdirty)(struct fb_info *info, int from, int to);
void (*update_display)(struct fbtft_par *par,
unsigned int start_line, unsigned int end_line);
int (*init_display)(struct fbtft_par *par);
int (*blank)(struct fbtft_par *par, bool on);
unsigned long (*request_gpios_match)(struct fbtft_par *par,
const struct fbtft_gpio *gpio);
int (*request_gpios)(struct fbtft_par *par);
int (*verify_gpios)(struct fbtft_par *par);
void (*register_backlight)(struct fbtft_par *par);
void (*unregister_backlight)(struct fbtft_par *par);
int (*set_var)(struct fbtft_par *par);
int (*set_gamma)(struct fbtft_par *par, u32 *curves);
};
/**
* struct fbtft_display - Describes the display properties
* @width: Width of display in pixels
* @height: Height of display in pixels
* @regwidth: LCD Controller Register width in bits
* @buswidth: Display interface bus width in bits
* @backlight: Backlight type.
* @fbtftops: FBTFT operations provided by driver or device (platform_data)
* @bpp: Bits per pixel
* @fps: Frames per second
* @txbuflen: Size of transmit buffer
* @init_sequence: Pointer to LCD initialization array
* @gamma: String representation of Gamma curve(s)
* @gamma_num: Number of Gamma curves
* @gamma_len: Number of values per Gamma curve
* @debug: Initial debug value
*
* This structure is not stored by FBTFT except for init_sequence.
*/
struct fbtft_display {
unsigned int width;
unsigned int height;
unsigned int regwidth;
unsigned int buswidth;
unsigned int backlight;
struct fbtft_ops fbtftops;
unsigned int bpp;
unsigned int fps;
int txbuflen;
const s16 *init_sequence;
char *gamma;
int gamma_num;
int gamma_len;
unsigned long debug;
};
/**
* struct fbtft_platform_data - Passes display specific data to the driver
* @display: Display properties
* @gpios: Pointer to an array of pinname to gpio mappings
* @rotate: Display rotation angle
* @bgr: LCD Controller BGR bit
* @fps: Frames per second (this will go away, use @fps in @fbtft_display)
* @txbuflen: Size of transmit buffer
* @startbyte: When set, enables use of Startbyte in transfers
* @gamma: String representation of Gamma curve(s)
* @extra: A way to pass extra info
*/
struct fbtft_platform_data {
struct fbtft_display display;
unsigned int rotate;
bool bgr;
unsigned int fps;
int txbuflen;
u8 startbyte;
char *gamma;
void *extra;
};
/**
* struct fbtft_par - Main FBTFT data structure
*
* This structure holds all relevant data to operate the display
*
* See sourcefile for documentation since nested structs is not
* supported by kernel-doc.
*
*/
/* @spi: Set if it is a SPI device
* @pdev: Set if it is a platform device
* @info: Pointer to framebuffer fb_info structure
* @pdata: Pointer to platform data
* @ssbuf: Not used
* @pseudo_palette: Used by fb_set_colreg()
* @txbuf.buf: Transmit buffer
* @txbuf.len: Transmit buffer length
* @buf: Small buffer used when writing init data over SPI
* @startbyte: Used by some controllers when in SPI mode.
* Format: 6 bit Device id + RS bit + RW bit
* @fbtftops: FBTFT operations provided by driver or device (platform_data)
* @dirty_lock: Protects dirty_lines_start and dirty_lines_end
* @dirty_lines_start: Where to begin updating display
* @dirty_lines_end: Where to end updating display
* @gpio.reset: GPIO used to reset display
* @gpio.dc: Data/Command signal, also known as RS
* @gpio.rd: Read latching signal
* @gpio.wr: Write latching signal
* @gpio.latch: Bus latch signal, eg. 16->8 bit bus latch
* @gpio.cs: LCD Chip Select with parallel interface bus
* @gpio.db[16]: Parallel databus
* @gpio.led[16]: Led control signals
* @gpio.aux[16]: Auxiliary signals, not used by core
* @init_sequence: Pointer to LCD initialization array
* @gamma.lock: Mutex for Gamma curve locking
* @gamma.curves: Pointer to Gamma curve array
* @gamma.num_values: Number of values per Gamma curve
* @gamma.num_curves: Number of Gamma curves
* @debug: Pointer to debug value
* @current_debug:
* @first_update_done: Used to only time the first display update
* @update_time: Used to calculate 'fps' in debug output
* @bgr: BGR mode/\n
* @extra: Extra info needed by driver
*/
struct fbtft_par {
struct spi_device *spi;
struct platform_device *pdev;
struct fb_info *info;
struct fbtft_platform_data *pdata;
u16 *ssbuf;
u32 pseudo_palette[16];
struct {
void *buf;
size_t len;
} txbuf;
u8 *buf;
u8 startbyte;
struct fbtft_ops fbtftops;
spinlock_t dirty_lock;
unsigned int dirty_lines_start;
unsigned int dirty_lines_end;
struct {
struct gpio_desc *reset;
struct gpio_desc *dc;
struct gpio_desc *rd;
struct gpio_desc *wr;
struct gpio_desc *latch;
struct gpio_desc *cs;
struct gpio_desc *db[16];
struct gpio_desc *led[16];
struct gpio_desc *aux[16];
} gpio;
const s16 *init_sequence;
struct {
struct mutex lock;
u32 *curves;
int num_values;
int num_curves;
} gamma;
unsigned long debug;
bool first_update_done;
ktime_t update_time;
bool bgr;
void *extra;
bool polarity;
};
#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int))
#define write_reg(par, ...) \
((par)->fbtftops.write_register(par, NUMARGS(__VA_ARGS__), __VA_ARGS__))
/* fbtft-core.c */
int fbtft_write_buf_dc(struct fbtft_par *par, void *buf, size_t len, int dc);
__printf(5, 6)
void fbtft_dbg_hex(const struct device *dev, int groupsize,
void *buf, size_t len, const char *fmt, ...);
struct fb_info *fbtft_framebuffer_alloc(struct fbtft_display *display,
struct device *dev,
struct fbtft_platform_data *pdata);
void fbtft_framebuffer_release(struct fb_info *info);
int fbtft_register_framebuffer(struct fb_info *fb_info);
int fbtft_unregister_framebuffer(struct fb_info *fb_info);
void fbtft_register_backlight(struct fbtft_par *par);
void fbtft_unregister_backlight(struct fbtft_par *par);
int fbtft_init_display(struct fbtft_par *par);
int fbtft_probe_common(struct fbtft_display *display, struct spi_device *sdev,
struct platform_device *pdev);
int fbtft_remove_common(struct device *dev, struct fb_info *info);
/* fbtft-io.c */
int fbtft_write_spi(struct fbtft_par *par, void *buf, size_t len);
int fbtft_write_spi_emulate_9(struct fbtft_par *par, void *buf, size_t len);
int fbtft_read_spi(struct fbtft_par *par, void *buf, size_t len);
int fbtft_write_gpio8_wr(struct fbtft_par *par, void *buf, size_t len);
int fbtft_write_gpio16_wr(struct fbtft_par *par, void *buf, size_t len);
int fbtft_write_gpio16_wr_latched(struct fbtft_par *par, void *buf, size_t len);
/* fbtft-bus.c */
int fbtft_write_vmem8_bus8(struct fbtft_par *par, size_t offset, size_t len);
int fbtft_write_vmem16_bus16(struct fbtft_par *par, size_t offset, size_t len);
int fbtft_write_vmem16_bus8(struct fbtft_par *par, size_t offset, size_t len);
int fbtft_write_vmem16_bus9(struct fbtft_par *par, size_t offset, size_t len);
void fbtft_write_reg8_bus8(struct fbtft_par *par, int len, ...);
void fbtft_write_reg8_bus9(struct fbtft_par *par, int len, ...);
void fbtft_write_reg16_bus8(struct fbtft_par *par, int len, ...);
void fbtft_write_reg16_bus16(struct fbtft_par *par, int len, ...);
#define FBTFT_REGISTER_DRIVER(_name, _compatible, _display) \
\
static int fbtft_driver_probe_spi(struct spi_device *spi) \
{ \
return fbtft_probe_common(_display, spi, NULL); \
} \
\
static int fbtft_driver_remove_spi(struct spi_device *spi) \
{ \
struct fb_info *info = spi_get_drvdata(spi); \
\
return fbtft_remove_common(&spi->dev, info); \
} \
\
static int fbtft_driver_probe_pdev(struct platform_device *pdev) \
{ \
return fbtft_probe_common(_display, NULL, pdev); \
} \
\
static int fbtft_driver_remove_pdev(struct platform_device *pdev) \
{ \
struct fb_info *info = platform_get_drvdata(pdev); \
\
return fbtft_remove_common(&pdev->dev, info); \
} \
\
static const struct of_device_id dt_ids[] = { \
{ .compatible = _compatible }, \
{}, \
}; \
\
MODULE_DEVICE_TABLE(of, dt_ids); \
\
\
static struct spi_driver fbtft_driver_spi_driver = { \
.driver = { \
.name = _name, \
.of_match_table = dt_ids, \
}, \
.probe = fbtft_driver_probe_spi, \
.remove = fbtft_driver_remove_spi, \
}; \
\
static struct platform_driver fbtft_driver_platform_driver = { \
.driver = { \
.name = _name, \
.owner = THIS_MODULE, \
.of_match_table = dt_ids, \
}, \
.probe = fbtft_driver_probe_pdev, \
.remove = fbtft_driver_remove_pdev, \
}; \
\
static int __init fbtft_driver_module_init(void) \
{ \
int ret; \
\
ret = spi_register_driver(&fbtft_driver_spi_driver); \
if (ret < 0) \
return ret; \
ret = platform_driver_register(&fbtft_driver_platform_driver); \
if (ret < 0) \
spi_unregister_driver(&fbtft_driver_spi_driver); \
return ret; \
} \
\
static void __exit fbtft_driver_module_exit(void) \
{ \
spi_unregister_driver(&fbtft_driver_spi_driver); \
platform_driver_unregister(&fbtft_driver_platform_driver); \
} \
\
module_init(fbtft_driver_module_init); \
module_exit(fbtft_driver_module_exit);
/* Debug macros */
/* shorthand debug levels */
#define DEBUG_LEVEL_1 DEBUG_REQUEST_GPIOS
#define DEBUG_LEVEL_2 (DEBUG_LEVEL_1 | DEBUG_DRIVER_INIT_FUNCTIONS \
| DEBUG_TIME_FIRST_UPDATE)
#define DEBUG_LEVEL_3 (DEBUG_LEVEL_2 | DEBUG_RESET | DEBUG_INIT_DISPLAY \
| DEBUG_BLANK | DEBUG_REQUEST_GPIOS \
| DEBUG_FREE_GPIOS \
| DEBUG_VERIFY_GPIOS \
| DEBUG_BACKLIGHT | DEBUG_SYSFS)
#define DEBUG_LEVEL_4 (DEBUG_LEVEL_2 | DEBUG_FB_READ | DEBUG_FB_WRITE \
| DEBUG_FB_FILLRECT \
| DEBUG_FB_COPYAREA \
| DEBUG_FB_IMAGEBLIT | DEBUG_FB_BLANK)
#define DEBUG_LEVEL_5 (DEBUG_LEVEL_3 | DEBUG_UPDATE_DISPLAY)
#define DEBUG_LEVEL_6 (DEBUG_LEVEL_4 | DEBUG_LEVEL_5)
#define DEBUG_LEVEL_7 0xFFFFFFFF
#define DEBUG_DRIVER_INIT_FUNCTIONS BIT(3)
#define DEBUG_TIME_FIRST_UPDATE BIT(4)
#define DEBUG_TIME_EACH_UPDATE BIT(5)
#define DEBUG_DEFERRED_IO BIT(6)
#define DEBUG_FBTFT_INIT_FUNCTIONS BIT(7)
/* fbops */
#define DEBUG_FB_READ BIT(8)
#define DEBUG_FB_WRITE BIT(9)
#define DEBUG_FB_FILLRECT BIT(10)
#define DEBUG_FB_COPYAREA BIT(11)
#define DEBUG_FB_IMAGEBLIT BIT(12)
#define DEBUG_FB_SETCOLREG BIT(13)
#define DEBUG_FB_BLANK BIT(14)
#define DEBUG_SYSFS BIT(16)
/* fbtftops */
#define DEBUG_BACKLIGHT BIT(17)
#define DEBUG_READ BIT(18)
#define DEBUG_WRITE BIT(19)
#define DEBUG_WRITE_VMEM BIT(20)
#define DEBUG_WRITE_REGISTER BIT(21)
#define DEBUG_SET_ADDR_WIN BIT(22)
#define DEBUG_RESET BIT(23)
#define DEBUG_MKDIRTY BIT(24)
#define DEBUG_UPDATE_DISPLAY BIT(25)
#define DEBUG_INIT_DISPLAY BIT(26)
#define DEBUG_BLANK BIT(27)
#define DEBUG_REQUEST_GPIOS BIT(28)
#define DEBUG_FREE_GPIOS BIT(29)
#define DEBUG_REQUEST_GPIOS_MATCH BIT(30)
#define DEBUG_VERIFY_GPIOS BIT(31)
#define fbtft_init_dbg(dev, format, arg...) \
do { \
if (unlikely((dev)->platform_data && \
(((struct fbtft_platform_data *)(dev)->platform_data)->display.debug & DEBUG_DRIVER_INIT_FUNCTIONS))) \
dev_info(dev, format, ##arg); \
} while (0)
#define fbtft_par_dbg(level, par, format, arg...) \
do { \
if (unlikely((par)->debug & (level))) \
dev_info((par)->info->device, format, ##arg); \
} while (0)
#define fbtft_par_dbg_hex(level, par, dev, type, buf, num, format, arg...) \
do { \
if (unlikely((par)->debug & (level))) \
fbtft_dbg_hex(dev, sizeof(type), buf,\
(num) * sizeof(type), format, ##arg); \
} while (0)
#endif /* __LINUX_FBTFT_H */

BIN
framework_drm/modetest Executable file

Binary file not shown.

View File

@ -0,0 +1,18 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=/opt/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := board_gpiod.o board_gpiod.o
out = bin/led_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) led_app.c
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm $(out)

View File

@ -0,0 +1,189 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "wifi_power_en"
#define DEV_CNT (1)
//定义字符设备的设备号
static dev_t led_devno;
//定义字符设备结构体chr_dev
static struct cdev led_chr_dev;
struct class *class_led; //保存创建的类
struct device *device; // 保存创建的设备
struct device_node *led_device_node; //led的设备树节点
int led; // 保存获取得到引脚编号
/*字符设备操作函数集,open函数*/
static int led_chr_dev_open(struct inode *inode, struct file *filp)
{
printk("open \n");
return 0;
}
/*字符设备操作函数集,write函数*/
static ssize_t led_chr_dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
unsigned char write_data; //用于保存接收到的数据
int error = copy_from_user(&write_data, buf, cnt);
if(error < 0) {
printk("led_test write failed \n");
return -1;
}
/*设置GPIO 输出电平 具体引脚根据板卡原理图*/
if(write_data)
{
gpio_direction_output(led, 1); // 引脚输出高电平,红灯灭
}
else
{
gpio_direction_output(led, 0); // 引脚输出底电平,红灯亮
}
return 0;
}
/*字符设备操作函数集*/
static struct file_operations led_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = led_chr_dev_open,
.write = led_chr_dev_write,
};
/*----------------平台驱动函数集-----------------*/
static int led_probe(struct platform_device *pdv)
{
int ret = 0; //用于保存申请设备号的结果
printk("match successed\n");
/*获取RGB的设备树节点*/
led_device_node = of_find_node_by_path("/wifi-pwrseq");
if(led_device_node == NULL)
{
printk(KERN_EMERG "\t get led_test failed! \n");
}
led = of_get_named_gpio(led_device_node, "reset-gpios", 0);
printk("led = %d \n",led);
/*设置gpio输出高电平*/
gpio_direction_output(led, 1);
/*---------------------注册 字符设备部分-----------------*/
//第一步
//采用动态分配的方式,获取设备编号,次设备号为0,
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&led_devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk("fail to alloc led_devno\n");
goto alloc_err;
}
//第二步
//关联字符设备结构体cdev与文件操作结构体file_operations
led_chr_dev.owner = THIS_MODULE;
cdev_init(&led_chr_dev, &led_chr_dev_fops);
//第三步
//添加设备至cdev_map散列表中
ret = cdev_add(&led_chr_dev, led_devno, DEV_CNT);
if(ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
//第四步
/*创建类 */
class_led = class_create(THIS_MODULE, DEV_NAME);
/*创建设备*/
device = device_create(class_led, NULL, led_devno, NULL, DEV_NAME);
return 0;
add_err:
//添加设备失败时,需要注销设备号
unregister_chrdev_region(led_devno, DEV_CNT);
printk("\n error! \n");
alloc_err:
return -1;
}
static const struct of_device_id led_ids[] = {
{ .compatible = "Yura,aic8800_usb_power_en"},
{ /* sentinel */ }
};
/*定义平台设备结构体*/
struct platform_driver led_platform_driver = {
.probe = led_probe,
.driver = {
.name = "leds-platform",
.owner = THIS_MODULE,
.of_match_table = led_ids,
}
};
/*
*驱动初始化函数
*/
static int __init led_platform_driver_init(void)
{
int DriverState;
DriverState = platform_driver_register(&led_platform_driver);
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0;
}
/*
*驱动注销函数
*/
static void __exit led_platform_driver_exit(void)
{
printk(KERN_EMERG "led_test exit!\n");
/*删除设备*/
device_destroy(class_led, led_devno); //清除设备
class_destroy(class_led); //清除类
cdev_del(&led_chr_dev); //清除设备号
unregister_chrdev_region(led_devno, DEV_CNT); //取消注册字符设备
platform_driver_unregister(&led_platform_driver);
}
module_init(led_platform_driver_init);
module_exit(led_platform_driver_exit);
MODULE_LICENSE("GPL");

Binary file not shown.

View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char *argv[])
{
printf("led_tiny test\n");
/*判断输入的命令是否合法*/
if(argc != 3)
{
printf(" command error ! \n");
printf(" usage : sudo test_app num [num can be 0 or 1]\n");
return -1;
}
/*打开文件*/
int fd = open(argv[1], O_RDWR);
if(fd < 0)
{
printf("open file : %s failed !\n", argv[0]);
return -1;
}
unsigned char command = atoi(argv[2]); //将受到的命令值转化为数字;
/*写入命令*/
int error = write(fd,&command,sizeof(command));
if(error < 0)
{
printf("write file error! \n");
close(fd);
/*判断是否关闭成功*/
}
/*关闭文件*/
error = close(fd);
if(error < 0)
{
printf("close file error! \n");
}
return 0;
}

View File

@ -0,0 +1,189 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
/*------------------字符设备内容----------------------*/
#define DEV_NAME "led_test"
#define DEV_CNT (1)
//定义字符设备的设备号
static dev_t led_devno;
//定义字符设备结构体chr_dev
static struct cdev led_chr_dev;
struct class *class_led; //保存创建的类
struct device *device; // 保存创建的设备
struct device_node *led_device_node; //led的设备树节点
int led; // 保存获取得到引脚编号
/*字符设备操作函数集,open函数*/
static int led_chr_dev_open(struct inode *inode, struct file *filp)
{
printk("open \n");
return 0;
}
/*字符设备操作函数集,write函数*/
static ssize_t led_chr_dev_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
unsigned char write_data; //用于保存接收到的数据
int error = copy_from_user(&write_data, buf, cnt);
if(error < 0) {
printk("led_test write failed \n");
return -1;
}
/*设置GPIO 输出电平 具体引脚根据板卡原理图*/
if(write_data)
{
gpio_direction_output(led, 1); // 引脚输出高电平,红灯灭
}
else
{
gpio_direction_output(led, 0); // 引脚输出底电平,红灯亮
}
return 0;
}
/*字符设备操作函数集*/
static struct file_operations led_chr_dev_fops =
{
.owner = THIS_MODULE,
.open = led_chr_dev_open,
.write = led_chr_dev_write,
};
/*----------------平台驱动函数集-----------------*/
static int led_probe(struct platform_device *pdv)
{
int ret = 0; //用于保存申请设备号的结果
printk("match successed\n");
/*获取RGB的设备树节点*/
led_device_node = of_find_node_by_path("/led_test");
if(led_device_node == NULL)
{
printk(KERN_EMERG "\t get led_test failed! \n");
}
led = of_get_named_gpio(led_device_node, "gpios", 0);
printk("led = %d \n",led);
/*设置gpio输出高电平*/
gpio_direction_output(led, 1);
/*---------------------注册 字符设备部分-----------------*/
//第一步
//采用动态分配的方式,获取设备编号,次设备号为0,
//DEV_CNT为1,当前只申请一个设备编号
ret = alloc_chrdev_region(&led_devno, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk("fail to alloc led_devno\n");
goto alloc_err;
}
//第二步
//关联字符设备结构体cdev与文件操作结构体file_operations
led_chr_dev.owner = THIS_MODULE;
cdev_init(&led_chr_dev, &led_chr_dev_fops);
//第三步
//添加设备至cdev_map散列表中
ret = cdev_add(&led_chr_dev, led_devno, DEV_CNT);
if(ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
//第四步
/*创建类 */
class_led = class_create(THIS_MODULE, DEV_NAME);
/*创建设备*/
device = device_create(class_led, NULL, led_devno, NULL, DEV_NAME);
return 0;
add_err:
//添加设备失败时,需要注销设备号
unregister_chrdev_region(led_devno, DEV_CNT);
printk("\n error! \n");
alloc_err:
return -1;
}
static const struct of_device_id led_ids[] = {
{ .compatible = "fire,led_test"},
{ /* sentinel */ }
};
/*定义平台设备结构体*/
struct platform_driver led_platform_driver = {
.probe = led_probe,
.driver = {
.name = "leds-platform",
.owner = THIS_MODULE,
.of_match_table = led_ids,
}
};
/*
*驱动初始化函数
*/
static int __init led_platform_driver_init(void)
{
int DriverState;
DriverState = platform_driver_register(&led_platform_driver);
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0;
}
/*
*驱动注销函数
*/
static void __exit led_platform_driver_exit(void)
{
printk(KERN_EMERG "led_test exit!\n");
/*删除设备*/
device_destroy(class_led, led_devno); //清除设备
class_destroy(class_led); //清除类
cdev_del(&led_chr_dev); //清除设备号
unregister_chrdev_region(led_devno, DEV_CNT); //取消注册字符设备
platform_driver_unregister(&led_platform_driver);
}
module_init(led_platform_driver_init);
module_exit(led_platform_driver_exit);
MODULE_LICENSE("GPL");

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

17
input_sub_system/Makefile Normal file
View File

@ -0,0 +1,17 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := input_sub_system.o
out = test_app
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
$(CROSS_COMPILE)gcc -o $(out) test_app.c
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean
rm test_app

View File

@ -0,0 +1,141 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/of.h> /*设备树*/
#include <linux/platform_device.h> /*平台设备*/
#include <linux/gpio/consumer.h> /*GPIO 接口描述符*/
#include <linux/input.h>
#include <linux/interrupt.h>
struct button_data {
struct gpio_desc *button_input_gpiod; //GPIO
struct input_dev *button_input_dev; //按键输入子系统结构体
struct platform_device *pdev; //平台设备结构体
int irq; //中断irq
};
/*按键中断处理函数*/
static irqreturn_t button_input_irq_hander(int irq, void *dev_id)
{
struct button_data *priv = dev_id;
int button_satus;
/*读取按键引脚的电平,根据读取得到的结果输入按键状态*/
button_satus = (gpiod_get_value(priv->button_input_gpiod) & 1);
if(button_satus)
{
input_report_key(priv->button_input_dev, BTN_0, 1);
input_sync(priv->button_input_dev);
}
else
{
input_report_key(priv->button_input_dev, BTN_0, 0);
input_sync(priv->button_input_dev);
}
return IRQ_HANDLED;
}
static int btn_open(struct input_dev *i_dev)
{
pr_info("input device opened()\n");
return 0;
}
static void btn_close(struct input_dev *i_dev)
{
pr_info("input device closed()\n");
}
static int button_probe(struct platform_device *pdev)
{
struct button_data *priv;
struct gpio_desc *gpiod;
struct input_dev *i_dev;
int ret;
pr_info("button_probe\n");
priv = devm_kzalloc(&pdev->dev,sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
i_dev = input_allocate_device();
if (!i_dev)
return -ENOMEM;
i_dev->open = btn_open;
i_dev->close = btn_close;
i_dev->name = "key input";
i_dev->dev.parent = &pdev->dev;
priv->button_input_dev = i_dev;
priv->pdev = pdev;
set_bit(EV_KEY, i_dev->evbit); /*设置要使用的输入事件类型*/
set_bit(BTN_0, i_dev->keybit); /* 按钮 0*/
gpiod = gpiod_get(&pdev->dev, "button", GPIOD_IN); /*获取gpio,并设置为输入*/
if (IS_ERR(gpiod))
return -ENODEV;
priv->irq = gpiod_to_irq(gpiod);
priv->button_input_gpiod = gpiod;
ret = input_register_device(priv->button_input_dev);
if (ret) {
pr_err("Failed to register inputdevice\n");
goto err_input;
}
platform_set_drvdata(pdev, priv);
ret = request_any_context_irq(priv->irq, button_input_irq_hander,IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "input-button", priv);
if (ret < 0) {
dev_err(&pdev->dev,"请求gpio中断失败\n");
goto err_btn;
}
return 0;
err_btn:
gpiod_put(priv->button_input_gpiod);
err_input:
input_free_device(priv->button_input_dev);
return ret;
}
static int button_remove(struct platform_device *pdev)
{
struct button_data *priv;
priv = platform_get_drvdata(pdev);
input_unregister_device(priv->button_input_dev);
input_free_device(priv->button_input_dev);
free_irq(priv->irq, priv);
gpiod_put(priv->button_input_gpiod);
return 0;
}
static const struct of_device_id button_dt_ids[] =
{
{ .compatible = "input_button", },
{ /*标记结束*/ }
};
static struct platform_driver button_input = {
.probe = button_probe,
.remove = button_remove,
.driver = {
.name = "button-input",
.of_match_table = of_match_ptr(button_dt_ids),
.owner = THIS_MODULE,
},
};
module_platform_driver(button_input);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("llh<zgwinli555@163.com>");
MODULE_DESCRIPTION("Embedfire,Input key device");

View File

@ -0,0 +1,26 @@
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/interrupt-controller/irq.h>
&{/} {
input_button: input_button {
status = "okay";
compatible = "input_button";
button-gpios = <&gpio0 RK_PB0 GPIO_ACTIVE_HIGH>; //引脚可以根据具体板卡修改,这里以GPIO0_B0
pinctrl-names = "default";
pinctrl-0 = <&input_button_pin>;
interrupt-parent = <&gpio0>;
interrupts = <RK_PB0 IRQ_TYPE_EDGE_BOTH>;
};
};
&{/pinctrl} {
pinctrl_button {
input_button_pin: input_button_pin {
rockchip,pins = <0 RK_PB0 RK_FUNC_GPIO &pcfg_pull_none>;
};
};
};

View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <linux/input.h>
struct input_event button_input_event;
int button_status = 0; //保存按键状态;
int main(int argc, char *argv[])
{
int error = -20;
int button_status = 0;
/*打开文件*/
int fd = open("/dev/input/event4", O_RDONLY);
if (fd < 0)
{
printf("open file : /dev/input/event4 error!\n");
return -1;
}
printf("wait button down... \n");
do
{
/*读取按键状态*/
error = read(fd, &button_input_event, sizeof(button_input_event));
if (error < 0)
{
printf("read file error! \n");
}
if((button_input_event.type == 1) && (button_input_event.code == 0x100))
{
if(button_input_event.value == 0)
{
printf("button up\n");
}
else if(button_input_event.value == 1)
{
printf("button down\n");
}
}
} while (1);
printf("button Down !\n");
/*关闭文件*/
error = close(fd);
if (error < 0)
{
printf("close file error! \n");
}
return 0;
}

15
led_cdev/Makefile Normal file
View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := led_cdev.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

150
led_cdev/led_cdev.c Normal file
View File

@ -0,0 +1,150 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define DEV_NAME "led_chrdev"
#define DEV_CNT (1)
#define GPIO0_BASE (0xfdd60000)
//每组GPIO,有2个寄存器,对应32个引脚,每个寄存器负责16个引脚;
//一个寄存器32位,其中高16位都是使能位,低16位对应16个引脚,每个引脚占用1比特位
#define GPIO0_DR_L (GPIO0_BASE + 0x0000)
#define GPIO0_DR_H (GPIO0_BASE + 0x0004)
#define GPIO0_DDR_L (GPIO0_BASE + 0x0008)
#define GPIO0_DDR_H (GPIO0_BASE + 0x000C)
static dev_t devno;
struct class *led_chrdev_class;
struct led_chrdev {
struct cdev dev;
unsigned int __iomem *va_dr; // 数据寄存器,设置输出的电压
unsigned int __iomem *va_ddr; // 数据方向寄存器,设置输入或者输出
unsigned int led_pin; // 偏移
};
static int led_chrdev_open(struct inode *inode, struct file *filp)
{
unsigned int val = 0;
struct led_chrdev *led_cdev = (struct led_chrdev *)container_of(inode->i_cdev, struct led_chrdev,dev);
filp->private_data = container_of(inode->i_cdev, struct led_chrdev, dev);
printk("open\n");
//设置输出模式
val = ioread32(led_cdev->va_ddr);
val |= ((unsigned int)0x1 << (led_cdev->led_pin+16));
val |= ((unsigned int)0X1 << (led_cdev->led_pin));
iowrite32(val,led_cdev->va_ddr);
//输出高电平
val = ioread32(led_cdev->va_dr);
val |= ((unsigned int)0x1 << (led_cdev->led_pin+16));
val |= ((unsigned int)0x1 << (led_cdev->led_pin));
iowrite32(val, led_cdev->va_dr);
return 0;
}
static int led_chrdev_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t led_chrdev_write(struct file *filp, const char __user * buf,
size_t count, loff_t * ppos)
{
unsigned long val = 0;
char ret = 0;
struct led_chrdev *led_cdev = (struct led_chrdev *)filp->private_data;
printk("write \n");
get_user(ret, buf);
val = ioread32(led_cdev->va_dr);
printk("val = %lx\n", val);
if (ret == '0'){
val |= ((unsigned int)0x1 << (led_cdev->led_pin+16));
val &= ~((unsigned int)0x01 << (led_cdev->led_pin)); /*设置GPIO引脚输出低电平*/
}
else{
val |= ((unsigned int)0x1 << (led_cdev->led_pin+16));
val |= ((unsigned int)0x01 << (led_cdev->led_pin)); /*设置GPIO引脚输出高电平*/
}
iowrite32(val, led_cdev->va_dr);
printk("val = %lx\n", val);
return count;
}
static struct file_operations led_chrdev_fops = {
.owner = THIS_MODULE,
.open = led_chrdev_open,
.release = led_chrdev_release,
.write = led_chrdev_write,
};
static struct led_chrdev led_cdev[DEV_CNT] = {
{.led_pin = 7}, //偏移,高16引脚,GPIO0_C7
};
static __init int led_chrdev_init(void)
{
int i = 0;
dev_t cur_dev;
printk("led_chrdev init (lubancat2 GPIO0_C7)\n");
led_cdev[0].va_dr = ioremap(GPIO0_DR_H, 4); //
led_cdev[0].va_ddr = ioremap(GPIO0_DDR_H, 4); //
alloc_chrdev_region(&devno, 0, DEV_CNT, DEV_NAME);
led_chrdev_class = class_create(THIS_MODULE, "led_chrdev");
for (; i < DEV_CNT; i++) {
cdev_init(&led_cdev[i].dev, &led_chrdev_fops);
led_cdev[i].dev.owner = THIS_MODULE;
cur_dev = MKDEV(MAJOR(devno), MINOR(devno) + i);
cdev_add(&led_cdev[i].dev, cur_dev, 1);
device_create(led_chrdev_class, NULL, cur_dev, NULL,
DEV_NAME "%d", i);
}
return 0;
}
module_init(led_chrdev_init);
static __exit void led_chrdev_exit(void)
{
int i;
dev_t cur_dev;
printk("led chrdev exit (lubancat2 GPIO0_C7)\n");
for (i = 0; i < DEV_CNT; i++) {
iounmap(led_cdev[i].va_dr); // 释放模式寄存器虚拟地址
iounmap(led_cdev[i].va_ddr); // 释放输出类型寄存器虚拟地址
}
for (i = 0; i < DEV_CNT; i++) {
cur_dev = MKDEV(MAJOR(devno), MINOR(devno) + i);
device_destroy(led_chrdev_class, cur_dev);
cdev_del(&led_cdev[i].dev);
}
unregister_chrdev_region(devno, DEV_CNT);
class_destroy(led_chrdev_class);
}
module_exit(led_chrdev_exit);
MODULE_AUTHOR("embedfire");
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := xdev.o xbus.o xdrv.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

72
linux_device_model/xbus.c Normal file
View File

@ -0,0 +1,72 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#define BUS_ATTR(_name, _mode, _show, _store) \
struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
/* 5.10.160内核已经更换该定义,参见内核源码/include/linux/device/bus.h,
此处自行定义,解决使用高版本内核源码编译驱动模块时报错 */
/************************************************************************
* 函数负责总线下的设备以及驱动匹配
* 使用字符串比较的方式,通过对比驱动以及设备的名字来确定是否匹配,
* 如果相同, 则说明匹配成功,返回1;反之,则返回0
***********************************************************************/
int xbus_match(struct device *dev, struct device_driver *drv)
{
printk("%s-%s\n", __FILE__, __func__);
if (!strncmp(dev_name(dev), drv->name, strlen(drv->name))) {
printk("dev & drv match\n");
return 1;
}
return 0;
}
//定义了一个bus_name变量,存放了该总线的名字
static char *bus_name = "xbus";
//提供show回调函数,这样用户便可以通过cat命令, 来查询总线的名称
ssize_t xbus_test_show(struct bus_type *bus, char *buf)
{
return sprintf(buf, "%s\n", bus_name);
}
//设置该文件的文件权限为文件拥有者可读,组内成员以及其他成员不可操作
BUS_ATTR(xbus_test, S_IRUSR, xbus_test_show, NULL);
//定义了一种新的总线,名为xbus,总线结构体中最重要的一个成员,便是match回调函数
static struct bus_type xbus = {
.name = "xbus",
.match = xbus_match,
};
EXPORT_SYMBOL(xbus);
//注册总线
static __init int xbus_init(void)
{
int ret;
printk("xbus init\n");
ret = bus_register(&xbus);
if(ret)
printk("xbus register failed\n");
ret = bus_create_file(&xbus, &bus_attr_xbus_test);
return 0;
}
module_init(xbus_init);
//注销总线
static __exit void xbus_exit(void)
{
printk("xbus exit\n");
bus_remove_file(&xbus, &bus_attr_xbus_test);
bus_unregister(&xbus);
}
module_exit(xbus_exit);
MODULE_AUTHOR("embedfire");
MODULE_LICENSE("GPL");

70
linux_device_model/xdev.c Normal file
View File

@ -0,0 +1,70 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
extern struct bus_type xbus;
void xdev_release(struct device *dev)
{
printk("%s-%s\n", __FILE__, __func__);
}
unsigned long id = 0;
//show回调函数中,直接将id的值通过sprintf函数拷贝至buf中。
ssize_t xdev_id_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "%ld\n", id);
}
/*********************************************************************************************
* store回调函数则是利用kstrtoul函数,
* 该函数有三个参数,其中第二个参数是采用几进制的方式, 这里我们传入的是10,意味着buf中的内容将转换为10进制的数传递给id,
* 实现了通过sysfs修改驱动的目的。
*********************************************************************************************/
ssize_t xdev_id_store(struct device * dev, struct device_attribute * attr,
const char *buf, size_t count)
{
int val = 0;
val = kstrtoul(buf, 10, &id);
return count;
}
//DEVICE_ATTR宏定义定义了xdev_id,设置该文件的文件权限是文件拥有者可读可写,组内成员以及其他成员不可操作
DEVICE_ATTR(xdev_id, S_IRUSR | S_IWUSR, xdev_id_show, xdev_id_store);
static struct device xdev = {
.init_name = "xdev",
.bus = &xbus,
.release = xdev_release,
};
//设备结构体以及属性文件结构体注册
static __init int xdev_init(void)
{
int ret;
printk("xdev init\n");
ret = device_register(&xdev);
if(ret)
printk("Unable to register xdev\n");
device_create_file(&xdev, &dev_attr_xdev_id);
return 0;
}
module_init(xdev_init);
//设备结构体以及属性文件结构体注销。
static __exit void xdev_exit(void)
{
printk("xdev exit\n");
device_remove_file(&xdev, &dev_attr_xdev_id);
device_unregister(&xdev);
}
module_exit(xdev_exit);
MODULE_AUTHOR("embedfire");
MODULE_LICENSE("GPL");

65
linux_device_model/xdrv.c Normal file
View File

@ -0,0 +1,65 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
extern struct bus_type xbus;
char *name = "xdrv";
//保证store和show函数的前缀与驱动属性文件一致,drvname_show()的前缀和drvname
ssize_t drvname_show(struct device_driver *drv, char *buf)
{
return sprintf(buf, "%s\n", name);
}
//DRIVER_ATTR_RO定义了一个drvname属性文件
DRIVER_ATTR_RO(drvname);
int xdrv_probe(struct device *dev)
{
printk("%s-%s\n", __FILE__, __func__);
return 0;
}
int xdrv_remove(struct device *dev)
{
printk("%s-%s\n", __FILE__, __func__);
return 0;
}
//定义了一个驱动结构体xdrv,名字需要和设备的名字相同,否则就不能成功匹配
static struct device_driver xdrv = {
.name = "xdev",
//该驱动挂载在已经注册好的总线xbus下。
.bus = &xbus,
//当驱动和设备匹配成功之后,便会执行驱动的probe函数
.probe = xdrv_probe,
//当注销驱动时,需要关闭物理设备的某些功能等
.remove = xdrv_remove,
};
//调用driver_register函数以及driver_create_file函数进行注册我们的驱动以及驱动属性文件
static __init int xdrv_init(void)
{
int ret;
printk("xdrv init\n");
ret = driver_register(&xdrv);
ret = driver_create_file(&xdrv, &driver_attr_drvname);
return 0;
}
module_init(xdrv_init);
//注销驱动以及驱动属性文件
static __exit void xdrv_exit(void)
{
printk("xdrv exit\n");
driver_remove_file(&xdrv, &driver_attr_drvname);
driver_unregister(&xdrv);
}
module_exit(xdrv_exit);
MODULE_AUTHOR("embedfire");
MODULE_LICENSE("GPL");

View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := hellomodule.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,16 @@
KERNEL_DIR=../../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := parametermodule.o calculation.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONE:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,26 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include "calculation.h"
static int __init calculation_init(void)
{
printk(KERN_ALERT "calculation init!\n");
printk(KERN_ALERT "itype+1 = %d, itype-1 = %d\n", my_add(itype,1), my_sub(itype,1));
return 0;
}
static void __exit calculation_exit(void)
{
printk(KERN_ALERT "calculation exit!\n");
}
module_init(calculation_init);
module_exit(calculation_exit);
MODULE_LICENSE("GPL2");
MODULE_AUTHOR("embedfire ");
MODULE_DESCRIPTION("calculation module");
MODULE_ALIAS("calculation_module");

View File

@ -0,0 +1,9 @@
#ifndef __CALCULATION_H__
#define __CALCULATION_H__
extern int itype;
int my_add(int a, int b);
int my_sub(int a, int b);
#endif

12
platform_driver/Makefile Normal file
View File

@ -0,0 +1,12 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := led_pdev.o led_pdrv.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
modules clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,57 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
//#define GPIO4_BASE (0xfe770000)
//#define GPIO4_DR (GPIO4_BASE + 0x0000)
//#define GPIO4_DDR (GPIO4_BASE + 0x0008)
#define GPIO0_BASE (0xfdd60000)
#define GPIO0_DR (GPIO0_BASE + 0x0004)
#define GPIO0_DDR (GPIO0_BASE + 0x000C)
static struct resource led_resource[] = {
[0] = DEFINE_RES_MEM(GPIO0_DR, 4),
[1] = DEFINE_RES_MEM(GPIO0_DDR, 4),
};
static void led_release(struct device *dev)
{
}
/* led hardware information */
unsigned int led_hwinfo[1] = { 7 }; //偏移
/* led device */
static struct platform_device led_pdev = {
.name = "led_pdev",
.id = 0,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,
.dev = {
.release = led_release,
.platform_data = led_hwinfo,
},
};
static __init int led_pdev_init(void)
{
printk("pdev init\n");
platform_device_register(&led_pdev);
return 0;
}
module_init(led_pdev_init);
static __exit void led_pdev_exit(void)
{
printk("pdev exit\n");
platform_device_unregister(&led_pdev);
}
module_exit(led_pdev_exit);
MODULE_DESCRIPTION("Platform LED device");
MODULE_AUTHOR("embedfire");
MODULE_LICENSE("GPL");

222
platform_driver/led_pdrv.c Normal file
View File

@ -0,0 +1,222 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#define DEV_MAJOR 243
#define DEV_NAME "led"
static struct class *led_test_class;
//结构体led_data来管理我们LED灯的硬件信息
struct led_data {
unsigned int led_pin;
unsigned int __iomem *va_DDR;
unsigned int __iomem *va_DR;
struct cdev led_cdev;
};
static int led_cdev_open(struct inode *inode, struct file *filp)
{
unsigned int val = 0;
struct led_data *cur_led = container_of(inode->i_cdev, struct led_data, led_cdev);
printk("led_cdev_open() \n");
// 设置引脚输出
val = readl(cur_led->va_DDR);
val |= ((unsigned int)0X1 << (cur_led->led_pin+16));
val |= ((unsigned int)0X1 << (cur_led->led_pin));
writel(val,cur_led->va_DDR);
//设置默认输出高电平
val = readl(cur_led->va_DR);
val |= ((unsigned int)0X1 << (cur_led->led_pin+16));
val |= ((unsigned int)0x1 << (cur_led->led_pin));
writel(val, cur_led->va_DR);
filp->private_data = cur_led;
return 0;
}
static int led_cdev_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t led_cdev_write(struct file *filp, const char __user * buf,
size_t count, loff_t * ppos)
{
unsigned long val = 0;
unsigned long ret = 0;
int tmp = count;
struct led_data *cur_led = (struct led_data *)filp->private_data;
val = kstrtoul_from_user(buf, tmp, 10, &ret);
val = readl(cur_led->va_DR);
if (ret == 0)
{
val |= ((unsigned int)0x1 << ((cur_led->led_pin)+16));
val &= ~((unsigned int)0X1 << (cur_led->led_pin));
}
else
{
val |= ((unsigned int)0x1 << (cur_led->led_pin+16));
val |= ((unsigned int)0X1 << (cur_led->led_pin));
}
writel(val, cur_led->va_DR);
*ppos += tmp;
return tmp;
}
static struct file_operations led_cdev_fops = {
.open = led_cdev_open,
.release = led_cdev_release,
.write = led_cdev_write,
};
//probe函数中,驱动需要去提取设备的资源,完成字符设备的注册等工作
static int led_pdrv_probe(struct platform_device *pdev)
{
struct led_data *cur_led;
unsigned int *led_hwinfo;
struct resource *mem_DR;
struct resource *mem_DDR;
dev_t cur_dev;
int ret = 0;
printk("led platform driver probe\n");
//第一步:提取平台设备提供的资源
//devm_kzalloc函数申请cur_led和led_hwinfo结构体内存大小
cur_led = devm_kzalloc(&pdev->dev, sizeof(struct led_data), GFP_KERNEL);
if(!cur_led)
return -ENOMEM;
led_hwinfo = devm_kzalloc(&pdev->dev, sizeof(unsigned int)*2, GFP_KERNEL);
if(!led_hwinfo)
return -ENOMEM;
/* get the pin for led and the reg's shift */
//dev_get_platdata函数获取私有数据,得到LED灯的寄存器偏移量,并赋值给cur_led->led_pin
led_hwinfo = dev_get_platdata(&pdev->dev);
cur_led->led_pin = led_hwinfo[0];
/* get platform resource */
//利用函数platform_get_resource可以获取到各个寄存器的地址
mem_DR = platform_get_resource(pdev, IORESOURCE_MEM, 0);
mem_DDR = platform_get_resource(pdev, IORESOURCE_MEM, 1);
//使用devm_ioremap将获取到的寄存器地址转化为虚拟地址
cur_led->va_DR = devm_ioremap(&pdev->dev, mem_DR->start, resource_size(mem_DR));
cur_led->va_DDR = devm_ioremap(&pdev->dev, mem_DDR->start, resource_size(mem_DDR));
//第二步:注册字符设备
cur_dev = MKDEV(DEV_MAJOR, pdev->id);
register_chrdev_region(cur_dev, 1, "led_cdev");
cdev_init(&cur_led->led_cdev, &led_cdev_fops);
ret = cdev_add(&cur_led->led_cdev, cur_dev, 1);
if(ret < 0)
{
printk("fail to add cdev\n");
goto add_err;
}
device_create(led_test_class, NULL, cur_dev, NULL, DEV_NAME "%d", pdev->id);
/* save as drvdata */
//platform_set_drvdata函数,将LED数据信息存入在平台驱动结构体中pdev->dev->driver_data中
platform_set_drvdata(pdev, cur_led);
return 0;
add_err:
unregister_chrdev_region(cur_dev, 1);
return ret;
}
static int led_pdrv_remove(struct platform_device *pdev)
{
dev_t cur_dev;
//platform_get_drvdata,获取当前LED灯对应的结构体
struct led_data *cur_data = platform_get_drvdata(pdev);
printk("led platform driver remove\n");
cur_dev = MKDEV(DEV_MAJOR, pdev->id);
//cdev_del删除对应的字符设备
cdev_del(&cur_data->led_cdev);
//删除/dev目录下的设备
device_destroy(led_test_class, cur_dev);
//unregister_chrdev_region, 注销掉当前的字符设备编号
unregister_chrdev_region(cur_dev, 1);
return 0;
}
static struct platform_device_id led_pdev_ids[] = {
{.name = "led_pdev"},
{}
};
MODULE_DEVICE_TABLE(platform, led_pdev_ids);
//平台总线匹配过程中 ,只会根据id_table中的name值进行匹配,若和平台设备的name值相等,则表示匹配成功; 反之,则匹配不成功,表明当前内核没有该驱动能够支持的设备。
static struct platform_driver led_pdrv = {
.probe = led_pdrv_probe,
.remove = led_pdrv_remove,
.driver.name = "led_pdev",
.id_table = led_pdev_ids,
};
static __init int led_pdrv_init(void)
{
printk("led platform driver init\n");
//class_create,来创建一个led类
led_test_class = class_create(THIS_MODULE, "test_leds");
//调用函数platform_driver_register,注册我们的平台驱动结构体,这样当加载该内核模块时, 就会有新的平台驱动加入到内核中。 第20-27行,注销
platform_driver_register(&led_pdrv);
return 0;
}
module_init(led_pdrv_init);
static __exit void led_pdrv_exit(void)
{
printk("led platform driver exit\n");
platform_driver_unregister(&led_pdrv);
class_destroy(led_test_class);
}
module_exit(led_pdrv_exit);
MODULE_AUTHOR("Embedfire");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("the example for platform driver");

15
power_management/Makefile Normal file
View File

@ -0,0 +1,15 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := regulator_test.o
all:
$(MAKE) EXTRA_CFLAGS=-fno-pic -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,104 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
static struct regulator_dev *my_regulator_test_rdev;
static struct regulator_consumer_supply relate={
.dev_name = "my_consumer",
.supply = "VCC",
};
static struct regulator_init_data my_regulator_initdata = {
.constraints = {
.name ="my_regulator",
.always_on = 0,
.min_uV = 500000,
.max_uV = 1350000,
.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,
.valid_modes_mask = REGULATOR_MODE_NORMAL,
},
.num_consumer_supplies = 1,
.consumer_supplies = &relate,
};
static struct regulator_ops my_regulator_ops;
static struct regulator_desc my_regulator_desc = {
.name = "my_regulator_test",
.id = -1,
.n_voltages = 16,
.type = REGULATOR_VOLTAGE,
.owner = THIS_MODULE,
.ops = &my_regulator_ops,
};
static int regulator_driver_probe(struct platform_device *pdev)
{
struct regulator_config config = { };
int ret;
config.dev = &pdev->dev;
config.init_data = &my_regulator_initdata;
my_regulator_test_rdev = regulator_register(&my_regulator_desc, &config);
if (IS_ERR(my_regulator_test_rdev)) {
ret = PTR_ERR(my_regulator_test_rdev);
pr_err("Failed to register regulator: %d\n", ret);
return ret;
}
return 0;
}
static struct platform_driver my_regulator_driver = {
.probe = regulator_driver_probe,
.driver = {
.name = "my_regulator",
.owner = THIS_MODULE,
},
};
static struct platform_device *regulator_pdev;
static int my_regulator_test_init(void)
{
int ret;
regulator_pdev = platform_device_alloc("my_regulator", -1);
if (!regulator_pdev) {
pr_err("Failed to allocate dummy regulator device\n");
return -1;
}
ret = platform_device_add(regulator_pdev);
if (ret != 0) {
pr_err("Failed to register dummy regulator device: %d\n", ret);
platform_device_put(regulator_pdev);
return -1;
}
ret = platform_driver_register(&my_regulator_driver);
if (ret != 0) {
pr_err("Failed to register dummy regulator driver: %d\n", ret);
platform_device_unregister(regulator_pdev);
return -1;
}
return 0;
}
static void my_regulator_test_exit(void)
{
regulator_unregister(my_regulator_test_rdev);
platform_device_unregister(regulator_pdev);
platform_driver_unregister(&my_regulator_driver);
}
module_init(my_regulator_test_init);
module_exit(my_regulator_test_exit);
MODULE_LICENSE("GPL");

14
pwm_sub_system/Makefile Normal file
View File

@ -0,0 +1,14 @@
KERNEL_DIR=../../kernel/
ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
export ARCH CROSS_COMPILE
obj-m := pwm_sub_system.o
all:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules
.PHONY:clean
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean

View File

@ -0,0 +1,32 @@
/*
* Copyright (C) 2022 - All Rights Reserved by
* EmbedFire LubanCat
*/
/dts-v1/;
/plugin/;
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/pinctrl/rockchip.h>
#include <dt-bindings/clock/rk3568-cru.h>
#include <dt-bindings/pwm/pwm.h>
&pwm0 {
status = "okay";
pinctrl-names = "active";
pinctrl-0 = <&pwm0m1_pins>;
};
&{/} {
pwm_demo: pwm_demo {
status = "okay";
compatible = "pwm_demo";
back {
pwm-names = "pwm-demo";
pwms = <&pwm0 0 10000 1>;
duty_ns = <5000>;
};
};
};

View File

@ -0,0 +1,73 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/of_platform.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/pwm.h>
#include <linux/leds_pwm.h>
#include <linux/slab.h>
struct pwm_device *pwm_demo; //定义pwm设备结构体
/*简单驱动*/
static int pwm_demo_probe(struct platform_device *pdev)
{
int ret = 0;
struct device *dev = &pdev->dev;
struct device_node *child; // 保存子节点
printk("match success \n");
child = of_get_next_child(dev->of_node, NULL);
if (child)
{
pwm_demo = devm_of_pwm_get(dev, child, NULL);
if (IS_ERR(pwm_demo))
{
printk(KERN_ERR" pwm_demo,get pwm error!!\n");
return -1;
}
}
else
{
printk(KERN_ERR" pwm_demoof_get_next_child error!!\n");
return -1;
}
/*配置频率100KHz 占空比80%*/
pwm_config(pwm_demo, 1000, 5000);
/*反相 频率100KHz 占空比20%*/
pwm_set_polarity(pwm_demo, PWM_POLARITY_INVERSED);
pwm_enable(pwm_demo);
return ret;
}
static int pwm_demo_remove(struct platform_device *pdev)
{
pwm_config(pwm_demo, 0, 5000);
pwm_free(pwm_demo);
return 0;
}
static const struct of_device_id of_pwm_leds_match[] = {
{.compatible = "pwm_demo",},
{},
};
static struct platform_driver pwm_demo_driver = {
.probe = pwm_demo_probe,
.remove = pwm_demo_remove,
.driver = {
.name = "pwm_demo",
.of_match_table = of_pwm_leds_match,
},
};
module_platform_driver(pwm_demo_driver);
MODULE_AUTHOR("llh <zgwinli555@163.com>");
MODULE_DESCRIPTION(" Embedfire PWM DEMO driver");
MODULE_LICENSE("GPL v2");