新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2022-5-20
- 在线时间
- 11 小时
|
1金钱
在学习IMX6ULL SPI教程中(SPI读写icm20608),发现, 1) 如果icm20608_read_regs采用spi_write_then_read()进行数据读写则正常
static int icm20608_read_regs(struct icm20608_dev *dev, u8 reg, void *buf, int len)
{
int ret = 0;
struct spi_device *spi = (struct spi_device *)dev->private_data;
u8 txdata;
//采用spi接口函数
txdata = reg | 0x80; //icm20608要求read请求,adress最高位为1
ret = spi_write_then_read(spi, &txdata, 1, buf, len);
return 0;
}
2)如果直接封装spi_transfer,驱动加载时读寄存器(单个)是OK的,但是执行./icm20608App /dev/icm20608则会报如下错误
函数封装如下:
static int icm20608_read_regs_xfer(struct icm20608_dev *dev, u8 reg, void *buf, int len)
{
// printk("icm20608_read_regs_xfer Enter\r\n");
int ret = 0;
int i = 0;
struct spi_device *spi = (struct spi_device *)dev->private_data;
struct spi_transfer xfer[2];
struct spi_message m;
if(len > XFER_BUF_LEN-1){
printk("len(%d) is invalid, max len is:%d\r\n", len, XFER_BUF_LEN);
return EPERM;
}
mutex_lock(&dev->mutex);
spi_message_init(&m);
memset(xfer, 0, sizeof(xfer));
dev->xfer_buf[0] = reg | 0x80; //icm20608要求read请求,adress最高位为1
xfer[0].tx_buf = &dev->xfer_buf[0];
xfer[0].len = sizeof(dev->xfer_buf[0]);
spi_message_add_tail(&xfer[0], &m);
xfer[1].rx_buf = &dev->xfer_buf[1];
xfer[1].len = sizeof(dev->xfer_buf[1]) * len;
spi_message_add_tail(&xfer[1], &m);
ret = spi_async(spi, &m);
if (unlikely(ret)) {
printk("SPI read error: %d\n", ret);
mutex_unlock(&dev->mutex);
return ret;
}
memcpy(buf, &dev->xfer_buf[1], len);
mutex_unlock(&dev->mutex);
// printk("icm20608_read_regs_xfer End\r\n");
return 0;
}
|
最佳答案
查看完整内容[请看2#楼]
最终发现,是把spi_sync()错写成了spi_async()(同时要去掉mutex,即可测试OK)
|