我使用下面的代码来输出来自embedded式主板的SPI端口(olimex imx233-micro – 这不是一个板子的具体问题)的数据。 当我运行代码ioctl返回“ 坏地址 ”。 我正在修改http://twilight.ponies.cz/spi-test.c的工作正常的代码。 谁能告诉我我做错了什么?
root@ubuntu:/home# gcc test.c -o test test.c:20: warning: conflicting types for 'msg_send' test.c:16: note: prevIoUs implicit declaration of 'msg_send' was here root@ubuntu:/home# ./test errno:Bad address - cannot send SPI message root@ubuntu:/home# uname -a Linux ubuntu 3.7.1 #2 Sun Mar 17 03:49:39 CET 2013 armv5tejl GNU/Linux
码:
//test.c #include <stdint.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <getopt.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/types.h> #include <linux/spi/spidev.h> #include <errno.h> static uint16_t delay; int main(int argc,char *argv[]){ msg_send(254); //the message that I want to send decimal "254" return 0; } void msg_send(int msg){ int fd; int ret = 0; fd = open("/dev/spidev32766.1",O_RDWR); //ls /dev outputs spidev32766.1 if(fd < 0){ fprintf(stderr,"errno:%s - FD Could be not openedn ",strerror(errno)); exit(1); } struct spi_ioc_transfer tr = { .len = 1,.delay_usecs = delay,.speed_hz = 500000,//500 kHz .bits_per_word = 8,.tx_buf = msg,.rx_buf = 0,//half duplex }; ret = ioctl(fd,SPI_IOC_MESSAGE(1),&tr); if (ret <1 ){ fprintf(stderr,"errno:%s - cannot send SPI messagen ",strerror(errno)); } close(fd); }
谢谢!
为什么mmap()比顺序IO更快?
如何用ALSA播放短音
激活优化选项时出现警告
不能得到BHO在64位工作
在Windows中获取C ++中唯一的硬件标识符
如何在Linux上编译C ++来创build一个Windows二进制文件
为什么Microsoft.Build.Evaluation在64位PC上将$(ProgramFiles)评估为“c: program files”?
使用XCode构build,分析,编辑unix linux控制台程序
错误消息“错误地址”来自错误代码EFAULT ,当您将地址传递给内核时,会发生错误代码,该错误代码在进程的虚拟地址空间中不是有效的虚拟地址。 你的tr结构的地址显然是有效的,所以问题必须与其中一个成员有关。
根据struct spi_ioc_transfer的定义 , .tx_buf和.rx_buf成员必须是指向用户空间缓冲区的指针,否则为null。 您将.tx_buf设置为整数254,这不是一个有效的用户空间指针,所以这是坏的地址来自哪里。
我不熟悉这个IOCTL,所以我最好的猜测是你需要以二进制方式低音数据。 一种方法是这样的:
struct spi_ioc_transfer tr = { .len = sizeof(msg),// Length of rx and tx buffers ... .tx_buf = (u64)&msg,// Pointer to tx buffer ... };
如果需要将其作为ASCII发送,则应使用snprintf(3)等函数将整数转换为ASCII字符串,然后将TX缓冲区指向该字符串并相应地设置长度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。