我试图在RaspberryPI B +(Master)和Arduino Uno(slave)之间建立全双工SPI通信。
主方代码:
#include<sys/ioctl.h>
#include<linux/spi/spidev.h>
#include<fcntl.h>
#include<cstring>
#include<iostream>
#include<unistd.h>
using namespace std;
int fd;
unsigned int val;
unsigned int result;
int spiTxRx(unsigned int txDat);
int main(void){
fd = open("/dev/spidev0.0",O_RDWR);
unsigned int speed = 1000000;
ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
int ret = 0;
while(ret <=5){
ret++;
cout<<"input:";
cin>>val;
result=spiTxRx(val);
cout<<result<<endl;
usleep(10);
}
close(fd);
}
int spiTxRx(unsigned int txDat){
unsigned char rxDat;
struct spi_ioc_transfer spi;
memset(&spi,0,sizeof(spi));
spi.tx_buf = (unsigned long) &txDat;
spi.rx_buf = (unsigned long) &rxDat;
spi.len = 1;
ioctl (fd, SPI_IOC_MESSAGE(1), &spi);
return rxDat;
}
Slave-Side代码
byte clr;
int x = 0;
int readInput;
void setup (void){
Serial.begin (9600);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode. SPCR determine Arduino SPI settings
SPCR |= _BV(SPE);
clr = SPSR;
clr = SPDR;
delay(10);
}
void loop (void){
if ((SPSR & _BV(SPIF)) !=0){ //if byte has been received
readInput = SPDR;
if (readInput == 7){
x++;
SPDR = x;
}
}
}
所以,简单地说,如果我发送抛出MOSI行输入(在本例中为7),Arduino增量var x并以x值回复我。
但我的输出看起来像这样:
init: x = 0;
(1st input)
Master send 7, Slave add x (so, x=1) and send me back unusual value
(2nd input)
Master send 7, Slave add x (x=2) and send me back 1 (previous value of x)
(3rd input)
Master send 7, slave add x (x=3) and send me back 2 (previous value of x)
(Nth input)
Master send 7, slave add x (x = k) and send me back k-1
换句话说,如果我发送7,Arduino递增x,在while行中循环并向我发回相同的输入值。如果我再次发送一个值,Arduino会以正确的回复回复我。
有人可以帮助我吗?
答案 0 :(得分:0)
我找到了一个关于我的问题的解决方案。
发送和接收消息之间的差异是由Arduino SPI数据寄存器(SPDR)引起的。 SPDR由8位移位寄存器和8位接收缓冲区定义。当Master发送一个字节时,它存储在接收缓冲区中,移位寄存器中的字节被发送回主机。因此,我们需要发送另一条消息以获得正确的响应。