[IMX6ULL driver development learning] 12.Linux SPI driver practice: DAC driver design process

Basic review:  [Learning about IMX6ULL driver development] 10.Linux I2C driver practice: AT24C02 driver design process_Aaron’s blog where he is still writing code-CSDN blog

[IMX6ULL driver development learning] 11. Linux SPI driver_Aaron is still writing code blog-CSDN blog

1. Write the driver

Looking at the chip manual, there are two DAC data formats, 12-bit and 16-bit. Here we choose 16-bit data (2 bytes) to write the driver.

 Focus on writing the function spi_drv_write in the driver program: the construction of the spi_transfer structure, in which tx_buf stores the sending data, len represents the sending length (number of bytes), and initiates SPI synchronous transmission

/**
 * 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);
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;    
}

 Complete driver: spi_drv.c

#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");


2. Modify the device tree

  • Place it under which SPI controller
  • Chip select pin of DAC module (check the chip manual)
  • SPI frequency
  • compatible attribute: used to address the driver

 Modify the device tree: vi arch/arm/boot/dts/100ask_imx6ull-14x14.dts in the kernel directory 

&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>;
    };
};
  • Recompile the device tree in the /home/book/100ask_imx6ull-sdk/Linux-4.9.88 directory: make dtbs
  • Copy it to the board as follows:
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
  • Enter the system firmware directory and view cd /sys/firmware/devicetree/base/

  • Check whether there is this device under the system bus i2c device 

 spi0.0 represents the 0th device under the 0th bus, the first 0 represents the controller, and the following 0 represents the 0th device under the controller. But there are no drivers yet.

  • Mount the network file system: mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
  • Load driver: insmod i2c_drv.ko
  • View the corresponding device node: ls /dev/myi2c -l  

There is a device but no driver files before loading the driver, but there are driver files after loading:

  •  View APP usage and test driver

Guess you like

Origin blog.csdn.net/qq_43460230/article/details/132504274