在Linux内核中更加推荐使用队列模式的SPI控制器驱动,而且队列模式的SPI控制器驱动也更加简单,只需要在驱动中实现单个spi_transfer的传输即可,将spi_message拆解为spi_transfer、片选GPIO控制、统计信息更新等均由SPI核心去完成。
这里编写一个虚拟的SPI控制器驱动,通过printk来输出SPI控制器的工作状态,同时它提供软件进行回环(将tx_buf拷贝到rx_buf中)。
在顶层设备树根节点中加入如下节点:
virtual_spi_master {
compatible = "atk,virtual_spi_master";
status = "okay";
//片选列表,一个spi_master至少有一个片选
cs-gpios = <&gpioh 6 GPIO_ACTIVE_LOW>;
//片选数量
num-chipselects = <1>;
//reg中地址字段的字数,必须为1
#address-cells = <1>;
//reg中地址空间大小的字数,必须为0
#size-cells = <0>;
//一个spidev的设备节点,以便在应用层通过spidev来测试SPI控制器驱动
virtual_spi_dev: virtual_spi_dev@0 {
compatible = "rohm,dh2228fv";
reg = <0>;
spi-max-frequency = <100000>;
};
};
用make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabihf- dtbs -j8编译设备树,用新的.dtb文件启动系统
完整的驱动代码如下所示:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/spi/spi.h>
#include <linux/of_gpio.h>
static int spi_virtual_setup(struct spi_device *spi_dev)
{
if(!gpio_is_valid(spi_dev->cs_gpio))
{
printk("%d is not a valid gpio\n", spi_dev->cs_gpio);
return -EINVAL;
}
//若采用GPIO编号模式还需要在驱动中将gpio设置为输出
return gpio_direction_output(spi_dev->cs_gpio, !(spi_dev->mode & SPI_CS_HIGH));
}
static int spi_virtual_transfer_one(struct spi_master *master, struct spi_device *spi, struct spi_transfer *transfer)
{
int status;
//模拟硬件传输数据
if((transfer->tx_buf || transfer->rx_buf) && (transfer->len > 0))
{
if(transfer->rx_buf && transfer->tx_buf)
memcpy(transfer->rx_buf, transfer->tx_buf, transfer->len);
else if(transfer->rx_buf)
memset(transfer->rx_buf, 0xAA, transfer->len);
printk("transfer one\n");
status = 0;
}
else if((transfer->tx_buf || transfer->rx_buf) && (transfer->len <= 0))
{
printk("transfer invalid\n");
status = -EREMOTEIO;
}
else
status = 0;
//传输完成需要调用此函数(如果使用中断应该在传输完成中断中调用)
spi_finalize_current_transfer(master);
return status;
}
static int spi_virtual_probe(struct platform_device *pdev)
{
int result;
int i, num_cs, cs_gpio;
struct spi_master *virtual_master;
printk("%s\r\n", __FUNCTION__);
//分配spi_master
virtual_master = spi_alloc_master(&pdev->dev, 0);
if(!virtual_master)
{
printk("alloc spi_master fail\n");
return -ENOMEM;
}
//设置平台设备的驱动私有数据
pdev->dev.driver_data = (void*)virtual_master;
//初始化spi_master
virtual_master->use_gpio_descriptors = 0;
virtual_master->setup = spi_virtual_setup;
virtual_master->transfer_one = spi_virtual_transfer_one;
virtual_master->dev.of_node = pdev->dev.of_node;
virtual_master->bus_num = pdev->id;
virtual_master->max_speed_hz = 1000000000;
virtual_master->min_speed_hz = 1000;
virtual_master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST | SPI_3WIRE;
//片选引脚采用GPIO编号模式时,SPI驱动框架仅仅是从设备树中获取片选引脚编号并记录,未进行request引脚
num_cs = of_gpio_named_count(pdev->dev.of_node, "cs-gpios");
for (i = 0; i < num_cs; i++)
{
cs_gpio = of_get_named_gpio(pdev->dev.of_node, "cs-gpios", i);
if (cs_gpio == -EPROBE_DEFER)
{
/* 释放前面分配的 spi_master,它通过于 spi_master 绑定的 dev 来实现
* 其调用流程如下:
* spi_master_put
* spi_controller_put
* put_device
* kobject_put
* kref_put
* kobject_release
* kobject_cleanup
* t->release,这里应该是device_initialize为其注册的device_ktype中的device_release
* dev->class->dev_release,这里应该是分配spi_master时为dev.class绑定的spi_master_class中的spi_controller_release
*/
spi_master_put(virtual_master);
return -EPROBE_DEFER;
}
if(gpio_is_valid(cs_gpio))
{
result = devm_gpio_request(&pdev->dev, cs_gpio, "virtual_spi_cs");
if(result < 0)
{
spi_master_put(virtual_master);
printk("can't get CS gpio %i\n", cs_gpio);
return result;
}
}
}
//注册 spi_master
result = spi_register_master(virtual_master);
if (result < 0)
{
printk("register spi_master fail\n");
spi_master_put(virtual_master);
return result;
}
return 0;
}
static int spi_virtual_remove(struct platform_device *pdev)
{
struct spi_master *virtual_master;
printk("%s\r\n", __FUNCTION__);
//提取平台设备的驱动私有数据
virtual_master = (struct spi_master*)pdev->dev.driver_data;
//注销spi_master,在注销过程中会执行put_device操作,所以无需再次执行spi_master_put
spi_unregister_master(virtual_master);
return 0;
}
static const struct of_device_id spi_virtual_of_match[] = {
{.compatible = "atk,virtual_spi_master"},
{ /* Sentinel */ }
};
static struct platform_driver spi_virtual_driver = {
.probe = spi_virtual_probe,
.remove = spi_virtual_remove,
.driver = {
.name = "virtual_spi",
.of_match_table = spi_virtual_of_match,
},
};
static int virtual_master_init(void)
{
printk("%s\r\n", __FUNCTION__);
return platform_driver_register(&spi_virtual_driver);
}
static void virtual_master_exit(void)
{
printk("%s\r\n", __FUNCTION__);
platform_driver_unregister(&spi_virtual_driver);
}
module_init(virtual_master_init);
module_exit(virtual_master_exit);
MODULE_DESCRIPTION("virtual SPI bus driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("csdn");
驱动测试程序基于spidev进行编写,它通过ioctl控制SPI总线进行数据收发,完整的代码如下所示:
/* 参考: tools\spi\spidev_fdx.c */
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <errno.h>
/* dac_test /dev/spidevB.D <val> */
int main(int argc, char **argv)
{
int fd;
int status;
struct spi_ioc_transfer xfer[1];
unsigned char tx_buf[1];
unsigned char rx_buf[1];
if(argc != 3)
{
printf("Usage: %s /dev/spidevB.D <val>\n", argv[0]);
return 0;
}
//打开spidev设备
fd = open(argv[1], O_RDWR);
if (fd < 0) {
printf("can not open %s\n", argv[1]);
return 1;
}
//通过ioctl控制SPI总线发送并接收一个字节的数据
tx_buf[0] = (unsigned char)strtoul(argv[2], NULL, 0);
rx_buf[0] = 0;
memset(xfer, 0, sizeof xfer);
xfer[0].tx_buf = (unsigned long)tx_buf;
xfer[0].rx_buf = (unsigned long)rx_buf;
xfer[0].len = 2;
status = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
if(status < 0)
{
printf("SPI_IOC_MESSAGE %d\n", errno);
return -1;
}
//打印接收到的数据
printf("Pre val = %d\n", rx_buf[0]);
return 0;
}