Bootstrap

Linux下的SPI通信

SPI通信


一. 1.SPI简介:

  1. SPI 是一种高速,全双工,同步串行总线。

  2. SPI 有主从俩种模式通常由一个主设备和一个或者多个从设备组从。SPI不支持多主机。

  3. SPI通信至少需要四根线,分别是 MISO(主设备数据输入,从设备输出),MOSI (主设数据输出从设备输入),SCLK(时钟信号),CS/SS (片选信号)。

  4. 8位或16位数据帧格式

连接方式:

2.工作模式:

极性和相位
SPI 的极性(polarity)和相位 (phase)一般写为 CPOLCPHA,即

CPOL(时钟极性):clock Polarity,CPHA(时钟相位) :Clock Phase

CPOL:表示时钟信号的初始电平状态。为 0表示时钟信号初始电平状态为低电平。为 1表示时钟信号初始电平状态为高电平。

CPHA:表示在第几个时钟跳变沿采样数据。为0表示在第一个时钟时钟跳变沿采样数据为1表示在第二个时钟跳变沿采样数据。

CPOL高/低--------CPHA高/低组成SPI的四种工作模式

二.SPI驱动框架图:

三.SPI的Device和Driver部分:

1.Devices部分使用设备树创建节点:

driver部分与设备树的匹配方式,需要先遍历设备树中是否有描述SPI设备的子节点,当发现节点信息是,会向内核中注册Device信息。

在注册Device时,会处理SPI的设备节点信息,判断必要的设备信息是否存在:

在遍历节点信息时,会判断设备树的节点信息中是否有Device speedDevice speed信息是否存在:否则spi_dev_put(spi);会释放刚添加的Device

        /* Device address */
        prop = of_get_property(nc, "reg", &len);
        if (!prop || len < sizeof(*prop)) {
            dev_err(&master->dev, "%s has no 'reg' property\n",
                nc->full_name);
            spi_dev_put(spi);
            continue;
        }
        spi->chip_select = be32_to_cpup(prop);

        /* Mode (clock phase/polarity/etc.) */
        if (of_find_property(nc, "spi-cpha", NULL))
            spi->mode |= SPI_CPHA;
        if (of_find_property(nc, "spi-cpol", NULL))
            spi->mode |= SPI_CPOL;
        if (of_find_property(nc, "spi-cs-high", NULL))
            spi->mode |= SPI_CS_HIGH;

        /* Device speed */
        prop = of_get_property(nc, "spi-max-frequency", &len);
        if (!prop || len < sizeof(*prop)) {
            dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n",
                nc->full_name);
            spi_dev_put(spi);
            continue;
        }

所以在添加设备树节点时,需要添加必要的两个信息即为:片选地址和通信速度

&spi0 {
    status = "okay";

    mcp2515:mcp2515@0{
        compatible = "my-mcp2515";
/*片选*/
        reg = <0>;
/*速度,不能超过50M*/
        spi-max-frequency = <24000000>;
    }
}

2.Driver部分:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>

static int spi_driver_probe(struct spi_device *spi)
{
    printk("This is SPI driver probe\n");
    return 0;
}
static int spi_driver_remove(struct spi_device *spi){

    return 0;
}

static const struct of_device_id spi_driver_of_match_table[] = {
    {.compatible = "my-mcp2515"},
    {},
}

const struct spi_device_id spi_driver_id_table[] = {{"spi_driver"}, {}}

struct spi_driver spi_driver = {
    .driver = {
        .name = "spi_driver",
        .owner = THIS_MODULE,
        .of_match_table = spi_driver_of_match_table,
    },
    .probe = spi_driver_probe,
    .remove = spi_driver_remove,
    .id_table = spi_driver_id_table,
}

static int __init
spi_driver_init(void)
{

    int ret = 0;
    ret = spi_register_driver(&spi_driver);
    if (ret != 0)
    {
        printk(KERN_ERR "Failed to unregister spi driver\n");
    }
    return 0;
}

static void __exit spi_driver_exit(void)
{

    spi_unregister_driver(&spi_driver);
}

module_init(spi_driver_init);
module_exit(spi_driver_exit);
MODULE_LICENSE("GPL");

3.完善Driver部分,在probe中使用字符设备框架添加设备控制节点(如果不需要与应用层交换数据,也可以不添加设备控制节点):

dev_t dev_num;
struct cdev mcp2515_cdev;
struct class *mcp2515_class;
struct device *mcp2515_device;
ssize_t mcp2515_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{

    return 0;
}
ssize_t mcp2515_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    return 0;
}
int mcp2515_open(struct inode *node, struct file *file)
{

    return 0;
}
int mcp2515_release(struct inode *node, struct file *file)
{

    return 0;
}
const struct file_operations spi_driver_fops = {
    .owner = THIS_MODULE,
    .open = mcp2515_open,
    .read = mcp2515_read,
    .write = mcp2515_write,
    .release = mcp2515_release,

};

static int spi_driver_probe(struct spi_device *spi)
{
    int ret = 0;
    printk("This is SPI driver probe\n");
    // 添加一个字符设备
    //  分配一个字符设备区域,dev_num 为设备号,cnt 为数量, name 为名字
    ret = alloc_chrdev_region(&dev_num, 0, 1, "my-mcp2515");
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to alloc_chrdev_region\n");
        return ret;
    }
    // 初始化cdev
    cdev_init(&mcp2515_cdev, &spi_driver_fops);
    // 添加cdev到内核
    ret = cdev_add(&mcp2515_cdev, dev_num, 1);
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to cdev_add\n");
    }

    // 创建设备节点
    // 创建类
    mcp2515_class = class_create(THIS_MODULE, "my-spi");
    if (IS_ERR(mcp2515_class))
    {
        printk(KERN_ERR "Failed to create class\n");
        return PTR_ERR(mcp2515_class);
    }
    // 创建设备
    mcp2515_device = device_create(mcp2515_class, NULL, dev_num, NULL, "my-mcp2515");
    if (IS_ERR(mcp2515_device))
    {
        printk(KERN_ERR "Failed to create device\n");
    }

    return 0;
}

4.编写驱动函数

配置工作模式:

在设备树配置,默认极性和相位都是0,高位先传输,cs是低电平选中:

/*节点中可以写入以下信息*/
spi-cpha;         *表示cpha为1*
spi-cpol;         *spi-cpol*
spi-lsb-first;    *低位先传输*
spi-cs-high;      *高电平表示选中*

常用的SPI收发接口:

函数原型:
static inline int spi_write(struct spi_device *spi, const void *buf, size_t len)
函数作用:
SPI 同步写函数
函数参数:

struct spi_device : spi 从设备
const void *buf: 要发送的数据
size_t len:发送数据的大小

返回值: 成功返回 0,失败返回负数

函数原型:
static inline int spi_read(struct spi_device *spi, void *buf, size_t len)
函数作用:
SPI 同步读函数

函数参数: struct spi_device: spi 从设备
const void *buf: 读取到的数据缓冲区
size_t len: 读到的数据大小
返回值: 成功返回 0,失败返回负数

函数原型:
extern int spi_write_then_read(struct spi_device *spi,const void *txbuf, unsigned n_tx,void *rxbuf, unsigned n_x);
函数作用:
先写后读函数,同步,8bit

函数参数: struct spi_device: spi 从设备

constvoid *txbuf:发送数据缓冲区
unsigned n_tx: 发送的数据大小
void *rxbuf: 读取数据缓冲区
unsigned n_rx: 读到的数据大小
返回值: 成功返回 0,失败返回负数

编写驱动层SPI的收发:

char mcp2515_read_reg(char reg)
{
    // 0x03是读取MCP2515寄存器的指令,reg是寄存器地址
    char data[] = {0x03, reg};
    char read_buf;
    // 读取MCP2515寄存器,直接发送一个读取指令
    int ret = spi_write_then_read(spi_dev, data, sizeof(data), &read_buf, sizeof(read_buf));
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to read MCP2515 register\n");
    }
    return read_buf;
}

void mcp2515_write_reg(char reg, char value)
{
    // 写入MCP2515寄存器,先发送写入指令,再发送寄存器地址和数据
    char data[] = {0x02, reg, value};
    int ret = spi_write(spi_dev, data, sizeof(data));
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to mcp2515_write_reg MCP2515\n");
    }
}

Driver的源程序:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>

dev_t dev_num;
struct cdev mcp2515_cdev;
struct class *mcp2515_class;
struct device *mcp2515_device;
struct spi_device *spi_dev;

void mcp2515_reset(void)
{
    char data[] = {0xc0};
    // 查看mcp2515手册后知道的复位指令,不同的芯片/设备有着不同的指令功能,不是spi固定的发送步骤
    //  复位MCP2515,直接发送一个复位指令
    int ret = spi_write(spi_dev, data, sizeof(data));
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to reset MCP2515\n");
    }
}

char mcp2515_read_reg(char reg)
{
    // 0x03是读取MCP2515寄存器的指令,reg是寄存器地址
    char data[] = {0x03, reg};
    char read_buf;
    // 读取MCP2515寄存器,直接发送一个读取指令
    int ret = spi_write_then_read(spi_dev, data, sizeof(data), &read_buf, sizeof(read_buf));
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to read MCP2515 register\n");
    }
    return read_buf;
}

void mcp2515_write_reg(char reg, char value)
{
    // 写入MCP2515寄存器,先发送写入指令,再发送寄存器地址和数据
    char data[] = {0x02, reg, value};
    int ret = spi_write(spi_dev, data, sizeof(data));
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to mcp2515_write_reg MCP2515\n");
    }
}

ssize_t mcp2515_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{

    return 0;
}
ssize_t mcp2515_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    return 0;
}
int mcp2515_open(struct inode *node, struct file *file)
{

    return 0;
}
int mcp2515_release(struct inode *node, struct file *file)
{

    return 0;
}
// 定义驱动的文件操作结构体
const struct file_operations spi_driver_fops = {
    .owner = THIS_MODULE,
    .open = mcp2515_open,
    .read = mcp2515_read,
    .write = mcp2515_write,
    .release = mcp2515_release,

};
// 驱动的probe函数
static int spi_driver_probe(struct spi_device *spi)
{
    int ret = 0;
    printk("This is SPI driver probe\n");
    spi_dev = spi;
    // 添加一个字符设备
    //  分配一个字符设备区域,dev_num 为设备号,cnt 为数量, name 为名字
    ret = alloc_chrdev_region(&dev_num, 0, 1, "my-mcp2515");
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to alloc_chrdev_region\n");
        return ret;
    }
    // 初始化cdev
    cdev_init(&mcp2515_cdev, &spi_driver_fops);
    // 添加cdev到内核
    ret = cdev_add(&mcp2515_cdev, dev_num, 1);
    if (ret < 0)
    {
        printk(KERN_ERR "Failed to cdev_add\n");
    }

    // 创建设备节点
    // 创建类
    mcp2515_class = class_create(THIS_MODULE, "my-spi");
    if (IS_ERR(mcp2515_class))
    {
        printk(KERN_ERR "Failed to create class\n");
        return PTR_ERR(mcp2515_class);
    }
    // 创建设备
    mcp2515_device = device_create(mcp2515_class, NULL, dev_num, NULL, "my-mcp2515");
    if (IS_ERR(mcp2515_device))
    {
        printk(KERN_ERR "Failed to create device\n");
    }

    return 0;
}
static int spi_driver_remove(struct spi_device *spi)
{

    return 0;
}
// 设备树匹配表
static const struct of_device_id spi_driver_of_match_table[] = {
    {.compatible = "my-mcp2515"},
    {},
};
// 平台client id 匹配表
const struct spi_device_id spi_driver_id_table[] = {
    {"spi_driver"},
    {},
};

struct spi_driver spi_driver = {
    .driver = {
        .name = "spi_driver",
        .owner = THIS_MODULE,
        .of_match_table = spi_driver_of_match_table,
    },
    .probe = spi_driver_probe,
    .remove = spi_driver_remove,
    .id_table = spi_driver_id_table,
};

static int spi_driver_init(void)
{

    int ret = 0;
    ret = spi_register_driver(&spi_driver);
    if (ret != 0)
    {
        printk(KERN_ERR "Failed to unregister spi driver\n");
    }
    return 0;
}

static void spi_driver_exit(void)
{

    device_destroy(mcp2515_class, dev_num);
    class_destroy(mcp2515_class);
    cdev_del(&mcp2515_cdev);
    unregister_chrdev_region(dev_num, 1);
    spi_unregister_driver(&spi_driver);
}

module_init(spi_driver_init);
module_exit(spi_driver_exit);
MODULE_LICENSE("GPL");

四.使用应用层直接SPI通信:

使用应用层的SPI通信,会调用Linux内核中封装好的SPI接口函数,无需再向内核中添加spi_device

将通用的SPI编译进内核中,修改镜像文件,使其支持内核通用SPI驱动

Device Driver
    [*]SPI support -->
        <*>User mode SPI device driver support

通用驱动的源码文件在 :driver/spi/spidev.c

spidev中将driverdevice同时加进内核中,并创建了字符设备操作节点,以供应用层使用。

编写应用层的SPI通信操作:

#include <stdio.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>

#define RESET_CMD 0xc0
#define CANSTAT_REG 0x80
#define READ_CMD 0xcc 
#define WRITE_CMD 0xc2
#define CANCTRL_REG 0x0f
#define CANSTAT_CTL 0x2b

int fd = 0;
int bits = 8;        // 8bit
int speed = 1000000; // 1MHz
int mode = 0;        // 0: mode0, 1: mode1
int spi_init(void)
{

    int ret = 0;
    fd = open("/dev/spidev0.0", O_RDWR);
    if (fd < 0)
    {
        printf("Error opening spi device\n");
        return -1;
    }

    /*
     *spi mode 模式设置读写
     */
    ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
    if (ret < 0)
    {
        printf("Error setting spi mode\n");
        return -1;
    }
    ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
    if (ret < 0)
    {
        printf("Error setting spi mode\n");
        return -1;
    }

    /*
     *spi bits per word 发送和接收的bit数
     */
    ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
    if (ret < 0)
    {
        printf("Error setting spi bits per word\n");
        return -1;
    }

    ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
    if (ret < 0)
    {
        printf("Error setting spi bits per word\n");
        return -1;
    }

    /*
     *spi max speed hz 最大传输速度
     */
    ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
    if (ret < 0)
    {
        printf("Error setting spi max speed hz\n");
        return -1;
    }

    ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
    if (ret < 0)
    {
        printf("Error setting spi max speed hz\n");
        return -1;
    }
    printf("spi init success\n");
    printf("spi mode:%x\n", mode);
    printf("spi bits per word:%d\n", bits);
    printf("spi max speed hz:%d\n", speed);

    return 0;
}

// 传输函数
//fd: spi设备文件描述符 tx: 发送数据 rx: 接收数据 len: 数据长度
int spi_transfer(int fd, char *tx, char *rx, int len)
{
    int ret = 0;
    struct spi_ioc_transfer tr = {
        .tx_buf = (unsigned long)tx,
        .rx_buf = (unsigned long)rx,
        .len = len,
        .delay_usecs = 0,
        .speed_hz = speed,
        .bits_per_word = bits,
    };
    // 标准的读()和写()操作显然只是半双工的,并且在这些操作之间去激活芯片选择。全双工接入,和无需芯片选择停用的复合操作,可使用SPI_IOC_ MESSAGE(N)请求。
    ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
    if (ret < 1)
    {
        printf("Error in spi transfer\n");
        return -1;
    }
    return 0;
}

int main()
{
    char reset_cmd[1] = {RESET_CMD};
    char rd_canstat[2] = {READ_CMD, CANSTAT_REG};
    char canstat[3] = {0};

    spi_init();
    //写操作则长度为reset_cmd,写命令数据的buff的长度
    spi_transfer(fd, reset_cmd, NULL, sizeof(reset_cmd));
    //读操作则长度为canstat,存储读取的数据的buff长度
    spi_transfer(fd, rd_canstat, canstat, sizeof(canstat));
    //存储读取的数据的buff中,会存有往设备中写入数据的值,第一个数据为READ_CMD,第二个数据为CANSTAT_REG,第三个是从机返回回来的值,所以要从第三个开始读取
    printf("canstat: %x\n", canstat[2]);

    char wr_canctrl[3] = {WRITE_CMD, CANSTAT_CTL, 0x00};


    char canstat_new[4] = {0};
    spi_transfer(fd, wr_canctrl, NULL, sizeof(wr_canctrl));
    spi_transfer(fd, rd_canstat, canstat_new, sizeof(canstat_new));
    //第一个数据为WRITE_CMD,第二个数据为CANSTAT_CTL,第二个数据为0x00,第四个是从机返回回来的值,所以要从第四个开始读取
    printf("canstat_new: %x\n", canstat_new[3]);

    return 0;
}

GPIO模拟SPI:

一般不需要用户手动编写SPI的模拟时序,大都直接使用Linux中提供的模拟通信:

使用时需要手动make menuconfig讲其添加进内核:

Device Driver
    [*]SPI support
        <*>GPIO-based bitbanging SPI Master

Linux下提供的模拟SPI源码目录:

kernel\drivers\spi\spi-gpio.c

模拟的驱动匹配名为:#define DRIVER_NAME "spi_gpio"

所以在使用模拟SPI的通信时,需要添加平台device,属性名为spi-gpio,且需要有以下属性:

/*
 * Because the overhead of going through four GPIO procedure calls
 * per transferred bit can make performance a problem, this code
 * is set up so that you can use it in either of two ways:
 *
 *   - The slow generic way:  set up platform_data to hold the GPIO
 *     numbers used for MISO/MOSI/SCK, and issue procedure calls for
 *     each of them.  This driver can handle several such busses.
 *
 *   - The quicker inlined way:  only helps with platform GPIO code
 *     that inlines operations for constant GPIOs.  This can give
 *     you tight (fast!) inner loops, but each such bus needs a
 *     new driver.  You'll define a new C file, with Makefile and
 *     Kconfig support; the C code can be a total of six lines:
 *
 *		#define DRIVER_NAME	"myboard_spi2"
 *		#define	SPI_MISO_GPIO	119
 *		#define	SPI_MOSI_GPIO	120
 *		#define	SPI_SCK_GPIO	121
 *		#define	SPI_N_CHIPSEL	4
 *		#include "spi-gpio.c"
 */

#define DRIVER_NAME “myboard_spi2”

#define SPI_MISO_GPIO 119

#define SPI_MOSI_GPIO 120

#define SPI_SCK_GPIO 121

#define SPI_N_CHIPSEL 4

#include “spi-gpio.c”

所以需要在设备树中添加相关属性:

spi:spi@gpio{
    compatible:"spi-gpio";
    #address-cells = <1>;
    gpio-sck = <&gpio0 RK_PB0 GPIO_ACTIVE_LOW>
    gpio-miso= <&gpio1 RK_PB0 GPIO_ACTIVE_LOW>
    gpio-mosi= <&gpio1 RK_PB1 GPIO_ACTIVE_LOW>
    cs-gpios= <&gpio1 RK_PB2 GPIO_ACTIVE_LOW>
    num-chipselects = <1>;

}

添加成功后会在dev下生成对应的SPI控制节点,使用方法和应用层直接SPI通信的操作一样

;