我使用下面的代码从嵌入式板的 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 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);
}
谢谢!