gpt4 book ai didi

c - ioctl "can' t 发送 spi 消息 : Invalid argument"Beaglebone Black

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:55 25 4
gpt4 key购买 nike

我正在尝试从 spidev1.0(总线 1 设备 0)上的 beaglebone black 向 IC 芯片发送 16 位消息 TLV5618AC

在移位寄存器开始在时钟下降沿拾取位之前,芯片选择线需要从高电平变为低电平。

我执行了 spidev_test.c 的修改版本(原始版本运行良好)。因为我需要芯片选择线在消息之间从高到低 - ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); 行将不起作用,因为它将数据作为单个传输信息。即使我将 ioctl() 放在 for 循环中,它也不会工作,因为它是第一条也是最后一条消息。

以下代码有效

static void transfer(int fd)
{
int ret;
unsigned short buffer[1];
int zero = 0;

FILE *iFile = fopen("firsttest.bin", "r");
if (iFile == NULL)
{
printf("Cannot open file \n");
exit(0);
}

struct spi_ioc_transfer tr = {
.tx_buf = (unsigned long)zero,
.rx_buf = (unsigned long)zero,
.len = 2,
.delay_usecs = delay,
.speed_hz = speed,
.bits_per_word = bits,
};


while(!feof(iFile)){
fread(buffer,2,1,iFile);
unsigned short *tx = buffer;
unsigned short rx[1] = {0, };
tr.tx_buf = (unsigned long)tx;
tr.rx_buf = (unsigned long)rx;
int size = ARRAY_SIZE(tx);


ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");

for (ret = 0; ret < 1; ret++) {
printf("0x%04x\n", tx[ret]);

}

}
}

第二个代码块没有

  {
int ret;
unsigned short buffer[1];
int zero = 0;

FILE *iFile = fopen("firsttest.bin", "r");
if (iFile == NULL)
{
printf("Cannot open file \n");
exit(0);
}

struct spi_ioc_transfer tr[100];
unsigned short *p = (unsigned short*) calloc(100, sizeof(unsigned short));
unsigned short *p2 = (unsigned short*) calloc(100, sizeof(unsigned short));


while(!feof(iFile)){
unsigned short *tx = p2;
unsigned short *rx=p;
for(int j = 0; j< 100; j++){
fread(buffer,2,1,iFile);
tx = (unsigned short*) &buffer+j;
tr[j].tx_buf = (unsigned long)tx;
tr[j].rx_buf = 0;
tr[j].len = 2;/* Total length of message in bytes*/
tr[j].delay_usecs = delay;
tr[j].speed_hz = speed;
tr[j].bits_per_word = bits;
tr[j].cs_change = 1;

}


ret = ioctl(fd, SPI_IOC_MESSAGE(100), &tr);
if (ret < 1)
pabort("can't send spi message");
for(int v = 0; v<100; v++){
printf("0x%04x\n", rx);
}
}
free(p);
}

第二个代码块产生这个错误

“无法发送 spi 消息:参数无效”

因为它是一个无效参数,所以我尝试传入 '&tr' 和 'tr' 都没有用。任何输入将不胜感激。

编辑:所以我决定使用第一个有效的代码。我想没有理由发送大笔转账。我的系统足够快,电压足够快,可以有效地实时!!谢谢您的帮助。 (我还大量清理了我的代码,使其更易于阅读!)

最佳答案

在 struct spi_ioc_transfer tr 之后添加 memset(&tr, 0, sizeof(tr));

https://community.nxp.com/thread/482375

关于c - ioctl "can' t 发送 spi 消息 : Invalid argument"Beaglebone Black,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56433271/

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