【IMX6ULL驱动开发学习】12.Linux SPI驱动实战:DAC驱动设计流程

基础回顾: 【IMX6ULL驱动开发学习】10.Linux I2C驱动实战:AT24C02驱动设计流程_阿龙还在写代码的博客-CSDN博客

【IMX6ULL驱动开发学习】11.Linux之SPI驱动_阿龙还在写代码的博客-CSDN博客

一、编写驱动

查看芯片手册,有两种DAC数据格式,12位和16位,这里选用16位数据(2字节)编写驱动。

重点在驱动程序中写函数spi_drv_write的编写:spi_transfer结构体的构造,其中tx_buf存放发送数据,len表示发送长度(字节数),发起SPI同步传输

objectivec 复制代码
/**
 * spi_sync_transfer - 同步的SPI传输函数
 * @spi: 读写哪个设备
 * @xfers: spi_transfers数组,用来描述传输
 * @num_xfers: 数组项个数
 * 上下文: 能休眠的上下文才可以使用这个函数
 *
 * 返回值: 0-成功, 负数-失败码
 */
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
	unsigned int num_xfers);
objectivec 复制代码
static ssize_t spi_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	short val;//2个字节
	unsigned char ker_buf[2];

	struct spi_transfer t;

	memset(&t, 0, sizeof(t));

	if (size != 2)
		return -EINVAL;	

	/* copy_from_user  */
	err = copy_from_user(&val, buf, size);
	val <<= 2;
	val &= 0x0fff;//DAC数据格式:高4位、低2位为0

	ker_buf[0] = val >> 8;	//高8位
	ker_buf[1] = val;		//低8位


	/* 初始化 spi_transfer */
	t.tx_buf = ker_buf;
	t.len    = 2;

	err = spi_sync_transfer(g_spi, &t, 1);
	
	return size;    
}

完整驱动程序:spi_drv.c

objectivec 复制代码
#include "asm/cacheflush.h"
#include <linux/spi/spi.h>
#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

/* 主设备号                                                                 */
static int major = 0;
static struct class *my_spi_class;

static struct spi_device *g_spi;

static DECLARE_WAIT_QUEUE_HEAD(gpio_wait);
struct fasync_struct *spi_fasync;


/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t spi_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	// int err;

	// struct spi_transfer msgs[2];

	/* 初始化 spi_transfer */

	// static inline int
    //   spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
	//   unsigned int num_xfers);

	/* copy_to_user  */
	
	return 0;
}

static ssize_t spi_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	short val;//2个字节
	unsigned char ker_buf[2];

	struct spi_transfer t;

	memset(&t, 0, sizeof(t));

	if (size != 2)
		return -EINVAL;	

	/* copy_from_user  */
	err = copy_from_user(&val, buf, size);
	val <<= 2;
	val &= 0x0fff;//DAC数据格式:高4位、低2位为0

	ker_buf[0] = val >> 8;	//高8位
	ker_buf[1] = val;		//低8位


	/* 初始化 spi_transfer */
	t.tx_buf = ker_buf;
	t.len    = 2;

	err = spi_sync_transfer(g_spi, &t, 1);
	
	return size;    
}


static unsigned int spi_drv_poll(struct file *fp, poll_table * wait)
{
	//printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &gpio_wait, wait);
	//return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
	return 0;
}

static int spi_drv_fasync(int fd, struct file *file, int on)
{
	if (fasync_helper(fd, file, on, &spi_fasync) >= 0)
		return 0;
	else
		return -EIO;
}


/* 定义自己的file_operations结构体                                              */
static struct file_operations spi_drv_fops = {
	.owner	 = THIS_MODULE,
	.read    = spi_drv_read,
	.write   = spi_drv_write,
	.poll    = spi_drv_poll,
	.fasync  = spi_drv_fasync,
};


static int spi_drv_probe(struct spi_device *spi)
{
	// struct device_node *np = client->dev.of_node;

	/* 记录spi_device */
	g_spi = spi;

	/* 注册字符设备 */
	/* 注册file_operations 	*/
	major = register_chrdev(0, "100ask_spi", &spi_drv_fops);  /* /dev/gpio_desc */

	my_spi_class = class_create(THIS_MODULE, "100ask_spi_class");
	if (IS_ERR(my_spi_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "100ask_spi");
		return PTR_ERR(my_spi_class);
	}

	device_create(my_spi_class, NULL, MKDEV(major, 0), NULL, "myspi"); /* /dev/myspi */
	
	return 0;
}

static int spi_drv_remove(struct spi_device *spi)
{
	/* 反注册字符设备 */
	device_destroy(my_spi_class, MKDEV(major, 0));
	class_destroy(my_spi_class);
	unregister_chrdev(major, "100ask_spi");

	return 0;
}

static const struct of_device_id myspi_dt_match[] = {
	{ .compatible = "100ask,spidev" },
	{},
};
static struct spi_driver my_spi_driver = {
	.driver = {
		   .name = "100ask_spi_drv",
		   .owner = THIS_MODULE,
		   .of_match_table = myspi_dt_match,
	},
	.probe = spi_drv_probe,
	.remove = spi_drv_remove,
};


static int __init spi_drv_init(void)
{
	/* 注册spi_driver */
	return spi_register_driver(&my_spi_driver);
}

static void __exit spi_drv_exit(void)
{
	/* 反注册spi_driver */
	spi_unregister_driver(&my_spi_driver);
}

/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(spi_drv_init);
module_exit(spi_drv_exit);

MODULE_LICENSE("GPL");

二、修改设备树

  • 放在哪个SPI控制器下面
  • DAC模块的片选引脚(查芯片手册)
  • SPI频率
  • compatible属性:用来寻址驱动程序

修改设备树:在内核目录下 vi arch/arm/boot/dts/100ask_imx6ull-14x14.dts

objectivec 复制代码
&ecspi1 {
    pinctrl-names = "default";
    pinctrl-0 = <&pinctrl_ecspi1>;

    fsl,spi-num-chipselects = <2>;
    cs-gpios = <&gpio4 26 GPIO_ACTIVE_LOW>, <&gpio4 24 GPIO_ACTIVE_LOW>;
    status = "okay";


    dac: dac {
        compatible = "100ask,spidev";
        reg = <0>;
        spi-max-frequency = <1000000>;
    };
};
  • 在/home/book/100ask_imx6ull-sdk/Linux-4.9.88目录下重新编译设备树:make dtbs
  • 复制到单板上,如下:
objectivec 复制代码
PC:
cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/
 
开发板:
mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
cp /mnt/100ask_imx6ull-14x14.dtb  /boot
reboot
  • 进入系统固件目录下查看cd /sys/firmware/devicetree/base/
  • 在系统 总线 i2c 设备下面查看是否有这个设备

spi0.0表示第0总线下第0个设备,前面0表示控制器,后面0表示控制器下面第0个设备。但还没有驱动程序。

  • 挂载网络文件系统:mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
  • 装载驱动程序:insmod i2c_drv.ko
  • 查看对应设备节点:ls /dev/myi2c -l

装载驱动程序前有设备无驱动文件,装载后有驱动文件:

  • 查看APP用法并测试驱动程序
相关推荐
stormsha7 分钟前
Linux中su与sudo命令的区别:权限管理的关键差异解析
linux·运维·服务器·鸿蒙系统·ux·batch命令
哆啦A梦的口袋呀16 分钟前
基于Python学习《Head First设计模式》第七章 适配器和外观模式
python·学习·设计模式
恰薯条的屑海鸥19 分钟前
零基础在实践中学习网络安全-皮卡丘靶场(第十期-Over Permission 模块)
学习·安全·web安全·渗透测试·网络安全学习
东京老树根1 小时前
SAP学习笔记 - 开发27 - 前端Fiori开发 Routing and Navigation(路由和导航)
笔记·学习
筏.k1 小时前
grep、wc 与管道符快速上手指南
linux
Johny_Zhao1 小时前
华为MAAS、阿里云PAI、亚马逊AWS SageMaker、微软Azure ML各大模型深度分析对比
linux·人工智能·ai·信息安全·云计算·系统运维
CodeOfCC1 小时前
c语言 封装跨平台线程头文件
linux·c语言·windows
广药门徒1 小时前
定时器时钟来源可以从输入捕获引脚输入
单片机·嵌入式硬件
科文小白狼1 小时前
Linux下VSCode开发环境配置(LSP)
linux·vscode·里氏替换原则·lsp
jugt3 小时前
CentOS 7.9安装Nginx1.24.0时报 checking for LuaJIT 2.x ... not found
linux·运维·centos