gpt4 book ai didi

c - SPI_IOC_MESSAGE 返回 EINVAL

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:22 24 4
gpt4 key购买 nike

我们正在从 ARM 嵌入式 Ubuntu 14.04 更新到 ARM 嵌入式 Ubuntu 16.04。在第一个平台上,我们可以毫无问题地访问使用 SPIDEV 的芯片。在 Ubuntu 平台上,我在 ioctl SPI_IOC_MESSAGE 之后收到 EINVAL 错误。

我看到了消息SPI_IOC_MESSAGE(N) macro giving me fits它并没有解决问题。

我的代码如下:

 SpiComm_t::Transfer(int i4Length) {   
int ret = -1;

m_tr.len = i4Length;

ret = ioctl(m_fd, SPI_IOC_MESSAGE(1), &m_tr);
if (ret ) {
printf("SPI IOCTL error %s\n", strerror(errno)); }

return ret;
}

调用代码如下:

  // Reset memory (Optional...  Helps diagnose failures to read.)
memset(m_c1BufTx, 0xFF, sizeof(m_c1BufTx));

// Put address
m_c1BufTx[0] = address;

// Reset memory (optional...)
memset(m_c1BufRx, 0xFF, sizeof(m_c1BufRx));

// Invoke ioctl transaction
int result = Transfer(size+1);

我们使用的是来自 TI SDK 4.0 的内核 4.9.59。我已将 EINVAL 追踪到 spidev.c 驱动程序,但无法理解为什么会出现错误。

spidev.c代码为:

tmp = _IOC_SIZE(cmd);
if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
return ERR_PTR(-EINVAL);
*n_ioc = tmp / sizeof(struct spi_ioc_transfer);
if (*n_ioc == 0)
return NULL;

/* copy into scratch area */

如有任何帮助,我们将不胜感激。

最佳答案

问题似乎是 spi_ioc_transfer 中有一些垃圾。将结构归零解决了这个问题。 cs_change 或 pad 是错误的原因。

此外,ioctl(fd, SPI_IOC_MESSAGE(1), ...) 的返回码并不表示失败或成功。相反,在这种情况下,它返回返回的消息的大小。要测试错误必须直接检查 errno。

传递函数的更新代码现在是:

errno = 0;
ret = ioctl(m_fd, SPI_IOC_MESSAGE(1), &m_tr);
// NOTE this ioctl returns the lenght of the answer not an indication of setting errno
// Check errno instead
if (errno != 0)
{
printf("SPI IOCTL ret(%d)error(%d) %s\n", ret, errno, strerror(errno));
}

关于c - SPI_IOC_MESSAGE 返回 EINVAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com