首页 > 解决方案 > Linux SPI 写入然后读取事务,无延迟

问题描述

我已经为嵌入式板构建了一个设备驱动程序,该驱动程序使用spi_write_then_read()函数通过 SPI 总线读取和写入外部设备。执行写入按预期工作。SPI接口是4总线(SCLK、CS、MOSI、MISO)。

下图显示了一个事务,其中(SCK = SCLK,SDI = MOSI 和 MUXOUT = MISO)

SDI = MOSI,MUXOUT = MISO

这是读取例程的内核片段,

static int lmx_read(struct lmx2xxx_driver *lmx2xxx, u16 reg, u16 * rbuf)                                                                                                                                           
{                                                                                                                                                                                                                  
    u8 buf[3];                                                                                                                                                                                                     
    u8 rbbuf[2];                                                                                                                                                                                                   
    int ret;                                                                                                                                                                                                       
    struct spi_device *spi = lmx2xxx->spi;                                                                                                                                                                         

    if (reg > lmx2xxx->nregs) {                                                                                                                                                                                    
        dev_err(&spi->dev, "Read Error, reg 0x%x out of range", reg);                                                                                                                                              
        return -1;                                                                                                                                                                                                 
    }                                                                                                                                                                                                              

    /* send lower 7 bits, highest bit = 1 when reading */                                                                                                                                                          
    buf[0] = (uint8_t)((reg & 0x7f) | 0x80);                                                                                                                                                                       
    /* pad with dummy bytes for shifting in reading */                                                                                                                                                             
    buf[1] = 0x0;                                                                                                                                                                                                  
    buf[2] = 0x0;                                                                                                                                                                                                  

    ret = spi_write_then_read(spi, &buf[0], 1, &rbbuf[0], 2);                                                                                                                                                      
    if (ret < 0) {                                                                                                                                                                                                 
        dev_err(&spi->dev, "Read Error %d", ret);                                                                                                                                                                  
        return ret;                                                                                                                                                                                                
    }                                                                                                                                                                                                              

    *rbuf = (uint16_t)((rbbuf[0]<<8) | (rbbuf[1]));                                                                                                                                                                

    return 0;                                                                                                                                                                                                      
}   

查看示波器上的信号,我发现 8 位写入后有一段时间的延迟(远大于时钟周期),然后 SCLK 在读取部分延迟后返回 16 位。然而,当时钟回来时,我看不到 MISO 数据,因为我相信我没有遵循他们的数据表中描述的协议(即写入和读取之间的大量延迟)而引入了错误。

ret = spi_write_then_read(spi, &buf[0], 3, &rbbuf[0], 2);如果我将写入缓冲区设置为 3 个字节(然后延迟断言 SCLK 为 2 字节长度,当然没有什么可读取的。

是否有另一个我可以使用的功能可以在不停止 SCLK 的情况下执行背靠背写入然后读取?我已经尝试过spi_w8r16(),但它在功能上与spi_write_then_read(spi, &buf[0], 1, &rbbuf[0], 2);方法相同。

在范围内捕获顶部 MISO 底部的 SCLK。

使用时输出,spi_write_then_read(spi, &buf[0], 1, &rbbuf[0], 2);.

在此处输入图像描述

使用时的完整输出,spi_write_then_read(spi, &buf[0], 3, &rbbuf[0], 2);显示延迟后标记的额外 2 个读取周期

在此处输入图像描述

使用时可以看到设备的正确期望输出,spi_write_then_read(spi, &buf[0], 3, &rbbuf[0], 2);在正确的位置显示数据以进行捕获,但当然这些数据不会保存到rbbuf仅查看在大量延迟后拖尾 2 个字节处捕获的数据.

在此处输入图像描述

标签: linux-kernellinux-device-driverspi

解决方案


可以通过将 SPI 消息设置为单个双向传输来改进时序,如以下未经测试的代码所示:

static int lmx_read(struct lmx2xxx_driver *lmx2xxx, u16 reg, u16 *rbuf)
{
    struct spi_device *spi = lmx2xxx->spi;
    struct spi_message message;
    struct spi_transfer x;
    u8 *buf;
    int ret;

    if (reg > lmx2xxx->nregs) {
        dev_err(&spi->dev, "Read Error, reg 0x%x out of range\n", reg);
        return -EINVAL;
    }

    /*
     * Allocate DMA-safe space for tx and rx buffers, both 3 bytes long.
     * Use a single 6-byte buffer for convenience.
     *
     * Note: this buffer should not be allocated on the stack because that
     * is not DMA-safe for all architectures.
     */
    buf = kmalloc(6, GFP_KERNEL);
    if (!buf) {
        dev_err(&spi->dev, "Memory allocation failure\n");
        return -ENOMEM;
    }

    /* send lower 7 bits, highest bit = 1 when reading */
    buf[0] = (uint8_t)((reg & 0x7f) | 0x80);
    /* pad with dummy bytes for shifting in reading */
    buf[1] = 0x0;
    buf[2] = 0x0;

    /* Set up SPI message with a single bi-directional transfer. */
    spi_message_init(&message);
    memset(&x, 0, sizeof(x));
    x.len = 3;
    x.tx_buf = &buf[0];
    x.rx_buf = &buf[3];
    spi_message_add_tail(&x, &message);

    /* Do the I/O. */
    ret = spi_sync(spi, &message);
    if (ret < 0) {
        dev_err(&spi->dev, "Read Error %d\n", ret);
        goto out;
    }

    /*
     * Read buffer starts at &buf[3], but first byte is the dummy byte read
     * while outputting the register number, so the read value starts at
     * &buf[4].
     */
    *rbuf = (uint16_t)((buf[4] << 8) | (buf[5]));

out:
    kfree(buf);
    return ret;
}

推荐阅读