【发布时间】:2013-04-02 16:50:50
【问题描述】:
我使用下面的代码从嵌入式板的 SPI 端口输出数据(olimex imx233-micro -- 这不是板特定的问题)。当我运行代码ioctl 时,返回“bad address”。我正在修改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 opened\n ", 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 message\n ", strerror(errno));
}
close(fd);
}
谢谢!
【问题讨论】:
-
您知道将
msg_send()函数移到main()上方会消除该警告吗? -
谢谢你,迈克,我很粗心。警告消失但
bad address错误仍然存在 -
.tx_buf=msg-- 你认为这里应该发生什么? -
n.m.,我以为它会将我要发送到发送器缓冲区的消息(
254)传递给发送器缓冲区,然后它将通过ioctl发送,是我做错了吗?